Rev 13901 | Rev 14150 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?phpApp::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('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) {if(!empty($userId) && !empty($storeProductId)){//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,'month');if(empty($product)) {$url = $this->apihost.'masterData/getSkuById/'.$storeProductId;$product = $this->make_request($url,null);Cache::write($cachekey,$product,'month');}$storeProduct = json_decode($product[0],1);$cachekey = 'store-'.$storeProduct['source_id'];$store = Cache::read($cachekey,'month');if(empty($store)) {$store = $this->Store->find('first',array('recursive'=>-1,'conditions'=>array('id'=>$storeProduct['source_id'])));Cache::write($cachekey,$store,'month');}$prefix = "SHA".$storeProduct['source_id'];$tag = $prefix.time();if($storeProduct['source_id'] == 2){$url_parts = parse_url($storeProduct['marketPlaceUrl']);// $url = "http://dl.flipkart.com/dl".$url_parts['path'].'?'.$url_parts['query'];if(isset($url_parts['query'])) {$url = "http://m.flipkart.com/".$url_parts['path'].'?'.$url_parts['query'];}else{$url = "http://m.flipkart.com/".$url_parts['path'];}} elseif($storeProduct['source_id'] == 3) {$url_parts = parse_url($storeProduct['marketPlaceUrl']);$url_parts['path'] = str_replace('viewAllSellers/','',$url_parts['path']);//quickfix for snapdealif(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';}}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'=>$storeProduct['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')));$url = $this->apihost.'Catalog/fetchLivePrices/?id='.$storeProductId;$this->make_request($url,null);$this->render('/Elements/jsonp');}}/*** 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'));}/*** admin_index method** @return void*/public function admin_index() {$this->Click->recursive = 0;$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'));}}