Rev 16243 | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?phpApp::uses('AppController', 'Controller');/*** Pushnotifications Controller** @property Pushnotification $Pushnotification* @property PaginatorComponent $Paginator*/class PushnotificationsController extends AppController {/*** Components** @var array*/public $components = array('Paginator');public function beforeFilter() {parent::beforeFilter();$this->Auth->allow('add','generateAffiliateUrl','sendPushNotification');}/*** index method** @return void*/public function index() {$this->Pushnotification->recursive = 0;$this->set('pushnotifications', $this->Paginator->paginate());}/*** view method** @throws NotFoundException* @param string $id* @return void*/public function view($id = null) {if (!$this->Pushnotification->exists($id)) {throw new NotFoundException(__('Invalid pushnotification'));}$total = $this->Article->find('count');/*$options = array('conditions' => array('Pushnotification.' . $this->Pushnotification->primaryKey => $id));$this->set('pushnotification', $this->Pushnotification->find('first', $options));*/}/*** add method** @return void*/public function add() {if ($this->request->is('post')) {$this->log(print_r($this->request->data,1),'pushnotifications');$data = $this->request->data;$data['pushed_by'] = 'php';$url = Configure::read('nodeurl')."/addPushNotificationByApk";//remove hardcoded value$response = $this->post_request($url,$data);$this->log(print_r($response,1),'pushnotifications');if(!empty($response)){$result = array('success' => true,'message'=>'The pushnotification has been saved.');} else {$result = array('success' => false,'message'=>'The pushnotification could not be saved. Please, try again.');}/*$data['type'] = $data['result'];$data['status'] = 1;$data['response_time'] = date('Y-m-d H:i:s',strtotime($data['timestamp']));$data['notification_campaign_id'] = $data['cid'];unset($data['result']);unset($data['cid']);unset($data['timestamp']);if(empty($data['user_id'])){unset($data['user_id']);}$this->Pushnotification->create();if ($this->Pushnotification->save($data)) {$result = array('success' => true,'message'=>'The pushnotification has been saved.');} else {$result = array('success' => false,'message'=>'The pushnotification could not be saved. Please, try again.');}*/}$this->set(array('result' => $result,'_serialize' => array('result')));$this->render('/Elements/json');}/*** edit method** @throws NotFoundException* @param string $id* @return void*/public function edit($id = null) {if (!$this->Pushnotification->exists($id)) {throw new NotFoundException(__('Invalid pushnotification'));}if ($this->request->is(array('post', 'put'))) {if ($this->Pushnotification->save($this->request->data)) {$this->Session->setFlash(__('The pushnotification has been saved.'));return $this->redirect(array('action' => 'index'));} else {$this->Session->setFlash(__('The pushnotification could not be saved. Please, try again.'));}} else {$options = array('conditions' => array('Pushnotification.' . $this->Pushnotification->primaryKey => $id));$this->request->data = $this->Pushnotification->find('first', $options);}$users = $this->Pushnotification->User->find('list');$this->set(compact('users'));}/*** delete method** @throws NotFoundException* @param string $id* @return void*/public function delete($id = null) {$this->Pushnotification->id = $id;if (!$this->Pushnotification->exists()) {throw new NotFoundException(__('Invalid pushnotification'));}$this->request->onlyAllow('post', 'delete');if ($this->Pushnotification->delete()) {$this->Session->setFlash(__('The pushnotification has been deleted.'));} else {$this->Session->setFlash(__('The pushnotification could not be deleted. Please, try again.'));}return $this->redirect(array('action' => 'index'));}/*** admin_index method** @return void*/public function admin_index() {$this->Pushnotification->recursive = 0;$this->set('pushnotifications', $this->Paginator->paginate());}/*** admin_view method** @throws NotFoundException* @param string $id* @return void*/public function admin_view($id = null) {if (!$this->Pushnotification->exists($id)) {throw new NotFoundException(__('Invalid pushnotification'));}$total = $this->Article->find('count');/* $options = array('conditions' => array('Pushnotification.' . $this->Pushnotification->primaryKey => $id));$this->set('pushnotification', $this->Pushnotification->find('first', $options));*/}/*** admin_add method** @return void*/public function admin_add() {if ($this->request->is('post')) {$this->Pushnotification->create();if ($this->Pushnotification->save($this->request->data)) {$this->Session->setFlash(__('The pushnotification has been saved.'));return $this->redirect(array('action' => 'index'));} else {$this->Session->setFlash(__('The pushnotification could not be saved. Please, try again.'));}}$users = $this->Pushnotification->User->find('list');$this->set(compact('users'));}/*** admin_edit method** @throws NotFoundException* @param string $id* @return void*/public function admin_edit($id = null) {if (!$this->Pushnotification->exists($id)) {throw new NotFoundException(__('Invalid pushnotification'));}if ($this->request->is(array('post', 'put'))) {if ($this->Pushnotification->save($this->request->data)) {$this->Session->setFlash(__('The pushnotification has been saved.'));return $this->redirect(array('action' => 'index'));} else {$this->Session->setFlash(__('The pushnotification could not be saved. Please, try again.'));}} else {$options = array('conditions' => array('Pushnotification.' . $this->Pushnotification->primaryKey => $id));$this->request->data = $this->Pushnotification->find('first', $options);}$users = $this->Pushnotification->User->find('list');$this->set(compact('users'));}/*** admin_delete method** @throws NotFoundException* @param string $id* @return void*/public function admin_delete($id = null) {$this->Pushnotification->id = $id;if (!$this->Pushnotification->exists()) {throw new NotFoundException(__('Invalid pushnotification'));}$this->request->onlyAllow('post', 'delete');if ($this->Pushnotification->delete()) {$this->Session->setFlash(__('The pushnotification has been deleted.'));} else {$this->Session->setFlash(__('The pushnotification could not be deleted. Please, try again.'));}return $this->redirect(array('action' => 'index'));}public function generateAffiliateUrl(){$url = $this->request->data['url'];$user_id = $this->request->data['userId'];$storeId = $this->request->data['storeId'];//Get StoreProduct Info$cachekey = 'store-'.$storeId;$store = Cache::read($cachekey,'month');if(empty($store)) {$this->loadModel('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);} elseif($storeId == 3) {$url_parts = parse_url($url);$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';}} elseif($storeId == 4){$next = str_replace('www','m',$url);$url = $this->getAutoLoginUrl($userId,$next);$url .= '?utm_source=profitmandi';} else {$url .= "?user_id=$user_id";}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'],'source'=>'notification');// $data = array('user_id' => $user_id,'store_product_id'=>0,'tag'=>$tag,'url'=>$url,'price'=>0,'extras'=>json_encode($extras));// $this->loadModel('Click');// $this->Click->create();// $this->Click->save($data);// return $url;$result = array('url' => $url );$this->layout = "ajax";$this->response->type('json');$this->set(array('result' => $result,'_serialize' => array('result')));$this->render('/Elements/json');}public function sendPushNotification() {$gcm_id = $this->request->data['gcmId'];$user_id = $this->request->data['userId'];$cid = $this->request->data['cid'];$title = $this->request->data['title'];$message = $this->request->data['message'];$type = $this->request->data['type'];$url = $this->request->data['url'];session_write_close();Configure::load('live');$msg = array('message' => $message,'cid' => $cid,'title' => $title,'type' => $type,'url' => $url,'vibrate' => 1,'sound' => 1,'largeIcon' => 'large_icon','smallIcon' => 'small_icon');// Set POST variables$url = 'https://android.googleapis.com/gcm/send';$registration_ids = array($gcm_id);$fields = array('registration_ids' => $registration_ids,'data' => $msg,);$headers = array('Authorization: key=' . Configure::read('googleapikey'),'Content-Type: application/json');$this->log(print_r($msg,1),'pushnotifications');// Open connection$ch = curl_init();// Set the url, number of POST vars, POST datacurl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// Disabling SSL Certificate support temporarlycurl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));// Execute post$result = curl_exec($ch);if ($result === FALSE) {die('Curl failed: ' . curl_error($ch));}$result = json_decode($result,1);// Close connectioncurl_close($ch);$this->log("For $user_id ".$registatoin_ids[0]." ".print_r($result,1),'pushnotifications');if($res['success']==1){$data = array('id'=>$cid,'type'=>'sent','status'=>$res['success'],'message'=>'success');$this->Pushnotification->save($data);}else if($res['success']==0){$message=$res['results'][0]['error'];$data = array('id'=>$cid,'type'=>'sent','status'=>$res['success'],'message'=>$message);$sqlQuery="update gcm_users set failurecount=failurecount+1 where gcm_regid='".$registration_ids[0]."'";$resultData=$this->GcmUser->query($sqlQuery);$this->Pushnotification->save($data);}$this->layout = "ajax";$this->response->type('json');$this->set(array('result' => $result,'_serialize' => array('result')));$this->render('/Elements/json');}}