Subversion Repositories SmartDukaan

Rev

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

<?php
App::uses('AppController', 'Controller');
/**
 * Stores Controller
 *
 * @property Store $Store
 * @property PaginatorComponent $Paginator
 */
class StoresController extends AppController {

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

        public function beforeFilter() {
                parent::beforeFilter();
                $this->Auth->allow('redirectto');
        }

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

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

        public function redirectto(){
                $this->layout = 'innerpages';
                $userId = $this->request->query('user_id');
//              if(isset($userId) && !empty($userId)){
//                      $this->loadModel('User');
//                      $dbuser = $this->User->findById($userId);
//                      $this->Auth->login($dbuser['User']);
//              }
                $storeId = $this->request->query('store_id');
                $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 = "http://dl.flipkart.com/dl/";
                } elseif($storeId == 3) {
                        $url = "https://m.snapdeal.com/products/mobiles";                       
                } elseif($storeId == 1){
                        $url = "http://www.amazon.in/gp/goldbox";
                } elseif($storeId == 4) {
                        $url = "http://m.saholic.com";                  
                } 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 = '&';
                }
                $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'=>0,'price'=>0,'tag'=>$tag,'url'=>$url,'extras'=>json_encode($extras));
                $this->loadModel('Click');
                $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->set(compact('store','url'));
        }

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

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

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

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

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

/**
 * admin_add method
 *
 * @return void
 */
        public function admin_add() {
                if ($this->request->is('post')) {
                        $this->Store->create();
                        if ($this->Store->save($this->request->data)) {
                                $this->Session->setFlash(__('The store has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The store could not be saved. Please, try again.'));
                        }
                }
                $statuses = array('active'=>'Active','paused'=>'Paused','deleted'=>'Deleted');
                $cashback_statuses = array('active'=>'Active','inactive'=>'In-active');
                $this->set(compact('statuses','cashback_statuses'));
        }

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

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