/app/Controller/ProductsController.php
PHP | 109 lines | 60 code | 6 blank | 43 comment | 12 complexity | 606d39779110f104228e70efea49ea50 MD5 | raw file
- <?php
- App::uses('AppController', 'Controller');
- /**
- * Products Controller
- *
- * @property Product $Product
- * @property PaginatorComponent $Paginator
- * @property SessionComponent $Session
- */
- class ProductsController extends AppController {
- /**
- * Components
- *
- * @var array
- */
- public $components = array('Paginator', 'Session');
- /**
- * index method
- *
- * @return void
- */
- public function index() {
- $this->Product->recursive = 0;
- $this->set('products', $this->Paginator->paginate());
- }
- /**
- * view method
- *
- * @throws NotFoundException
- * @param string $id
- * @return void
- */
- public function view($id = null) {
- if (!$this->Product->exists($id)) {
- throw new NotFoundException(__('Invalid product'));
- }
- $options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id));
- $this->set('product', $this->Product->find('first', $options));
- }
- /**
- * add method
- *
- * @return void
- */
- public function add() {
- if ($this->request->is('post')) {
- $this->Product->create();
- if ($this->Product->save($this->request->data)) {
- $this->Session->setFlash(__('The product has been saved.'));
- return $this->redirect(array('action' => 'index'));
- } else {
- $this->Session->setFlash(__('The product could not be saved. Please, try again.'));
- }
- }
- $categories = $this->Product->Category->find('list');
- $this->set(compact('categories'));
- }
- /**
- * edit method
- *
- * @throws NotFoundException
- * @param string $id
- * @return void
- */
- public function edit($id = null) {
- if (!$this->Product->exists($id)) {
- throw new NotFoundException(__('Invalid product'));
- }
- if ($this->request->is(array('post', 'put'))) {
- if ($this->Product->save($this->request->data)) {
- $this->Session->setFlash(__('The product has been saved.'));
- return $this->redirect(array('action' => 'index'));
- } else {
- $this->Session->setFlash(__('The product could not be saved. Please, try again.'));
- }
- } else {
- $options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id));
- $this->request->data = $this->Product->find('first', $options);
- }
- $categories = $this->Product->Category->find('list');
- $this->set(compact('categories'));
- }
- /**
- * delete method
- *
- * @throws NotFoundException
- * @param string $id
- * @return void
- */
- public function delete($id = null) {
- $this->Product->id = $id;
- if (!$this->Product->exists()) {
- throw new NotFoundException(__('Invalid product'));
- }
- $this->request->allowMethod('post', 'delete');
- if ($this->Product->delete()) {
- $this->Session->setFlash(__('The product has been deleted.'));
- } else {
- $this->Session->setFlash(__('The product could not be deleted. Please, try again.'));
- }
- return $this->redirect(array('action' => 'index'));
- }
- }