Subversion Repositories SmartDukaan

Rev

Rev 13794 | Rev 13815 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
App::uses('AppController', 'Controller');
3
/**
4
 * StoreProducts Controller
5
 *
6
 * @property StoreProduct $StoreProduct
7
 * @property PaginatorComponent $Paginator
8
 */
9
class StoreProductsController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
13808 anikendra 17
	public $apihost;	
13532 anikendra 18
 
19
	public function beforeFilter() {
20
		parent::beforeFilter();
13808 anikendra 21
		$this->Auth->allow('bycategory','category','mine','getdeals');
22
		$callback = $this->request->query('callback');		
13532 anikendra 23
	}
13570 anikendra 24
 
13808 anikendra 25
	public function search() {
26
		$q = $this->request->query('q');
27
		if(isset($q) && !empty($q)){
28
			//Handle with solr
29
		}
30
	}
31
 
13736 anikendra 32
	public function mine() {		
13591 anikendra 33
		$userId = $this->request->query('user_id');
34
		if(isset($userId) && !empty($userId)){
13597 anikendra 35
			$this->loadModel('User');
36
			$dbuser = $this->User->findById($userId);
37
			$this->Auth->login($dbuser['User']);
13591 anikendra 38
		}
13808 anikendra 39
		// $this->checkMobileNumber();
13583 anikendra 40
		$likedDeals = $disLikedDeals = array();
13570 anikendra 41
		$this->loadModel('Api');
42
		$page = $this->request->query('page');
43
		if(!isset($page)){
44
			$page = 1;
13808 anikendra 45
		}				
46
		//Sort and Direction
47
		$sort = $this->request->query('sort');
48
		$direction = $this->request->query('direction');
49
		$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),0,$sort,$direction);		
13794 anikendra 50
		$deals = $this->make_request($url,null);
51
		// debug($deals);
13583 anikendra 52
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
53
		if(!empty($myactions)) {
54
			foreach ($myactions['actions'] as $key => $value) {
55
				if($value['UserAction']['action'] == 'like'){
56
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
57
				}else{
58
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
59
				}
60
			}
61
		}
13794 anikendra 62
		$title_for_layout = "Get Cashback on favourite";
63
		$description = "Get cashback on your favourite products. Mouth watering deals";
13808 anikendra 64
		$this->set(compact('page', 'title_for_layout', 'description', 'deals', 'likedDeals','disLikedDeals','sort','direction'));
13570 anikendra 65
		$this->render('/Pages/home');
66
	}
67
 
68
	public function getdeals() {
13583 anikendra 69
		$likedDeals = $disLikedDeals = array();
13570 anikendra 70
		$this->layout = 'ajax';
71
		$this->loadModel('Api');
72
		$page = $this->request->query('page');
73
		if(!isset($page)){
74
			$page = 1;
13794 anikendra 75
		}	
76
		// $apideals = $this->Api->getDeals($this->Auth->User('id'),$page);
77
		// $deals = $apideals['products'];
13808 anikendra 78
		$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),0,$sort,$direction);
13794 anikendra 79
		$deals = $this->make_request($url,null);
13583 anikendra 80
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
81
		if(!empty($myactions)) {
82
			foreach ($myactions['actions'] as $key => $value) {
83
				if($value['UserAction']['action'] == 'like'){
84
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
85
				}else{
86
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
87
				}
88
			}
89
		}
13794 anikendra 90
		$title_for_layout = "Get Cashback on your favourite products";
91
		$description = "Get cashback on your favourite products. Mouth watering deals";
13808 anikendra 92
		$this->set(compact('page', 'title_for_layout', 'description', 'deals', 'likedDeals','disLikedDeals','sort','direction'));
13570 anikendra 93
		$this->render('/Elements/deals');
94
	}	
13532 anikendra 95
/**
96
 * index method
97
 *
98
 * @return void
99
 */
13550 anikendra 100
	public function index($user_id=null) {
13532 anikendra 101
		$this->response->type('json');
102
		$this->layout = 'ajax';
13550 anikendra 103
		$limit = 20;
104
		$this->StoreProduct->recursive = -1;
105
		$count = $this->StoreProduct->find('count');
106
		$this->StoreProduct->Behaviors->attach('Containable');
13689 anikendra 107
		$this->Paginator->settings = array('order'=>array('rank'=>'asc'),'contain'=>array('Product'),'limit'=>$limit,'fields'=>array('Product.category_id','StoreProduct.id','StoreProduct.title','StoreProduct.thumbnail','StoreProduct.available_price','StoreProduct.cashback','StoreProduct.tagline','StoreProduct.offer','StoreProduct.cashback','StoreProduct.available_price','Product.name'));
13550 anikendra 108
		$result = array('products' => $this->Paginator->paginate(),'maxresults'=>ceil($count/$limit));
13532 anikendra 109
		$callback = $this->request->query('callback');
110
		$this->set(array(
111
		    'result' => $result,
112
		    'callback' => $callback,
113
		    '_serialize' => array('result')
114
		));
13567 anikendra 115
		$this->render('/Elements/json');
13532 anikendra 116
	}
117
 
13579 anikendra 118
	public function category($user_id=null,$categoryId=null) {
119
		$this->response->type('json');
120
		$this->layout = 'ajax';
121
		$limit = 20;
122
		$this->StoreProduct->recursive = -1;
123
		// $count = $this->StoreProduct->find('count');
124
		$this->StoreProduct->Behaviors->attach('Containable');
13723 anikendra 125
		$this->Paginator->settings = array('order'=>array('rank'=>'asc'),'conditions'=>array('StoreProduct.category_id'=>$categoryId),'contain'=>array('Product'),'limit'=>$limit,'fields'=>array('Product.category_id','StoreProduct.id','StoreProduct.title','StoreProduct.thumbnail','StoreProduct.available_price','StoreProduct.cashback','StoreProduct.tagline','StoreProduct.offer','StoreProduct.available_price'));
13583 anikendra 126
		$result = array('products' => $this->Paginator->paginate());
13579 anikendra 127
		$callback = $this->request->query('callback');
128
		$this->set(array(
129
		    'result' => $result,
130
		    'callback' => $callback,
131
		    '_serialize' => array('result')
132
		));
133
		$this->render('/Elements/json');
134
	}
135
 
13550 anikendra 136
	public function bycategory($userId=null) {
13541 anikendra 137
		$this->loadModel('UserCategory');
138
		$options = array('conditions' => array('user_id'=>$userId),'recursive'=>-1);
139
		$userCategories = $this->UserCategory->find('all',$options);		
140
		$this->response->type('json');
141
		$this->layout = 'ajax';
13550 anikendra 142
		$limit = 20;
13541 anikendra 143
		$conditions = null;
13550 anikendra 144
		$this->StoreProduct->recursive = -1;
145
		$this->StoreProduct->Behaviors->attach('Containable');
146
		$products = array();
13700 anikendra 147
		// if(!empty($userCategories)){
148
		if(1==2){
13541 anikendra 149
			foreach ($userCategories as $key => $value) {
150
				$categoryIds[] = $value['UserCategory']['category_id'];
13700 anikendra 151
				$conditions = array('StoreProduct.category_id'=>$value['UserCategory']['category_id']);
13689 anikendra 152
				$this->Paginator->settings = array('contain'=>array('Product'),'limit'=>$limit,'fields'=>array('Product.category_id','StoreProduct.id','StoreProduct.title','StoreProduct.thumbnail','StoreProduct.available_price','StoreProduct.cashback','StoreProduct.available_price','StoreProduct.tagline','StoreProduct.offer'),'conditions'=>$conditions);
13550 anikendra 153
				$rows = $this->Paginator->paginate();				
154
				foreach($rows AS $key => $product){
155
					$products[$value['UserCategory']['category_id']][$key][] = $product;
156
				}
13541 anikendra 157
			}
13567 anikendra 158
		}else{
159
			//Fetch all categories
160
			$categories = $this->StoreProduct->Product->Category->find('all');
161
			foreach ($categories as $key => $value) {
162
				$categoryIds[] = $value['Category']['id'];
13700 anikendra 163
				$conditions = array('StoreProduct.category_id'=>$value['Category']['id']);
13689 anikendra 164
				$this->Paginator->settings = array('contain'=>array('Product'),'limit'=>$limit,'fields'=>array('Product.category_id','StoreProduct.id','StoreProduct.title','StoreProduct.thumbnail','StoreProduct.available_price','StoreProduct.cashback','StoreProduct.available_price','StoreProduct.tagline','StoreProduct.offer'),'conditions'=>$conditions);
13567 anikendra 165
				$rows = $this->Paginator->paginate();				
166
				foreach($rows AS $key => $product){
167
					$products[$value['Category']['id']][$key][] = $product;
168
				}
169
			}
13700 anikendra 170
		}		
171
		$this->log(print_r($products,1),'bycategory')	;		
13550 anikendra 172
		$result = array('products' => $products);//,'categories'=>$categoryIds);
13541 anikendra 173
		$callback = $this->request->query('callback');
174
		$this->set(array(
175
		    'result' => $result,
176
		    'callback' => $callback,
177
		    '_serialize' => array('result')
178
		));
13567 anikendra 179
		$this->render('/Elements/json');
13541 anikendra 180
	}
181
 
13532 anikendra 182
/**
183
 * view method
184
 *
185
 * @throws NotFoundException
186
 * @param string $id
187
 * @return void
188
 */
189
	public function view($id = null) {
190
		if (!$this->StoreProduct->exists($id)) {
191
			throw new NotFoundException(__('Invalid store product'));
192
		}
193
		$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
194
		$this->set('storeProduct', $this->StoreProduct->find('first', $options));
195
	}
196
 
197
/**
198
 * add method
199
 *
200
 * @return void
201
 */
202
	public function add() {
203
		if ($this->request->is('post')) {
204
			$this->StoreProduct->create();
205
			if ($this->StoreProduct->save($this->request->data)) {
206
				$this->Session->setFlash(__('The store product has been saved.'));
207
				return $this->redirect(array('action' => 'index'));
208
			} else {
209
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
210
			}
211
		}
212
		$stores = $this->StoreProduct->Store->find('list');
213
		$products = $this->StoreProduct->Product->find('list');
214
		$this->set(compact('stores', 'products'));
215
	}
216
 
217
/**
218
 * edit method
219
 *
220
 * @throws NotFoundException
221
 * @param string $id
222
 * @return void
223
 */
224
	public function edit($id = null) {
225
		if (!$this->StoreProduct->exists($id)) {
226
			throw new NotFoundException(__('Invalid store product'));
227
		}
228
		if ($this->request->is(array('post', 'put'))) {
229
			if ($this->StoreProduct->save($this->request->data)) {
230
				$this->Session->setFlash(__('The store product has been saved.'));
231
				return $this->redirect(array('action' => 'index'));
232
			} else {
233
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
234
			}
235
		} else {
236
			$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
237
			$this->request->data = $this->StoreProduct->find('first', $options);
238
		}
239
		$stores = $this->StoreProduct->Store->find('list');
240
		$products = $this->StoreProduct->Product->find('list');
241
		$this->set(compact('stores', 'products'));
242
	}
243
 
244
/**
245
 * delete method
246
 *
247
 * @throws NotFoundException
248
 * @param string $id
249
 * @return void
250
 */
251
	public function delete($id = null) {
252
		$this->StoreProduct->id = $id;
253
		if (!$this->StoreProduct->exists()) {
254
			throw new NotFoundException(__('Invalid store product'));
255
		}
256
		$this->request->onlyAllow('post', 'delete');
257
		if ($this->StoreProduct->delete()) {
258
			$this->Session->setFlash(__('The store product has been deleted.'));
259
		} else {
260
			$this->Session->setFlash(__('The store product could not be deleted. Please, try again.'));
261
		}
262
		return $this->redirect(array('action' => 'index'));
263
	}
264
 
265
/**
266
 * admin_index method
267
 *
268
 * @return void
269
 */
270
	public function admin_index() {
271
		$this->StoreProduct->recursive = 0;
272
		$this->set('storeProducts', $this->Paginator->paginate());
273
	}
274
 
275
/**
276
 * admin_view method
277
 *
278
 * @throws NotFoundException
279
 * @param string $id
280
 * @return void
281
 */
282
	public function admin_view($id = null) {
283
		if (!$this->StoreProduct->exists($id)) {
284
			throw new NotFoundException(__('Invalid store product'));
285
		}
286
		$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
287
		$this->set('storeProduct', $this->StoreProduct->find('first', $options));
288
	}
289
 
290
/**
291
 * admin_add method
292
 *
293
 * @return void
294
 */
295
	public function admin_add() {
296
		if ($this->request->is('post')) {
297
			$this->StoreProduct->create();
298
			if ($this->StoreProduct->save($this->request->data)) {
299
				$this->Session->setFlash(__('The store product has been saved.'));
300
				return $this->redirect(array('action' => 'index'));
301
			} else {
302
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
303
			}
304
		}
305
		$stores = $this->StoreProduct->Store->find('list');
306
		$products = $this->StoreProduct->Product->find('list');
307
		$this->set(compact('stores', 'products'));
308
	}
309
 
310
/**
311
 * admin_edit method
312
 *
313
 * @throws NotFoundException
314
 * @param string $id
315
 * @return void
316
 */
317
	public function admin_edit($id = null) {
318
		if (!$this->StoreProduct->exists($id)) {
319
			throw new NotFoundException(__('Invalid store product'));
320
		}
321
		if ($this->request->is(array('post', 'put'))) {
322
			if ($this->StoreProduct->save($this->request->data)) {
323
				$this->Session->setFlash(__('The store product has been saved.'));
324
				return $this->redirect(array('action' => 'index'));
325
			} else {
326
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
327
			}
328
		} else {
329
			$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
330
			$this->request->data = $this->StoreProduct->find('first', $options);
331
		}
332
		$stores = $this->StoreProduct->Store->find('list');
333
		$products = $this->StoreProduct->Product->find('list');
334
		$this->set(compact('stores', 'products'));
335
	}
336
 
337
/**
338
 * admin_delete method
339
 *
340
 * @throws NotFoundException
341
 * @param string $id
342
 * @return void
343
 */
344
	public function admin_delete($id = null) {
345
		$this->StoreProduct->id = $id;
346
		if (!$this->StoreProduct->exists()) {
347
			throw new NotFoundException(__('Invalid store product'));
348
		}
349
		$this->request->onlyAllow('post', 'delete');
350
		if ($this->StoreProduct->delete()) {
351
			$this->Session->setFlash(__('The store product has been deleted.'));
352
		} else {
353
			$this->Session->setFlash(__('The store product could not be deleted. Please, try again.'));
354
		}
355
		return $this->redirect(array('action' => 'index'));
356
	}}