Subversion Repositories SmartDukaan

Rev

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