Subversion Repositories SmartDukaan

Rev

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

<?php
App::uses('AppController', 'Controller');
/**
 * GcmUsers Controller
 *
 * @property GcmUser $GcmUser
 * @property PaginatorComponent $Paginator
 */
class GcmUsersController 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->GcmUser->recursive = 0;
                $this->set('gcmUsers', $this->Paginator->paginate());
        }

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

/**
 * add method
 *
 * @return void
 */
        public function add() {
                $this->layout = 'ajax';
                $this->response->type('json');
                if ($this->request->is('post')) {
                        if(empty($this->request->data['user_id']) || empty($this->request->data['gcm_regid'])){
                                $result = array('success'=>false);
                                $this->log('error'.print_r($this->request->data,1),'gcm');
                        } else {
                                $dataForModel = $this->request->data;
                                $this->log('before find '.print_r($dataForModel,1),'gcm');
                                $data = $this->GcmUser->find('first',array('conditions'=>array('user_id'=>$dataForModel['user_id'],'imeinumber'=>$dataForModel['imeinumber']),'order'=>array('id'=>'desc')));
                                $this->log("found row ".print_r($data,1),'gcm');
                                if(!empty($data)){
                                        $dataForModel['id'] = $data['GcmUser']['id'];
                                        if(!array_key_exists('notification_type', $dataForModel)) {
                                                if($dataForModel['gcm_regid']!=$data['GcmUser']['gcm_regid']){
                                                        $dataForModel['notification_type'] = 'gcm';
                                                }
                                        } 
                                        $dataForModel['failurecount'] = 0;                                      
                                }else{
                                        $this->GcmUser->create();
                                }                       
                                $this->log('lets save '.print_r($dataForModel,1),'gcm');
                                if ($this->GcmUser->save($dataForModel)) {
                                        $result = array('success'=>true);       
                                        if(!empty($dataForModel['androidid'])){
                                                $url = Configure::read('nodeurl')."/addingTopNotifications?user_id=".$dataForModel['user_id']."&android_id=".$dataForModel['androidid'];
                                                $this->make_request($url,null);
                                        }
                                } else {
                                        $result = array('success'=>false);
                                        $this->log("failure ".print_r($this->GcmUser->validationErrors,1));
                                }
                        }
                }
                $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->GcmUser->exists($id)) {
                        throw new NotFoundException(__('Invalid gcm user'));
                }
                if ($this->request->is(array('post', 'put'))) {
                        if ($this->GcmUser->save($this->request->data)) {
                                $this->Session->setFlash(__('The gcm user has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The gcm user could not be saved. Please, try again.'));
                        }
                } else {
                        $options = array('conditions' => array('GcmUser.' . $this->GcmUser->primaryKey => $id));
                        $this->request->data = $this->GcmUser->find('first', $options);
                }
        }

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

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

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

/**
 * admin_add method
 *
 * @return void
 */
        public function admin_add() {
                if ($this->request->is('post')) {
                        $this->GcmUser->create();
                        if ($this->GcmUser->save($this->request->data)) {
                                $this->Session->setFlash(__('The gcm user has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The gcm user 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->GcmUser->exists($id)) {
                        throw new NotFoundException(__('Invalid gcm user'));
                }
                if ($this->request->is(array('post', 'put'))) {
                        if ($this->GcmUser->save($this->request->data)) {
                                $this->Session->setFlash(__('The gcm user has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The gcm user could not be saved. Please, try again.'));
                        }
                } else {
                        $options = array('conditions' => array('GcmUser.' . $this->GcmUser->primaryKey => $id));
                        $this->request->data = $this->GcmUser->find('first', $options);
                }
        }

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