Subversion Repositories SmartDukaan

Rev

Rev 13567 | Rev 13579 | 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();
13541 anikendra 20
		$this->Auth->allow('bycategory');
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');
64
		$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'));
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
 
13550 anikendra 75
	public function bycategory($userId=null) {
13541 anikendra 76
		$this->loadModel('UserCategory');
77
		$options = array('conditions' => array('user_id'=>$userId),'recursive'=>-1);
78
		$userCategories = $this->UserCategory->find('all',$options);		
79
		$this->response->type('json');
80
		$this->layout = 'ajax';
13550 anikendra 81
		$limit = 20;
13541 anikendra 82
		$conditions = null;
13550 anikendra 83
		$this->StoreProduct->recursive = -1;
84
		$this->StoreProduct->Behaviors->attach('Containable');
85
		$products = array();
13541 anikendra 86
		if(!empty($userCategories)){
87
			foreach ($userCategories as $key => $value) {
88
				$categoryIds[] = $value['UserCategory']['category_id'];
13550 anikendra 89
				$conditions = array('Product.category_id'=>$value['UserCategory']['category_id']);
90
				$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);
91
				// $products[$value['UserCategory']['category_id']]['products'] = $this->Paginator->paginate();				
92
				$rows = $this->Paginator->paginate();				
93
				foreach($rows AS $key => $product){
94
					$products[$value['UserCategory']['category_id']][$key][] = $product;
95
				}
13541 anikendra 96
			}
13567 anikendra 97
		}else{
98
			//Fetch all categories
99
			$categories = $this->StoreProduct->Product->Category->find('all');
100
			foreach ($categories as $key => $value) {
101
				$categoryIds[] = $value['Category']['id'];
102
				$conditions = array('Product.category_id'=>$value['Category']['id']);
103
				$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);
104
				$rows = $this->Paginator->paginate();				
105
				foreach($rows AS $key => $product){
106
					$products[$value['Category']['id']][$key][] = $product;
107
				}
108
			}
13550 anikendra 109
		}				
110
		$result = array('products' => $products);//,'categories'=>$categoryIds);
13541 anikendra 111
		$callback = $this->request->query('callback');
112
		$this->set(array(
113
		    'result' => $result,
114
		    'callback' => $callback,
115
		    '_serialize' => array('result')
116
		));
13567 anikendra 117
		$this->render('/Elements/json');
13541 anikendra 118
	}
119
 
13532 anikendra 120
/**
121
 * view method
122
 *
123
 * @throws NotFoundException
124
 * @param string $id
125
 * @return void
126
 */
127
	public function view($id = null) {
128
		if (!$this->StoreProduct->exists($id)) {
129
			throw new NotFoundException(__('Invalid store product'));
130
		}
131
		$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
132
		$this->set('storeProduct', $this->StoreProduct->find('first', $options));
133
	}
134
 
135
/**
136
 * add method
137
 *
138
 * @return void
139
 */
140
	public function add() {
141
		if ($this->request->is('post')) {
142
			$this->StoreProduct->create();
143
			if ($this->StoreProduct->save($this->request->data)) {
144
				$this->Session->setFlash(__('The store product has been saved.'));
145
				return $this->redirect(array('action' => 'index'));
146
			} else {
147
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
148
			}
149
		}
150
		$stores = $this->StoreProduct->Store->find('list');
151
		$products = $this->StoreProduct->Product->find('list');
152
		$this->set(compact('stores', 'products'));
153
	}
154
 
155
/**
156
 * edit method
157
 *
158
 * @throws NotFoundException
159
 * @param string $id
160
 * @return void
161
 */
162
	public function edit($id = null) {
163
		if (!$this->StoreProduct->exists($id)) {
164
			throw new NotFoundException(__('Invalid store product'));
165
		}
166
		if ($this->request->is(array('post', 'put'))) {
167
			if ($this->StoreProduct->save($this->request->data)) {
168
				$this->Session->setFlash(__('The store product has been saved.'));
169
				return $this->redirect(array('action' => 'index'));
170
			} else {
171
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
172
			}
173
		} else {
174
			$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
175
			$this->request->data = $this->StoreProduct->find('first', $options);
176
		}
177
		$stores = $this->StoreProduct->Store->find('list');
178
		$products = $this->StoreProduct->Product->find('list');
179
		$this->set(compact('stores', 'products'));
180
	}
181
 
182
/**
183
 * delete method
184
 *
185
 * @throws NotFoundException
186
 * @param string $id
187
 * @return void
188
 */
189
	public function delete($id = null) {
190
		$this->StoreProduct->id = $id;
191
		if (!$this->StoreProduct->exists()) {
192
			throw new NotFoundException(__('Invalid store product'));
193
		}
194
		$this->request->onlyAllow('post', 'delete');
195
		if ($this->StoreProduct->delete()) {
196
			$this->Session->setFlash(__('The store product has been deleted.'));
197
		} else {
198
			$this->Session->setFlash(__('The store product could not be deleted. Please, try again.'));
199
		}
200
		return $this->redirect(array('action' => 'index'));
201
	}
202
 
203
/**
204
 * admin_index method
205
 *
206
 * @return void
207
 */
208
	public function admin_index() {
209
		$this->StoreProduct->recursive = 0;
210
		$this->set('storeProducts', $this->Paginator->paginate());
211
	}
212
 
213
/**
214
 * admin_view method
215
 *
216
 * @throws NotFoundException
217
 * @param string $id
218
 * @return void
219
 */
220
	public function admin_view($id = null) {
221
		if (!$this->StoreProduct->exists($id)) {
222
			throw new NotFoundException(__('Invalid store product'));
223
		}
224
		$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
225
		$this->set('storeProduct', $this->StoreProduct->find('first', $options));
226
	}
227
 
228
/**
229
 * admin_add method
230
 *
231
 * @return void
232
 */
233
	public function admin_add() {
234
		if ($this->request->is('post')) {
235
			$this->StoreProduct->create();
236
			if ($this->StoreProduct->save($this->request->data)) {
237
				$this->Session->setFlash(__('The store product has been saved.'));
238
				return $this->redirect(array('action' => 'index'));
239
			} else {
240
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
241
			}
242
		}
243
		$stores = $this->StoreProduct->Store->find('list');
244
		$products = $this->StoreProduct->Product->find('list');
245
		$this->set(compact('stores', 'products'));
246
	}
247
 
248
/**
249
 * admin_edit method
250
 *
251
 * @throws NotFoundException
252
 * @param string $id
253
 * @return void
254
 */
255
	public function admin_edit($id = null) {
256
		if (!$this->StoreProduct->exists($id)) {
257
			throw new NotFoundException(__('Invalid store product'));
258
		}
259
		if ($this->request->is(array('post', 'put'))) {
260
			if ($this->StoreProduct->save($this->request->data)) {
261
				$this->Session->setFlash(__('The store product has been saved.'));
262
				return $this->redirect(array('action' => 'index'));
263
			} else {
264
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
265
			}
266
		} else {
267
			$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
268
			$this->request->data = $this->StoreProduct->find('first', $options);
269
		}
270
		$stores = $this->StoreProduct->Store->find('list');
271
		$products = $this->StoreProduct->Product->find('list');
272
		$this->set(compact('stores', 'products'));
273
	}
274
 
275
/**
276
 * admin_delete method
277
 *
278
 * @throws NotFoundException
279
 * @param string $id
280
 * @return void
281
 */
282
	public function admin_delete($id = null) {
283
		$this->StoreProduct->id = $id;
284
		if (!$this->StoreProduct->exists()) {
285
			throw new NotFoundException(__('Invalid store product'));
286
		}
287
		$this->request->onlyAllow('post', 'delete');
288
		if ($this->StoreProduct->delete()) {
289
			$this->Session->setFlash(__('The store product has been deleted.'));
290
		} else {
291
			$this->Session->setFlash(__('The store product could not be deleted. Please, try again.'));
292
		}
293
		return $this->redirect(array('action' => 'index'));
294
	}}