Subversion Repositories SmartDukaan

Rev

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