PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Controller/ProductsController.php

https://gitlab.com/fouzia23chowdhury/cakephpCRUD
PHP | 109 lines | 60 code | 6 blank | 43 comment | 12 complexity | 606d39779110f104228e70efea49ea50 MD5 | raw file
  1. <?php
  2. App::uses('AppController', 'Controller');
  3. /**
  4. * Products Controller
  5. *
  6. * @property Product $Product
  7. * @property PaginatorComponent $Paginator
  8. * @property SessionComponent $Session
  9. */
  10. class ProductsController extends AppController {
  11. /**
  12. * Components
  13. *
  14. * @var array
  15. */
  16. public $components = array('Paginator', 'Session');
  17. /**
  18. * index method
  19. *
  20. * @return void
  21. */
  22. public function index() {
  23. $this->Product->recursive = 0;
  24. $this->set('products', $this->Paginator->paginate());
  25. }
  26. /**
  27. * view method
  28. *
  29. * @throws NotFoundException
  30. * @param string $id
  31. * @return void
  32. */
  33. public function view($id = null) {
  34. if (!$this->Product->exists($id)) {
  35. throw new NotFoundException(__('Invalid product'));
  36. }
  37. $options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id));
  38. $this->set('product', $this->Product->find('first', $options));
  39. }
  40. /**
  41. * add method
  42. *
  43. * @return void
  44. */
  45. public function add() {
  46. if ($this->request->is('post')) {
  47. $this->Product->create();
  48. if ($this->Product->save($this->request->data)) {
  49. $this->Session->setFlash(__('The product has been saved.'));
  50. return $this->redirect(array('action' => 'index'));
  51. } else {
  52. $this->Session->setFlash(__('The product could not be saved. Please, try again.'));
  53. }
  54. }
  55. $categories = $this->Product->Category->find('list');
  56. $this->set(compact('categories'));
  57. }
  58. /**
  59. * edit method
  60. *
  61. * @throws NotFoundException
  62. * @param string $id
  63. * @return void
  64. */
  65. public function edit($id = null) {
  66. if (!$this->Product->exists($id)) {
  67. throw new NotFoundException(__('Invalid product'));
  68. }
  69. if ($this->request->is(array('post', 'put'))) {
  70. if ($this->Product->save($this->request->data)) {
  71. $this->Session->setFlash(__('The product has been saved.'));
  72. return $this->redirect(array('action' => 'index'));
  73. } else {
  74. $this->Session->setFlash(__('The product could not be saved. Please, try again.'));
  75. }
  76. } else {
  77. $options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id));
  78. $this->request->data = $this->Product->find('first', $options);
  79. }
  80. $categories = $this->Product->Category->find('list');
  81. $this->set(compact('categories'));
  82. }
  83. /**
  84. * delete method
  85. *
  86. * @throws NotFoundException
  87. * @param string $id
  88. * @return void
  89. */
  90. public function delete($id = null) {
  91. $this->Product->id = $id;
  92. if (!$this->Product->exists()) {
  93. throw new NotFoundException(__('Invalid product'));
  94. }
  95. $this->request->allowMethod('post', 'delete');
  96. if ($this->Product->delete()) {
  97. $this->Session->setFlash(__('The product has been deleted.'));
  98. } else {
  99. $this->Session->setFlash(__('The product could not be deleted. Please, try again.'));
  100. }
  101. return $this->redirect(array('action' => 'index'));
  102. }
  103. }