Subversion Repositories SmartDukaan

Rev

Rev 13583 | Rev 13674 | 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');
        }

        public function mine() {
                $this->layout = "innerpages";
                $this->UserAction->Behaviors->attach('Containable');
                $contain = array('StoreProduct.title','StoreProduct.thumbnail','StoreProduct.mrp','StoreProduct.available_price');
                $options = array('contain'=>$contain,'conditions'=>array('UserAction.user_id'=>$this->Auth->User('id'),'UserAction.action'=>'like'));
                $myfavorites = $this->UserAction->find('all',$options);
                $this->set(compact(myfavorites));
        }

        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 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'));
        }}