Subversion Repositories SmartDukaan

Rev

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

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

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

/**
 * add method
 *
 * @return void
 */
        public function add() {
                if ($this->request->is('post')) {
                        $this->log(print_r($this->request->data,1),'devices');
                        $device = $this->Device->find('first',array('conditions'=>$this->request->data));
                        $this->log(print_r($device,1),'devices');                       
                        if(!$device) {
                                $cachekey = 'webnotifications-'.$this->request->data['user_id'];
                                $activeNotifications = Cache::read($cachekey,$config = 'hour');
                                $this->log(print_r($activeNotifications,1),'checknotification');
                                if(!empty($activeNotifications)){
                                        Cache::delete($cachekey, $config = 'hour');
                                }
                                $this->Device->create();
                                if ($this->Device->save($this->request->data)) {
                                        $result = array('success'=>true,'message'=>__('Record Saved.'));
                                } else {
                                        $result = array('success'=>false,'message'=>print_r($this->Device->validationErrors,1));
                                }
                        } else {
                                $device['Device']['modified']=date('Y-m-d H:i:s');
                                $this->Device->save($device);
                                $result = array('success'=>false,'message'=>__('Identical record found. Updating.'));
                        }
                        $this->response->type('json');
                        $this->layout = 'ajax';
                        $this->set(array(
                            'result' => $response,
                            // 'callback' => $callback,
                            '_serialize' => array('result')
                        ));
                        $this->render('/Elements/json');
                }
        }

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

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

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

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

        public function admin_by($userId = null) {
                $options = array('conditions' => array('Device.user_id' => $userId),'order'=>array('id'=>'desc'),'limit' => 100);
                $this->Paginator->settings = $options;
                $devices = $this->Paginator->paginate();                
                $this->set(compact('devices'));
                $this->render('admin_index');
        }

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

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