Subversion Repositories SmartDukaan

Rev

Rev 18617 | Rev 19306 | Go to most recent revision | 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');
                $this->apihost = Configure::read('pythonapihost');
                $this->limit = Configure::read('dealsperpage');
                $this->mobileapi = Configure::read('saholicapihost');
                
        }

/**
 * 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(){
                $userId = $this->request->query('user_id');
                $error = $this->request->query('error');
                if(isset($userId) && !empty($userId)){
                        $this->loadModel('User');
                        $dbuser = $this->User->findById($userId);
                        $this->Auth->login($dbuser['User']);    
                }
                $likedDeals = $disLikedDeals = array();
                // $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.'deals/'.$this->Auth->User('id').'?categoryId=0&limit='.$this->limit.'&offset='.$offset;
                //debug($url);
                $deals = $this->make_request($url,null);                
                $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'];
                                }
                        }
                }
                
                $errorstr = '';
                                
                if(isset($error)){
                        $errorstr = 'Oops!! Some Error Occured. <br> Please try after Some Time';
                }
                $rows = sizeof($categorydeals);
                if($rows>=1){
                        $this->set('deals',$categorydeals);
                        $this->set('likedDeals',$likedDeals);
                        $this->set('disLikedDeals',$disLikedDeals);
                        $this->set('errorstr',$errorstr);
                        $this->render('categorydeals');
                }else{
                        foreach ($categorydeals as $key => $dealarr) {
                                foreach ($dealarr as $key => $deal) {
                                        $deals[] = $deal[0];
                                }                               
                        }
                        $this->set(compact('deals','likedDeals','disLikedDeals','errorstr'));
                }               
        }
/*
        *
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function view($id = null) {
                $userId = $this->request->query('user_id');
                if(isset($userId) && !empty($userId)) {
                        $this->loadModel('User');
                        $dbuser = $this->User->findById($userId);
                        $this->Auth->login($dbuser['User']);
                }                               
                //Sort - type, ordering
                $sort = $this->request->query('sort');
                $direction = $this->request->query('direction');
                
                //Filter - brand/subcategory            
                $brands = $this->request->query('brands');
                $subcategories = $this->request->query('subcategories');
                
                $searchableSubCategories = array('20'=>'Tempered Glasses', '27'=>'Back Cover', '29'=>'Batteries');
                
                $page = $this->request->query('page');
                $searchfor = $this->request->query('searchFor');
                // echo "page=>",$page;
                $error = $this->request->query('error');
                if(!isset($page)){
                        $page = 1;
                }

                //$filter = $this->request->query('filter');
                // $brands = $this->request->query('brands');
                if($id != 2) {
                        //Fetch deals
                        $likedDeals = $disLikedDeals = array();
                        if(!empty($brands) && !empty($subcategories)){
                                $filter = 'brand|subcategory';
                        }                       
                        else if(!empty($brands)){
                                $filter = 'brand';
                        }
                        else if(!empty($subcategories)){
                                $filter = 'subcategory';
                        }
                        
                        $sortlabel = 'recommended';
                        if      ($sort=='bestSellerPoints'){
                                $sortlabel  = 'bestseller';
                        }
                        else if ($sort=='available_price' && $direction==-1){
                                $sortlabel  = 'pricehigh';
                        }
                        else if ($sort=='available_price' && $direction==1){
                                $sortlabel  = 'pricelow';
                        }
                        
                        $url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands,$subcategories);
                        $response = $this->make_request($url,null);
                        $deals = array();
                        
                        $response_count =1;
                        if($response == '')
                        {
                                $response_count = 0;
                        }
                        // debug($response_count);
                        if(!empty($response)){
                                foreach ($response as $key => $value) {
                                        if(!empty($value)){
                                                $deals[] = $value;
                                        }
                                }
                        }
                        #print_r($deals);
                        $this->loadModel('Api');
                        $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->loadModel('NotificationRule');
                        $notification = $this->NotificationRule->getNotification($this->Auth->User('id'));      
                    $filterstr = '';
                        if(isset($filter) && !empty($filter)){
                                
                            $filterstr = '&filter='.$filter.'&brands='.$brands.'&subcategories='.$subcategories;
                        } 
                        $get_url = "'".$_SERVER['REQUEST_URI']."'";
                        $urlArray = explode('=',$_SERVER['REQUEST_URI']);
                        $last = $urlArray[sizeof($urlArray)-1];
                        
                        $errorstr = '';
                        
                        if(isset($error)){
                                $errorstr = 'Oops!! Some Error Occured. <br> Please try after Some Time';
                        }

                        $filterstr = '&brands='.$brands.'&subcategories='.$subcategories;
                        $nexturl = "/categories/getdeals/".$id."/?page=".($page+1)."&sort=".$sort."&direction=".$direction."".$filterstr;                       
                        
                        $this->set(compact('response_count','deals','id','likedDeals','disLikedDeals','page','sort','direction','notification','filter','brands','filterstr','subcategories','errorstr','nexturl','searchfor', 'searchableSubCategories', 'sortlabel'));
                }else{
                        //Check for apk support of sharing
                        $sharable = 0;
                        if(isset($_COOKIE['shareApps']) && !empty($_COOKIE['shareApps'])) {
                                $sharable = 1;
                        }                       
                        $url = $this->apihost."appOffers/1";
                        $appOffers = $this->make_request($url,null);
                        $this->set(compact('page','id','sharable','appOffers'));
                        $this->render('viewapps');
                }
        }

        public function getapps($id=null){
                $this->layout = 'ajax';
                $page = $this->request->query('page');
                if(!isset($page)){
                        $page = 1;
                }
                $this->loadModel('AppOffer');
                $this->AppOffer->recursive = -1;
                $options = array('conditions'=>array('offer_active'=>1,'show'=>1),'limit' => Configure::read('searchresultsperpage'),'page'=>$page);
                $this->Paginator->settings = $options;
                try{
                        $appOffers = $this->Paginator->paginate('AppOffer');
                } catch (NotFoundException $e) {
                //get current page
                $page = $this->request->params['named']['page'];
                $appOffers = array();   
                }
                debug($appOffers);              
                if(!empty($appOffers)){
                        $this->set(compact('page','id','appOffers'));
                        $this->render('/Elements/appoffers');
                }else{
                        $this->render('/Elements/nooffers');
                }               
        }

        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');
                $brands = $this->request->query('brands');
                $subcategories = $this->request->query('subcategories');        

                $likedDeals = $disLikedDeals = array();
                if(!empty($brands) && !empty($subcategories)){
                        $filter = 'brand|subcategory';
                }
                else if(!empty($brands)){
                        $filter = 'brand';
                }
                else if(!empty($subcategories)){
                        $filter = 'subcategory';
                }
                
                $url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands,$subcategories);
                $response = $this->make_request($url,null);
                $deals = array();
                if(!empty($response)){
                        foreach ($response as $key => $value) {
                                if(!empty($value)){
                                        $deals[] = $value;
                                }
                        }
                }
                // 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'];
                $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'];
                                }
                        }
                }
                
                $filterstr = '&brands='.$brands.'&subcategories='.$subcategories;
                $nexturl = "/categories/getdeals/".$id."/?page=".($page+1)."&sort=".$sort."&direction=".$direction."".$filterstr;

                $this->set(compact('nexturl','deals','id','page','likedDeals','disLikedDeals','sort','direction','brands','filter','filterstr'));
                if(!empty($deals) && !empty($deals[0])){
                        $this->render('/Elements/deals');
                }else{
                        $this->render('/Elements/nodeals');
                }



        }

        public function dealdetail($item_id = null){
                $this->layout = "innerpages";
                $dealinfo = $deal = array();
                $likedDeals = $disLikedDeals = array();
                $url = $this->mobileapi.'entity/'.$item_id;
                $dealinfo = $this->make_request($url,null);
                $this->loadModel('Api');
                $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('deal','dealinfo','likedDeals','disLikedDeals'));
        }
        
        public function saholicdeal($proid = null){
                $likedDeals = $disLikedDeals = array();
                $this->layout = 'ajax';
                $deal = array();
                $url = $this->apihost."getDealById/".$proid;
                $deal = $this->make_request($url,null);
                $this->loadModel('Api');
                
                $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('deal','likedDeals','disLikedDeals'));
                
                $this->render('/Elements/saholicdeal');
                
                
        }
        
        public function getdealsforsearchterm($searchterm,$page = null){
                $likedDeals = $disLikedDeals = array();
                $this->layout = 'ajax';
                $page = $this->request->query('page');
                $subcategories = $this->request->query('subcategories'); 
                if(!isset($page)){
                        $page = 1;
                        $offset = 0;
                        $limit=10;
                }
                else{
                        $offset = ($page*20) - 30;
                        $limit = 20;
                }
                $sort = $this->request->query('sort');
                $direction = $this->request->query('direction');

                $url = $this->apihost."searchSubCategory/?offset=".$offset."&limit=".$limit."&searchTerm=".urlencode($searchterm)."&subCategoryId=".$subcategories;
                $response = $this->make_request($url,null);
                $deals = array();
                if(!empty($response)){
                        foreach ($response as $key => $value) {
                                if(!empty($value)){
                                        $deals[] = $value;
                                }
                        }
                }

                $this->loadModel('Api');

                $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'];
                                }
                        }
                }
                
                $nexturl = "/categories/getdealsforsearchterm/".urlencode($searchterm)."/?page=".($page+1).'&subcategories='.$subcategories;
        
                $this->set(compact('deals','id','page','likedDeals','disLikedDeals','sort','direction','brands','filter','nexturl'));
                if(!empty($deals) && !empty($deals[0])){
                        $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'));
        }}