Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

<?php
App::uses('AppController', 'Controller');
/**
 * NotificationCampaigns Controller
 *
 * @property NotificationCampaign $NotificationCampaign
 * @property PaginatorComponent $Paginator
 */
class NotificationCampaignsController extends AppController {

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

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

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

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

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

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

/**
 * admin_index method
 *
 * @return void
 */
        public function admin_index() {
                $this->NotificationCampaign->recursive = 0;
                $this->Paginator->settings = array('order' => array('id'=>'desc'));
                $vari = $this->Paginator->paginate();
                foreach ($vari as $key => $value) {
                        $cid=$value['NotificationCampaign']['id'];
                        $sqlQuery = "SELECT status as status,notification_campaign_id,type,count(*) as count FROM pushnotifications group by notification_campaign_id, type,status";
                        $resul=$this->NotificationCampaign->query($sqlQuery);   
                }
                $finalResult = array();
                foreach ($resul as $key => $value) {
                        $finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['type']]=$value[0]['count'];
                        $finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['status']]=$value[0]['count'];
                        //$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['type']]=$value[0]['status'];                       
                }
                /*debug($finalResult);*/
                $this->set('notificationCampaigns', $vari);
                $this->set('notificationCampaignsCount', $finalResult);

        }

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_view($id = null) {
                if (!$this->NotificationCampaign->exists($id)) {
                        throw new NotFoundException(__('Invalid notification campaign'));
                }
                $sort = $this->request->query('type');
                $direction = $this->request->query('order');
                $options = array('conditions' => array('NotificationCampaign.' . $this->NotificationCampaign->primaryKey => $id));
                $userData=$this->NotificationCampaign->find('first', $options);
                $userActions = array();
                foreach ($userData['Pushnotification'] as $key => $value) {
                        if($value['type']=='sent'){
                        $userActions[$value['user_id']][$value['type']] = $value['created'];
                        $userActions[$value['user_id']]['status'] =$value['status'];
                        }else{
                                $userActions[$value['user_id']][$value['type']] = $value['response_time'];
                        }
                         //debug($value);
                }
                $cid=$userData['NotificationCampaign']['id'];
                $sqlQuery = "SELECT status as status,notification_campaign_id,type,count(*) as count FROM pushnotifications where notification_campaign_id=$cid group by type,status";
                $resul=$this->NotificationCampaign->query($sqlQuery);   
                $finalResult = array();
                foreach ($resul as $key => $value) {
                        $finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['type']]=$value[0]['count'];
                        $finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['status']]=$value[0]['count'];
                        //$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['type']]=$value[0]['status'];                       
                }
                //debug($finalResult);
                $this->set('notificationData', $userData);
                $this->set('notificationCampaign', $userActions);
                $this->set('notificationCount', $finalResult);
        }

/**
 * admin_add method
 *
 * @return void
 */
        public function admin_add() {
                if ($this->request->is('post')) {
                        $this->NotificationCampaign->create();
                        if ($this->NotificationCampaign->save($this->request->data)) {
                                $this->Session->setFlash(__('The notification campaign has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The notification campaign could not be saved. Please, try again.'));
                        }
                }
        }

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

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_delete($id = null) {
                $this->NotificationCampaign->id = $id;
                if (!$this->NotificationCampaign->exists()) {
                        throw new NotFoundException(__('Invalid notification campaign'));
                }
                $this->request->onlyAllow('post', 'delete');
                if ($this->NotificationCampaign->delete()) {
                        $this->Session->setFlash(__('The notification campaign has been deleted.'));
                } else {
                        $this->Session->setFlash(__('The notification campaign could not be deleted. Please, try again.'));
                }
                return $this->redirect(array('action' => 'index'));
        }
        
        public function admin_show($id = null,$type=null,$status=null) {
                $sqlQuery = "SELECT * FROM pushnotifications where notification_campaign_id=$id and type='$type' and status=$status" ;
                $resul=$this->NotificationCampaign->query($sqlQuery);
                
                $sqlQuery1 = "SELECT * FROM notification_campaigns where id=$id" ;
                $resultData=$this->NotificationCampaign->query($sqlQuery1);
                
                $this->set('data', $resul);
                $this->set('notificationData', $resultData);
                //$resul = $this->Paginator->paginate();
                //debug($resultData);
        }

        public function admin_user($id = null) {
                $sqlQuery = "SELECT * FROM pushnotifications where user_id=$id order by id desc" ;
                $resul=$this->NotificationCampaign->query($sqlQuery);
                $this->set('userdata', $resul);
                //$this->set('notificationData', $resultData);
                //$resul = $this->Paginator->paginate();
                //debug($resul);
        }
        public function admin_sort($id = null) {
                $sort = $this->request->query('type');
                $direction = $this->request->query('order');
                //$cid = $this->request->query('cid');
                if($sort=='user_id'){
                        $sqlQuery = "SELECT * FROM pushnotifications where notification_campaign_id =$id order by $sort $direction" ;                   
                }
                else{
                        $sqlQuery = "SELECT * FROM pushnotifications where notification_campaign_id =$id order by type='$sort' $direction" ;
                }
                $resul=$this->NotificationCampaign->query($sqlQuery);
                $userActions = array();
                foreach ($resul as $key => $value) {
                        if($value['pushnotifications']['type']=='sent'){
                        $userActions[$value['pushnotifications']['user_id']][$value['pushnotifications']['type']] = $value['pushnotifications']['created'];
                        $userActions[$value['pushnotifications']['user_id']]['status'] =$value['pushnotifications']['status'];
                        }else{
                                $userActions[$value['pushnotifications']['user_id']][$value['pushnotifications']['type']] = $value['pushnotifications']['response_time'];
                        }                       
                }
                $this->set('notificationId',$id);
                $this->set('sortdata', $userActions);
        }

        public function notificationactive(){
                $cid = $this->request->query('cid');
                $options = array('conditions' => array('id'=> $cid,'status'=>'active','expiresAt >'=>date('Y-m-d H:i:s',time())));
                $count = $this->NotificationCampaign->find('count',$options);
                
                if(!$count){
                        $result = array('success'=>false);
                }else{
                        $result = array('success'=>true);
                }
                
                $this->response->type('json');
                $this->layout = 'ajax';
                $this->set(array(
                    'result' => $result,
                    '_serialize' => array('result')
                ));
                $this->render('/Elements/json');
        }
}