| 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 |
*/
|
|
|
28 |
public function index() {
|
| 13541 |
anikendra |
29 |
$user_id = $this->request->query('user_id');
|
| 13532 |
anikendra |
30 |
$this->response->type('json');
|
|
|
31 |
$this->layout = 'ajax';
|
|
|
32 |
// $this->Product->recursive = -1;
|
|
|
33 |
$this->StoreProduct->recursive = 1;
|
| 13541 |
anikendra |
34 |
$this->Paginator->settings = array('limit'=>300);
|
| 13532 |
anikendra |
35 |
$result = array('products' => $this->Paginator->paginate());
|
|
|
36 |
$callback = $this->request->query('callback');
|
|
|
37 |
$this->set(array(
|
|
|
38 |
'result' => $result,
|
|
|
39 |
'callback' => $callback,
|
|
|
40 |
'_serialize' => array('result')
|
|
|
41 |
));
|
|
|
42 |
$this->render('/Elements/jsonp');
|
|
|
43 |
}
|
|
|
44 |
|
| 13541 |
anikendra |
45 |
public function bycategory() {
|
|
|
46 |
$userId = $this->request->query('user_id');
|
|
|
47 |
// $userId = 1;//delete me
|
|
|
48 |
$this->loadModel('UserCategory');
|
|
|
49 |
$options = array('conditions' => array('user_id'=>$userId),'recursive'=>-1);
|
|
|
50 |
$userCategories = $this->UserCategory->find('all',$options);
|
|
|
51 |
$this->response->type('json');
|
|
|
52 |
$this->layout = 'ajax';
|
|
|
53 |
$conditions = null;
|
|
|
54 |
if(!empty($userCategories)){
|
|
|
55 |
foreach ($userCategories as $key => $value) {
|
|
|
56 |
$categoryIds[] = $value['UserCategory']['category_id'];
|
|
|
57 |
}
|
|
|
58 |
$conditions = array('Product.category_id'=>$categoryIds);
|
|
|
59 |
}
|
|
|
60 |
$this->StoreProduct->recursive = -1;
|
|
|
61 |
$this->StoreProduct->Behaviors->attach('Containable');
|
|
|
62 |
$this->Paginator->settings = array('contain'=>array('Product'),'limit'=>50,'fields'=>array('Product.category_id','StoreProduct.id','StoreProduct.title','StoreProduct.thumbnail','StoreProduct.price','StoreProduct.cashback','StoreProduct.available_price'),'conditions'=>$conditions);
|
|
|
63 |
$result = array('products' => $this->Paginator->paginate(),'categories'=>$categoryIds);
|
|
|
64 |
$callback = $this->request->query('callback');
|
|
|
65 |
$this->set(array(
|
|
|
66 |
'result' => $result,
|
|
|
67 |
'callback' => $callback,
|
|
|
68 |
'_serialize' => array('result')
|
|
|
69 |
));
|
|
|
70 |
$this->render('/Elements/jsonp');
|
|
|
71 |
}
|
|
|
72 |
|
| 13532 |
anikendra |
73 |
/**
|
|
|
74 |
* view method
|
|
|
75 |
*
|
|
|
76 |
* @throws NotFoundException
|
|
|
77 |
* @param string $id
|
|
|
78 |
* @return void
|
|
|
79 |
*/
|
|
|
80 |
public function view($id = null) {
|
|
|
81 |
if (!$this->StoreProduct->exists($id)) {
|
|
|
82 |
throw new NotFoundException(__('Invalid store product'));
|
|
|
83 |
}
|
|
|
84 |
$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
|
|
|
85 |
$this->set('storeProduct', $this->StoreProduct->find('first', $options));
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* add method
|
|
|
90 |
*
|
|
|
91 |
* @return void
|
|
|
92 |
*/
|
|
|
93 |
public function add() {
|
|
|
94 |
if ($this->request->is('post')) {
|
|
|
95 |
$this->StoreProduct->create();
|
|
|
96 |
if ($this->StoreProduct->save($this->request->data)) {
|
|
|
97 |
$this->Session->setFlash(__('The store product has been saved.'));
|
|
|
98 |
return $this->redirect(array('action' => 'index'));
|
|
|
99 |
} else {
|
|
|
100 |
$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
$stores = $this->StoreProduct->Store->find('list');
|
|
|
104 |
$products = $this->StoreProduct->Product->find('list');
|
|
|
105 |
$this->set(compact('stores', 'products'));
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* edit method
|
|
|
110 |
*
|
|
|
111 |
* @throws NotFoundException
|
|
|
112 |
* @param string $id
|
|
|
113 |
* @return void
|
|
|
114 |
*/
|
|
|
115 |
public function edit($id = null) {
|
|
|
116 |
if (!$this->StoreProduct->exists($id)) {
|
|
|
117 |
throw new NotFoundException(__('Invalid store product'));
|
|
|
118 |
}
|
|
|
119 |
if ($this->request->is(array('post', 'put'))) {
|
|
|
120 |
if ($this->StoreProduct->save($this->request->data)) {
|
|
|
121 |
$this->Session->setFlash(__('The store product has been saved.'));
|
|
|
122 |
return $this->redirect(array('action' => 'index'));
|
|
|
123 |
} else {
|
|
|
124 |
$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
|
|
|
125 |
}
|
|
|
126 |
} else {
|
|
|
127 |
$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
|
|
|
128 |
$this->request->data = $this->StoreProduct->find('first', $options);
|
|
|
129 |
}
|
|
|
130 |
$stores = $this->StoreProduct->Store->find('list');
|
|
|
131 |
$products = $this->StoreProduct->Product->find('list');
|
|
|
132 |
$this->set(compact('stores', 'products'));
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
/**
|
|
|
136 |
* delete method
|
|
|
137 |
*
|
|
|
138 |
* @throws NotFoundException
|
|
|
139 |
* @param string $id
|
|
|
140 |
* @return void
|
|
|
141 |
*/
|
|
|
142 |
public function delete($id = null) {
|
|
|
143 |
$this->StoreProduct->id = $id;
|
|
|
144 |
if (!$this->StoreProduct->exists()) {
|
|
|
145 |
throw new NotFoundException(__('Invalid store product'));
|
|
|
146 |
}
|
|
|
147 |
$this->request->onlyAllow('post', 'delete');
|
|
|
148 |
if ($this->StoreProduct->delete()) {
|
|
|
149 |
$this->Session->setFlash(__('The store product has been deleted.'));
|
|
|
150 |
} else {
|
|
|
151 |
$this->Session->setFlash(__('The store product could not be deleted. Please, try again.'));
|
|
|
152 |
}
|
|
|
153 |
return $this->redirect(array('action' => 'index'));
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* admin_index method
|
|
|
158 |
*
|
|
|
159 |
* @return void
|
|
|
160 |
*/
|
|
|
161 |
public function admin_index() {
|
|
|
162 |
$this->StoreProduct->recursive = 0;
|
|
|
163 |
$this->set('storeProducts', $this->Paginator->paginate());
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* admin_view method
|
|
|
168 |
*
|
|
|
169 |
* @throws NotFoundException
|
|
|
170 |
* @param string $id
|
|
|
171 |
* @return void
|
|
|
172 |
*/
|
|
|
173 |
public function admin_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 |
* admin_add method
|
|
|
183 |
*
|
|
|
184 |
* @return void
|
|
|
185 |
*/
|
|
|
186 |
public function admin_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 |
* admin_edit method
|
|
|
203 |
*
|
|
|
204 |
* @throws NotFoundException
|
|
|
205 |
* @param string $id
|
|
|
206 |
* @return void
|
|
|
207 |
*/
|
|
|
208 |
public function admin_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 |
* admin_delete method
|
|
|
230 |
*
|
|
|
231 |
* @throws NotFoundException
|
|
|
232 |
* @param string $id
|
|
|
233 |
* @return void
|
|
|
234 |
*/
|
|
|
235 |
public function admin_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 |
}}
|