Subversion Repositories SmartDukaan

Rev

Blame | 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')) {
                        $dataForModel = $this->request->data;
                        $this->log('before find '.print_r($dataForModel,1),'gcm');
                        $data = $this->GcmUser->find('first',array('conditions'=>array('user_id'=>$this->request->data['user_id'],'imeinumber'=>$this->request->data['imeinumber'])));
                        $this->log("found row ".print_r($data,1),'gcm');
                        if(!empty($data)){
                                $dataForModel['id'] = $data['GcmUser']['id'];
                        }else{
                                $this->GcmUser->create();
                        }                       
                        $this->log('lets save '.print_r($dataForModel,1),'gcm');
                        if ($this->GcmUser->save($dataForModel)) {
                                $result = array('success'=>true);
//                              $this->Session->setFlash(__('The gcm user has been saved.'));
//                              return $this->redirect(array('action' => 'index'));
                        } else {
                                $result = array('success'=>false);
//                              $this->Session->setFlash(__('The gcm user 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->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'));
        }}