1 | <?php |
||
2 | |||
3 | namespace Backend\Modules\Locale\Actions; |
||
4 | |||
5 | use Common\Uri as CommonUri; |
||
6 | use Backend\Core\Engine\Base\ActionAdd as BackendBaseActionAdd; |
||
7 | use Backend\Core\Engine\Authentication as BackendAuthentication; |
||
8 | use Backend\Core\Engine\Form as BackendForm; |
||
9 | use Backend\Core\Language\Language as BL; |
||
10 | use Backend\Core\Engine\Model as BackendModel; |
||
11 | use Backend\Modules\Locale\Engine\Model as BackendLocaleModel; |
||
12 | |||
13 | /** |
||
14 | * This is the add action, it will display a form to add an item to the locale. |
||
15 | */ |
||
16 | class Add extends BackendBaseActionAdd |
||
17 | { |
||
18 | /** |
||
19 | * Filter variables |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | private $filter; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $filterQuery; |
||
29 | |||
30 | public function execute(): void |
||
31 | { |
||
32 | parent::execute(); |
||
33 | $this->setFilter(); |
||
34 | $this->loadForm(); |
||
35 | $this->validateForm(); |
||
36 | $this->parse(); |
||
37 | $this->display(); |
||
38 | } |
||
39 | |||
40 | private function loadForm(): void |
||
41 | { |
||
42 | $originalTranslation = null; |
||
43 | |||
44 | if ($this->getRequest()->query->getInt('id') !== 0) { |
||
45 | // get the translation |
||
46 | $originalTranslation = BackendLocaleModel::get($this->getRequest()->query->getInt('id')); |
||
47 | |||
48 | if (empty($originalTranslation)) { |
||
49 | $this->redirect(BackendModel::createUrlForAction('Index') . '&error=non-existing' . $this->filterQuery); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | // create form |
||
54 | $this->form = new BackendForm('add', BackendModel::createUrlForAction() . $this->filterQuery); |
||
55 | |||
56 | // create and add elements |
||
57 | $this->form->addDropdown( |
||
58 | 'application', |
||
59 | ['Backend' => 'Backend', 'Frontend' => 'Frontend'], |
||
60 | $originalTranslation ? $originalTranslation['application'] : $this->filter['application'] |
||
61 | ); |
||
62 | $this->form->addDropdown( |
||
63 | 'module', |
||
64 | BackendModel::getModulesForDropDown(), |
||
65 | $originalTranslation ? $originalTranslation['module'] : $this->filter['module'] |
||
66 | ); |
||
67 | $this->form->addDropdown( |
||
68 | 'type', |
||
69 | BackendLocaleModel::getTypesForDropDown(), |
||
70 | $originalTranslation ? $originalTranslation['type'] : $this->filter['type'][0] |
||
71 | ); |
||
72 | $this->form->addText( |
||
73 | 'name', |
||
74 | $originalTranslation ? $originalTranslation['name'] : $this->filter['name'] |
||
75 | ); |
||
76 | $this->form->addTextarea( |
||
77 | 'value', |
||
78 | $originalTranslation ? $originalTranslation['value'] : $this->filter['value'], |
||
79 | null, |
||
80 | null, |
||
81 | true |
||
82 | ); |
||
83 | $this->form->addDropdown( |
||
84 | 'language', |
||
85 | BL::getWorkingLanguages(), |
||
86 | $originalTranslation ? $originalTranslation['language'] : $this->filter['language'][0] |
||
87 | ); |
||
88 | } |
||
89 | |||
90 | protected function parse(): void |
||
91 | { |
||
92 | parent::parse(); |
||
93 | |||
94 | // prevent XSS |
||
95 | $filter = \SpoonFilter::arrayMapRecursive('htmlspecialchars', $this->filter); |
||
96 | |||
97 | $this->template->assignArray($filter); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Sets the filter based on the $_GET array. |
||
102 | */ |
||
103 | private function setFilter(): void |
||
104 | { |
||
105 | $this->filter['language'] = $this->getRequest()->query->get('language', []); |
||
106 | if (empty($this->filter['language'])) { |
||
107 | $this->filter['language'] = BL::getWorkingLanguage(); |
||
108 | } |
||
109 | $this->filter['application'] = $this->getRequest()->query->get('application'); |
||
110 | $this->filter['module'] = $this->getRequest()->query->get('module'); |
||
111 | $this->filter['type'] = $this->getRequest()->query->get('type', ''); |
||
112 | if ($this->filter['type'] === '') { |
||
113 | $this->filter['type'] = null; |
||
114 | } |
||
115 | $this->filter['name'] = $this->getRequest()->query->get('name'); |
||
116 | $this->filter['value'] = $this->getRequest()->query->get('value'); |
||
117 | |||
118 | // build query for filter |
||
119 | $this->filterQuery = '&' . http_build_query($this->filter, null, '&', PHP_QUERY_RFC3986); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
120 | } |
||
121 | |||
122 | private function validateForm(): void |
||
123 | { |
||
124 | if ($this->form->isSubmitted()) { |
||
125 | $this->form->cleanupFields(); |
||
126 | |||
127 | // redefine fields |
||
128 | $txtName = $this->form->getField('name'); |
||
129 | $txtValue = $this->form->getField('value'); |
||
130 | |||
131 | // name checks |
||
132 | if ($txtName->isFilled(BL::err('FieldIsRequired'))) { |
||
133 | // allowed regex (a-z and 0-9) |
||
134 | if ($txtName->isValidAgainstRegexp('|^([a-z0-9])+$|i', BL::err('AlphaNumericCharactersOnly'))) { |
||
135 | // first letter does not seem to be a capital one |
||
136 | if (!in_array(mb_substr($txtName->getValue(), 0, 1), range('A', 'Z'))) { |
||
137 | $txtName->setError(BL::err('FirstLetterMustBeACapitalLetter')); |
||
138 | } else { |
||
139 | // this name already exists in this language |
||
140 | if (BackendLocaleModel::existsByName( |
||
141 | $txtName->getValue(), |
||
142 | $this->form->getField('type')->getValue(), |
||
143 | $this->form->getField('module')->getValue(), |
||
144 | $this->form->getField('language')->getValue(), |
||
145 | $this->form->getField('application')->getValue() |
||
146 | ) |
||
147 | ) { |
||
148 | $txtName->setError(BL::err('AlreadyExists')); |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 | |||
154 | // value checks |
||
155 | if ($txtValue->isFilled(BL::err('FieldIsRequired'))) { |
||
156 | // in case this is a 'act' type, there are special rules concerning possible values |
||
157 | if ($this->form->getField('type')->getValue() == 'act') { |
||
158 | if (rawurlencode($txtValue->getValue()) != CommonUri::getUrl($txtValue->getValue())) { |
||
159 | $txtValue->addError(BL::err('InvalidValue')); |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | |||
164 | // module should be 'core' for any other application than backend |
||
165 | if ($this->form->getField('application')->getValue() != 'Backend' && $this->form->getField('module')->getValue() != 'Core') { |
||
166 | $this->form->getField('module')->setError(BL::err('ModuleHasToBeCore')); |
||
167 | } |
||
168 | |||
169 | if ($this->form->isCorrect()) { |
||
170 | // build item |
||
171 | $item = []; |
||
172 | $item['user_id'] = BackendAuthentication::getUser()->getUserId(); |
||
173 | $item['language'] = $this->form->getField('language')->getValue(); |
||
174 | $item['application'] = $this->form->getField('application')->getValue(); |
||
175 | $item['module'] = $this->form->getField('module')->getValue(); |
||
176 | $item['type'] = $this->form->getField('type')->getValue(); |
||
177 | $item['name'] = $this->form->getField('name')->getValue(); |
||
178 | $item['value'] = $this->form->getField('value')->getValue(); |
||
179 | $item['edited_on'] = BackendModel::getUTCDate(); |
||
180 | |||
181 | // update item |
||
182 | $item['id'] = BackendLocaleModel::insert($item); |
||
183 | |||
184 | // everything is saved, so redirect to the overview |
||
185 | $this->redirect(BackendModel::createUrlForAction('Index', null, null, null) . '&report=added&var=' . rawurlencode($item['name']) . '&highlight=row-' . $item['id'] . $this->filterQuery); |
||
186 | } |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 |