Subversion Repositories SmartDukaan

Rev

Rev 14215 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
App::uses('AppController', 'Controller');
/**
 * UserActions Controller
 *
 * @property UserAction $UserAction
 * @property PaginatorComponent $Paginator
 */
class UserActionsController extends AppController {

/**
 * Components
 *
 * @var array
 */
        public $components = array('Paginator');

        public function beforeFilter() {
                parent::beforeFilter();
                $this->Auth->allow('update','by','rem','mine');
        }

        public function mine() {
                $userId = $this->request->query('user_id');
                if(isset($userId) && !empty($userId)){
                        $this->loadModel('User');
                        $dbuser = $this->User->findById($userId);
                        $this->Auth->login($dbuser['User']);    
                }
                $this->layout = "innerpages";
                $this->UserAction->Behaviors->attach('Containable');
                $options = array('recursive'=>-1,'conditions'=>array('UserAction.user_id'=>$this->Auth->User('id'),'UserAction.action'=>'like'));
                // $options = array('recursive'=>-1,'conditions'=>array('UserAction.user_id'=>$this->Auth->User('id')));
                $myfavorites = $this->UserAction->find('all',$options);
                if(!empty($myfavorites)){
                        foreach ($myfavorites as $key => $value) {                              
                                $cachekey = 'storeproduct-'.$value['UserAction']['store_product_id'];
                                $product = Cache::read($cachekey,'fivemin');
                                if(empty($product)) {
                                        $url = $this->apihost.'masterData/getSkuById/'.$value['UserAction']['store_product_id'];
                                        $product = $this->make_request($url,null);
                                        Cache::write($cachekey,$product,'fivemin');                     
                                }
                                $myfavorites[$key]['StoreProduct'] = json_decode($product[0],1);
                        }
                }
                $options = array('recursive'=>-1,'conditions'=>array('UserAction.user_id'=>$this->Auth->User('id'),'UserAction.action'=>'dislike'));
                // $options = array('recursive'=>-1,'conditions'=>array('UserAction.user_id'=>$this->Auth->User('id')));
                $mydislikes = $this->UserAction->find('all',$options);
                if(!empty($mydislikes)){
                        foreach ($mydislikes as $key => $value) {                               
                                $cachekey = 'storeproduct-'.$value['UserAction']['store_product_id'];
                                $product = Cache::read($cachekey,'fivemin');
                                if(empty($product)) {
                                        $url = $this->apihost.'masterData/getSkuById/'.$value['UserAction']['store_product_id'];
                                        $product = $this->make_request($url,null);
                                        Cache::write($cachekey,$product,'fivemin');                     
                                }
                                $mydislikes[$key]['StoreProduct'] = json_decode($product[0],1);
                        }
                }
                $this->set(compact('myfavorites','mydislikes'));
        }

        public function update($user_id,$store_product_id,$action) {
                if(!empty($user_id) && !empty($store_product_id) && !empty($action)) {
                        $data = array('user_id'=>$user_id,'store_product_id'=>$store_product_id);
                        $this->UserAction->deleteAll(array($data),false);
                        $this->response->type('json');
                        $this->layout = 'ajax';
                        $callback = $this->request->query('callback');
                        $data['action'] = $action;
                        $this->UserAction->create();
                        if ($this->UserAction->save($data)) {
                                $result = array('success'=>true,'message'=>(__('The user action has been saved.')));
                        } else {
                                $result = array('success'=>false,(__('The user action could not be saved. Please, try again.')));
                        }
                        $this->set(array(
                            'result' => $result,
                            'callback' => $callback,
                            '_serialize' => array('result')
                        ));
                        $this->render('/Elements/jsonp');
                }               
        }

        public function rem($user_id,$store_product_id,$action) {
                if(!empty($user_id) && !empty($store_product_id) && !empty($action)) {
                        $data = array('user_id'=>$user_id,'store_product_id'=>$store_product_id);
                        $this->UserAction->deleteAll(array($data),false);
                        $this->response->type('json');
                        $this->layout = 'ajax';
                        $callback = $this->request->query('callback');                  
                        $result = array('success'=>true,'message'=>(__('The user action has been deleted.')));
                        $this->set(array(
                            'result' => $result,
                            'callback' => $callback,
                            '_serialize' => array('result')
                        ));
                        $this->render('/Elements/jsonp');
                }               
        }

        public function deletefav($user_id,$id) {
                if(!empty($user_id) && !empty($id)) {
                        $loggedInUserId = $this->Auth->User('id');
                        if($user_id != $loggedInUserId){
                                $result = array('success'=>false,'message'=>(__('Unauthorized access. Try again later')));
                        } else {
                                $sql = "DELETE FROM user_actions WHERE id = $id AND user_id = $user_id";
                                $this->UserAction->query($sql);
                                $result = array('success'=>true,'message'=>(__('The favourite product has been removed.')));                            
                        }
                        $this->response->type('json');
                        $this->layout = 'ajax';
                        $callback = $this->request->query('callback');                  
                        $this->set(array(
                            'result' => $result,
                            'callback' => $callback,
                            '_serialize' => array('result')
                        ));
                        $this->render('/Elements/jsonp');
                }               
        }

        public function deleteallfavs($user_id,$type='like') {
                if(!empty($user_id)) {
                        $loggedInUserId = $this->Auth->User('id');
                        if($user_id != $loggedInUserId){
                                $result = array('success'=>false,'message'=>(__('Unauthorized access. Try again later')));
                        } else {
                                $sql = "DELETE FROM user_actions WHERE user_id = $user_id AND action = '$type'";
                                $this->UserAction->query($sql);
                                $result = array('success'=>true,'message'=>(__('All favourite products have been removed.')));                          
                        }
                        $this->response->type('json');
                        $this->layout = 'ajax';
                        $callback = $this->request->query('callback');                  
                        $this->set(array(
                            'result' => $result,
                            'callback' => $callback,
                            '_serialize' => array('result')
                        ));
                        $this->render('/Elements/jsonp');
                }               
        }

        public function by($user_id=null){
                if(!empty($user_id)){
                        $this->response->type('json');
                        $this->layout = 'ajax';
                        $callback = $this->request->query('callback');
                        $this->UserAction->recursive = -1;
                        $conditions = array('user_id' => $user_id);
                        $result['actions'] = $this->UserAction->find('all',array('conditions'=>$conditions));
                        $this->set(array(
                            'result' => $result,
                            'callback' => $callback,
                            '_serialize' => array('result')
                        ));
                        $this->render('/Elements/json');
                }
        }
/**
 * index method
 *
 * @return void
 */
        public function index() {
                $this->UserAction->recursive = 0;
                $this->set('userActions', $this->Paginator->paginate());
        }

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function view($id = null) {
                if (!$this->UserAction->exists($id)) {
                        throw new NotFoundException(__('Invalid user action'));
                }
                $options = array('conditions' => array('UserAction.' . $this->UserAction->primaryKey => $id));
                $this->set('userAction', $this->UserAction->find('first', $options));
        }

/**
 * add method
 *
 * @return void
 */
        public function add() {
                if ($this->request->is('post')) {
                        $this->UserAction->create();
                        if ($this->UserAction->save($this->request->data)) {
                                $this->Session->setFlash(__('The user action has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The user action could not be saved. Please, try again.'));
                        }
                }
                // $users = $this->UserAction->User->find('list');
                // $storeProducts = $this->UserAction->StoreProduct->find('list');
                // $this->set(compact('users', 'storeProducts'));
        }

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function edit($id = null) {
                if (!$this->UserAction->exists($id)) {
                        throw new NotFoundException(__('Invalid user action'));
                }
                if ($this->request->is(array('post', 'put'))) {
                        if ($this->UserAction->save($this->request->data)) {
                                $this->Session->setFlash(__('The user action has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The user action could not be saved. Please, try again.'));
                        }
                } else {
                        $options = array('conditions' => array('UserAction.' . $this->UserAction->primaryKey => $id));
                        $this->request->data = $this->UserAction->find('first', $options);
                }
                $users = $this->UserAction->User->find('list');
                $storeProducts = $this->UserAction->StoreProduct->find('list');
                $this->set(compact('users', 'storeProducts'));
        }

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function delete($id = null) {
                $this->UserAction->id = $id;
                if (!$this->UserAction->exists()) {
                        throw new NotFoundException(__('Invalid user action'));
                }
                $this->request->onlyAllow('post', 'delete');
                if ($this->UserAction->delete()) {
                        $this->Session->setFlash(__('The user action has been deleted.'));
                } else {
                        $this->Session->setFlash(__('The user action could not be deleted. Please, try again.'));
                }
                return $this->redirect(array('action' => 'index'));
        }

/**
 * admin_index method
 *
 * @return void
 */
        public function admin_index() {
                $this->UserAction->recursive = 0;
                $this->set('userActions', $this->Paginator->paginate());
        }

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_view($id = null) {
                if (!$this->UserAction->exists($id)) {
                        throw new NotFoundException(__('Invalid user action'));
                }
                $options = array('conditions' => array('UserAction.' . $this->UserAction->primaryKey => $id));
                $this->set('userAction', $this->UserAction->find('first', $options));
        }

/**
 * admin_add method
 *
 * @return void
 */
        public function admin_add() {
                if ($this->request->is('post')) {
                        $this->UserAction->create();
                        if ($this->UserAction->save($this->request->data)) {
                                $this->Session->setFlash(__('The user action has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The user action could not be saved. Please, try again.'));
                        }
                }
                $users = $this->UserAction->User->find('list');
                $storeProducts = $this->UserAction->StoreProduct->find('list');
                $this->set(compact('users', 'storeProducts'));
        }

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_edit($id = null) {
                if (!$this->UserAction->exists($id)) {
                        throw new NotFoundException(__('Invalid user action'));
                }
                if ($this->request->is(array('post', 'put'))) {
                        if ($this->UserAction->save($this->request->data)) {
                                $this->Session->setFlash(__('The user action has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The user action could not be saved. Please, try again.'));
                        }
                } else {
                        $options = array('conditions' => array('UserAction.' . $this->UserAction->primaryKey => $id));
                        $this->request->data = $this->UserAction->find('first', $options);
                }
                $users = $this->UserAction->User->find('list');
                $storeProducts = $this->UserAction->StoreProduct->find('list');
                $this->set(compact('users', 'storeProducts'));
        }

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_delete($id = null) {
                $this->UserAction->id = $id;
                if (!$this->UserAction->exists()) {
                        throw new NotFoundException(__('Invalid user action'));
                }
                $this->request->onlyAllow('post', 'delete');
                if ($this->UserAction->delete()) {
                        $this->Session->setFlash(__('The user action has been deleted.'));
                } else {
                        $this->Session->setFlash(__('The user action could not be deleted. Please, try again.'));
                }
                return $this->redirect(array('action' => 'index'));
        }}