Subversion Repositories SmartDukaan

Rev

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