Subversion Repositories SmartDukaan

Rev

Rev 15467 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
App::uses('AppController', 'Controller');
/**
 * Categories Controller
 *
 * @property Category $Category
 * @property PaginatorComponent $Paginator
 */
class CategoriesController extends AppController {

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

        public function beforeFilter() {
                parent::beforeFilter();
                $this->Auth->allow('deals','getdeals');
                $this->apihost = Configure::read('pythonapihost');
                $this->limit = Configure::read('dealsperpage');
        }

/**
 * index method
 *
 * @return void
 */
        public function index() {
                // $this->Category->recursive = 0;
                // $this->set('categories', $this->Paginator->paginate());
                $userId = $this->request->query('user_id');
                $this->loadModel('UserCategory');
                $options = array('conditions' => array('user_id'=>$userId),'order'=>array('rank','asc'),'recursive'=>-1);
                $userCategories = $this->UserCategory->find('all',$options);            
                $this->response->type('json');
                $this->layout = 'ajax';
                $conditions = array(array('Category.parent_id !='=>0));
                if(!empty($userCategories)){
                        foreach ($userCategories as $key => $value) {
                                $categoryIds[] = $value['UserCategory']['category_id'];
                        }
                        array_push($conditions,array('Category.id'=>$categoryIds));
                }
                $this->Category->recursive = -1;                
                $categories = $this->Paginator->paginate(null,$conditions);
                $callback = $this->request->query('callback');
                $result = array('categories' => $categories);
                $this->set(array(
                    'result' => $result,
                    'callback' => $callback,
                    '_serialize' => array('result')
                ));
                $this->render('/Elements/jsonp');
        }

        public function deals(){
                // $this->loadModel('Api');
                // $apideals = $this->Api->getCategoryDeals($this->Auth->User('id'),1);
                // $categorydeals = $apideals['products'];
                $page = $this->request->query('page');
                if(!isset($page)){
                        $page = 1;
                }
                $offset = ($page - 1) * $this->limit;
                $url = $this->apihost.'getStaticDeals/?categoryId=0&limit='.$this->limit.'&offset='.$offset;
                $deals = $this->make_request($url,null);                
                
                $storeicons = Configure::read('storeicons');
                $this->loadModel('Campaign');
                $campaigns = $this->Campaign->getActiveCampaigns();     
                $rows = sizeof($categorydeals);
                $showCategories = True;
                if($rows>=1){
                        $this->set('deals',$categorydeals);
                        $this->set('storeicons',$storeicons);
                        $this->set('campaigns',$campaigns);
                        $this->set('showCategories', $showCategories);
                        $this->render('categorydeals');
                }else{
                        foreach ($categorydeals as $key => $dealarr) {
                                foreach ($dealarr as $key => $deal) {
                                        $deals[] = $deal[0];
                                }                               
                        }
                        $this->set(compact('deals','storeicons','campaigns','showCategories'));
                }               
        }
/*
        *
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function view($id = null) {
                // if (!$this->Category->exists($id)) {
                        // throw new NotFoundException(__('Invalid category'));
                // }
                $page = $this->request->query('page');
                if(!isset($page)){
                        $page = 1;
                }       
                $sort = $this->request->query('sort');
                $direction = $this->request->query('direction');
                // $filter = $this->request->query('filter');
                // $brands = $this->request->query('brands');
                $brandschosen = '';     
                $filter= '';
                if(!empty($brandschosen)){
                        $filter = 'brand';
                }
                $brands = str_replace(',', '^', $brandschosen);
                $url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands);
                // $url = $this->apihost.'deals/'.$this->Auth->User('id').'?categoryId='.$id;
                $deals = $this->make_request($url,null);
                $this->loadModel('Api');
                // $apideals = $this->Api->getDealsByCategory($this->Auth->User('id'),$id,$page);
                // $deals = $apideals['products'];
                $myactions = $this->Api->getMyActions($this->Auth->User('id'));
                $storeicons = Configure::read('storeicons');
                $this->loadModel('Campaign');
                $showCategories = True;
                $campaigns = $this->Campaign->getActiveCampaigns();     
                $this->set(compact('deals','id','page','sort','direction','notification','storeicons','campaigns','showCategories'));
        }

        public function getdeals($id = null) {
                $this->log('getdeal id '.$id,'api');
                $likedDeals = $disLikedDeals = array();
                $this->layout = 'ajax';
                $page = $this->request->query('page');
                if(!isset($page)){
                        $page = 1;
                }
                $sort = $this->request->query('sort');
                $direction = $this->request->query('direction');
                // $filter = $this->request->query('filter');
                // $brands = $this->request->query('brands');
                $brandschosen = '';
                $filter='' ;
                $brands = str_replace(',', '^', $brandschosen);
                $url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands);
                $deals = $this->make_request($url,null);
                // if (!$this->Category->exists($id)) {
                        // throw new NotFoundException(__('Invalid category'));
                // }
                $this->loadModel('Api');
                // $apideals = $this->Api->getDealsByCategory($this->Auth->User('id'),$id,$page);
                // $deals = $apideals['products'];
                
                $storeicons = Configure::read('storeicons');
                $this->loadModel('Campaign');
                $showCategories = False;
                $campaigns = $this->Campaign->getActiveCampaigns();     
                $this->set(compact('deals','id','page','sort','direction','storeicons','campaigns','showCategories'));
                if(!empty($deals)){
                        $this->render('/Elements/deals');
                }else{
                        $this->render('/Elements/nodeals');
                }
        }
/**
 * add method
 *
 * @return void
 */
        public function add() {
                if ($this->request->is('post')) {
                        $this->Category->create();
                        if ($this->Category->save($this->request->data)) {
                                $this->Session->setFlash(__('The category has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The category could not be saved. Please, try again.'));
                        }
                }
        }

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

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

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

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

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

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

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