Subversion Repositories SmartDukaan

Rev

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