Subversion Repositories SmartDukaan

Rev

Rev 10726 | Go to most recent revision | Details | 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
 
58
	public function add($id=null,$email=null)
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
 
97
		if(isset($response['response']['addcart'][0]->message) && !empty($response['response']['addcart'][0]->message)){
98
			echo json_encode($response['response']);
99
		}
100
		else{
101
			$cartCount = sizeof($response['response']['addcart'][0]->lines);
102
			$authorized = $this->session->userdata('authorized');
103
			if(isset($authorized) && !empty($authorized)){
104
				$newValues = $authorized;
105
				$newValues['totalItems'] = $cartCount;
106
				$this->session->set_userdata('authorized',$newValues);
107
			}
108
			echo json_encode($response['response']);
109
			//redirect(base_url().strtolower(__CLASS__),'refresh');
110
		}
111
	}
112
	public function update($id=null,$qty=null)
113
	{
114
		$response=array();
115
		$authorized = $this->session->userdata('authorized');
116
		if(isset($id) and !empty($id))
117
		{
118
			//if user logged-in
119
			$_POST['itemId'] = $id;
120
		}
121
		if(isset($qty) and !empty($qty))
122
		{
123
			//if user logged-in
124
			$_POST['quantity'] = $qty;
125
		}
126
		if(isset($authorized) && !empty($authorized)){
127
		    $_POST['userId'] = $authorized['Id'];
128
			$_POST['_method'] = 'put';
129
			$cartId = $authorized['cartId'];
130
			if($authorized['isLoggedIn'] == 1){
131
				$_POST['isLoggedIn'] = 'true';
132
			}
133
			elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
134
				$_GET['isLoggedIn'] = 'false';
135
			}
136
		}
137
		$response=$this->cart_model->getCart($this->input->get(),$this->input->post(),$this->config->item('cart_update'),$cartId);
138
		redirect(base_url().strtolower(__CLASS__),'refresh');
139
 
140
	}
141
	public function delete($id=null)
142
	{
143
 
144
		$authorized = $this->session->userdata('authorized');
145
		if(isset($id) and !empty($id) and is_numeric($id))
146
			{
147
				//if user logged-in
148
				$_POST['itemId'] = $id;
149
			}
150
		else{
151
			redirect(base_url());
152
		}
153
		if(isset($authorized) && !empty($authorized)){
154
			$_POST['userId'] = $authorized['Id'];
155
			$_POST['_method'] = 'delete';
156
			$cartId = $authorized['cartId'];
157
			if($authorized['isLoggedIn'] == 1){
158
				$_POST['isLoggedIn'] = 'true';
159
			}
160
			elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
161
				$_POST['isLoggedIn'] = 'false';
162
			}
163
		}
164
		$response=array();
165
		if(isset($id) and !empty($id))
166
		{
167
			$response=$this->cart_model->getCart($this->input->get(),$this->input->post(),$this->config->item('cart_delete'),$cartId);
168
			if(isset($response['response']['deletecart'][0]->lines)) {
169
				$cartCount = sizeof($response['response']['deletecart'][0]->lines);
170
				$authorized = $this->session->userdata('authorized');
171
				if(isset($authorized) && !empty($authorized)){
172
					$newValues = $authorized;
173
					$newValues['totalItems'] = $cartCount;
174
					$this->session->set_userdata('authorized',$newValues);
175
				}
176
			}else{
177
				redirect(base_url().strtolower(__CLASS__), 'refresh');		
178
			}
179
		}
180
		else
181
		{
182
			$response['response']['msg'] = 'Invalid Parameter!';
183
			redirect(base_url().strtolower(__CLASS__), 'refresh');
184
 
185
		}
186
		redirect(base_url().strtolower(__CLASS__), 'refresh');
187
 
188
	}
189
	public function coupon($code=null)
190
	{
191
		$response=array();
192
		$authorized = $this->session->userdata('authorized');
193
		if(isset($authorized) && !empty($authorized)){
194
		    $_GET['userId'] = $authorized['Id'];
195
			$_GET['action'] = 'applycoupon';
196
			$cartId = $authorized['cartId'];
197
			if($authorized['isLoggedIn'] == 1){
198
				$_GET['isLoggedIn'] = 'true';
199
			}
200
			elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
201
				$_GET['isLoggedIn'] = 'false';
202
			}
203
		}
204
		if(isset($code) and !empty($code))
205
		{
206
			//if user logged-in
207
			$_GET['coupon_code'] = $code;
208
			//$d=$this->input->get();
209
			}
210
		$response=$this->cart_model->getCart($this->input->get(),$this->input->post(),$this->config->item('cart_coupon'),$cartId);
211
		$result = $response['response']['cartcoupon'][0];
212
		if(isset($result->couponCode) && !empty($result->couponCode)){
213
			$couponCode = $result->couponCode;
214
		}
215
		else{
216
			$couponCode = '';
217
		}
218
		$res = array('totalPrice'=>$result->totalPrice,'discountedPrice'=>$result->discountedPrice,'message'=>$result->message,'couponCode'=>$couponCode);
219
		echo json_encode($res);
220
	}
221
	public function couponClear($code=null)
222
	{
223
		$response=array();
224
		$authorized = $this->session->userdata('authorized');
225
		if(isset($authorized) && !empty($authorized)){
226
		    $_GET['userId'] = $authorized['Id'];
227
			$_GET['action'] = 'removecoupon';
228
			$cartId = $authorized['cartId'];
229
			if($authorized['isLoggedIn'] == 1){
230
				$_GET['isLoggedIn'] = 'true';
231
			}
232
			elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
233
				$_GET['isLoggedIn'] = 'false';
234
			}
235
		}
236
		if(isset($code) and !empty($code))
237
		{
238
			//if user logged-in
239
			$_GET['coupon_code'] = $code;
240
			//$d=$this->input->get();
241
			}
242
		//print_r($this->config->item('cart_couponclear'));
243
		$response=$this->cart_model->getCart($this->input->get(),$this->input->post(),$this->config->item('cart_couponclear'),$cartId);
244
		$result = $response['response']['cartcouponclear'][0];
245
		$res = array('totalPrice'=>$result->totalPrice,'discountedPrice'=>$result->discountedPrice,'message'=>$result->message);
246
		echo json_encode($res);
247
	}
248
	public function cartCount()
249
	{
250
		$response=array();
251
		$authorized = $this->session->userdata('authorized');
252
		$shoppingId =$this->session->userdata('shoppingId');
253
		if((isset($authorized) and !empty($authorized['id'])) || ((isset($shoppingId) && !empty($shoppingId)))){
254
		$response=$this->cart_model->getCart($this->input->get(),$this->input->post(),$this->config->item('cart_count'));
255
		if(isset($response['response']['cartcount'][0])){
256
			$result = $response['response']['cartcount'][0];
257
			echo $result;
258
			}
259
			else{
260
				$result = 0;
261
				echo $result;
262
			}
263
		}
264
		else{
265
			$result = 0;
266
			echo $result;
267
		}
268
	}
269
 
270
	public function insure($itemId,$toInsure,$insuranceType){
271
		$authorized = $this->session->userdata('authorized');
272
		if(isset($authorized) && !empty($authorized)){
273
			$_GET['userId'] = $authorized['Id'];
274
			$_GET['Id'] = $authorized['cartId'];
275
			if($authorized['isLoggedIn'] == 1){
276
				$_GET['isLoggedIn'] = 'true';
277
			}
278
			elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
279
				$_GET['isLoggedIn'] = 'false';
280
			}
281
		}
282
		$_GET['itemId'] = $itemId;
283
		$_GET['toInsure'] = $toInsure;
284
		$_GET['insuranceType'] = $insuranceType;
285
		$response=$this->cart_model->getCart($this->input->get(),$this->input->post(),$this->config->item('cart_insure'));
286
		$insurance = $response['response']['cartinsure'][0]->insuranceResult;
287
		if(strcasecmp($insurance, 'success') == 0){
288
			//$status[0] = true;
289
			$res = array('status'=>true);
290
			echo json_encode($res);
291
		}
292
		elseif(strcasecmp($insurance, 'failure') == 0) {
293
			$status[0] = false;
294
			echo json_encode($status);
295
		}
296
	}
297
	//improve functinalities using ajax
298
	public function anonymous()
299
	{
300
		$url = $this->config->item('anonymous');
301
		$url = $url['url'];
302
		$url = $this->config->item('curl_base_url').$url;
303
		$params = array();
304
		$this->mcurl->add_call('anonymous','get',$url,$params);
305
        $responses = $this->mcurl->execute($url);
306
        $response = array();
307
		if(isset($responses['anonymous']['response']) && !empty($responses['anonymous']['response'])){
308
			$data = json_decode($responses['anonymous']['response']);
309
			$childarray=array();
310
			$childarray['Id']=$data->userId;
311
			$childarray['isLoggedIn']=$data->isLoggedIn;
312
			$childarray['pincode']=$data->pincode;
313
			$childarray['email']=$data->email;
314
			$childarray['totalItems']=$data->totalItems;
315
      		$childarray['cartId']=$data->cartId;
316
			$response['authorized']=$childarray;
317
			$this->session->set_userdata('authorized',$response['authorized']);
318
		}
319
		return $response;
320
	}
321
	public function delete_ajax()
322
	{}
323
}
324
 
325
/* End of file welcome.php */
326
/* Location: ./application/controllers/welcome.php */