Subversion Repositories SmartDukaan

Rev

Rev 13695 | Rev 13926 | 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
 * Preferences Controller
5
 *
6
 * @property Preference $Preference
7
 * @property PaginatorComponent $Paginator
8
 */
9
class PreferencesController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
17
 
13688 anikendra 18
	public function beforeFilter() {
19
		parent::beforeFilter();
20
		$this->Auth->allow('mine');
13695 anikendra 21
		$callback = $this->request->query('callback');		
13688 anikendra 22
	}
23
 
13682 anikendra 24
	public function brand($action='hide') {
25
		$this->response->type('json');
26
		$this->layout = 'ajax';
27
		$userId = $this->request->query('user_id');
28
		$productId = $this->request->query('product_id');
29
		$hide = $this->request->query('hide');
30
		if(isset($hide) && !empty($hide) && $hide == 1){
31
			$this->loadModel('StoreProduct');
32
			$options = array('conditions'=>array('id'=>$productId),'fields'=>array('category_id','brand'),'recursive'=>-1);
33
			$product = $this->StoreProduct->find('first',$options);
13695 anikendra 34
			$this->loadModel('BrandPreference');
35
			$data = array('user_id' => $userId, 'category_id' => $product['StoreProduct']['category_id'],'brand'=>$product['StoreProduct']['brand'],'status'=>'hide');
36
			$this->BrandPreference->create();
37
			if($this->BrandPreference->save($data)){
13682 anikendra 38
				$result = array('success'=>true,'message'=>'brand hidden');
39
			}else{
40
				$result = array('success'=>false,'message'=>'database issue');
41
			}
42
		}else{
43
			$result = array('success'=>false,'message'=>'brand not hidden');
44
		}
45
		// $callback = $this->request->query('callback');
46
		$this->set(array(
47
		    'result' => $result,
48
		    // 'callback' => $callback,
49
		    '_serialize' => array('result')
50
		));
51
		$this->render('/Elements/json');
52
	}
53
 
13688 anikendra 54
	public function mine(){
13695 anikendra 55
		$this->loadModel('User');
56
		$this->loadModel('Category');
57
		$this->layout = 'innerpages';
13688 anikendra 58
		$userId = $this->request->query('user_id');
13695 anikendra 59
		if(isset($userId) && !empty($userId)){			
13688 anikendra 60
			$dbuser = $this->User->findById($userId);
61
			$this->Auth->login($dbuser['User']);
62
		}
13695 anikendra 63
		//Get all categories
64
		$this->Category->Behaviors->attach('Containable');
13759 anikendra 65
		$options = array('conditions'=>array('parent_id !='=>0),'contain'=>(array('Brand.name','Brand.displayed_in_preference_page')));
13695 anikendra 66
		$categories = $this->Category->find('all',$options);
67
		$this->User->Behaviors->attach('Containable');
68
		$options = array('conditions'=>array('User.id'=>$this->request->query('user_id')),'fields'=>array('User.id'),'contain'=>array('PricePreference','BrandPreference'=>array('conditions'=>array('BrandPreference.status'=>'show'))));
69
		$user = $this->User->find('first',$options);
70
		$preferredBrands = $preferredPrices = array();
71
		if(!empty($user['BrandPreference'])){
72
			foreach ($user['BrandPreference'] as $key => $value) {
73
				$preferredBrands[$value['category_id']][] = $value['brand'];
74
			}			
75
		}
76
		if(!empty($user['PricePreference'])){
77
			foreach ($user['PricePreference'] as $key => $value) {
78
				$preferredPrices[$value['category_id']] = $value;
79
			}
80
		}
81
		$this->set(compact('categories','user','preferredPrices','preferredBrands'));
13688 anikendra 82
	}
83
 
13532 anikendra 84
/**
85
 * index method
86
 *
87
 * @return void
88
 */
89
	public function index() {
90
		$this->Preference->recursive = 0;
91
		$this->set('preferences', $this->Paginator->paginate());
92
	}
93
 
94
/**
95
 * view method
96
 *
97
 * @throws NotFoundException
98
 * @param string $id
99
 * @return void
100
 */
101
	public function view($id = null) {
102
		if (!$this->Preference->exists($id)) {
103
			throw new NotFoundException(__('Invalid preference'));
104
		}
105
		$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));
106
		$this->set('preference', $this->Preference->find('first', $options));
107
	}
108
 
109
/**
110
 * add method
111
 *
112
 * @return void
113
 */
13695 anikendra 114
	public function add() {		
115
		$priceSaved = $brandSaved = false;
116
		$errors = array();
13532 anikendra 117
		if ($this->request->is('post')) {
13695 anikendra 118
			// print_r($this->request->data);die;
119
			if(!empty($this->request->data['pricerange'])){
120
				//First delete exisitng price preferences
121
				$this->loadModel('PricePreference');
122
				$this->PricePreference->recursive = -1;
123
				// $conditions = array('user_id'=>$this->request->data['user_id'],'category_id'=>$this->request->data['category_id']);
124
				// $this->PricePreference->deleteAll($conditions,false);
125
				$sql = "DELETE FROM price_preferences WHERE user_id = ".$this->request->data['user_id']." AND category_id = ".$this->request->data['category_id'];
126
				$this->PricePreference->query($sql);
127
				$pricerange = explode(',', $this->request->data['pricerange']);				
128
				$data = array('category_id'=>$this->request->data['category_id'],'user_id'=>$this->request->data['user_id'],'min_price'=>$pricerange[0],'max_price'=>$pricerange[1]);
129
				$this->PricePreference->create();
130
				if($this->PricePreference->save($data)){
131
					$priceSaved = true;
132
				}else{
133
					$errors[] = $this->PricePreference->validationErrors;
134
				}
13532 anikendra 135
			}
13695 anikendra 136
			if(!empty($this->request->data['brand'])){
137
				//First delete all shown brands of this category
138
				$this->loadModel('BrandPreference');
139
				$this->BrandPreference->recursive = -1;
140
				// $conditions = array('user_id'=>$this->request->data['user_id'],'category_id'=>$this->request->data['category_id'],'Brand.status'=>'show');
141
				// $this->BrandPreference->deleteAll($conditions,false);
142
				$sql = "DELETE FROM brand_preferences WHERE user_id = ".$this->request->data['user_id']." AND category_id = ".$this->request->data['category_id']." AND status = 'show'";
143
				$this->BrandPreference->query($sql);
144
				$data = array();
145
				foreach ($this->request->data['brand'] as $key => $brand) {
146
					$temp = array('user_id' => $this->request->data['user_id'], 'category_id' => $this->request->data['category_id'], 'brand' => $brand, 'status' => 'show');
147
					$data[] = $temp;
148
				}				
149
				$this->BrandPreference->create();
150
				if($this->BrandPreference->saveAll($data)){
151
					$brandSaved = true;
152
				}else{
153
					$errors[] = $this->BrandPreference->validationErrors;
154
				}
155
			}
156
			$this->response->type('json');
157
			$this->layout = 'ajax';		
158
			if($brandSaved || $priceSaved) {
159
				$result = array('success'=>true,'message'=>'Preferences Saved');
160
			}else{
161
				$result = array('success'=>false,'message'=>$errors);
162
			}
163
			$this->set(array(
164
			    'result' => $result,
165
			    'callback' => $callback,
166
			    '_serialize' => array('result')
167
			));
168
			$this->render('/Elements/json');
13532 anikendra 169
		}
170
	}
171
 
172
/**
173
 * edit method
174
 *
175
 * @throws NotFoundException
176
 * @param string $id
177
 * @return void
178
 */
179
	public function edit($id = null) {
180
		if (!$this->Preference->exists($id)) {
181
			throw new NotFoundException(__('Invalid preference'));
182
		}
183
		if ($this->request->is(array('post', 'put'))) {
184
			if ($this->Preference->save($this->request->data)) {
185
				$this->Session->setFlash(__('The preference has been saved.'));
186
				return $this->redirect(array('action' => 'index'));
187
			} else {
188
				$this->Session->setFlash(__('The preference could not be saved. Please, try again.'));
189
			}
190
		} else {
191
			$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));
192
			$this->request->data = $this->Preference->find('first', $options);
193
		}
194
		$users = $this->Preference->User->find('list');
195
		$this->set(compact('users'));
196
	}
197
 
198
/**
199
 * delete method
200
 *
201
 * @throws NotFoundException
202
 * @param string $id
203
 * @return void
204
 */
205
	public function delete($id = null) {
206
		$this->Preference->id = $id;
207
		if (!$this->Preference->exists()) {
208
			throw new NotFoundException(__('Invalid preference'));
209
		}
210
		$this->request->onlyAllow('post', 'delete');
211
		if ($this->Preference->delete()) {
212
			$this->Session->setFlash(__('The preference has been deleted.'));
213
		} else {
214
			$this->Session->setFlash(__('The preference could not be deleted. Please, try again.'));
215
		}
216
		return $this->redirect(array('action' => 'index'));
217
	}
218
 
219
/**
220
 * admin_index method
221
 *
222
 * @return void
223
 */
224
	public function admin_index() {
225
		$this->Preference->recursive = 0;
226
		$this->set('preferences', $this->Paginator->paginate());
227
	}
228
 
229
/**
230
 * admin_view method
231
 *
232
 * @throws NotFoundException
233
 * @param string $id
234
 * @return void
235
 */
236
	public function admin_view($id = null) {
237
		if (!$this->Preference->exists($id)) {
238
			throw new NotFoundException(__('Invalid preference'));
239
		}
240
		$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));
241
		$this->set('preference', $this->Preference->find('first', $options));
242
	}
243
 
244
/**
245
 * admin_add method
246
 *
247
 * @return void
248
 */
249
	public function admin_add() {
250
		if ($this->request->is('post')) {
251
			$this->Preference->create();
252
			if ($this->Preference->save($this->request->data)) {
253
				$this->Session->setFlash(__('The preference has been saved.'));
254
				return $this->redirect(array('action' => 'index'));
255
			} else {
256
				$this->Session->setFlash(__('The preference could not be saved. Please, try again.'));
257
			}
258
		}
259
		$users = $this->Preference->User->find('list');
260
		$this->set(compact('users'));
261
	}
262
 
263
/**
264
 * admin_edit method
265
 *
266
 * @throws NotFoundException
267
 * @param string $id
268
 * @return void
269
 */
270
	public function admin_edit($id = null) {
271
		if (!$this->Preference->exists($id)) {
272
			throw new NotFoundException(__('Invalid preference'));
273
		}
274
		if ($this->request->is(array('post', 'put'))) {
275
			if ($this->Preference->save($this->request->data)) {
276
				$this->Session->setFlash(__('The preference has been saved.'));
277
				return $this->redirect(array('action' => 'index'));
278
			} else {
279
				$this->Session->setFlash(__('The preference could not be saved. Please, try again.'));
280
			}
281
		} else {
282
			$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));
283
			$this->request->data = $this->Preference->find('first', $options);
284
		}
285
		$users = $this->Preference->User->find('list');
286
		$this->set(compact('users'));
287
	}
288
 
289
/**
290
 * admin_delete method
291
 *
292
 * @throws NotFoundException
293
 * @param string $id
294
 * @return void
295
 */
296
	public function admin_delete($id = null) {
297
		$this->Preference->id = $id;
298
		if (!$this->Preference->exists()) {
299
			throw new NotFoundException(__('Invalid preference'));
300
		}
301
		$this->request->onlyAllow('post', 'delete');
302
		if ($this->Preference->delete()) {
303
			$this->Session->setFlash(__('The preference has been deleted.'));
304
		} else {
305
			$this->Session->setFlash(__('The preference could not be deleted. Please, try again.'));
306
		}
307
		return $this->redirect(array('action' => 'index'));
308
	}}