Subversion Repositories SmartDukaan

Rev

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