Subversion Repositories SmartDukaan

Rev

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