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