Subversion Repositories SmartDukaan

Rev

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

<?php
App::uses('AppController', 'Controller');
/**
 * StoreProducts Controller
 *
 * @property StoreProduct $StoreProduct
 * @property PaginatorComponent $Paginator
 */
class StoreProductsController extends AppController {

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

        public function beforeFilter() {
                parent::beforeFilter();
                $this->Auth->allow('bycategory','category','mine');
                $callback = $this->request->query('callback');
        }

        public function mine() {
                $userId = $this->request->query('user_id');
                if(isset($userId) && !empty($userId)){
                        $this->Auth->login($userId);
                }
                $likedDeals = $disLikedDeals = array();
                $this->loadModel('Api');
                $page = $this->request->query('page');
                if(!isset($page)){
                        $page = 1;
                }               
                $title_for_layout = "Get Cashback on favourite";
                $description = "Get cashback on your favourite products.Mouth watering deals";
                $apideals = $this->Api->getDeals($this->Auth->User('id'));
                $deals = $apideals['products'];
                $myactions = $this->Api->getMyActions($this->Auth->User('id'));
                if(!empty($myactions)) {
                        foreach ($myactions['actions'] as $key => $value) {
                                if($value['UserAction']['action'] == 'like'){
                                        $likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
                                }else{
                                        $disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
                                }
                        }
                }
                $this->set(compact('page', 'title_for_layout', 'description', 'deals', 'likedDeals','disLikedDeals'));
                $this->render('/Pages/home');
        }

        public function getdeals() {
                $likedDeals = $disLikedDeals = array();
                $this->layout = 'ajax';
                $this->loadModel('Api');
                $page = $this->request->query('page');
                if(!isset($page)){
                        $page = 1;
                }               
                $title_for_layout = "Get Cashback on favourite";
                $description = "Get cashback on your favourite products.Mouth watering deals";
                $apideals = $this->Api->getDeals($this->Auth->User('id'),$page);
                $deals = $apideals['products'];
                $myactions = $this->Api->getMyActions($this->Auth->User('id'));
                if(!empty($myactions)) {
                        foreach ($myactions['actions'] as $key => $value) {
                                if($value['UserAction']['action'] == 'like'){
                                        $likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
                                }else{
                                        $disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
                                }
                        }
                }
                $this->set(compact('page', 'title_for_layout', 'description', 'deals', 'likedDeals','disLikedDeals'));
                $this->render('/Elements/deals');
        }       
/**
 * index method
 *
 * @return void
 */
        public function index($user_id=null) {
                $this->response->type('json');
                $this->layout = 'ajax';
                $limit = 20;
                $this->StoreProduct->recursive = -1;
                $count = $this->StoreProduct->find('count');
                $this->StoreProduct->Behaviors->attach('Containable');
                $this->Paginator->settings = array('order'=>array('available_price'=>'desc'), 'contain'=>array('Product'),'limit'=>$limit,'fields'=>array('Product.category_id','StoreProduct.id','StoreProduct.title','StoreProduct.thumbnail','StoreProduct.price','StoreProduct.cashback','StoreProduct.available_price'));
                $result = array('products' => $this->Paginator->paginate(),'maxresults'=>ceil($count/$limit));
                $callback = $this->request->query('callback');
                $this->set(array(
                    'result' => $result,
                    'callback' => $callback,
                    '_serialize' => array('result')
                ));
                $this->render('/Elements/json');
        }

        public function category($user_id=null,$categoryId=null) {
                $this->response->type('json');
                $this->layout = 'ajax';
                $limit = 20;
                $this->StoreProduct->recursive = -1;
                // $count = $this->StoreProduct->find('count');
                $this->StoreProduct->Behaviors->attach('Containable');
                $this->Paginator->settings = array('conditions'=>array('Product.category_id'=>$categoryId),'contain'=>array('Product'),'limit'=>$limit,'fields'=>array('Product.category_id','StoreProduct.id','StoreProduct.title','StoreProduct.thumbnail','StoreProduct.price','StoreProduct.cashback','StoreProduct.available_price'));
                $result = array('products' => $this->Paginator->paginate());
                $callback = $this->request->query('callback');
                $this->set(array(
                    'result' => $result,
                    'callback' => $callback,
                    '_serialize' => array('result')
                ));
                $this->render('/Elements/json');
        }

        public function bycategory($userId=null) {
                $this->loadModel('UserCategory');
                $options = array('conditions' => array('user_id'=>$userId),'recursive'=>-1);
                $userCategories = $this->UserCategory->find('all',$options);            
                $this->response->type('json');
                $this->layout = 'ajax';
                $limit = 20;
                $conditions = null;
                $this->StoreProduct->recursive = -1;
                $this->StoreProduct->Behaviors->attach('Containable');
                $products = array();
                if(!empty($userCategories)){
                        foreach ($userCategories as $key => $value) {
                                $categoryIds[] = $value['UserCategory']['category_id'];
                                $conditions = array('Product.category_id'=>$value['UserCategory']['category_id']);
                                $this->Paginator->settings = array('contain'=>array('Product'),'limit'=>$limit,'fields'=>array('Product.category_id','StoreProduct.id','StoreProduct.title','StoreProduct.thumbnail','StoreProduct.price','StoreProduct.cashback','StoreProduct.available_price'),'conditions'=>$conditions);
                                // $products[$value['UserCategory']['category_id']]['products'] = $this->Paginator->paginate();                         
                                $rows = $this->Paginator->paginate();                           
                                foreach($rows AS $key => $product){
                                        $products[$value['UserCategory']['category_id']][$key][] = $product;
                                }
                        }
                }else{
                        //Fetch all categories
                        $categories = $this->StoreProduct->Product->Category->find('all');
                        foreach ($categories as $key => $value) {
                                $categoryIds[] = $value['Category']['id'];
                                $conditions = array('Product.category_id'=>$value['Category']['id']);
                                $this->Paginator->settings = array('contain'=>array('Product'),'limit'=>$limit,'fields'=>array('Product.category_id','StoreProduct.id','StoreProduct.title','StoreProduct.thumbnail','StoreProduct.price','StoreProduct.cashback','StoreProduct.available_price'),'conditions'=>$conditions);
                                $rows = $this->Paginator->paginate();                           
                                foreach($rows AS $key => $product){
                                        $products[$value['Category']['id']][$key][] = $product;
                                }
                        }
                }                               
                $result = array('products' => $products);//,'categories'=>$categoryIds);
                $callback = $this->request->query('callback');
                $this->set(array(
                    'result' => $result,
                    'callback' => $callback,
                    '_serialize' => array('result')
                ));
                $this->render('/Elements/json');
        }

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

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

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

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

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

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

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

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

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