Subversion Repositories SmartDukaan

Rev

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