Subversion Repositories SmartDukaan

Rev

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