|
1
|
|
|
<?php |
|
2
|
|
|
namespace App\Controller; |
|
3
|
|
|
|
|
4
|
|
|
use App\Controller\AppController; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Documents Controller |
|
8
|
|
|
* |
|
9
|
|
|
* @property \App\Model\Table\DocumentsTable $Documents |
|
10
|
|
|
* |
|
11
|
|
|
* @method \App\Model\Entity\Document[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = []) |
|
12
|
|
|
*/ |
|
13
|
|
View Code Duplication |
class DocumentsController extends AppController |
|
|
|
|
|
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Index method |
|
18
|
|
|
* |
|
19
|
|
|
* @return \Cake\Http\Response|void |
|
20
|
|
|
*/ |
|
21
|
|
|
public function index() |
|
22
|
|
|
{ |
|
23
|
|
|
$documents = $this->paginate($this->Documents); |
|
24
|
|
|
|
|
25
|
|
|
$this->set(compact('documents')); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* View method |
|
30
|
|
|
* |
|
31
|
|
|
* @param string|null $id Document id. |
|
32
|
|
|
* @return \Cake\Http\Response|void |
|
33
|
|
|
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function view($id = null) |
|
36
|
|
|
{ |
|
37
|
|
|
$document = $this->Documents->get($id, [ |
|
38
|
|
|
'contain' => [] |
|
39
|
|
|
]); |
|
40
|
|
|
|
|
41
|
|
|
$this->set('document', $document); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Add method |
|
46
|
|
|
* |
|
47
|
|
|
* @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise. |
|
48
|
|
|
*/ |
|
49
|
|
|
public function add() |
|
50
|
|
|
{ |
|
51
|
|
|
$document = $this->Documents->newEntity(); |
|
52
|
|
|
if ($this->request->is('post')) { |
|
53
|
|
|
$document = $this->Documents->patchEntity($document, $this->request->getData()); |
|
54
|
|
|
if ($this->Documents->save($document)) { |
|
55
|
|
|
$this->Flash->success(__('The document has been saved.')); |
|
56
|
|
|
|
|
57
|
|
|
return $this->redirect(['action' => 'index']); |
|
58
|
|
|
} |
|
59
|
|
|
$this->Flash->error(__('The document could not be saved. Please, try again.')); |
|
60
|
|
|
} |
|
61
|
|
|
$this->set(compact('document')); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Edit method |
|
66
|
|
|
* |
|
67
|
|
|
* @param string|null $id Document id. |
|
68
|
|
|
* @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise. |
|
69
|
|
|
* @throws \Cake\Network\Exception\NotFoundException When record not found. |
|
70
|
|
|
*/ |
|
71
|
|
|
public function edit($id = null) |
|
72
|
|
|
{ |
|
73
|
|
|
$document = $this->Documents->get($id, [ |
|
74
|
|
|
'contain' => [] |
|
75
|
|
|
]); |
|
76
|
|
|
if ($this->request->is(['patch', 'post', 'put'])) { |
|
77
|
|
|
$document = $this->Documents->patchEntity($document, $this->request->getData()); |
|
78
|
|
|
if ($this->Documents->save($document)) { |
|
79
|
|
|
$this->Flash->success(__('The document has been saved.')); |
|
80
|
|
|
|
|
81
|
|
|
return $this->redirect(['action' => 'index']); |
|
82
|
|
|
} |
|
83
|
|
|
$this->Flash->error(__('The document could not be saved. Please, try again.')); |
|
84
|
|
|
} |
|
85
|
|
|
$this->set(compact('document')); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Delete method |
|
90
|
|
|
* |
|
91
|
|
|
* @param string|null $id Document id. |
|
92
|
|
|
* @return \Cake\Http\Response|null Redirects to index. |
|
93
|
|
|
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. |
|
94
|
|
|
*/ |
|
95
|
|
|
public function delete($id = null) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->request->allowMethod(['post', 'delete']); |
|
98
|
|
|
$document = $this->Documents->get($id); |
|
99
|
|
|
if ($this->Documents->delete($document)) { |
|
100
|
|
|
$this->Flash->success(__('The document has been deleted.')); |
|
101
|
|
|
} else { |
|
102
|
|
|
$this->Flash->error(__('The document could not be deleted. Please, try again.')); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $this->redirect(['action' => 'index']); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.