Subversion Repositories SmartDukaan

Rev

Rev 14929 | Rev 15015 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 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');
13794 anikendra 17
	public $apihost;
18
	public $limit;
13532 anikendra 19
 
13596 anikendra 20
	public function beforeFilter() {
21
		parent::beforeFilter();
22
		$this->Auth->allow('deals');
13794 anikendra 23
		$this->apihost = Configure::read('pythonapihost');
24
		$this->limit = Configure::read('dealsperpage');
13596 anikendra 25
	}
26
 
13532 anikendra 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());
13541 anikendra 35
		$userId = $this->request->query('user_id');
36
		$this->loadModel('UserCategory');
13689 anikendra 37
		$options = array('conditions' => array('user_id'=>$userId),'order'=>array('rank','asc'),'recursive'=>-1);
13541 anikendra 38
		$userCategories = $this->UserCategory->find('all',$options);		
13532 anikendra 39
		$this->response->type('json');
40
		$this->layout = 'ajax';
13541 anikendra 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;		
13532 anikendra 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
 
13567 anikendra 60
	public function deals(){
13591 anikendra 61
		$userId = $this->request->query('user_id');
62
		if(isset($userId) && !empty($userId)){
13597 anikendra 63
			$this->loadModel('User');
64
			$dbuser = $this->User->findById($userId);
65
			$this->Auth->login($dbuser['User']);	
13591 anikendra 66
		}
13583 anikendra 67
		$likedDeals = $disLikedDeals = array();
13794 anikendra 68
		// $this->loadModel('Api');
69
		// $apideals = $this->Api->getCategoryDeals($this->Auth->User('id'),1);
70
		// $categorydeals = $apideals['products'];
71
		$page = $this->request->query('page');
72
		if(!isset($page)){
73
			$page = 1;
74
		}
75
		$offset = ($page - 1) * $this->limit;
76
		$url = $this->apihost.'deals/'.$this->Auth->User('id').'?categoryId=0&limit='.$this->limit.'&offset='.$offset;
77
		$deals = $this->make_request($url,null);		
13583 anikendra 78
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));		
79
		if(!empty($myactions)) {
80
			foreach ($myactions['actions'] as $key => $value) {
81
				if($value['UserAction']['action'] == 'like'){
82
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
83
				}else{
84
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
85
				}
86
			}
87
		}
13567 anikendra 88
		$rows = sizeof($categorydeals);
89
		if($rows>=1){
90
			$this->set('deals',$categorydeals);
13583 anikendra 91
			$this->set('likedDeals',$likedDeals);
92
			$this->set('disLikedDeals',$disLikedDeals);
13567 anikendra 93
			$this->render('categorydeals');
94
		}else{
95
			foreach ($categorydeals as $key => $dealarr) {
96
				foreach ($dealarr as $key => $deal) {
97
					$deals[] = $deal[0];
98
				}				
99
			}
13583 anikendra 100
			$this->set(compact('deals','likedDeals','disLikedDeals'));
13567 anikendra 101
		}		
102
	}
103
/*
104
	*
13532 anikendra 105
 * view method
106
 *
107
 * @throws NotFoundException
108
 * @param string $id
109
 * @return void
110
 */
111
	public function view($id = null) {
14930 anikendra 112
		$userId = $this->request->query('user_id');
113
		if(isset($userId) && !empty($userId)){
114
			$this->loadModel('User');
115
			$dbuser = $this->User->findById($userId);
116
			$this->Auth->login($dbuser['User']);
117
		}
13583 anikendra 118
		$likedDeals = $disLikedDeals = array();
13794 anikendra 119
		// if (!$this->Category->exists($id)) {
120
			// throw new NotFoundException(__('Invalid category'));
121
		// }
13583 anikendra 122
		$page = $this->request->query('page');
123
		if(!isset($page)){
124
			$page = 1;
125
		}	
13808 anikendra 126
		$sort = $this->request->query('sort');
127
		$direction = $this->request->query('direction');
128
		$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction);
129
		// $url = $this->apihost.'deals/'.$this->Auth->User('id').'?categoryId='.$id;
13794 anikendra 130
		$deals = $this->make_request($url,null);
13579 anikendra 131
		$this->loadModel('Api');
13794 anikendra 132
		// $apideals = $this->Api->getDealsByCategory($this->Auth->User('id'),$id,$page);
133
		// $deals = $apideals['products'];
13583 anikendra 134
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
135
		if(!empty($myactions)) {
136
			foreach ($myactions['actions'] as $key => $value) {
137
				if($value['UserAction']['action'] == 'like'){
138
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
139
				}else{
140
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
141
				}
142
			}
143
		}
14929 anikendra 144
		$this->loadModel('NotificationRule');
145
		$notification = $this->NotificationRule->getNotification($this->Auth->User('id'));	
146
		$this->set(compact('deals','id','likedDeals','disLikedDeals','page','sort','direction','notification'));
13532 anikendra 147
	}
148
 
13579 anikendra 149
	public function getdeals($id = null) {
13808 anikendra 150
		$this->log('getdeal id '.$id,'api');
13583 anikendra 151
		$likedDeals = $disLikedDeals = array();
13579 anikendra 152
		$this->layout = 'ajax';
153
		$page = $this->request->query('page');
154
		if(!isset($page)){
155
			$page = 1;
156
		}
13808 anikendra 157
		$sort = $this->request->query('sort');
158
		$direction = $this->request->query('direction');
159
		$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction);
160
		// $url = $this->apihost.'deals/'.$this->Auth->User('id').'?categoryId='.$id;
13794 anikendra 161
		$deals = $this->make_request($url,null);
162
		// if (!$this->Category->exists($id)) {
163
			// throw new NotFoundException(__('Invalid category'));
164
		// }
13579 anikendra 165
		$this->loadModel('Api');
13794 anikendra 166
		// $apideals = $this->Api->getDealsByCategory($this->Auth->User('id'),$id,$page);
167
		// $deals = $apideals['products'];
13583 anikendra 168
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
169
		if(!empty($myactions)) {
170
			foreach ($myactions['actions'] as $key => $value) {
171
				if($value['UserAction']['action'] == 'like'){
172
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
173
				}else{
174
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
175
				}
176
			}
177
		}
13808 anikendra 178
		$this->set(compact('deals','id','page','likedDeals','disLikedDeals','sort','direction'));
13719 anikendra 179
		$this->render('/Elements/deals');
13579 anikendra 180
	}
13532 anikendra 181
/**
182
 * add method
183
 *
184
 * @return void
185
 */
186
	public function add() {
187
		if ($this->request->is('post')) {
188
			$this->Category->create();
189
			if ($this->Category->save($this->request->data)) {
190
				$this->Session->setFlash(__('The category has been saved.'));
191
				return $this->redirect(array('action' => 'index'));
192
			} else {
193
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
194
			}
195
		}
196
	}
197
 
198
/**
199
 * edit method
200
 *
201
 * @throws NotFoundException
202
 * @param string $id
203
 * @return void
204
 */
205
	public function edit($id = null) {
206
		if (!$this->Category->exists($id)) {
207
			throw new NotFoundException(__('Invalid category'));
208
		}
209
		if ($this->request->is(array('post', 'put'))) {
210
			if ($this->Category->save($this->request->data)) {
211
				$this->Session->setFlash(__('The category has been saved.'));
212
				return $this->redirect(array('action' => 'index'));
213
			} else {
214
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
215
			}
216
		} else {
217
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
218
			$this->request->data = $this->Category->find('first', $options);
219
		}
220
	}
221
 
222
/**
223
 * delete method
224
 *
225
 * @throws NotFoundException
226
 * @param string $id
227
 * @return void
228
 */
229
	public function delete($id = null) {
230
		$this->Category->id = $id;
231
		if (!$this->Category->exists()) {
232
			throw new NotFoundException(__('Invalid category'));
233
		}
234
		$this->request->onlyAllow('post', 'delete');
235
		if ($this->Category->delete()) {
236
			$this->Session->setFlash(__('The category has been deleted.'));
237
		} else {
238
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
239
		}
240
		return $this->redirect(array('action' => 'index'));
241
	}
242
 
243
/**
244
 * admin_index method
245
 *
246
 * @return void
247
 */
248
	public function admin_index() {
249
		$this->Category->recursive = 0;
250
		$this->set('categories', $this->Paginator->paginate());
251
	}
252
 
253
/**
254
 * admin_view method
255
 *
256
 * @throws NotFoundException
257
 * @param string $id
258
 * @return void
259
 */
260
	public function admin_view($id = null) {
261
		if (!$this->Category->exists($id)) {
262
			throw new NotFoundException(__('Invalid category'));
263
		}
264
		$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
265
		$this->set('category', $this->Category->find('first', $options));
266
	}
267
 
268
/**
269
 * admin_add method
270
 *
271
 * @return void
272
 */
273
	public function admin_add() {
274
		if ($this->request->is('post')) {
275
			// print_r($this->request->data);die;
276
			$this->Category->create();
277
			if ($this->Category->save($this->request->data)) {
278
				$this->Session->setFlash(__('The category has been saved.'));
279
				return $this->redirect(array('action' => 'index'));
280
			} else {
281
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
282
			}
283
		}
284
		$this->set('parent_id',$this->Category->find('list'));
285
	}
286
 
287
/**
288
 * admin_edit method
289
 *
290
 * @throws NotFoundException
291
 * @param string $id
292
 * @return void
293
 */
294
	public function admin_edit($id = null) {
295
		if (!$this->Category->exists($id)) {
296
			throw new NotFoundException(__('Invalid category'));
297
		}
298
		if ($this->request->is(array('post', 'put'))) {
299
			if ($this->Category->save($this->request->data)) {
300
				$this->Session->setFlash(__('The category has been saved.'));
301
				return $this->redirect(array('action' => 'index'));
302
			} else {
303
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
304
			}
305
		} else {
306
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
307
			$this->request->data = $this->Category->find('first', $options);
308
		}
309
	}
310
 
311
/**
312
 * admin_delete method
313
 *
314
 * @throws NotFoundException
315
 * @param string $id
316
 * @return void
317
 */
318
	public function admin_delete($id = null) {
319
		$this->Category->id = $id;
320
		if (!$this->Category->exists()) {
321
			throw new NotFoundException(__('Invalid category'));
322
		}
323
		$this->request->onlyAllow('post', 'delete');
324
		if ($this->Category->delete()) {
325
			$this->Session->setFlash(__('The category has been deleted.'));
326
		} else {
327
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
328
		}
329
		return $this->redirect(array('action' => 'index'));
330
	}}