Subversion Repositories SmartDukaan

Rev

Rev 13633 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
App::uses('AppController', 'Controller');
/**
 * CategoryDiscounts Controller
 *
 * @property CategoryDiscount $CategoryDiscount
 * @property PaginatorComponent $Paginator
 */
class CategoryDiscountsController extends AppController {

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

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

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

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

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

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

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

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

/**
 * admin_add method
 *
 * @return void
 */
        public function admin_add() {           
                if ($this->request->is('post')) {
                        $conditions = array('category_id'=>$this->request->data['CategoryDiscount']['category_id'],'brand'=>$this->request->data['CategoryDiscount']['brand']);
                        $count = $this->CategoryDiscount->find('count',array('conditions'=>$conditions,'recursive'=>-1));
                        if($count > 0){
                                $this->Session->setFlash(__('Duplicate category and brand combination.'));
                        }else{
                                $this->CategoryDiscount->create();
                                if ($this->CategoryDiscount->save($this->request->data)) {
                                        $this->Session->setFlash(__('The category discount has been saved.'));
                                        return $this->redirect(array('action' => 'index'));
                                } else {
                                        $this->Session->setFlash(__('The category discount could not be saved. Please, try again.'));
                                }
                        }
                }
                $this->loadModel('Brand');
                $allbrands = $this->Brand->find('list');
                foreach ($allbrands as $key => $value) {
                        $brands[$value] = $value;
                }
                $categories = $this->CategoryDiscount->Category->find('list',array('conditions'=>array('parent_id !='=>0)));
                $this->set(compact('categories','brands'));
        }

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

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