Subversion Repositories SmartDukaan

Rev

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