1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @package sitemaker |
5
|
|
|
* @copyright (c) 2016 Daniel A. (blitze) |
6
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace blitze\content\services\form; |
11
|
|
|
|
12
|
|
|
class form |
13
|
|
|
{ |
14
|
|
|
/** @var \phpbb\request\request_interface */ |
15
|
|
|
protected $request; |
16
|
|
|
|
17
|
|
|
/** @var \phpbb\template\context */ |
18
|
|
|
protected $template_context; |
19
|
|
|
|
20
|
|
|
/** @var \phpbb\language\language */ |
21
|
|
|
protected $language; |
22
|
|
|
|
23
|
|
|
/** @var \blitze\sitemaker\services\auto_lang */ |
|
|
|
|
24
|
|
|
protected $auto_lang; |
25
|
|
|
|
26
|
|
|
/** @var \blitze\content\services\form\fields_factory */ |
27
|
|
|
protected $fields_factory; |
28
|
|
|
|
29
|
|
|
/** @var \blitze\sitemaker\services\template */ |
|
|
|
|
30
|
|
|
protected $ptemplate; |
31
|
|
|
|
32
|
|
|
/** @var array */ |
33
|
|
|
protected $db_fields = array(); |
34
|
|
|
|
35
|
|
|
/** @var array */ |
36
|
|
|
protected $data = array(); |
37
|
|
|
|
38
|
|
|
/** @var array */ |
39
|
|
|
protected $form = array(); |
40
|
|
|
|
41
|
|
|
/** @var array */ |
42
|
|
|
protected $errors = array(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor |
46
|
|
|
* |
47
|
|
|
* @param \phpbb\request\request_interface $request Request object |
48
|
|
|
* @param \phpbb\template\context $template_context Template context object |
49
|
|
|
* @param \phpbb\language\language $language Language object |
50
|
|
|
* @param \blitze\sitemaker\services\auto_lang $auto_lang Auto add lang file |
51
|
|
|
* @param \blitze\content\services\form\fields_factory $fields_factory Form fields factory |
52
|
|
|
* @param \blitze\sitemaker\services\template $ptemplate Sitemaker template object |
53
|
|
|
*/ |
54
|
|
|
public function __construct(\phpbb\request\request_interface $request, \phpbb\template\context $template_context, \phpbb\language\language $language, \blitze\sitemaker\services\auto_lang $auto_lang, \blitze\content\services\form\fields_factory $fields_factory, \blitze\sitemaker\services\template $ptemplate) |
55
|
|
|
{ |
56
|
|
|
$this->request = $request; |
57
|
|
|
$this->template_context = $template_context; |
58
|
|
|
$this->language = $language; |
59
|
|
|
$this->auto_lang = $auto_lang; |
60
|
|
|
$this->fields_factory = $fields_factory; |
61
|
|
|
$this->ptemplate = $ptemplate; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $form_name |
66
|
|
|
* @param string $form_key |
67
|
|
|
* @param string $action |
68
|
|
|
* @param string $legend |
69
|
|
|
* @param string $method |
70
|
|
|
* @return \blitze\content\services\form\form |
71
|
|
|
*/ |
72
|
|
|
public function create($form_name, $form_key, $action = '', $legend = '', $method = 'post') |
73
|
|
|
{ |
74
|
|
|
$this->auto_lang->add('form_fields'); |
75
|
|
|
$this->language->add_lang('posting'); |
76
|
|
|
|
77
|
|
|
add_form_key($form_key); |
78
|
|
|
|
79
|
|
|
$this->form = array( |
80
|
|
|
'form_name' => $form_name, |
81
|
|
|
'form_action' => $action, |
82
|
|
|
'form_legend' => $legend, |
83
|
|
|
'form_method' => $method, |
84
|
|
|
'form_key' => $this->template_context->get_root_ref()['S_FORM_TOKEN'], |
85
|
|
|
'is_submitted' => $this->request->is_set_post('form_token'), |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function is_created() |
95
|
|
|
{ |
96
|
|
|
return (bool) count($this->form); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $name |
101
|
|
|
* @param string $type |
102
|
|
|
* @param array $field_data |
103
|
|
|
* @param int $forum_id |
104
|
|
|
* @param int $topic_id |
105
|
|
|
* @return \blitze\content\services\form\form |
106
|
|
|
*/ |
107
|
|
|
public function add($name, $type, array $field_data, $forum_id = 0, $topic_id = 0) |
108
|
|
|
{ |
109
|
|
|
$field_data += array('field_id' => 'field-' . $name); |
110
|
|
|
$field_data += $this->get_default_field_data(); |
111
|
|
|
|
112
|
|
|
if (($field = $this->fields_factory->get($type)) !== null) |
113
|
|
|
{ |
114
|
|
|
$field_data['field_name'] = $name; |
115
|
|
|
$field_data['field_type'] = $type; |
116
|
|
|
$field_data['field_props'] += $field->get_default_props(); |
117
|
|
|
$field_data['field_label'] = $this->language->lang($field_data['field_label']); |
118
|
|
|
$field_data['field_value'] = $field->get_submitted_value($field_data, $this->form['is_submitted']); |
119
|
|
|
$field_data['field_view'] = $field->show_form_field($field_data); |
120
|
|
|
|
121
|
|
|
if ($field_data['field_view']) |
122
|
|
|
{ |
123
|
|
|
$this->data[$name] = $field_data; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param bool $wrap_form_element |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function get_form($wrap_form_element = true) |
135
|
|
|
{ |
136
|
|
|
foreach ($this->data as $row) |
137
|
|
|
{ |
138
|
|
|
$key = $this->get_field_key($row['field_type']); |
139
|
|
|
$this->ptemplate->assign_block_vars($key, array_change_key_case($row, CASE_UPPER)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->ptemplate->assign_vars(array_merge( |
143
|
|
|
array( |
144
|
|
|
'S_EDITOR' => true, |
145
|
|
|
'S_WRAP_FORM' => $wrap_form_element |
146
|
|
|
), |
147
|
|
|
array_change_key_case($this->form, CASE_UPPER)) |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
return $this->ptemplate->render_view('blitze/content', 'form.html', 'form'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
public function handle_request() |
157
|
|
|
{ |
158
|
|
|
$field_data = array(); |
159
|
|
|
if ($this->request->server('REQUEST_METHOD') === 'POST') |
160
|
|
|
{ |
161
|
|
|
if (!check_form_key($this->form['form_key'])) |
162
|
|
|
{ |
163
|
|
|
$this->errors[] = 'FORM_INVALID'; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$req_mod_input = false; |
167
|
|
|
$field_data = $this->get_submitted_data($this->data, $req_mod_input); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $field_data; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
|
|
public function get_data() |
177
|
|
|
{ |
178
|
|
|
return $this->data; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return array |
183
|
|
|
*/ |
184
|
|
|
public function get_errors() |
185
|
|
|
{ |
186
|
|
|
// Replace "error" strings with their real, localised form |
187
|
|
|
return array_map(array($this->language, 'lang'), array_filter($this->errors)); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param array $content_fields |
192
|
|
|
* @param bool $req_mod_input |
193
|
|
|
* @param string $cp_class ucp|mcp |
194
|
|
|
* @return array |
195
|
|
|
*/ |
196
|
|
|
public function get_submitted_data(array $content_fields, &$req_mod_input, $cp_class = 'ucp') |
197
|
|
|
{ |
198
|
|
|
$previewing = $this->request->is_set('preview'); |
199
|
|
|
|
200
|
|
|
$fields_data = array(); |
201
|
|
|
foreach ($content_fields as $field => $row) |
202
|
|
|
{ |
203
|
|
|
$row += $this->get_default_field_data(); |
204
|
|
|
$value = $this->get_submitted_field_data($row, $req_mod_input, $cp_class); |
205
|
|
|
|
206
|
|
|
if ($previewing || empty($row['field_props']['is_db_field'])) |
207
|
|
|
{ |
208
|
|
|
$fields_data[$field] = $value; |
209
|
|
|
} |
210
|
|
|
else |
211
|
|
|
{ |
212
|
|
|
$this->db_fields[$field] = $value; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return array_filter($fields_data); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param array $topic_data |
221
|
|
|
* @param array $content_fields |
222
|
|
|
* @return void |
223
|
|
|
*/ |
224
|
|
|
public function save_db_fields(array $topic_data, array $content_fields) |
225
|
|
|
{ |
226
|
|
|
foreach ($this->db_fields as $field_type => $value) |
227
|
|
|
{ |
228
|
|
|
$field = $this->fields_factory->get($field_type); |
229
|
|
|
|
230
|
|
|
$field_data = $content_fields[$field_type]; |
231
|
|
|
$field_data['field_value'] = $value; |
232
|
|
|
$field_data['field_props'] += $field->get_default_props(); |
233
|
|
|
|
234
|
|
|
$field->save_field($field_data, $topic_data); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param array $row |
240
|
|
|
* @param bool $req_mod_input |
241
|
|
|
* @param string $cp_class |
242
|
|
|
* @return mixed |
243
|
|
|
*/ |
244
|
|
|
protected function get_submitted_field_data(array &$row, &$req_mod_input, $cp_class) |
245
|
|
|
{ |
246
|
|
|
if ($field = $this->fields_factory->get($row['field_type'])) |
247
|
|
|
{ |
248
|
|
|
$row['field_props'] += $field->get_default_props(); |
249
|
|
|
$row['field_value'] = $field->get_submitted_value($row, $this->form['is_submitted']); |
250
|
|
|
|
251
|
|
|
$this->validate_field($field, $row, $req_mod_input, $cp_class); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return $row['field_value']; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param \blitze\content\services\form\field\field_interface $field |
259
|
|
|
* @param array $row |
260
|
|
|
* @param bool $req_mod_input |
261
|
|
|
* @param string $cp_class |
262
|
|
|
* @return void |
263
|
|
|
*/ |
264
|
|
|
protected function validate_field(\blitze\content\services\form\field\field_interface $field, $row, &$req_mod_input, $cp_class) |
265
|
|
|
{ |
266
|
|
|
if (!empty($row['field_value'])) |
267
|
|
|
{ |
268
|
|
|
$this->errors[] = $field->validate_field($row); |
269
|
|
|
} |
270
|
|
|
else if ($row['field_required']) |
271
|
|
|
{ |
272
|
|
|
if (!$row['field_mod_only'] || $cp_class === 'mcp') |
273
|
|
|
{ |
274
|
|
|
$this->errors[] = $this->language->lang_array('CONTENT_FIELD_REQUIRED', array($row['field_label'])); |
275
|
|
|
} |
276
|
|
|
else |
277
|
|
|
{ |
278
|
|
|
$req_mod_input = true; |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @return array |
285
|
|
|
*/ |
286
|
|
|
protected function get_default_field_data() |
287
|
|
|
{ |
288
|
|
|
return array( |
289
|
|
|
'field_label' => '', |
290
|
|
|
'field_explain' => '', |
291
|
|
|
'field_value' => '', |
292
|
|
|
'field_required' => false, |
293
|
|
|
'field_props' => array(), |
294
|
|
|
); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @param string $field_type |
299
|
|
|
* @return string |
300
|
|
|
*/ |
301
|
|
|
protected function get_field_key($field_type) |
302
|
|
|
{ |
303
|
|
|
return ($field_type === 'hidden') ? 'hidden' : 'element'; |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths