Subversion Repositories SmartDukaan

Rev

Rev 13596 | Rev 13599 | 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');
13579 anikendra 92
		$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 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');
110
		$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 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();
13541 anikendra 132
		if(!empty($userCategories)){
133
			foreach ($userCategories as $key => $value) {
134
				$categoryIds[] = $value['UserCategory']['category_id'];
13550 anikendra 135
				$conditions = array('Product.category_id'=>$value['UserCategory']['category_id']);
136
				$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);
137
				// $products[$value['UserCategory']['category_id']]['products'] = $this->Paginator->paginate();				
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'];
148
				$conditions = array('Product.category_id'=>$value['Category']['id']);
149
				$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);
150
				$rows = $this->Paginator->paginate();				
151
				foreach($rows AS $key => $product){
152
					$products[$value['Category']['id']][$key][] = $product;
153
				}
154
			}
13550 anikendra 155
		}				
156
		$result = array('products' => $products);//,'categories'=>$categoryIds);
13541 anikendra 157
		$callback = $this->request->query('callback');
158
		$this->set(array(
159
		    'result' => $result,
160
		    'callback' => $callback,
161
		    '_serialize' => array('result')
162
		));
13567 anikendra 163
		$this->render('/Elements/json');
13541 anikendra 164
	}
165
 
13532 anikendra 166
/**
167
 * view method
168
 *
169
 * @throws NotFoundException
170
 * @param string $id
171
 * @return void
172
 */
173
	public function view($id = null) {
174
		if (!$this->StoreProduct->exists($id)) {
175
			throw new NotFoundException(__('Invalid store product'));
176
		}
177
		$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
178
		$this->set('storeProduct', $this->StoreProduct->find('first', $options));
179
	}
180
 
181
/**
182
 * add method
183
 *
184
 * @return void
185
 */
186
	public function add() {
187
		if ($this->request->is('post')) {
188
			$this->StoreProduct->create();
189
			if ($this->StoreProduct->save($this->request->data)) {
190
				$this->Session->setFlash(__('The store product has been saved.'));
191
				return $this->redirect(array('action' => 'index'));
192
			} else {
193
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
194
			}
195
		}
196
		$stores = $this->StoreProduct->Store->find('list');
197
		$products = $this->StoreProduct->Product->find('list');
198
		$this->set(compact('stores', 'products'));
199
	}
200
 
201
/**
202
 * edit method
203
 *
204
 * @throws NotFoundException
205
 * @param string $id
206
 * @return void
207
 */
208
	public function edit($id = null) {
209
		if (!$this->StoreProduct->exists($id)) {
210
			throw new NotFoundException(__('Invalid store product'));
211
		}
212
		if ($this->request->is(array('post', 'put'))) {
213
			if ($this->StoreProduct->save($this->request->data)) {
214
				$this->Session->setFlash(__('The store product has been saved.'));
215
				return $this->redirect(array('action' => 'index'));
216
			} else {
217
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
218
			}
219
		} else {
220
			$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
221
			$this->request->data = $this->StoreProduct->find('first', $options);
222
		}
223
		$stores = $this->StoreProduct->Store->find('list');
224
		$products = $this->StoreProduct->Product->find('list');
225
		$this->set(compact('stores', 'products'));
226
	}
227
 
228
/**
229
 * delete method
230
 *
231
 * @throws NotFoundException
232
 * @param string $id
233
 * @return void
234
 */
235
	public function delete($id = null) {
236
		$this->StoreProduct->id = $id;
237
		if (!$this->StoreProduct->exists()) {
238
			throw new NotFoundException(__('Invalid store product'));
239
		}
240
		$this->request->onlyAllow('post', 'delete');
241
		if ($this->StoreProduct->delete()) {
242
			$this->Session->setFlash(__('The store product has been deleted.'));
243
		} else {
244
			$this->Session->setFlash(__('The store product could not be deleted. Please, try again.'));
245
		}
246
		return $this->redirect(array('action' => 'index'));
247
	}
248
 
249
/**
250
 * admin_index method
251
 *
252
 * @return void
253
 */
254
	public function admin_index() {
255
		$this->StoreProduct->recursive = 0;
256
		$this->set('storeProducts', $this->Paginator->paginate());
257
	}
258
 
259
/**
260
 * admin_view method
261
 *
262
 * @throws NotFoundException
263
 * @param string $id
264
 * @return void
265
 */
266
	public function admin_view($id = null) {
267
		if (!$this->StoreProduct->exists($id)) {
268
			throw new NotFoundException(__('Invalid store product'));
269
		}
270
		$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
271
		$this->set('storeProduct', $this->StoreProduct->find('first', $options));
272
	}
273
 
274
/**
275
 * admin_add method
276
 *
277
 * @return void
278
 */
279
	public function admin_add() {
280
		if ($this->request->is('post')) {
281
			$this->StoreProduct->create();
282
			if ($this->StoreProduct->save($this->request->data)) {
283
				$this->Session->setFlash(__('The store product has been saved.'));
284
				return $this->redirect(array('action' => 'index'));
285
			} else {
286
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
287
			}
288
		}
289
		$stores = $this->StoreProduct->Store->find('list');
290
		$products = $this->StoreProduct->Product->find('list');
291
		$this->set(compact('stores', 'products'));
292
	}
293
 
294
/**
295
 * admin_edit method
296
 *
297
 * @throws NotFoundException
298
 * @param string $id
299
 * @return void
300
 */
301
	public function admin_edit($id = null) {
302
		if (!$this->StoreProduct->exists($id)) {
303
			throw new NotFoundException(__('Invalid store product'));
304
		}
305
		if ($this->request->is(array('post', 'put'))) {
306
			if ($this->StoreProduct->save($this->request->data)) {
307
				$this->Session->setFlash(__('The store product has been saved.'));
308
				return $this->redirect(array('action' => 'index'));
309
			} else {
310
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
311
			}
312
		} else {
313
			$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
314
			$this->request->data = $this->StoreProduct->find('first', $options);
315
		}
316
		$stores = $this->StoreProduct->Store->find('list');
317
		$products = $this->StoreProduct->Product->find('list');
318
		$this->set(compact('stores', 'products'));
319
	}
320
 
321
/**
322
 * admin_delete method
323
 *
324
 * @throws NotFoundException
325
 * @param string $id
326
 * @return void
327
 */
328
	public function admin_delete($id = null) {
329
		$this->StoreProduct->id = $id;
330
		if (!$this->StoreProduct->exists()) {
331
			throw new NotFoundException(__('Invalid store product'));
332
		}
333
		$this->request->onlyAllow('post', 'delete');
334
		if ($this->StoreProduct->delete()) {
335
			$this->Session->setFlash(__('The store product has been deleted.'));
336
		} else {
337
			$this->Session->setFlash(__('The store product could not be deleted. Please, try again.'));
338
		}
339
		return $this->redirect(array('action' => 'index'));
340
	}}