Subversion Repositories SmartDukaan

Rev

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