Subversion Repositories SmartDukaan

Rev

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