Subversion Repositories SmartDukaan

Rev

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