Subversion Repositories SmartDukaan

Rev

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