Subversion Repositories SmartDukaan

Rev

Rev 11523 | Rev 11749 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10582 lgm 1
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
 
3
class Cart extends MY_Controller {
4
 
5
public $layoutName ='';
6
 
7
	function __construct() {
8
 
9
		// Call the CI_controller constructor
10
		parent::__construct();
11108 lgm 11
		// $admin = $this->session->userdata('admin');
12
		// if(!isset($admin) || empty($admin)) {
13
		// 	redirect(base_url().'authorize');
14
		// }
10582 lgm 15
		$this->output->set_header("Expires: Tue, 01 Jan 2000 00:00:00 GMT");
16
		$this->output->set_header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
17
		$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
18
		$this->output->set_header("Cache-Control: post-check=0, pre-check=0", false);
19
		$this->output->set_header("Pragma: no-cache");
20
		$this->layout->setlayout('layout/layout_main');
21
		$this->load->model('cart_model');
22
		$this->layoutName= $this->layout->getLayout();
23
		$this->layoutName =substr($this->layoutName , 0, strrpos($this->layoutName , "/")).'/';
24
		//print_r($this->session->userdata);
25
 
26
	}
11517 lgm 27
		public function index()
10582 lgm 28
	{
29
		$authorized = $this->session->userdata('authorized');
11529 anikendra 30
		error_log("[cart] authorized ".print_r($authorized,1));
11517 lgm 31
		//standard array
32
		$configdata = $this->config->item('cart');
33
		//$checkConfig = $this->config->item('home');
34
		$cachemodule = array('header','footer');
35
		$configdata = getCache($configdata,$cachemodule);
11529 anikendra 36
		error_log("[cart] configdata ".print_r($authorized,1));
11517 lgm 37
		$data = array();
10582 lgm 38
		if(isset($authorized) && !empty($authorized)){
39
			$_GET['userId'] = $authorized['Id'];
40
			$_GET['Id'] = $authorized['cartId'];
41
			if($authorized['isLoggedIn'] == 1){
42
				$_GET['isLoggedIn'] = 'true';
43
			}
44
			elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
45
				$_GET['isLoggedIn'] = 'false';
46
			}
11517 lgm 47
			unset($configdata['module']['response'][0]);
48
			$data['response']=$this->cart_model->getCart($this->input->get(),$this->input->post(),$configdata['module']);
11529 anikendra 49
			error_log("[cart] authorized response ".print_r($data['response'],1));
11517 lgm 50
			//print_r($data['response']['response']['carts']);
51
			if(isset($data['response']['response']['carts'][0]->lines))
52
			$cartCount = sizeof($data['response']['response']['carts'][0]->lines);
53
			$authorized = $this->session->userdata('authorized');
54
			if(isset($authorized) && !empty($authorized)){
55
				$newValues = $authorized;
56
				$newValues['totalItems'] = $cartCount;
57
				$this->session->set_userdata('authorized',$newValues);
58
			}
59
			$this->lessphp->object()->ccompile('assets/css/cart.less','assets/css/cart.css');
60
			$data['stylesheet'] = 'cart.css';
61
			if(isset($cachemodule) and !empty($cachemodule) && !isset($api))
11169 lgm 62
			{
11517 lgm 63
				foreach($cachemodule as $cm)
11169 lgm 64
				{
11517 lgm 65
					if(isset($configdata[$cm]) and !empty($configdata[$cm]))
66
					{
67
						$data['response'][$cm]=$configdata[$cm];
68
					}
11169 lgm 69
				}
70
			}
11517 lgm 71
			setCache($configdata['module'],$cachemodule,$data['response']);
72
			$this->layout->view('cart/cart_view',$data);
73
		}else{
74
			unset($configdata['module']['response'][1]);
75
			$data['response']=$this->cart_model->getCart($this->input->get(),$this->input->post(),$configdata['module']);
11529 anikendra 76
			error_log("[cart] unauthorized response ".print_r($data['response'],1));
11517 lgm 77
			$this->lessphp->object()->ccompile('assets/css/cart.less','assets/css/cart.css');
78
			$data['stylesheet'] = 'cart.css';
79
			if(isset($cachemodule) and !empty($cachemodule) && !isset($api))
80
			{
81
				foreach($cachemodule as $cm)
82
				{
83
					if(isset($configdata[$cm]) and !empty($configdata[$cm]))
84
					{
85
						$data['response'][$cm]=$configdata[$cm];
86
					}
87
				}
88
			}
89
			setCache($configdata['module'],$cachemodule,$data['response']);
90
			$this->layout->view('cart/carts',$data);
11169 lgm 91
		}
10582 lgm 92
	}
93
 
94
 
10914 lgm 95
	public function add($id=null,$email=null,$itemname)
10582 lgm 96
	{
97
		$response=array();
98
 
99
		$authorized = $this->session->userdata('authorized');
100
		if(isset($id) and !empty($id) and is_numeric($id))
101
			{
102
				//if user logged-in
103
				$_POST['itemId'] = $id;
104
			}
105
		else{
106
			redirect(base_url());
107
		}
108
		if(isset($authorized) && !empty($authorized)){
109
			$_POST['userId'] = $authorized['Id'];
110
			$_POST['Id'] = $authorized['cartId'];
111
			if($authorized['isLoggedIn'] == 1){
112
				$_POST['isLoggedIn'] = 'true';
113
			}
114
			elseif((isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn']))) {
115
				$_POST['isLoggedIn'] = 'false';
116
			}
117
		}
118
		else{
119
			$response = $this->anonymous();
120
			if(isset($response['authorized']) && !empty($response['authorized'])){
121
				$authorized = $response['authorized'];
122
				$_POST['userId'] = $authorized['Id'];
123
				$_POST['Id'] = $authorized['cartId'];
124
				if(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])){
125
					$_POST['isLoggedIn'] = 'false';
126
				}
127
			}
128
		}
129
		if(isset($email) && $email != 'null'){
130
			$_POST['email'] = $email;
131
		}
132
		$response=$this->cart_model->getCart($this->input->get(),$this->input->post(),$this->config->item('cart_add'));
133
		if(isset($response['response']['addcart'][0]->message) && !empty($response['response']['addcart'][0]->message)){
10914 lgm 134
			if(isset($itemname) && $itemname != 'null'){
135
				if(!isset($email) || $email == 'null'){
136
					$productinfo = 'productinfo'.$itemname;
137
					deleteFileCache($productinfo);
138
				}
139
			}
10582 lgm 140
			echo json_encode($response['response']);
141
		}
10726 lgm 142
		elseif(isset($response['response']['addcart'][0]->redirectUrl) && $response['response']['addcart'][0]->redirectUrl == 'cart'){
10582 lgm 143
			$authorized = $this->session->userdata('authorized');
144
			if(isset($authorized) && !empty($authorized)){
10726 lgm 145
				$cartCount = $authorized['totalItems'];
10582 lgm 146
				$newValues = $authorized;
10726 lgm 147
				$newValues['totalItems'] = $cartCount+1;
10582 lgm 148
				$this->session->set_userdata('authorized',$newValues);
149
			}
150
			echo json_encode($response['response']);
151
			//redirect(base_url().strtolower(__CLASS__),'refresh');
10726 lgm 152
		}elseif(isset($response['response']['addcart'][0]->redirectUrl) && $response['response']['addcart'][0]->redirectUrl != 'cart'){
153
			echo json_encode($response['response']);
10582 lgm 154
		}
155
	}
156
	public function update($id=null,$qty=null)
157
	{
158
		$response=array();
159
		$authorized = $this->session->userdata('authorized');
160
		if(isset($id) and !empty($id))
161
		{
162
			//if user logged-in
163
			$_POST['itemId'] = $id;
164
		}
165
		if(isset($qty) and !empty($qty))
166
		{
167
			//if user logged-in
168
			$_POST['quantity'] = $qty;
169
		}
170
		if(isset($authorized) && !empty($authorized)){
171
		    $_POST['userId'] = $authorized['Id'];
172
			$_POST['_method'] = 'put';
173
			$cartId = $authorized['cartId'];
174
			if($authorized['isLoggedIn'] == 1){
175
				$_POST['isLoggedIn'] = 'true';
176
			}
177
			elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
11523 lgm 178
				$_POST['isLoggedIn'] = 'false';
10582 lgm 179
			}
180
		}
181
		$response=$this->cart_model->getCart($this->input->get(),$this->input->post(),$this->config->item('cart_update'),$cartId);
182
		redirect(base_url().strtolower(__CLASS__),'refresh');
183
 
184
	}
185
	public function delete($id=null)
186
	{
187
 
188
		$authorized = $this->session->userdata('authorized');
189
		if(isset($id) and !empty($id) and is_numeric($id))
190
			{
191
				//if user logged-in
192
				$_POST['itemId'] = $id;
193
			}
194
		else{
195
			redirect(base_url());
196
		}
197
		if(isset($authorized) && !empty($authorized)){
198
			$_POST['userId'] = $authorized['Id'];
199
			$_POST['_method'] = 'delete';
200
			$cartId = $authorized['cartId'];
201
			if($authorized['isLoggedIn'] == 1){
202
				$_POST['isLoggedIn'] = 'true';
203
			}
204
			elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
205
				$_POST['isLoggedIn'] = 'false';
206
			}
207
		}
208
		$response=array();
209
		if(isset($id) and !empty($id))
210
		{
211
			$response=$this->cart_model->getCart($this->input->get(),$this->input->post(),$this->config->item('cart_delete'),$cartId);
212
			if(isset($response['response']['deletecart'][0]->lines)) {
213
				$cartCount = sizeof($response['response']['deletecart'][0]->lines);
214
				$authorized = $this->session->userdata('authorized');
215
				if(isset($authorized) && !empty($authorized)){
216
					$newValues = $authorized;
217
					$newValues['totalItems'] = $cartCount;
218
					$this->session->set_userdata('authorized',$newValues);
219
				}
220
			}else{
221
				redirect(base_url().strtolower(__CLASS__), 'refresh');		
222
			}
223
		}
224
		else
225
		{
226
			$response['response']['msg'] = 'Invalid Parameter!';
227
			redirect(base_url().strtolower(__CLASS__), 'refresh');
228
 
229
		}
230
		redirect(base_url().strtolower(__CLASS__), 'refresh');
231
 
232
	}
233
	public function coupon($code=null)
234
	{
235
		$response=array();
236
		$authorized = $this->session->userdata('authorized');
237
		if(isset($authorized) && !empty($authorized)){
238
		    $_GET['userId'] = $authorized['Id'];
239
			$_GET['action'] = 'applycoupon';
240
			$cartId = $authorized['cartId'];
241
			if($authorized['isLoggedIn'] == 1){
242
				$_GET['isLoggedIn'] = 'true';
243
			}
244
			elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
245
				$_GET['isLoggedIn'] = 'false';
246
			}
247
		}
248
		if(isset($code) and !empty($code))
249
		{
250
			//if user logged-in
251
			$_GET['coupon_code'] = $code;
252
			//$d=$this->input->get();
253
			}
254
		$response=$this->cart_model->getCart($this->input->get(),$this->input->post(),$this->config->item('cart_coupon'),$cartId);
255
		$result = $response['response']['cartcoupon'][0];
256
		if(isset($result->couponCode) && !empty($result->couponCode)){
257
			$couponCode = $result->couponCode;
258
		}
259
		else{
260
			$couponCode = '';
261
		}
262
		$res = array('totalPrice'=>$result->totalPrice,'discountedPrice'=>$result->discountedPrice,'message'=>$result->message,'couponCode'=>$couponCode);
263
		echo json_encode($res);
264
	}
265
	public function couponClear($code=null)
266
	{
267
		$response=array();
268
		$authorized = $this->session->userdata('authorized');
269
		if(isset($authorized) && !empty($authorized)){
270
		    $_GET['userId'] = $authorized['Id'];
271
			$_GET['action'] = 'removecoupon';
272
			$cartId = $authorized['cartId'];
273
			if($authorized['isLoggedIn'] == 1){
274
				$_GET['isLoggedIn'] = 'true';
275
			}
276
			elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
277
				$_GET['isLoggedIn'] = 'false';
278
			}
279
		}
280
		if(isset($code) and !empty($code))
281
		{
282
			//if user logged-in
283
			$_GET['coupon_code'] = $code;
284
			//$d=$this->input->get();
285
			}
286
		//print_r($this->config->item('cart_couponclear'));
287
		$response=$this->cart_model->getCart($this->input->get(),$this->input->post(),$this->config->item('cart_couponclear'),$cartId);
288
		$result = $response['response']['cartcouponclear'][0];
289
		$res = array('totalPrice'=>$result->totalPrice,'discountedPrice'=>$result->discountedPrice,'message'=>$result->message);
290
		echo json_encode($res);
291
	}
292
	public function cartCount()
293
	{
294
		$response=array();
295
		$authorized = $this->session->userdata('authorized');
296
		$shoppingId =$this->session->userdata('shoppingId');
297
		if((isset($authorized) and !empty($authorized['id'])) || ((isset($shoppingId) && !empty($shoppingId)))){
298
		$response=$this->cart_model->getCart($this->input->get(),$this->input->post(),$this->config->item('cart_count'));
299
		if(isset($response['response']['cartcount'][0])){
300
			$result = $response['response']['cartcount'][0];
301
			echo $result;
302
			}
303
			else{
304
				$result = 0;
305
				echo $result;
306
			}
307
		}
308
		else{
309
			$result = 0;
310
			echo $result;
311
		}
312
	}
313
 
314
	public function insure($itemId,$toInsure,$insuranceType){
315
		$authorized = $this->session->userdata('authorized');
316
		if(isset($authorized) && !empty($authorized)){
317
			$_GET['userId'] = $authorized['Id'];
318
			$_GET['Id'] = $authorized['cartId'];
319
			if($authorized['isLoggedIn'] == 1){
320
				$_GET['isLoggedIn'] = 'true';
321
			}
322
			elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
323
				$_GET['isLoggedIn'] = 'false';
324
			}
325
		}
326
		$_GET['itemId'] = $itemId;
327
		$_GET['toInsure'] = $toInsure;
328
		$_GET['insuranceType'] = $insuranceType;
329
		$response=$this->cart_model->getCart($this->input->get(),$this->input->post(),$this->config->item('cart_insure'));
330
		$insurance = $response['response']['cartinsure'][0]->insuranceResult;
331
		if(strcasecmp($insurance, 'success') == 0){
332
			//$status[0] = true;
333
			$res = array('status'=>true);
334
			echo json_encode($res);
335
		}
336
		elseif(strcasecmp($insurance, 'failure') == 0) {
337
			$status[0] = false;
338
			echo json_encode($status);
339
		}
340
	}
341
	//improve functinalities using ajax
342
	public function anonymous()
343
	{
344
		$url = $this->config->item('anonymous');
345
		$url = $url['url'];
346
		$url = $this->config->item('curl_base_url').$url;
347
		$params = array();
348
		$this->mcurl->add_call('anonymous','get',$url,$params);
349
        $responses = $this->mcurl->execute($url);
350
        $response = array();
351
		if(isset($responses['anonymous']['response']) && !empty($responses['anonymous']['response'])){
352
			$data = json_decode($responses['anonymous']['response']);
353
			$childarray=array();
354
			$childarray['Id']=$data->userId;
355
			$childarray['isLoggedIn']=$data->isLoggedIn;
356
			$childarray['pincode']=$data->pincode;
357
			$childarray['email']=$data->email;
358
			$childarray['totalItems']=$data->totalItems;
359
      		$childarray['cartId']=$data->cartId;
360
			$response['authorized']=$childarray;
361
			$this->session->set_userdata('authorized',$response['authorized']);
362
		}
363
		return $response;
364
	}
365
	public function delete_ajax()
366
	{}
367
}
368
 
369
/* End of file welcome.php */
11529 anikendra 370
/* Location: ./application/controllers/welcome.php */