Subversion Repositories SmartDukaan

Rev

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