Subversion Repositories SmartDukaan

Rev

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