PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/app/Controller/TagsController.php

https://github.com/Bancha/cakephp
PHP | 98 lines | 60 code | 6 blank | 32 comment | 13 complexity | dcd4d7158a061f45c5f06cc14847ca79 MD5 | raw file
  1. <?php
  2. /**
  3. * Tags Controller
  4. *
  5. */
  6. class TagsController extends AppController {
  7. /**
  8. * index method
  9. *
  10. * @return void
  11. */
  12. public function index() {
  13. $this->Tag->recursive = 0;
  14. $this->set('tags', $this->paginate());
  15. }
  16. /**
  17. * view method
  18. *
  19. * @param string $id
  20. * @return void
  21. */
  22. public function view($id = null) {
  23. $this->Tag->id = $id;
  24. if (!$this->Tag->exists()) {
  25. throw new NotFoundException(__('Invalid tag'));
  26. }
  27. $this->set('tag', $this->Tag->read(null, $id));
  28. }
  29. /**
  30. * add method
  31. *
  32. * @return void
  33. */
  34. public function add() {
  35. if ($this->request->is('post')) {
  36. $this->Tag->create();
  37. if ($this->Tag->save($this->request->data)) {
  38. $this->Session->setFlash(__('The tag has been saved'));
  39. $this->redirect(array('action' => 'index'));
  40. } else {
  41. $this->Session->setFlash(__('The tag could not be saved. Please, try again.'));
  42. }
  43. }
  44. $articles = $this->Tag->Article->find('list');
  45. $this->set(compact('articles'));
  46. }
  47. /**
  48. * edit method
  49. *
  50. * @param string $id
  51. * @return void
  52. */
  53. public function edit($id = null) {
  54. $this->Tag->id = $id;
  55. if (!$this->Tag->exists()) {
  56. throw new NotFoundException(__('Invalid tag'));
  57. }
  58. if ($this->request->is('post') || $this->request->is('put')) {
  59. if ($this->Tag->save($this->request->data)) {
  60. $this->Session->setFlash(__('The tag has been saved'));
  61. $this->redirect(array('action' => 'index'));
  62. } else {
  63. $this->Session->setFlash(__('The tag could not be saved. Please, try again.'));
  64. }
  65. } else {
  66. $this->request->data = $this->Tag->read(null, $id);
  67. }
  68. $articles = $this->Tag->Article->find('list');
  69. $this->set(compact('articles'));
  70. }
  71. /**
  72. * delete method
  73. *
  74. * @param string $id
  75. * @return void
  76. */
  77. public function delete($id = null) {
  78. if (!$this->request->is('post')) {
  79. throw new MethodNotAllowedException();
  80. }
  81. $this->Tag->id = $id;
  82. if (!$this->Tag->exists()) {
  83. throw new NotFoundException(__('Invalid tag'));
  84. }
  85. if ($this->Tag->delete()) {
  86. $this->Session->setFlash(__('Tag deleted'));
  87. $this->redirect(array('action'=>'index'));
  88. }
  89. $this->Session->setFlash(__('Tag was not deleted'));
  90. $this->redirect(array('action' => 'index'));
  91. }
  92. }