Guest User

Untitled

a guest
Dec 18th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. <?php
  2. namespace App\Controller;
  3.  
  4. use App\Controller\AppController;
  5. use Api\Service\Invoker;
  6. use CakeDC\Api\Exception\ValidationException;
  7.  
  8. /**
  9. * Blogs Controller
  10. *
  11. * @property \App\Model\Table\BlogsTable $Blogs
  12. */
  13. class BlogsController extends AppController
  14. {
  15.  
  16. public function initialize()
  17. {
  18. parent::initialize();
  19. $this->loadComponent('ApiExtra.ApiPaginator');
  20. }
  21.  
  22. /**
  23. * Index method
  24. *
  25. * @return \Cake\Network\Response|null
  26. */
  27. public function index()
  28. {
  29. $blogs = Invoker::call('blogs', $this);
  30. $this->ApiPaginator->paginate($blogs, ['alias' => 'Blogs']);
  31.  
  32. $blogs = $blogs->data();
  33. $this->set(compact('blogs'));
  34. $this->set('_serialize', ['blogs']);
  35. }
  36.  
  37. /**
  38. * View method
  39. *
  40. * @param string|null $id Blog id.
  41. * @return \Cake\Network\Response|null
  42. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
  43. */
  44. public function view($id = null)
  45. {
  46. $blog = Invoker::call('blogs', $this, ['suffix' => $id]);
  47.  
  48. $this->set('blog', $blog->data());
  49. $this->set('_serialize', ['blog']);
  50. }
  51.  
  52. /**
  53. * Add method
  54. *
  55. * @return \Cake\Network\Response|null Redirects on successful add, renders view otherwise.
  56. */
  57. public function add()
  58. {
  59. $blog = $this->Blogs->newEntity();
  60. try {
  61. if ($this->request->is('post')) {
  62. $blog = Invoker::call('blogs', $this);
  63. $result = $blog->data();
  64. if ($result) {
  65. $this->Flash->success(__('The blog has been saved.'));
  66. if ($this->request->is('ajax')) {
  67. return $this->redirect([
  68. 'action' => 'view',
  69. $result->id,
  70. '_ext' => 'json'
  71. ]);
  72. }
  73.  
  74. return $this->redirect(['action' => 'index']);
  75. }
  76. $this->Flash->error(__('The blog could not be saved. Please, try again.'));
  77. }
  78. } catch (ValidationException $e) {
  79. debug($e->getValidationErrors());
  80. $blog->setErrors($e->getValidationErrors());
  81.  
  82. }
  83. // $this->set($this->Blogs->buildFormParams());
  84. $this->set(compact('blog'));
  85. $this->set('_serialize', ['blog']);
  86. }
  87.  
  88. /**
  89. * Edit method
  90. *
  91. * @param string|null $id Blog id.
  92. * @return \Cake\Network\Response|null Redirects on successful edit, renders view otherwise.
  93. * @throws \Cake\Network\Exception\NotFoundException When record not found.
  94. */
  95. public function edit($id = null)
  96. {
  97. try {
  98. if ($this->request->is(['patch', 'post', 'put'])) {
  99. $blog = Invoker::call('blogs', $this, ['suffix' => $id]);
  100. if ($blog->data()) {
  101. $this->Flash->success(__('The blog has been saved.'));
  102.  
  103. return $this->redirect(['action' => 'index']);
  104. }
  105. $blog = $blog->data();
  106. $this->Flash->error(__('The blog could not be saved. Please, try again.'));
  107. } else {
  108. $blog = Invoker::call('blogs', $this, ['suffix' => $id]);
  109. $blog = $blog->data();
  110. }
  111. } catch (ValidationException $e) {
  112. $blog = $this->Blogs->newEntity($this->request->data());
  113. $blog->setErrors($e->getValidationErrors());
  114.  
  115. }
  116. $this->set(compact('blog'));
  117. $this->set('_serialize', ['blog']);
  118. }
  119.  
  120. /**
  121. * Delete method
  122. *
  123. * @param string|null $id Blog id.
  124. * @return \Cake\Network\Response|null Redirects to index.
  125. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
  126. */
  127. public function delete($id = null)
  128. {
  129. $this->request->allowMethod(['post', 'delete']);
  130. try {
  131. $blog = Invoker::call('blogs', $this, ['suffix' => $id, 'method' => 'DELETE']);
  132. $result = $blog->data();
  133. if ($result) {
  134. $this->Flash->success(__('The blog has been deleted.'));
  135. } else {
  136. $this->Flash->error(__('The blog could not be deleted. Please, try again.'));
  137. }
  138. } catch (Exception $e) {
  139. $this->Flash->error(__('The blog could not be deleted. Please, check if there is something associated with it.'));
  140. }
  141.  
  142. return $this->redirect(['action' => 'index']);
  143. }
  144. }
Add Comment
Please, Sign In to add comment