Subversion Repositories SmartDukaan

Rev

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