Subversion Repositories SmartDukaan

Rev

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