Subversion Repositories SmartDukaan

Rev

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