Subversion Repositories SmartDukaan

Rev

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

<?php
App::uses('AppController', 'Controller');
/**
 * Clicks Controller
 *
 * @property Click $Click
 * @property PaginatorComponent $Paginator
 */
class ClicksController extends AppController {

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

        public function beforeFilter() {
                parent::beforeFilter();
                $this->Auth->allow('redirecttosaholic');
                $this->apihost = Configure::read('pythonapihost');
                $this->livepriceurl = Configure::read('livepriceurl');
        }
/**
 * index method
 *
 * @return void
 */
        public function index() {
                $this->Click->recursive = 0;
                $this->set('clicks', $this->Paginator->paginate());
        }

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

/**
 * add method
 *
 * @return void
 */
        public function add($userId=null,$storeProductId=null,$storeId=null) {
                session_write_close();
                if(!empty($userId) && !empty($storeProductId)){
                        $url = $this->request->query('url');
                        $available_price = $this->request->query('price');
                        //Get StoreProduct Info
                        $this->loadModel('Store');
                        // $options = array('conditions'=>array('id'=>$storeProductId),'fields'=>array('source_id','available_price','url'),'recursive'=>-1);
                        // $storeProduct = $this->StoreProduct->find('first',$options);
                        $cachekey = 'storeproduct-'.$storeProductId;
                        
                        $product = Cache::read($cachekey,'five');
                        if(empty($product)) {
                                $producturl = $this->apihost.'masterData/getSkuById/'.$storeProductId;
                                $product = $this->make_request($producturl,null);
                                Cache::write($cachekey,$product,'five');                        
                        }
                        $storeProduct = json_decode($product[0],1);
                        $storeProductName = $storeProduct['source_product_name'];
                        $storeProductBrand = $storeProduct['brand'];
                        $storeProductCategory = $storeProduct['category_id'];
                        if($url=='undefined' || $available_price=='undefined' || $storeId=='undefined') {
                                $url = $storeProduct['marketPlaceUrl'];
                                $storeId =      $storeProduct['source_id'];
                                $available_price = $storeProduct['available_price'];
                        }
                        $url1=$url;
                        $cachekey = 'store-'.$storeId;
                        //$store = Cache::read($cachekey,'month');
                        if(empty($store)) {
                                $store = $this->Store->find('first',array('recursive'=>-1,'conditions'=>array('id'=>$storeId)));
                                Cache::write($cachekey,$store,'month');                 
                        }
                        $prefix = "SHA".$storeId;
                        $tag = $prefix.time();
                        if($storeId == 2){                              
                                // $url = str_replace('www','m',$url);
                                $url = str_replace('www.','',$url);
                $url = str_replace('http://','',$url);
                $url = str_replace('flipkart.com','http://dl.flipkart.com/dl',$url);
                        } elseif($storeId == 3) {
                                //$tag='SHASNAPDEAL123';
                                $url_parts = parse_url($url);
                                $url_parts['path'] = str_replace('viewAllSellers/','',$url_parts['path']);//quickfix for snapdeal
                                if(isset($url_parts['query'])) {
                                        //$url = "http://m.snapdeal.com".$url_parts['path'].'?'.$url_parts['query']."&utm_source=aff_prog&utm_campaign=afts&offer_id=17";
                                        $url = "http://m.snapdeal.com".$url_parts['path'].'?'.'utm_source=aff_prog&utm_campaign=afts&offer_id=17';
                                }else{
                                        $url = "http://m.snapdeal.com".$url_parts['path'].'?';
                                }       
                        } elseif($storeId == 4){
                                $urlparts = parse_url($url);
                                $url = $urlparts['path'];
                                if(!empty($urlparts['query'])){
                                        $url .= '?'.$urlparts['query']; 
                                }               
                                $tokenValidated = $this->checkToken($userId);
                                if(isset($userId) && !empty($userId) && $tokenValidated==1){                    
                                        $url = $this->getAutoLoginUrl($userId,$url);
                                } else{                                 
                                        if($tokenValidated == 0){
                                                $url = '/special/native/login';
                                        }elseif($tokenValidated == -1){
                                                $url = '/abouts/askforupdate';
                                        }
                                }
                                $this->log(print_r($url,1),'headers');
                        } elseif($storeId==5){
                                $url = "http://clk.omgt5.com/?PID=10314&r=$url";
                        } elseif($storeId==6){
                                $url = "http://clk.omgt5.com/?PID=11365&r=$url";
                        } elseif($storeId==7){
                                $url = "http://clk.omgt5.com/?PID=9419&r=$url";
                        }
                        if( strpos($url, '?') === false ) {
                                $firstChar = '?';
                        } else {
                                $firstChar = '&';
                        }
                        if ($storeId!=3) {
                                $url .= $firstChar.$store['Store']['affid_param'].'='.$store['Store']['affiliate_id'];
                                if(!empty($store['Store']['sub_tag_param'])){
                                        $url .= '&'.$store['Store']['sub_tag_param'].'='.$tag;
                                }
                        }
                        else{
                $url .= $firstChar.$store['Store']['affid_param'].'='.$store['Store']['affiliate_id'];
                if(!empty($store['Store']['sub_tag_param'])){
                        $url .= '&'.$store['Store']['sub_tag_param'].'='.$tag;
                }
            }
                        $extras = array('store'=>$store['Store']['name']);
                        $data = array('user_id'=>$userId,'store_product_id'=>$storeProductId,'price'=>$available_price, 'product_name'=>$storeProductName, 'brand'=>$storeProductBrand, 'category_id'=>$storeProductCategory, 'tag'=>$tag,'url'=>$url,'extras'=>json_encode($extras));
                        $this->Click->create();
                        $curl = $this->livepriceurl."!latestPriceById?id=$storeProductId&source_id=$storeId&url=".base64_encode($url1);
                        $response = $this->make_request($curl,null);
                        $this->log("live price response ".print_r($response,1),'api');
                        if ($this->Click->save($data)) {
                                if($response['result']){
                                        $result = array('success'=>true,'message'=> $response['message'],'type'=>'redirect','url'=>$url,'showmessage'=>1);
                                }else{
                                        $result = array('success'=>true,'message'=> __('The click has been saved.'),'type'=>'redirect','url'=>$url,'showmessage'=>0);
                                }                               
                        } else {
                                $result = array('success'=>false,'message'=>__('The click could not be saved. Please, try again.'));
                        }
                        $this->response->type('json');
                        $this->layout = 'ajax';
                        $callback = $this->request->query('callback');
                        $this->set(array(
                            'result' => $result,
                            'callback' => $callback,
                            '_serialize' => array('result')
                        ));
                        // $url = $this->apihost.'Catalog/fetchLivePrices/?id='.$storeProductId;                        
                        $this->render('/Elements/jsonp');
                }
        }

        public function redirecttosaholic() {
                $userId = $this->request->query('user_id');
                $tokenValidated = $this->checkToken($userId);
                if(isset($userId) && !empty($userId) && $tokenValidated==1){
                        $next = "&next=/private-deals/1";
                        $redirectUrl = $this->getAutoLoginUrl($userId,$next);
                        header('location:'.$redirectUrl);
                        exit();
                } else{
                        // $this->redirect(array('controller'=>'special','action'=>'native','login'));
                        if($tokenValidated == 0){
                                $this->redirect(array('controller'=>'special','action'=>'native','login'));
                        }elseif($tokenValidated == -1){
                                $this->redirect(array('controller'=>'abouts','action'=>'askforupdate'));
                        }
                }
        }       

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

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

        public function admin_by($userId = null) {              
                $this->Click->recursive = 0;
                $q = $this->request->query('q');
                if(isset($q) && !empty($q)){
                        $options = array('conditions' => array('Click.user_id' => $userId, 'Click.url LIKE'=>'%'.$q.'%'),'order'=>array('id'=>'desc'),'limit' => 100);
                        $this->set(compact('q'));
                } else {
                        $options = array('conditions' => array('Click.user_id' => $userId),'order'=>array('id'=>'desc'),'limit' => 100);
                }
                $this->Paginator->settings = $options;
                $clicks = $this->Paginator->paginate();         
                $this->set(compact('clicks'));
        }

/**
 * admin_index method
 *
 * @return void
 */
        public function admin_index() {
                $this->Click->recursive = 0;
                $q = $this->request->query('q');
                if(isset($q) && !empty($q)){
                        $this->Paginator->settings = array('conditions' => array('Click.url LIKE'=>'%'.$q.'%'),'order' => array('id'=>'desc'));
                        $this->set(compact('q'));
                } else {
                        $this->Paginator->settings = array('order' => array('id'=>'desc'));
                }
                $this->set('clicks', $this->Paginator->paginate());
        }

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

/**
 * admin_add method
 *
 * @return void
 */
        public function admin_add() {
                if ($this->request->is('post')) {
                        $this->Click->create();
                        if ($this->Click->save($this->request->data)) {
                                $this->Session->setFlash(__('The click has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The click could not be saved. Please, try again.'));
                        }
                }
                $users = $this->Click->User->find('list');
                $storeProducts = $this->Click->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->Click->exists($id)) {
                        throw new NotFoundException(__('Invalid click'));
                }
                if ($this->request->is(array('post', 'put'))) {
                        if ($this->Click->save($this->request->data)) {
                                $this->Session->setFlash(__('The click has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The click could not be saved. Please, try again.'));
                        }
                } else {
                        $options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
                        $this->request->data = $this->Click->find('first', $options);
                }
                $users = $this->Click->User->find('list');
                $storeProducts = $this->Click->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->Click->id = $id;
                if (!$this->Click->exists()) {
                        throw new NotFoundException(__('Invalid click'));
                }
                $this->request->onlyAllow('post', 'delete');
                if ($this->Click->delete()) {
                        $this->Session->setFlash(__('The click has been deleted.'));
                } else {
                        $this->Session->setFlash(__('The click could not be deleted. Please, try again.'));
                }
                return $this->redirect(array('action' => 'index'));
        }
}