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

/app/Controller/ArticlesController.php

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