Subversion Repositories SmartDukaan

Rev

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