Rev 16013 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?phpApp::uses('AppController', 'Controller');/*** Preferences Controller** @property Preference $Preference* @property PaginatorComponent $Paginator*/class PreferencesController extends AppController {/*** Components** @var array*/public $components = array('Paginator');public function beforeFilter() {parent::beforeFilter();$this->Auth->allow('mine');$callback = $this->request->query('callback');}public function brand($action='hide') {$this->response->type('json');$this->layout = 'ajax';$userId = $this->request->query('user_id');$productId = $this->request->query('product_id');$hide = $this->request->query('hide');if(isset($hide) && !empty($hide) && $hide == 1){// $this->loadModel('StoreProduct');// $options = array('conditions'=>array('id'=>$productId),'fields'=>array('category_id','brand'),'recursive'=>-1);// $product = $this->StoreProduct->find('first',$options);$cachekey = 'storeproduct-'.$productId;$product = Cache::read($cachekey,'fivemin');if(empty($product)) {$url = $this->apihost.'masterData/getSkuById/'.$productId;$productarr = $this->make_request($url,null);$product = json_decode($productarr[0],1);Cache::write($cachekey,$product,'fivemin');}$this->loadModel('BrandPreference');$data = array('user_id' => $userId, 'category_id' => $product['category_id'],'brand'=>$product['brand'],'status'=>'hide');try{$this->BrandPreference->create();if($this->BrandPreference->save($data)){$result = array('success'=>true,'message'=>'brand hidden');}else{$result = array('success'=>false,'message'=>'database issue');}}catch(exception $ex){$result = array('success'=>true,'message'=>$ex->getMessage());}}else{$result = array('success'=>false,'message'=>'brand not hidden');}// $callback = $this->request->query('callback');$this->set(array('result' => $result,// 'callback' => $callback,'_serialize' => array('result')));$this->render('/Elements/json');}public function mine(){$this->loadModel('User');$this->loadModel('Category');$this->layout = 'innerpages';$userId = $this->request->query('user_id');$firsttime = $this->request->query('firsttime');if(isset($firsttime) && !empty($firsttime)){$this->set('firsttime',1);}// if(isset($userId) && !empty($userId)){// $dbuser = $this->User->findById($userId);// $this->Auth->login($dbuser['User']);// }//Get all categories$this->Category->Behaviors->attach('Containable');$options = array('conditions'=>array('parent_id !='=>0),'contain'=>(array('Brand.name','Brand.displayed_in_preference_page')));$categories = $this->Category->find('all',$options);$this->User->Behaviors->attach('Containable');$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'))));$user = $this->User->find('first',$options);$preferredBrands = $preferredPrices = array();if(!empty($user['BrandPreference'])){foreach ($user['BrandPreference'] as $key => $value) {$preferredBrands[$value['category_id']][] = $value['brand'];}}if(!empty($user['PricePreference'])){foreach ($user['PricePreference'] as $key => $value) {$preferredPrices[$value['category_id']] = $value;}}$priceranges = Configure::read('priceranges');$this->set(compact('categories','user','preferredPrices','preferredBrands','priceranges','userId'));}/*** index method** @return void*/public function index() {$this->Preference->recursive = 0;$this->set('preferences', $this->Paginator->paginate());}/*** view method** @throws NotFoundException* @param string $id* @return void*/public function view($id = null) {if (!$this->Preference->exists($id)) {throw new NotFoundException(__('Invalid preference'));}$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));$this->set('preference', $this->Preference->find('first', $options));}/*** add method** @return void*/public function add() {$priceSaved = $brandSaved = false;$errors = array();$this->log('request '.print_r($this->request->data,1),'preferences');if ($this->request->is('post')) {// print_r($this->request->data);die;if(!empty($this->request->data['pricerange'])){//First delete exisitng price preferences$this->loadModel('PricePreference');$this->PricePreference->recursive = -1;// $conditions = array('user_id'=>$this->request->data['user_id'],'category_id'=>$this->request->data['category_id']);// $this->PricePreference->deleteAll($conditions,false);$sql = "DELETE FROM price_preferences WHERE user_id = ".$this->request->data['user_id']." AND category_id = ".$this->request->data['category_id'];$this->PricePreference->query($sql);$pricerange = explode(',', $this->request->data['pricerange']);$data = array('category_id'=>$this->request->data['category_id'],'user_id'=>$this->request->data['user_id'],'min_price'=>$pricerange[0],'max_price'=>$pricerange[1]);$this->PricePreference->create();if($this->PricePreference->save($data)){$priceSaved = true;}else{$errors[] = $this->PricePreference->validationErrors;}}//if(!empty($this->request->data['brand'])){//First delete all shown brands of this category$this->loadModel('BrandPreference');$this->BrandPreference->recursive = -1;// $conditions = array('user_id'=>$this->request->data['user_id'],'category_id'=>$this->request->data['category_id'],'Brand.status'=>'show');// $this->BrandPreference->deleteAll($conditions,false);$sql = "DELETE FROM brand_preferences WHERE user_id = ".$this->request->data['user_id']." AND category_id = ".$this->request->data['category_id']." AND status = 'show'";$this->BrandPreference->query($sql);$data = array();if(!empty($this->request->data['brand'])){foreach ($this->request->data['brand'] as $key => $brand) {$temp = array('user_id' => $this->request->data['user_id'], 'category_id' => $this->request->data['category_id'], 'brand' => $brand, 'status' => 'show');$data[] = $temp;}$this->BrandPreference->create();if($this->BrandPreference->saveAll($data)){$brandSaved = true;}else{$errors[] = $this->BrandPreference->validationErrors;}}// }$this->response->type('json');$this->layout = 'ajax';if($brandSaved || $priceSaved) {$result = array('success'=>true,'message'=>'Preferences Saved');}else{$result = array('success'=>false,'message'=>$errors);}$url = $this->apihost.'resetCache/'.$this->request->data['user_id'];$response = $this->make_request($url,null);$this->log('response '.print_r($response,1),'preferences');$this->set(array('result' => $result,'callback' => $callback,'_serialize' => array('result')));$this->render('/Elements/json');}}/*** edit method** @throws NotFoundException* @param string $id* @return void*/public function edit($id = null) {if (!$this->Preference->exists($id)) {throw new NotFoundException(__('Invalid preference'));}if ($this->request->is(array('post', 'put'))) {if ($this->Preference->save($this->request->data)) {$this->Session->setFlash(__('The preference has been saved.'));return $this->redirect(array('action' => 'index'));} else {$this->Session->setFlash(__('The preference could not be saved. Please, try again.'));}} else {$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));$this->request->data = $this->Preference->find('first', $options);}$users = $this->Preference->User->find('list');$this->set(compact('users'));}/*** delete method** @throws NotFoundException* @param string $id* @return void*/public function delete($id = null) {$this->Preference->id = $id;if (!$this->Preference->exists()) {throw new NotFoundException(__('Invalid preference'));}$this->request->onlyAllow('post', 'delete');if ($this->Preference->delete()) {$this->Session->setFlash(__('The preference has been deleted.'));} else {$this->Session->setFlash(__('The preference could not be deleted. Please, try again.'));}return $this->redirect(array('action' => 'index'));}/*** admin_index method** @return void*/public function admin_index() {$this->Preference->recursive = 0;$this->set('preferences', $this->Paginator->paginate());}/*** admin_view method** @throws NotFoundException* @param string $id* @return void*/public function admin_view($id = null) {if (!$this->Preference->exists($id)) {throw new NotFoundException(__('Invalid preference'));}$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));$this->set('preference', $this->Preference->find('first', $options));}/*** admin_add method** @return void*/public function admin_add() {if ($this->request->is('post')) {$this->Preference->create();if ($this->Preference->save($this->request->data)) {$this->Session->setFlash(__('The preference has been saved.'));return $this->redirect(array('action' => 'index'));} else {$this->Session->setFlash(__('The preference could not be saved. Please, try again.'));}}$users = $this->Preference->User->find('list');$this->set(compact('users'));}/*** admin_edit method** @throws NotFoundException* @param string $id* @return void*/public function admin_edit($id = null) {if (!$this->Preference->exists($id)) {throw new NotFoundException(__('Invalid preference'));}if ($this->request->is(array('post', 'put'))) {if ($this->Preference->save($this->request->data)) {$this->Session->setFlash(__('The preference has been saved.'));return $this->redirect(array('action' => 'index'));} else {$this->Session->setFlash(__('The preference could not be saved. Please, try again.'));}} else {$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));$this->request->data = $this->Preference->find('first', $options);}$users = $this->Preference->User->find('list');$this->set(compact('users'));}/*** admin_delete method** @throws NotFoundException* @param string $id* @return void*/public function admin_delete($id = null) {$this->Preference->id = $id;if (!$this->Preference->exists()) {throw new NotFoundException(__('Invalid preference'));}$this->request->onlyAllow('post', 'delete');if ($this->Preference->delete()) {$this->Session->setFlash(__('The preference has been deleted.'));} else {$this->Session->setFlash(__('The preference could not be deleted. Please, try again.'));}return $this->redirect(array('action' => 'index'));}}