Subversion Repositories SmartDukaan

Rev

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

<?php
App::uses('AppController', 'Controller');
/**
 * Brands Controller
 *
 * @property Brand $Brand
 * @property PaginatorComponent $Paginator
 */
class BrandsController extends AppController {

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

        public function beforeFilter() {
                parent::beforeFilter();
                $this->Auth->allow('filter');
        }

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

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

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

        public function filter($str,$catId=3) {
                setcookie("brandschosen", urldecode($str), time()+6*3600, '/');
                setcookie("old_cid", $catId, time()+6*3600, '/');
                $this->redirect('/category/'.$catId);
        }       

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

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

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

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

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

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_edit($id = null) {
                $this->response->type('json');
                $this->layout = 'ajax';
                if (!$this->Brand->exists($id)) {
                        throw new NotFoundException(__('Invalid brand'));
                }
                if ($this->request->is(array('post', 'put'))) {
                        if ($this->Brand->save($this->request->data)) {
                                $result = array('success'=>true,'message'=>'The brand has been edited');
                        } else {
                                $result = array('success'=>false,'message'=>'The brand could not be edited. Try again later');
                        }
                } 
                $this->set(array(
                    'result' => $result,
                    '_serialize' => array('result')
                )); 
                $this->render('/Elements/json');  
        }

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

        public function admin_genurl() {
                $this->Brand->Category->Behaviors->attach('Containable');
                $options = array('conditions'=>array('parent_id !='=>0),'contain'=>(array('Brand.name','Brand.displayed_in_preference_page','Brand.id')));
                $categories = $this->Brand->Category->find('all',$options);
                $this->set(compact('categories'));
        }
}