Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

<?php
App::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');
        }
/**
 * 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'));
                }
                $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['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->log(print_r($data,1),'pushnotifications');
                        $this->Pushnotification->create();
                        if ($this->Pushnotification->save($data)) {
                                // $this->Session->setFlash(__('The pushnotification has been saved.'));
                                // return $this->redirect(array('action' => 'index'));
                                $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->Session->setFlash(__('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'));
                }
                $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'));
        }}