Subversion Repositories SmartDukaan

Rev

Rev 15467 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 1
<?php
2
App::uses('AppController', 'Controller');
3
/**
4
 * Categories Controller
5
 *
6
 * @property Category $Category
7
 * @property PaginatorComponent $Paginator
8
 */
9
class CategoriesController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
17
	public $apihost;
18
	public $limit;
19
 
20
	public function beforeFilter() {
21
		parent::beforeFilter();
15408 manish.sha 22
		$this->Auth->allow('deals','getdeals');
15403 manish.sha 23
		$this->apihost = Configure::read('pythonapihost');
24
		$this->limit = Configure::read('dealsperpage');
25
	}
26
 
27
/**
28
 * index method
29
 *
30
 * @return void
31
 */
32
	public function index() {
33
		// $this->Category->recursive = 0;
34
		// $this->set('categories', $this->Paginator->paginate());
35
		$userId = $this->request->query('user_id');
36
		$this->loadModel('UserCategory');
37
		$options = array('conditions' => array('user_id'=>$userId),'order'=>array('rank','asc'),'recursive'=>-1);
38
		$userCategories = $this->UserCategory->find('all',$options);		
39
		$this->response->type('json');
40
		$this->layout = 'ajax';
41
		$conditions = array(array('Category.parent_id !='=>0));
42
		if(!empty($userCategories)){
43
			foreach ($userCategories as $key => $value) {
44
				$categoryIds[] = $value['UserCategory']['category_id'];
45
			}
46
			array_push($conditions,array('Category.id'=>$categoryIds));
47
		}
48
		$this->Category->recursive = -1;		
49
		$categories = $this->Paginator->paginate(null,$conditions);
50
		$callback = $this->request->query('callback');
51
		$result = array('categories' => $categories);
52
		$this->set(array(
53
		    'result' => $result,
54
		    'callback' => $callback,
55
		    '_serialize' => array('result')
56
		));
57
		$this->render('/Elements/jsonp');
58
	}
59
 
60
	public function deals(){
61
		// $this->loadModel('Api');
62
		// $apideals = $this->Api->getCategoryDeals($this->Auth->User('id'),1);
63
		// $categorydeals = $apideals['products'];
64
		$page = $this->request->query('page');
65
		if(!isset($page)){
66
			$page = 1;
67
		}
68
		$offset = ($page - 1) * $this->limit;
69
		$url = $this->apihost.'getStaticDeals/?categoryId=0&limit='.$this->limit.'&offset='.$offset;
70
		$deals = $this->make_request($url,null);		
71
 
72
		$storeicons = Configure::read('storeicons');
73
		$this->loadModel('Campaign');
74
		$campaigns = $this->Campaign->getActiveCampaigns();	
75
		$rows = sizeof($categorydeals);
15839 manish.sha 76
		$showCategories = True;
15403 manish.sha 77
		if($rows>=1){
78
			$this->set('deals',$categorydeals);
79
			$this->set('storeicons',$storeicons);
80
			$this->set('campaigns',$campaigns);
15839 manish.sha 81
			$this->set('showCategories', $showCategories);
15403 manish.sha 82
			$this->render('categorydeals');
83
		}else{
84
			foreach ($categorydeals as $key => $dealarr) {
85
				foreach ($dealarr as $key => $deal) {
86
					$deals[] = $deal[0];
87
				}				
88
			}
15839 manish.sha 89
			$this->set(compact('deals','storeicons','campaigns','showCategories'));
15403 manish.sha 90
		}		
91
	}
92
/*
93
	*
94
 * view method
95
 *
96
 * @throws NotFoundException
97
 * @param string $id
98
 * @return void
99
 */
100
	public function view($id = null) {
101
		// if (!$this->Category->exists($id)) {
102
			// throw new NotFoundException(__('Invalid category'));
103
		// }
104
		$page = $this->request->query('page');
105
		if(!isset($page)){
106
			$page = 1;
107
		}	
108
		$sort = $this->request->query('sort');
109
		$direction = $this->request->query('direction');
110
		// $filter = $this->request->query('filter');
111
		// $brands = $this->request->query('brands');
112
		$brandschosen = '';	
113
		$filter= '';
114
		if(!empty($brandschosen)){
115
			$filter = 'brand';
116
		}
117
		$brands = str_replace(',', '^', $brandschosen);
118
		$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands);
119
		// $url = $this->apihost.'deals/'.$this->Auth->User('id').'?categoryId='.$id;
120
		$deals = $this->make_request($url,null);
121
		$this->loadModel('Api');
122
		// $apideals = $this->Api->getDealsByCategory($this->Auth->User('id'),$id,$page);
123
		// $deals = $apideals['products'];
124
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
125
	  	$storeicons = Configure::read('storeicons');
126
	  	$this->loadModel('Campaign');
15839 manish.sha 127
	  	$showCategories = True;
15403 manish.sha 128
	  	$campaigns = $this->Campaign->getActiveCampaigns();	
15839 manish.sha 129
		$this->set(compact('deals','id','page','sort','direction','notification','storeicons','campaigns','showCategories'));
15403 manish.sha 130
	}
131
 
132
	public function getdeals($id = null) {
133
		$this->log('getdeal id '.$id,'api');
134
		$likedDeals = $disLikedDeals = array();
135
		$this->layout = 'ajax';
136
		$page = $this->request->query('page');
137
		if(!isset($page)){
138
			$page = 1;
139
		}
140
		$sort = $this->request->query('sort');
141
		$direction = $this->request->query('direction');
142
		// $filter = $this->request->query('filter');
143
		// $brands = $this->request->query('brands');
144
		$brandschosen = '';
145
		$filter='' ;
146
		$brands = str_replace(',', '^', $brandschosen);
147
		$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands);
148
		$deals = $this->make_request($url,null);
149
		// if (!$this->Category->exists($id)) {
150
			// throw new NotFoundException(__('Invalid category'));
151
		// }
152
		$this->loadModel('Api');
153
		// $apideals = $this->Api->getDealsByCategory($this->Auth->User('id'),$id,$page);
154
		// $deals = $apideals['products'];
155
 
156
	  	$storeicons = Configure::read('storeicons');
157
	  	$this->loadModel('Campaign');
15839 manish.sha 158
	  	$showCategories = False;
15403 manish.sha 159
	  	$campaigns = $this->Campaign->getActiveCampaigns();	
15839 manish.sha 160
		$this->set(compact('deals','id','page','sort','direction','storeicons','campaigns','showCategories'));
15403 manish.sha 161
		if(!empty($deals)){
162
			$this->render('/Elements/deals');
163
		}else{
164
			$this->render('/Elements/nodeals');
165
		}
166
	}
167
/**
168
 * add method
169
 *
170
 * @return void
171
 */
172
	public function add() {
173
		if ($this->request->is('post')) {
174
			$this->Category->create();
175
			if ($this->Category->save($this->request->data)) {
176
				$this->Session->setFlash(__('The category has been saved.'));
177
				return $this->redirect(array('action' => 'index'));
178
			} else {
179
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
180
			}
181
		}
182
	}
183
 
184
/**
185
 * edit method
186
 *
187
 * @throws NotFoundException
188
 * @param string $id
189
 * @return void
190
 */
191
	public function edit($id = null) {
192
		if (!$this->Category->exists($id)) {
193
			throw new NotFoundException(__('Invalid category'));
194
		}
195
		if ($this->request->is(array('post', 'put'))) {
196
			if ($this->Category->save($this->request->data)) {
197
				$this->Session->setFlash(__('The category has been saved.'));
198
				return $this->redirect(array('action' => 'index'));
199
			} else {
200
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
201
			}
202
		} else {
203
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
204
			$this->request->data = $this->Category->find('first', $options);
205
		}
206
	}
207
 
208
/**
209
 * delete method
210
 *
211
 * @throws NotFoundException
212
 * @param string $id
213
 * @return void
214
 */
215
	public function delete($id = null) {
216
		$this->Category->id = $id;
217
		if (!$this->Category->exists()) {
218
			throw new NotFoundException(__('Invalid category'));
219
		}
220
		$this->request->onlyAllow('post', 'delete');
221
		if ($this->Category->delete()) {
222
			$this->Session->setFlash(__('The category has been deleted.'));
223
		} else {
224
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
225
		}
226
		return $this->redirect(array('action' => 'index'));
227
	}
228
 
229
/**
230
 * admin_index method
231
 *
232
 * @return void
233
 */
234
	public function admin_index() {
235
		$this->Category->recursive = 0;
236
		$this->set('categories', $this->Paginator->paginate());
237
	}
238
 
239
/**
240
 * admin_view method
241
 *
242
 * @throws NotFoundException
243
 * @param string $id
244
 * @return void
245
 */
246
	public function admin_view($id = null) {
247
		if (!$this->Category->exists($id)) {
248
			throw new NotFoundException(__('Invalid category'));
249
		}
250
		$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
251
		$this->set('category', $this->Category->find('first', $options));
252
	}
253
 
254
/**
255
 * admin_add method
256
 *
257
 * @return void
258
 */
259
	public function admin_add() {
260
		if ($this->request->is('post')) {
261
			// print_r($this->request->data);die;
262
			$this->Category->create();
263
			if ($this->Category->save($this->request->data)) {
264
				$this->Session->setFlash(__('The category has been saved.'));
265
				return $this->redirect(array('action' => 'index'));
266
			} else {
267
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
268
			}
269
		}
270
		$this->set('parent_id',$this->Category->find('list'));
271
	}
272
 
273
/**
274
 * admin_edit method
275
 *
276
 * @throws NotFoundException
277
 * @param string $id
278
 * @return void
279
 */
280
	public function admin_edit($id = null) {
281
		if (!$this->Category->exists($id)) {
282
			throw new NotFoundException(__('Invalid category'));
283
		}
284
		if ($this->request->is(array('post', 'put'))) {
285
			if ($this->Category->save($this->request->data)) {
286
				$this->Session->setFlash(__('The category has been saved.'));
287
				return $this->redirect(array('action' => 'index'));
288
			} else {
289
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
290
			}
291
		} else {
292
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
293
			$this->request->data = $this->Category->find('first', $options);
294
		}
295
	}
296
 
297
/**
298
 * admin_delete method
299
 *
300
 * @throws NotFoundException
301
 * @param string $id
302
 * @return void
303
 */
304
	public function admin_delete($id = null) {
305
		$this->Category->id = $id;
306
		if (!$this->Category->exists()) {
307
			throw new NotFoundException(__('Invalid category'));
308
		}
309
		$this->request->onlyAllow('post', 'delete');
310
		if ($this->Category->delete()) {
311
			$this->Session->setFlash(__('The category has been deleted.'));
312
		} else {
313
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
314
		}
315
		return $this->redirect(array('action' => 'index'));
316
	}}