|
| 1 | +<?php |
| 2 | +namespace App\Controller; |
| 3 | + |
| 4 | +use App\Controller\AppController; |
| 5 | + |
| 6 | +/** |
| 7 | + * Tags Controller |
| 8 | + * |
| 9 | + * @property \App\Model\Table\TagsTable $Tags |
| 10 | + * |
| 11 | + * @method \App\Model\Entity\Tag[] paginate($object = null, array $settings = []) |
| 12 | + */ |
| 13 | +class TagsController extends AppController |
| 14 | +{ |
| 15 | + |
| 16 | + /** |
| 17 | + * Index method |
| 18 | + * |
| 19 | + * @return \Cake\Http\Response|void |
| 20 | + */ |
| 21 | + public function index() |
| 22 | + { |
| 23 | + $tags = $this->paginate($this->Tags); |
| 24 | + |
| 25 | + $this->set(compact('tags')); |
| 26 | + $this->set('_serialize', ['tags']); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * View method |
| 31 | + * |
| 32 | + * @param string|null $id Tag id. |
| 33 | + * @return \Cake\Http\Response|void |
| 34 | + * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. |
| 35 | + */ |
| 36 | + public function view($id = null) |
| 37 | + { |
| 38 | + $tag = $this->Tags->get($id, [ |
| 39 | + 'contain' => ['Articles'] |
| 40 | + ]); |
| 41 | + |
| 42 | + $this->set('tag', $tag); |
| 43 | + $this->set('_serialize', ['tag']); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Add method |
| 48 | + * |
| 49 | + * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise. |
| 50 | + */ |
| 51 | + public function add() |
| 52 | + { |
| 53 | + $tag = $this->Tags->newEntity(); |
| 54 | + if ($this->request->is('post')) { |
| 55 | + $tag = $this->Tags->patchEntity($tag, $this->request->getData()); |
| 56 | + if ($this->Tags->save($tag)) { |
| 57 | + $this->Flash->success(__('The tag has been saved.')); |
| 58 | + |
| 59 | + return $this->redirect(['action' => 'index']); |
| 60 | + } |
| 61 | + $this->Flash->error(__('The tag could not be saved. Please, try again.')); |
| 62 | + } |
| 63 | + $articles = $this->Tags->Articles->find('list', ['limit' => 200]); |
| 64 | + $this->set(compact('tag', 'articles')); |
| 65 | + $this->set('_serialize', ['tag']); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Edit method |
| 70 | + * |
| 71 | + * @param string|null $id Tag id. |
| 72 | + * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise. |
| 73 | + * @throws \Cake\Network\Exception\NotFoundException When record not found. |
| 74 | + */ |
| 75 | + public function edit($id = null) |
| 76 | + { |
| 77 | + $tag = $this->Tags->get($id, [ |
| 78 | + 'contain' => ['Articles'] |
| 79 | + ]); |
| 80 | + if ($this->request->is(['patch', 'post', 'put'])) { |
| 81 | + $tag = $this->Tags->patchEntity($tag, $this->request->getData()); |
| 82 | + if ($this->Tags->save($tag)) { |
| 83 | + $this->Flash->success(__('The tag has been saved.')); |
| 84 | + |
| 85 | + return $this->redirect(['action' => 'index']); |
| 86 | + } |
| 87 | + $this->Flash->error(__('The tag could not be saved. Please, try again.')); |
| 88 | + } |
| 89 | + $articles = $this->Tags->Articles->find('list', ['limit' => 200]); |
| 90 | + $this->set(compact('tag', 'articles')); |
| 91 | + $this->set('_serialize', ['tag']); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Delete method |
| 96 | + * |
| 97 | + * @param string|null $id Tag id. |
| 98 | + * @return \Cake\Http\Response|null Redirects to index. |
| 99 | + * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. |
| 100 | + */ |
| 101 | + public function delete($id = null) |
| 102 | + { |
| 103 | + $this->request->allowMethod(['post', 'delete']); |
| 104 | + $tag = $this->Tags->get($id); |
| 105 | + if ($this->Tags->delete($tag)) { |
| 106 | + $this->Flash->success(__('The tag has been deleted.')); |
| 107 | + } else { |
| 108 | + $this->Flash->error(__('The tag could not be deleted. Please, try again.')); |
| 109 | + } |
| 110 | + |
| 111 | + return $this->redirect(['action' => 'index']); |
| 112 | + } |
| 113 | +} |
0 commit comments