Subversion Repositories SmartDukaan

Rev

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