Subversion Repositories SmartDukaan

Rev

Rev 13541 | Rev 13567 | 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
		));
43
		$this->render('/Elements/jsonp');
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
			}
13550 anikendra 68
		}				
69
		$result = array('products' => $products);//,'categories'=>$categoryIds);
13541 anikendra 70
		$callback = $this->request->query('callback');
71
		$this->set(array(
72
		    'result' => $result,
73
		    'callback' => $callback,
74
		    '_serialize' => array('result')
75
		));
76
		$this->render('/Elements/jsonp');
77
	}
78
 
13532 anikendra 79
/**
80
 * view method
81
 *
82
 * @throws NotFoundException
83
 * @param string $id
84
 * @return void
85
 */
86
	public function view($id = null) {
87
		if (!$this->StoreProduct->exists($id)) {
88
			throw new NotFoundException(__('Invalid store product'));
89
		}
90
		$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
91
		$this->set('storeProduct', $this->StoreProduct->find('first', $options));
92
	}
93
 
94
/**
95
 * add method
96
 *
97
 * @return void
98
 */
99
	public function add() {
100
		if ($this->request->is('post')) {
101
			$this->StoreProduct->create();
102
			if ($this->StoreProduct->save($this->request->data)) {
103
				$this->Session->setFlash(__('The store product has been saved.'));
104
				return $this->redirect(array('action' => 'index'));
105
			} else {
106
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
107
			}
108
		}
109
		$stores = $this->StoreProduct->Store->find('list');
110
		$products = $this->StoreProduct->Product->find('list');
111
		$this->set(compact('stores', 'products'));
112
	}
113
 
114
/**
115
 * edit method
116
 *
117
 * @throws NotFoundException
118
 * @param string $id
119
 * @return void
120
 */
121
	public function edit($id = null) {
122
		if (!$this->StoreProduct->exists($id)) {
123
			throw new NotFoundException(__('Invalid store product'));
124
		}
125
		if ($this->request->is(array('post', 'put'))) {
126
			if ($this->StoreProduct->save($this->request->data)) {
127
				$this->Session->setFlash(__('The store product has been saved.'));
128
				return $this->redirect(array('action' => 'index'));
129
			} else {
130
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
131
			}
132
		} else {
133
			$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
134
			$this->request->data = $this->StoreProduct->find('first', $options);
135
		}
136
		$stores = $this->StoreProduct->Store->find('list');
137
		$products = $this->StoreProduct->Product->find('list');
138
		$this->set(compact('stores', 'products'));
139
	}
140
 
141
/**
142
 * delete method
143
 *
144
 * @throws NotFoundException
145
 * @param string $id
146
 * @return void
147
 */
148
	public function delete($id = null) {
149
		$this->StoreProduct->id = $id;
150
		if (!$this->StoreProduct->exists()) {
151
			throw new NotFoundException(__('Invalid store product'));
152
		}
153
		$this->request->onlyAllow('post', 'delete');
154
		if ($this->StoreProduct->delete()) {
155
			$this->Session->setFlash(__('The store product has been deleted.'));
156
		} else {
157
			$this->Session->setFlash(__('The store product could not be deleted. Please, try again.'));
158
		}
159
		return $this->redirect(array('action' => 'index'));
160
	}
161
 
162
/**
163
 * admin_index method
164
 *
165
 * @return void
166
 */
167
	public function admin_index() {
168
		$this->StoreProduct->recursive = 0;
169
		$this->set('storeProducts', $this->Paginator->paginate());
170
	}
171
 
172
/**
173
 * admin_view method
174
 *
175
 * @throws NotFoundException
176
 * @param string $id
177
 * @return void
178
 */
179
	public function admin_view($id = null) {
180
		if (!$this->StoreProduct->exists($id)) {
181
			throw new NotFoundException(__('Invalid store product'));
182
		}
183
		$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
184
		$this->set('storeProduct', $this->StoreProduct->find('first', $options));
185
	}
186
 
187
/**
188
 * admin_add method
189
 *
190
 * @return void
191
 */
192
	public function admin_add() {
193
		if ($this->request->is('post')) {
194
			$this->StoreProduct->create();
195
			if ($this->StoreProduct->save($this->request->data)) {
196
				$this->Session->setFlash(__('The store product has been saved.'));
197
				return $this->redirect(array('action' => 'index'));
198
			} else {
199
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
200
			}
201
		}
202
		$stores = $this->StoreProduct->Store->find('list');
203
		$products = $this->StoreProduct->Product->find('list');
204
		$this->set(compact('stores', 'products'));
205
	}
206
 
207
/**
208
 * admin_edit method
209
 *
210
 * @throws NotFoundException
211
 * @param string $id
212
 * @return void
213
 */
214
	public function admin_edit($id = null) {
215
		if (!$this->StoreProduct->exists($id)) {
216
			throw new NotFoundException(__('Invalid store product'));
217
		}
218
		if ($this->request->is(array('post', 'put'))) {
219
			if ($this->StoreProduct->save($this->request->data)) {
220
				$this->Session->setFlash(__('The store product has been saved.'));
221
				return $this->redirect(array('action' => 'index'));
222
			} else {
223
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
224
			}
225
		} else {
226
			$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
227
			$this->request->data = $this->StoreProduct->find('first', $options);
228
		}
229
		$stores = $this->StoreProduct->Store->find('list');
230
		$products = $this->StoreProduct->Product->find('list');
231
		$this->set(compact('stores', 'products'));
232
	}
233
 
234
/**
235
 * admin_delete method
236
 *
237
 * @throws NotFoundException
238
 * @param string $id
239
 * @return void
240
 */
241
	public function admin_delete($id = null) {
242
		$this->StoreProduct->id = $id;
243
		if (!$this->StoreProduct->exists()) {
244
			throw new NotFoundException(__('Invalid store product'));
245
		}
246
		$this->request->onlyAllow('post', 'delete');
247
		if ($this->StoreProduct->delete()) {
248
			$this->Session->setFlash(__('The store product has been deleted.'));
249
		} else {
250
			$this->Session->setFlash(__('The store product could not be deleted. Please, try again.'));
251
		}
252
		return $this->redirect(array('action' => 'index'));
253
	}}