Subversion Repositories SmartDukaan

Rev

Rev 13759 | Rev 14049 | 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
		}
93
		$this->set(compact('categories','user','preferredPrices','preferredBrands'));
13688 anikendra 94
	}
95
 
13532 anikendra 96
/**
97
 * index method
98
 *
99
 * @return void
100
 */
101
	public function index() {
102
		$this->Preference->recursive = 0;
103
		$this->set('preferences', $this->Paginator->paginate());
104
	}
105
 
106
/**
107
 * view method
108
 *
109
 * @throws NotFoundException
110
 * @param string $id
111
 * @return void
112
 */
113
	public function view($id = null) {
114
		if (!$this->Preference->exists($id)) {
115
			throw new NotFoundException(__('Invalid preference'));
116
		}
117
		$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));
118
		$this->set('preference', $this->Preference->find('first', $options));
119
	}
120
 
121
/**
122
 * add method
123
 *
124
 * @return void
125
 */
13695 anikendra 126
	public function add() {		
127
		$priceSaved = $brandSaved = false;
128
		$errors = array();
13532 anikendra 129
		if ($this->request->is('post')) {
13695 anikendra 130
			// print_r($this->request->data);die;
131
			if(!empty($this->request->data['pricerange'])){
132
				//First delete exisitng price preferences
133
				$this->loadModel('PricePreference');
134
				$this->PricePreference->recursive = -1;
135
				// $conditions = array('user_id'=>$this->request->data['user_id'],'category_id'=>$this->request->data['category_id']);
136
				// $this->PricePreference->deleteAll($conditions,false);
137
				$sql = "DELETE FROM price_preferences WHERE user_id = ".$this->request->data['user_id']." AND category_id = ".$this->request->data['category_id'];
138
				$this->PricePreference->query($sql);
139
				$pricerange = explode(',', $this->request->data['pricerange']);				
140
				$data = array('category_id'=>$this->request->data['category_id'],'user_id'=>$this->request->data['user_id'],'min_price'=>$pricerange[0],'max_price'=>$pricerange[1]);
141
				$this->PricePreference->create();
142
				if($this->PricePreference->save($data)){
143
					$priceSaved = true;
144
				}else{
145
					$errors[] = $this->PricePreference->validationErrors;
146
				}
13532 anikendra 147
			}
13695 anikendra 148
			if(!empty($this->request->data['brand'])){
149
				//First delete all shown brands of this category
150
				$this->loadModel('BrandPreference');
151
				$this->BrandPreference->recursive = -1;
152
				// $conditions = array('user_id'=>$this->request->data['user_id'],'category_id'=>$this->request->data['category_id'],'Brand.status'=>'show');
153
				// $this->BrandPreference->deleteAll($conditions,false);
154
				$sql = "DELETE FROM brand_preferences WHERE user_id = ".$this->request->data['user_id']." AND category_id = ".$this->request->data['category_id']." AND status = 'show'";
155
				$this->BrandPreference->query($sql);
156
				$data = array();
157
				foreach ($this->request->data['brand'] as $key => $brand) {
158
					$temp = array('user_id' => $this->request->data['user_id'], 'category_id' => $this->request->data['category_id'], 'brand' => $brand, 'status' => 'show');
159
					$data[] = $temp;
160
				}				
161
				$this->BrandPreference->create();
162
				if($this->BrandPreference->saveAll($data)){
163
					$brandSaved = true;
164
				}else{
165
					$errors[] = $this->BrandPreference->validationErrors;
166
				}
167
			}
168
			$this->response->type('json');
169
			$this->layout = 'ajax';		
170
			if($brandSaved || $priceSaved) {
171
				$result = array('success'=>true,'message'=>'Preferences Saved');
172
			}else{
173
				$result = array('success'=>false,'message'=>$errors);
174
			}
175
			$this->set(array(
176
			    'result' => $result,
177
			    'callback' => $callback,
178
			    '_serialize' => array('result')
179
			));
180
			$this->render('/Elements/json');
13532 anikendra 181
		}
182
	}
183
 
184
/**
185
 * edit method
186
 *
187
 * @throws NotFoundException
188
 * @param string $id
189
 * @return void
190
 */
191
	public function edit($id = null) {
192
		if (!$this->Preference->exists($id)) {
193
			throw new NotFoundException(__('Invalid preference'));
194
		}
195
		if ($this->request->is(array('post', 'put'))) {
196
			if ($this->Preference->save($this->request->data)) {
197
				$this->Session->setFlash(__('The preference has been saved.'));
198
				return $this->redirect(array('action' => 'index'));
199
			} else {
200
				$this->Session->setFlash(__('The preference could not be saved. Please, try again.'));
201
			}
202
		} else {
203
			$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));
204
			$this->request->data = $this->Preference->find('first', $options);
205
		}
206
		$users = $this->Preference->User->find('list');
207
		$this->set(compact('users'));
208
	}
209
 
210
/**
211
 * delete method
212
 *
213
 * @throws NotFoundException
214
 * @param string $id
215
 * @return void
216
 */
217
	public function delete($id = null) {
218
		$this->Preference->id = $id;
219
		if (!$this->Preference->exists()) {
220
			throw new NotFoundException(__('Invalid preference'));
221
		}
222
		$this->request->onlyAllow('post', 'delete');
223
		if ($this->Preference->delete()) {
224
			$this->Session->setFlash(__('The preference has been deleted.'));
225
		} else {
226
			$this->Session->setFlash(__('The preference could not be deleted. Please, try again.'));
227
		}
228
		return $this->redirect(array('action' => 'index'));
229
	}
230
 
231
/**
232
 * admin_index method
233
 *
234
 * @return void
235
 */
236
	public function admin_index() {
237
		$this->Preference->recursive = 0;
238
		$this->set('preferences', $this->Paginator->paginate());
239
	}
240
 
241
/**
242
 * admin_view method
243
 *
244
 * @throws NotFoundException
245
 * @param string $id
246
 * @return void
247
 */
248
	public function admin_view($id = null) {
249
		if (!$this->Preference->exists($id)) {
250
			throw new NotFoundException(__('Invalid preference'));
251
		}
252
		$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));
253
		$this->set('preference', $this->Preference->find('first', $options));
254
	}
255
 
256
/**
257
 * admin_add method
258
 *
259
 * @return void
260
 */
261
	public function admin_add() {
262
		if ($this->request->is('post')) {
263
			$this->Preference->create();
264
			if ($this->Preference->save($this->request->data)) {
265
				$this->Session->setFlash(__('The preference has been saved.'));
266
				return $this->redirect(array('action' => 'index'));
267
			} else {
268
				$this->Session->setFlash(__('The preference could not be saved. Please, try again.'));
269
			}
270
		}
271
		$users = $this->Preference->User->find('list');
272
		$this->set(compact('users'));
273
	}
274
 
275
/**
276
 * admin_edit method
277
 *
278
 * @throws NotFoundException
279
 * @param string $id
280
 * @return void
281
 */
282
	public function admin_edit($id = null) {
283
		if (!$this->Preference->exists($id)) {
284
			throw new NotFoundException(__('Invalid preference'));
285
		}
286
		if ($this->request->is(array('post', 'put'))) {
287
			if ($this->Preference->save($this->request->data)) {
288
				$this->Session->setFlash(__('The preference has been saved.'));
289
				return $this->redirect(array('action' => 'index'));
290
			} else {
291
				$this->Session->setFlash(__('The preference could not be saved. Please, try again.'));
292
			}
293
		} else {
294
			$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));
295
			$this->request->data = $this->Preference->find('first', $options);
296
		}
297
		$users = $this->Preference->User->find('list');
298
		$this->set(compact('users'));
299
	}
300
 
301
/**
302
 * admin_delete method
303
 *
304
 * @throws NotFoundException
305
 * @param string $id
306
 * @return void
307
 */
308
	public function admin_delete($id = null) {
309
		$this->Preference->id = $id;
310
		if (!$this->Preference->exists()) {
311
			throw new NotFoundException(__('Invalid preference'));
312
		}
313
		$this->request->onlyAllow('post', 'delete');
314
		if ($this->Preference->delete()) {
315
			$this->Session->setFlash(__('The preference has been deleted.'));
316
		} else {
317
			$this->Session->setFlash(__('The preference could not be deleted. Please, try again.'));
318
		}
319
		return $this->redirect(array('action' => 'index'));
320
	}}