|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
* @author Mathias Weitz <[email protected]> |
|
9
|
|
|
* @author Carsten Bleek <[email protected]> |
|
10
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
11
|
|
|
* @author Miroslav Fedeleš <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/** Settings controller */ |
|
15
|
|
|
namespace Settings\Controller; |
|
16
|
|
|
|
|
17
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
|
18
|
|
|
use Zend\View\Model\JsonModel; |
|
19
|
|
|
use Zend\EventManager\Event; |
|
20
|
|
|
use Zend\Http\PhpEnvironment\Response; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Main Action Controller for Settings module |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
class IndexController extends AbstractActionController |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* attaches further Listeners for generating / processing the output |
|
30
|
|
|
* |
|
31
|
|
|
* @return $this |
|
32
|
|
|
*/ |
|
33
|
|
View Code Duplication |
public function attachDefaultListeners() |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
parent::attachDefaultListeners(); |
|
36
|
|
|
$serviceLocator = $this->serviceLocator; |
|
37
|
|
|
$defaultServices = $serviceLocator->get('DefaultListeners'); |
|
38
|
|
|
$events = $this->getEventManager(); |
|
39
|
|
|
$events->attach($defaultServices); |
|
|
|
|
|
|
40
|
|
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function indexAction() |
|
44
|
|
|
{ |
|
45
|
|
|
$services = $this->serviceLocator; |
|
46
|
|
|
$translator = $services->get('translator'); |
|
47
|
|
|
$moduleName = $this->params('module', 'Core'); |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
|
|
$settings = $this->settings($moduleName); |
|
|
|
|
|
|
52
|
|
|
} catch (\InvalidArgumentException $e) { |
|
53
|
|
|
$this->getResponse()->setStatusCode(Response::STATUS_CODE_404); |
|
|
|
|
|
|
54
|
|
|
return [ |
|
55
|
|
|
'message' => sprintf($translator->translate('Settings "%s" does not exists'), $moduleName), |
|
56
|
|
|
'exception' => $e |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$jsonFormat = 'json' == $this->params()->fromQuery('format'); |
|
61
|
|
|
if (!$this->getRequest()->isPost() && $jsonFormat) { |
|
|
|
|
|
|
62
|
|
|
return $settings->toArray(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$mvcEvent = $this->getEvent(); |
|
66
|
|
|
$mvcEvent->setParam('__settings_active_module', $moduleName); |
|
67
|
|
|
|
|
68
|
|
|
$formManager = $this->serviceLocator->get('FormElementManager'); |
|
69
|
|
|
$formName = $moduleName . '/SettingsForm'; |
|
70
|
|
|
if (!$formManager->has($formName)) { |
|
71
|
|
|
$formName = "Settings/Form"; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// Fetching an distinct Settings |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
// Write-Access is per default only granted to the own module - change that |
|
78
|
|
|
$settings->enableWriteAccess(); |
|
79
|
|
|
|
|
80
|
|
|
$form = $formManager->get($formName); |
|
81
|
|
|
|
|
82
|
|
|
// Binding the Entity to the Formular |
|
83
|
|
|
$form->bind($settings); |
|
84
|
|
|
$data = $this->getRequest()->getPost(); |
|
|
|
|
|
|
85
|
|
|
if (0 < count($data)) { |
|
86
|
|
|
$form->setData($data); |
|
87
|
|
|
|
|
88
|
|
|
$valid = $form->isValid(); |
|
89
|
|
|
$partial = $services->get('viewhelpermanager')->get('partial'); |
|
|
|
|
|
|
90
|
|
|
$text = $valid |
|
91
|
|
|
? /*@translate*/'Changes successfully saved' |
|
92
|
|
|
: /*@translate*/'Changes could not be saved'; |
|
93
|
|
|
|
|
94
|
|
|
$vars = array(); |
|
95
|
|
|
$this->notification()->success($translator->translate($text)); |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
if ($valid) { |
|
98
|
|
|
$event = new Event( |
|
99
|
|
|
'SETTINGS_CHANGED', |
|
100
|
|
|
$this, |
|
101
|
|
|
array('settings' => $settings) |
|
102
|
|
|
); |
|
103
|
|
|
$this->getEventManager()->trigger($event); |
|
104
|
|
|
} else { |
|
105
|
|
|
$vars['error'] = $form->getMessages(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return new JsonModel($vars); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$vars['form']=$form; |
|
|
|
|
|
|
112
|
|
|
return $vars; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
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.