Subversion Repositories SmartDukaan

Rev

Rev 11846 | Rev 11924 | 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);
11749 lgm 182
		redirect(base_url().strtolower(__CLASS__));
10582 lgm 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);
11749 lgm 219
					redirect(base_url().strtolower(__CLASS__));
10582 lgm 220
				}
221
			}else{
11749 lgm 222
				redirect(base_url().strtolower(__CLASS__));		
10582 lgm 223
			}
224
		}
225
		else
226
		{
227
			$response['response']['msg'] = 'Invalid Parameter!';
11749 lgm 228
			redirect(base_url().strtolower(__CLASS__));
10582 lgm 229
 
230
		}
11749 lgm 231
		redirect(base_url().strtolower(__CLASS__));
10582 lgm 232
 
233
	}
234
	public function coupon($code=null)
235
	{
236
		$response=array();
237
		$authorized = $this->session->userdata('authorized');
238
		if(isset($authorized) && !empty($authorized)){
239
		    $_GET['userId'] = $authorized['Id'];
240
			$_GET['action'] = 'applycoupon';
241
			$cartId = $authorized['cartId'];
242
			if($authorized['isLoggedIn'] == 1){
243
				$_GET['isLoggedIn'] = 'true';
244
			}
245
			elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
246
				$_GET['isLoggedIn'] = 'false';
247
			}
248
		}
249
		if(isset($code) and !empty($code))
250
		{
251
			//if user logged-in
252
			$_GET['coupon_code'] = $code;
253
			//$d=$this->input->get();
254
			}
255
		$response=$this->cart_model->getCart($this->input->get(),$this->input->post(),$this->config->item('cart_coupon'),$cartId);
11885 anikendra 256
		$dealTextArray = array();
257
		if(!empty($response)){			
258
			foreach($response['response']['cartcoupon'][0]->lines AS $line) {
259
				$dealTextArray[$line->itemId] = $line->dealText;
260
			}
261
		}
10582 lgm 262
		$result = $response['response']['cartcoupon'][0];
263
		if(isset($result->couponCode) && !empty($result->couponCode)){
264
			$couponCode = $result->couponCode;
265
		}
266
		else{
267
			$couponCode = '';
268
		}
11846 anikendra 269
		if($result->discountedPrice==0){
270
			$res = array('totalPrice'=>$result->totalPrice,'discountedPrice'=>$result->totalPrice,'message'=>$result->message,'couponCode'=>$couponCode);	
271
		} else {
272
			$res = array('totalPrice'=>$result->totalPrice,'discountedPrice'=>$result->discountedPrice,'message'=>$result->message,'couponCode'=>$couponCode);
273
		}
11885 anikendra 274
		$res['dealTextArray']=$dealTextArray;
10582 lgm 275
		echo json_encode($res);
276
	}
277
	public function couponClear($code=null)
278
	{
279
		$response=array();
280
		$authorized = $this->session->userdata('authorized');
281
		if(isset($authorized) && !empty($authorized)){
282
		    $_GET['userId'] = $authorized['Id'];
283
			$_GET['action'] = 'removecoupon';
284
			$cartId = $authorized['cartId'];
285
			if($authorized['isLoggedIn'] == 1){
286
				$_GET['isLoggedIn'] = 'true';
287
			}
288
			elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
289
				$_GET['isLoggedIn'] = 'false';
290
			}
291
		}
292
		if(isset($code) and !empty($code))
293
		{
294
			//if user logged-in
295
			$_GET['coupon_code'] = $code;
296
			//$d=$this->input->get();
297
			}
298
		//print_r($this->config->item('cart_couponclear'));
299
		$response=$this->cart_model->getCart($this->input->get(),$this->input->post(),$this->config->item('cart_couponclear'),$cartId);
300
		$result = $response['response']['cartcouponclear'][0];
301
		$res = array('totalPrice'=>$result->totalPrice,'discountedPrice'=>$result->discountedPrice,'message'=>$result->message);
302
		echo json_encode($res);
303
	}
304
	public function cartCount()
305
	{
306
		$response=array();
307
		$authorized = $this->session->userdata('authorized');
308
		$shoppingId =$this->session->userdata('shoppingId');
309
		if((isset($authorized) and !empty($authorized['id'])) || ((isset($shoppingId) && !empty($shoppingId)))){
310
		$response=$this->cart_model->getCart($this->input->get(),$this->input->post(),$this->config->item('cart_count'));
311
		if(isset($response['response']['cartcount'][0])){
312
			$result = $response['response']['cartcount'][0];
313
			echo $result;
314
			}
315
			else{
316
				$result = 0;
317
				echo $result;
318
			}
319
		}
320
		else{
321
			$result = 0;
322
			echo $result;
323
		}
324
	}
325
 
326
	public function insure($itemId,$toInsure,$insuranceType){
327
		$authorized = $this->session->userdata('authorized');
328
		if(isset($authorized) && !empty($authorized)){
329
			$_GET['userId'] = $authorized['Id'];
330
			$_GET['Id'] = $authorized['cartId'];
331
			if($authorized['isLoggedIn'] == 1){
332
				$_GET['isLoggedIn'] = 'true';
333
			}
334
			elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
335
				$_GET['isLoggedIn'] = 'false';
336
			}
337
		}
338
		$_GET['itemId'] = $itemId;
339
		$_GET['toInsure'] = $toInsure;
340
		$_GET['insuranceType'] = $insuranceType;
341
		$response=$this->cart_model->getCart($this->input->get(),$this->input->post(),$this->config->item('cart_insure'));
342
		$insurance = $response['response']['cartinsure'][0]->insuranceResult;
343
		if(strcasecmp($insurance, 'success') == 0){
344
			//$status[0] = true;
345
			$res = array('status'=>true);
346
			echo json_encode($res);
347
		}
348
		elseif(strcasecmp($insurance, 'failure') == 0) {
349
			$status[0] = false;
350
			echo json_encode($status);
351
		}
352
	}
353
	//improve functinalities using ajax
354
	public function anonymous()
355
	{
356
		$url = $this->config->item('anonymous');
357
		$url = $url['url'];
358
		$url = $this->config->item('curl_base_url').$url;
359
		$params = array();
360
		$this->mcurl->add_call('anonymous','get',$url,$params);
361
        $responses = $this->mcurl->execute($url);
362
        $response = array();
363
		if(isset($responses['anonymous']['response']) && !empty($responses['anonymous']['response'])){
364
			$data = json_decode($responses['anonymous']['response']);
365
			$childarray=array();
366
			$childarray['Id']=$data->userId;
367
			$childarray['isLoggedIn']=$data->isLoggedIn;
368
			$childarray['pincode']=$data->pincode;
369
			$childarray['email']=$data->email;
370
			$childarray['totalItems']=$data->totalItems;
371
      		$childarray['cartId']=$data->cartId;
372
			$response['authorized']=$childarray;
373
			$this->session->set_userdata('authorized',$response['authorized']);
374
		}
375
		return $response;
376
	}
377
	public function delete_ajax()
378
	{}
379
}
380
 
381
/* End of file welcome.php */
11529 anikendra 382
/* Location: ./application/controllers/welcome.php */