Subversion Repositories SmartDukaan

Rev

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