Subversion Repositories SmartDukaan

Rev

Rev 15403 | 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','add');
                $this->apihost = Configure::read('pythonapihost');
        }
/**
 * 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();
                $this->Click->setDatabase('mobilehotdtr');
                $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;
                
                $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) {
                        $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";
                        }else{
                                $url = "http://m.snapdeal.com".$url_parts['path'].'?utm_source=aff_prog&utm_campaign=afts&offer_id=17';
                        }
                } elseif($storeId == 4){
                        $urlparts = parse_url($url);
                        $url = "http://m.saholic.com".$urlparts['path'];
                        if(!empty($urlparts['query'])){
                                $url .= '?'.$urlparts['query']; 
                        }                               
                        //$url = $this->getAutoLoginUrl($userId,$url);
                }/*else{
                        $url = $storeProduct['marketPlaceUrl'];
                }*/
                if( strpos($url, '?') === false ) {
                        $firstChar = '?';
                } else {
                        $firstChar = '&';
                }
                $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,'tag'=>$tag,'url'=>$url,'extras'=>json_encode($extras));
                $this->Click->create();
                if ($this->Click->save($data)) {
                        $result = array('success'=>true,'message'=>__('The click has been saved.'),'type'=>'redirect','url'=>$url);
                } 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')
                ));
                $this->render('/Elements/jsonp');
        }

        public function redirecttosaholic() {
                $userId = $this->request->query('user_id');
                if(isset($userId) && !empty($userId)){  
                        $next = "&next=/private-deals/1";                                       
                        $redirectUrl = $this->getAutoLoginUrl($userId,$next);
                        header('location:'.$redirectUrl);
                        exit();
                }
        }       

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