Subversion Repositories SmartDukaan

Rev

Rev 14049 | Rev 14139 | 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
			}
14051 anikendra 149
			//if(!empty($this->request->data['brand'])){
13695 anikendra 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
				}
14051 anikendra 168
			// }
13695 anikendra 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
			}
14051 anikendra 176
			$url = $this->apihost.'resetCache/'.$this->request->data['user_id'];
177
			$this->make_request($url,null);
13695 anikendra 178
			$this->set(array(
179
			    'result' => $result,
180
			    'callback' => $callback,
181
			    '_serialize' => array('result')
182
			));
183
			$this->render('/Elements/json');
13532 anikendra 184
		}
185
	}
186
 
187
/**
188
 * edit method
189
 *
190
 * @throws NotFoundException
191
 * @param string $id
192
 * @return void
193
 */
194
	public function edit($id = null) {
195
		if (!$this->Preference->exists($id)) {
196
			throw new NotFoundException(__('Invalid preference'));
197
		}
198
		if ($this->request->is(array('post', 'put'))) {
199
			if ($this->Preference->save($this->request->data)) {
200
				$this->Session->setFlash(__('The preference has been saved.'));
201
				return $this->redirect(array('action' => 'index'));
202
			} else {
203
				$this->Session->setFlash(__('The preference could not be saved. Please, try again.'));
204
			}
205
		} else {
206
			$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));
207
			$this->request->data = $this->Preference->find('first', $options);
208
		}
209
		$users = $this->Preference->User->find('list');
210
		$this->set(compact('users'));
211
	}
212
 
213
/**
214
 * delete method
215
 *
216
 * @throws NotFoundException
217
 * @param string $id
218
 * @return void
219
 */
220
	public function delete($id = null) {
221
		$this->Preference->id = $id;
222
		if (!$this->Preference->exists()) {
223
			throw new NotFoundException(__('Invalid preference'));
224
		}
225
		$this->request->onlyAllow('post', 'delete');
226
		if ($this->Preference->delete()) {
227
			$this->Session->setFlash(__('The preference has been deleted.'));
228
		} else {
229
			$this->Session->setFlash(__('The preference could not be deleted. Please, try again.'));
230
		}
231
		return $this->redirect(array('action' => 'index'));
232
	}
233
 
234
/**
235
 * admin_index method
236
 *
237
 * @return void
238
 */
239
	public function admin_index() {
240
		$this->Preference->recursive = 0;
241
		$this->set('preferences', $this->Paginator->paginate());
242
	}
243
 
244
/**
245
 * admin_view method
246
 *
247
 * @throws NotFoundException
248
 * @param string $id
249
 * @return void
250
 */
251
	public function admin_view($id = null) {
252
		if (!$this->Preference->exists($id)) {
253
			throw new NotFoundException(__('Invalid preference'));
254
		}
255
		$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));
256
		$this->set('preference', $this->Preference->find('first', $options));
257
	}
258
 
259
/**
260
 * admin_add method
261
 *
262
 * @return void
263
 */
264
	public function admin_add() {
265
		if ($this->request->is('post')) {
266
			$this->Preference->create();
267
			if ($this->Preference->save($this->request->data)) {
268
				$this->Session->setFlash(__('The preference has been saved.'));
269
				return $this->redirect(array('action' => 'index'));
270
			} else {
271
				$this->Session->setFlash(__('The preference could not be saved. Please, try again.'));
272
			}
273
		}
274
		$users = $this->Preference->User->find('list');
275
		$this->set(compact('users'));
276
	}
277
 
278
/**
279
 * admin_edit method
280
 *
281
 * @throws NotFoundException
282
 * @param string $id
283
 * @return void
284
 */
285
	public function admin_edit($id = null) {
286
		if (!$this->Preference->exists($id)) {
287
			throw new NotFoundException(__('Invalid preference'));
288
		}
289
		if ($this->request->is(array('post', 'put'))) {
290
			if ($this->Preference->save($this->request->data)) {
291
				$this->Session->setFlash(__('The preference has been saved.'));
292
				return $this->redirect(array('action' => 'index'));
293
			} else {
294
				$this->Session->setFlash(__('The preference could not be saved. Please, try again.'));
295
			}
296
		} else {
297
			$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));
298
			$this->request->data = $this->Preference->find('first', $options);
299
		}
300
		$users = $this->Preference->User->find('list');
301
		$this->set(compact('users'));
302
	}
303
 
304
/**
305
 * admin_delete method
306
 *
307
 * @throws NotFoundException
308
 * @param string $id
309
 * @return void
310
 */
311
	public function admin_delete($id = null) {
312
		$this->Preference->id = $id;
313
		if (!$this->Preference->exists()) {
314
			throw new NotFoundException(__('Invalid preference'));
315
		}
316
		$this->request->onlyAllow('post', 'delete');
317
		if ($this->Preference->delete()) {
318
			$this->Session->setFlash(__('The preference has been deleted.'));
319
		} else {
320
			$this->Session->setFlash(__('The preference could not be deleted. Please, try again.'));
321
		}
322
		return $this->redirect(array('action' => 'index'));
323
	}}