Subversion Repositories SmartDukaan

Rev

Rev 15408 | Go to most recent revision | 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);
76
		if($rows>=1){
77
			$this->set('deals',$categorydeals);
78
			$this->set('storeicons',$storeicons);
79
			$this->set('campaigns',$campaigns);
80
			$this->render('categorydeals');
81
		}else{
82
			foreach ($categorydeals as $key => $dealarr) {
83
				foreach ($dealarr as $key => $deal) {
84
					$deals[] = $deal[0];
85
				}				
86
			}
87
			$this->set(compact('deals','storeicons','campaigns'));
88
		}		
89
	}
90
/*
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->Category->exists($id)) {
100
			// throw new NotFoundException(__('Invalid category'));
101
		// }
102
		$page = $this->request->query('page');
103
		if(!isset($page)){
104
			$page = 1;
105
		}	
106
		$sort = $this->request->query('sort');
107
		$direction = $this->request->query('direction');
108
		// $filter = $this->request->query('filter');
109
		// $brands = $this->request->query('brands');
110
		$brandschosen = '';	
111
		$filter= '';
112
		if(!empty($brandschosen)){
113
			$filter = 'brand';
114
		}
115
		$brands = str_replace(',', '^', $brandschosen);
116
		$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands);
117
		// $url = $this->apihost.'deals/'.$this->Auth->User('id').'?categoryId='.$id;
118
		$deals = $this->make_request($url,null);
119
		$this->loadModel('Api');
120
		// $apideals = $this->Api->getDealsByCategory($this->Auth->User('id'),$id,$page);
121
		// $deals = $apideals['products'];
122
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
123
	  	$storeicons = Configure::read('storeicons');
124
	  	$this->loadModel('Campaign');
125
	  	$campaigns = $this->Campaign->getActiveCampaigns();	
126
		$this->set(compact('deals','id','page','sort','direction','notification','storeicons','campaigns'));
127
	}
128
 
129
	public function getdeals($id = null) {
130
		$this->log('getdeal id '.$id,'api');
131
		$likedDeals = $disLikedDeals = array();
132
		$this->layout = 'ajax';
133
		$page = $this->request->query('page');
134
		if(!isset($page)){
135
			$page = 1;
136
		}
137
		$sort = $this->request->query('sort');
138
		$direction = $this->request->query('direction');
139
		// $filter = $this->request->query('filter');
140
		// $brands = $this->request->query('brands');
141
		$brandschosen = '';
142
		$filter='' ;
143
		$brands = str_replace(',', '^', $brandschosen);
144
		$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands);
145
		$deals = $this->make_request($url,null);
146
		// if (!$this->Category->exists($id)) {
147
			// throw new NotFoundException(__('Invalid category'));
148
		// }
149
		$this->loadModel('Api');
150
		// $apideals = $this->Api->getDealsByCategory($this->Auth->User('id'),$id,$page);
151
		// $deals = $apideals['products'];
152
 
153
	  	$storeicons = Configure::read('storeicons');
154
	  	$this->loadModel('Campaign');
155
	  	$campaigns = $this->Campaign->getActiveCampaigns();	
156
		$this->set(compact('deals','id','page','sort','direction','storeicons','campaigns'));
157
		if(!empty($deals)){
158
			$this->render('/Elements/deals');
159
		}else{
160
			$this->render('/Elements/nodeals');
161
		}
162
	}
163
/**
164
 * add method
165
 *
166
 * @return void
167
 */
168
	public function add() {
169
		if ($this->request->is('post')) {
170
			$this->Category->create();
171
			if ($this->Category->save($this->request->data)) {
172
				$this->Session->setFlash(__('The category has been saved.'));
173
				return $this->redirect(array('action' => 'index'));
174
			} else {
175
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
176
			}
177
		}
178
	}
179
 
180
/**
181
 * edit method
182
 *
183
 * @throws NotFoundException
184
 * @param string $id
185
 * @return void
186
 */
187
	public function edit($id = null) {
188
		if (!$this->Category->exists($id)) {
189
			throw new NotFoundException(__('Invalid category'));
190
		}
191
		if ($this->request->is(array('post', 'put'))) {
192
			if ($this->Category->save($this->request->data)) {
193
				$this->Session->setFlash(__('The category has been saved.'));
194
				return $this->redirect(array('action' => 'index'));
195
			} else {
196
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
197
			}
198
		} else {
199
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
200
			$this->request->data = $this->Category->find('first', $options);
201
		}
202
	}
203
 
204
/**
205
 * delete method
206
 *
207
 * @throws NotFoundException
208
 * @param string $id
209
 * @return void
210
 */
211
	public function delete($id = null) {
212
		$this->Category->id = $id;
213
		if (!$this->Category->exists()) {
214
			throw new NotFoundException(__('Invalid category'));
215
		}
216
		$this->request->onlyAllow('post', 'delete');
217
		if ($this->Category->delete()) {
218
			$this->Session->setFlash(__('The category has been deleted.'));
219
		} else {
220
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
221
		}
222
		return $this->redirect(array('action' => 'index'));
223
	}
224
 
225
/**
226
 * admin_index method
227
 *
228
 * @return void
229
 */
230
	public function admin_index() {
231
		$this->Category->recursive = 0;
232
		$this->set('categories', $this->Paginator->paginate());
233
	}
234
 
235
/**
236
 * admin_view method
237
 *
238
 * @throws NotFoundException
239
 * @param string $id
240
 * @return void
241
 */
242
	public function admin_view($id = null) {
243
		if (!$this->Category->exists($id)) {
244
			throw new NotFoundException(__('Invalid category'));
245
		}
246
		$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
247
		$this->set('category', $this->Category->find('first', $options));
248
	}
249
 
250
/**
251
 * admin_add method
252
 *
253
 * @return void
254
 */
255
	public function admin_add() {
256
		if ($this->request->is('post')) {
257
			// print_r($this->request->data);die;
258
			$this->Category->create();
259
			if ($this->Category->save($this->request->data)) {
260
				$this->Session->setFlash(__('The category has been saved.'));
261
				return $this->redirect(array('action' => 'index'));
262
			} else {
263
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
264
			}
265
		}
266
		$this->set('parent_id',$this->Category->find('list'));
267
	}
268
 
269
/**
270
 * admin_edit method
271
 *
272
 * @throws NotFoundException
273
 * @param string $id
274
 * @return void
275
 */
276
	public function admin_edit($id = null) {
277
		if (!$this->Category->exists($id)) {
278
			throw new NotFoundException(__('Invalid category'));
279
		}
280
		if ($this->request->is(array('post', 'put'))) {
281
			if ($this->Category->save($this->request->data)) {
282
				$this->Session->setFlash(__('The category has been saved.'));
283
				return $this->redirect(array('action' => 'index'));
284
			} else {
285
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
286
			}
287
		} else {
288
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
289
			$this->request->data = $this->Category->find('first', $options);
290
		}
291
	}
292
 
293
/**
294
 * admin_delete method
295
 *
296
 * @throws NotFoundException
297
 * @param string $id
298
 * @return void
299
 */
300
	public function admin_delete($id = null) {
301
		$this->Category->id = $id;
302
		if (!$this->Category->exists()) {
303
			throw new NotFoundException(__('Invalid category'));
304
		}
305
		$this->request->onlyAllow('post', 'delete');
306
		if ($this->Category->delete()) {
307
			$this->Session->setFlash(__('The category has been deleted.'));
308
		} else {
309
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
310
		}
311
		return $this->redirect(array('action' => 'index'));
312
	}}