1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BB's Zend Framework 2 Components |
4
|
|
|
* |
5
|
|
|
* AdminModule |
6
|
|
|
* |
7
|
|
|
* @package [MyApplication] |
8
|
|
|
* @package BB's Zend Framework 2 Components |
9
|
|
|
* @package AdminModule |
10
|
|
|
* @author Björn Bartels <[email protected]> |
11
|
|
|
* @link https://gitlab.bjoernbartels.earth/groups/zf2 |
12
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 |
13
|
|
|
* @copyright copyright (c) 2016 Björn Bartels <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Admin\Controller; |
17
|
|
|
|
18
|
|
|
use Application\Controller\BaseActionController; |
19
|
|
|
use Zend\View\Model\ViewModel; |
20
|
|
|
use Admin\Model\Settings; |
21
|
|
|
use Admin\Form\SettingsForm; |
22
|
|
|
|
23
|
|
View Code Duplication |
class SettingsController extends BaseActionController |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array|\Admin\Model\SettingsTable |
28
|
|
|
*/ |
29
|
|
|
protected $settingsTable; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* initialize titles and toolbar items |
33
|
|
|
* |
34
|
|
|
* {@inheritDoc} |
35
|
|
|
* @see \Zend\Mvc\Controller\AbstractActionController::onDispatch() |
36
|
|
|
*/ |
37
|
|
|
public function onDispatch(\Zend\Mvc\MvcEvent $e) |
38
|
|
|
{ |
39
|
|
|
$this->setToolbarItems( |
40
|
|
|
array( |
41
|
|
|
"index" => array( |
42
|
|
|
array( |
43
|
|
|
'label' => 'add setting', |
44
|
|
|
'icon' => 'plus', |
45
|
|
|
'class' => 'button btn btn-default small btn-sm btn-cta-xhr cta-xhr-modal', |
46
|
|
|
'route' => 'admin/settingsedit', |
47
|
|
|
'action' => 'add', |
48
|
|
|
'resource' => 'mvc:user', |
49
|
|
|
), |
50
|
|
|
), |
51
|
|
|
) |
52
|
|
|
); |
53
|
|
|
$this->setActionTitles( |
54
|
|
|
array( |
55
|
|
|
'index' => $this->translate("manage settings"), |
56
|
|
|
'add' => $this->translate("add setting"), |
57
|
|
|
'edit' => $this->translate("edit setting"), |
58
|
|
|
'delete' => $this->translate("delete setting"), |
59
|
|
|
) |
60
|
|
|
); |
61
|
|
|
return parent::onDispatch($e); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* list settings in a table |
66
|
|
|
* @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel |
67
|
|
|
*/ |
68
|
1 |
|
public function indexAction() |
69
|
|
|
{ |
70
|
1 |
|
$tmplVars = $this->getTemplateVars(); |
|
|
|
|
71
|
1 |
|
$aSettingslist = $this->getSettingsTable()->fetchAll(); |
72
|
1 |
|
if ( $this->isXHR() ) { |
73
|
|
|
$datatablesData = array('data' => $aSettingslist->toArray()); |
74
|
|
|
$oController = $this; |
75
|
|
|
$datatablesData['data'] = array_map( |
76
|
|
|
function ($row) use ($oController) { |
77
|
|
|
$actions = '<div class="button-group tiny btn-group btn-group-xs">'. |
78
|
|
|
'<a class="button btn btn-default tiny btn-xs btn-clean btn-cta-xhr cta-xhr-modal" href="'.$oController->url()->fromRoute( |
79
|
|
|
'admin/settingsedit', |
80
|
|
|
array('action'=>'edit', 'set_id' => $row["settings_id"]) |
81
|
|
|
).'"><span class="fa fa-pencil"></span> '.$oController->translate("edit").'</a>'. |
82
|
|
|
'<a class="button btn btn-default tiny btn-xs btn-clean btn-cta-xhr cta-xhr-modal" href="'.$oController->url()->fromRoute( |
83
|
|
|
'admin/settingsedit', |
84
|
|
|
array('action'=>'delete', 'set_id' => $row["settings_id"]) |
85
|
|
|
).'"><span class="fa fa-trash-o"></span> '.$oController->translate("delete").'</a>'. |
86
|
|
|
'</div>'; |
87
|
|
|
$row["_actions_"] = $actions; |
88
|
|
|
return $row; |
89
|
|
|
}, $datatablesData['data'] |
90
|
|
|
); |
91
|
|
|
return $this->getResponse()->setContent(json_encode($datatablesData)); |
92
|
|
|
} |
93
|
1 |
|
return new ViewModel( |
94
|
|
|
array( |
95
|
1 |
|
'settingsdata' => $aSettingslist, |
96
|
|
|
) |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* add setting entry |
102
|
|
|
* @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel |
103
|
|
|
*/ |
104
|
1 |
|
public function addAction() |
105
|
|
|
{ |
106
|
1 |
|
$tmplVars = $this->getTemplateVars( |
107
|
|
|
array( |
108
|
1 |
|
'showForm' => true, |
109
|
|
|
) |
110
|
|
|
); |
111
|
|
|
|
112
|
1 |
|
$form = new SettingsForm(); |
113
|
|
|
|
114
|
1 |
|
$request = $this->getRequest(); |
115
|
1 |
|
$settings = new Settings(); |
116
|
1 |
|
if ($request->isPost()) { |
117
|
|
|
$form->setInputFilter($settings->getInputFilter()); |
118
|
|
|
$form->setData($request->getPost()); |
119
|
|
|
|
120
|
|
|
if ($form->isValid()) { |
121
|
|
|
$settings->exchangeArray($form->getData()); |
122
|
|
|
$this->getSettingsTable()->saveSettings($settings); |
123
|
|
|
$this->flashMessenger()->addSuccessMessage($this->translate('setting has been saved')); |
124
|
|
|
if ( $this->isXHR() ) { |
125
|
|
|
$tmplVars["showForm"] = false; |
126
|
|
|
} else { |
127
|
|
|
return $this->redirect()->toRoute('admin/settingsedit', array('action' => 'index')); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
$tmplVars["settings"] = $settings; |
131
|
|
|
} |
132
|
1 |
|
$tmplVars["form"] = $form; |
133
|
1 |
|
return new ViewModel($tmplVars); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* edit setting entry |
138
|
|
|
* @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel |
139
|
|
|
*/ |
140
|
1 |
|
public function editAction() |
141
|
|
|
{ |
142
|
1 |
|
$tmplVars = $this->getTemplateVars( |
143
|
|
|
array( |
144
|
1 |
|
'showForm' => true, |
145
|
|
|
) |
146
|
|
|
); |
147
|
|
|
|
148
|
1 |
|
$id = (int) $this->params()->fromRoute('set_id', 0); |
149
|
1 |
|
if (!$id) { |
150
|
|
|
$this->flashMessenger()->addWarningMessage($this->translate("missing parameters")); |
151
|
|
|
return $this->redirect()->toRoute( |
152
|
|
|
'admin/settingsedit', array( |
153
|
|
|
'action' => 'index' |
154
|
|
|
) |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
try { |
158
|
1 |
|
$settings = $this->getSettingsTable()->getSettings($id); |
159
|
|
|
} catch (\Exception $e) { |
160
|
|
|
$this->flashMessenger()->addWarningMessage($this->translate("invalid parameters")); |
161
|
|
|
return $this->redirect()->toRoute('admin/settingsedit'); |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
$form = new SettingsForm(); |
165
|
1 |
|
$form->bind($settings); |
166
|
|
|
|
167
|
1 |
|
$request = $this->getRequest(); |
168
|
1 |
|
if ($request->isPost()) { |
169
|
|
|
$form->setInputFilter($settings->getInputFilter()); |
170
|
|
|
$form->setData($request->getPost()); |
171
|
|
|
|
172
|
|
|
if ($form->isValid()) { |
173
|
|
|
$this->getSettingsTable()->saveSettings($settings); |
174
|
|
|
$this->flashMessenger()->addSuccessMessage($this->translate("setting has been saved")); |
175
|
|
|
if ( $this->isXHR() ) { |
176
|
|
|
$tmplVars["showForm"] = false; |
177
|
|
|
} else { |
178
|
|
|
return $this->redirect()->toRoute('admin/settingsedit', array('action' => 'index')); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} else { |
182
|
1 |
|
$form->bind($settings); |
183
|
|
|
} |
184
|
1 |
|
$tmplVars["settings_id"] = $id; |
185
|
1 |
|
$tmplVars["form"] = $form; |
186
|
1 |
|
return new ViewModel($tmplVars); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* delete setting entry |
191
|
|
|
* @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel |
192
|
|
|
*/ |
193
|
1 |
|
public function deleteAction() |
194
|
|
|
{ |
195
|
1 |
|
$tmplVars = $this->getTemplateVars( |
196
|
|
|
array( |
197
|
1 |
|
'showForm' => true, |
198
|
|
|
) |
199
|
|
|
); |
200
|
|
|
|
201
|
1 |
|
$id = (int) $this->params()->fromRoute('set_id', 0); |
202
|
1 |
|
if (!$id) { |
203
|
|
|
$this->flashMessenger()->addWarningMessage($this->translate("missing parameters")); |
204
|
|
|
return $this->redirect()->toRoute('admin/settingsedit', array('action' => 'index')); |
205
|
|
|
} |
206
|
|
|
|
207
|
1 |
|
$tmplVars["settings_id"] = $id; |
208
|
|
|
try { |
209
|
1 |
|
$settings = $this->getSettingsTable()->getSettings($id); |
210
|
|
|
} catch (\Exception $e) { |
211
|
|
|
$this->flashMessenger()->addWarningMessage($this->translate("invalid parameters")); |
212
|
|
|
return $this->redirect()->toRoute('admin/settingsedit'); |
213
|
|
|
} |
214
|
1 |
|
$tmplVars["settings"] = $settings; |
215
|
|
|
|
216
|
1 |
|
$request = $this->getRequest(); |
217
|
1 |
|
if ($request->isPost()) { |
218
|
|
|
$del = $request->getPost('del', ''); |
219
|
|
|
|
220
|
|
|
if (!empty($del)) { |
221
|
|
|
$id = (int) $request->getPost('id'); |
222
|
|
|
$this->getSettingsTable()->deleteSettings($id); |
223
|
|
|
$this->flashMessenger()->addSuccessMessage($this->translate("setting has been deleted")); |
224
|
|
|
if ( $this->isXHR() ) { |
225
|
|
|
$tmplVars["showForm"] = false; |
226
|
|
|
} else { |
227
|
|
|
return $this->redirect()->toRoute('admin/settingsedit', array('action' => 'index')); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
1 |
|
return new ViewModel($tmplVars); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* retrieve setting entry table |
237
|
|
|
* @return array|\Admin\Model\SettingsTable |
238
|
|
|
*/ |
239
|
|
|
public function getSettingsTable() |
240
|
|
|
{ |
241
|
|
|
if (!$this->settingsTable) { |
242
|
|
|
$sm = $this->getServiceLocator(); |
243
|
|
|
$this->settingsTable = $sm->get('AdminSettingsTable'); |
244
|
|
|
} |
245
|
|
|
return $this->settingsTable; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
} |
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.