|
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
|
|
|
); |
|
86
|
|
|
unset($rootref); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $name |
|
93
|
|
|
* @param string $type |
|
94
|
|
|
* @param array $field_data |
|
95
|
|
|
* @param int $forum_id |
|
96
|
|
|
* @param int $topic_id |
|
97
|
|
|
* @return \blitze\content\services\form\form |
|
98
|
|
|
*/ |
|
99
|
|
|
public function add($name, $type, array $field_data, $forum_id = 0, $topic_id = 0) |
|
100
|
|
|
{ |
|
101
|
|
|
$field_data += array('field_id' => 'field-' . $name); |
|
102
|
|
|
$field_data += $this->get_default_field_data(); |
|
103
|
|
|
|
|
104
|
|
|
if ($this->fields_factory->exists($type)) |
|
105
|
|
|
{ |
|
106
|
|
|
$obj = $this->fields_factory->get($type); |
|
107
|
|
|
|
|
108
|
|
|
$field_data['field_type'] = $type; |
|
109
|
|
|
$field_data['field_props'] += $obj->get_default_props(); |
|
110
|
|
|
$field_data['field_label'] = $this->language->lang($field_data['field_label']); |
|
111
|
|
|
$field_data['field_view'] = $obj->show_form_field($name, $field_data, $forum_id, $topic_id); |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
$this->data[$name] = $field_data; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param bool $wrap_form_element |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
|
|
public function get_form($wrap_form_element = true) |
|
124
|
|
|
{ |
|
125
|
|
|
foreach ($this->data as $row) |
|
126
|
|
|
{ |
|
127
|
|
|
$key = $this->get_field_key($row['field_type']); |
|
128
|
|
|
$this->ptemplate->assign_block_vars($key, array_change_key_case($row, CASE_UPPER)); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$this->ptemplate->assign_vars(array_merge( |
|
132
|
|
|
array( |
|
133
|
|
|
'S_EDITOR' => true, |
|
134
|
|
|
'S_WRAP_FORM' => $wrap_form_element |
|
135
|
|
|
), |
|
136
|
|
|
array_change_key_case($this->form, CASE_UPPER)) |
|
137
|
|
|
); |
|
138
|
|
|
|
|
139
|
|
|
return $this->ptemplate->render_view('blitze/content', 'form.html', 'form'); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return array |
|
144
|
|
|
*/ |
|
145
|
|
|
public function handle_request() |
|
146
|
|
|
{ |
|
147
|
|
|
$field_data = array(); |
|
148
|
|
|
if ($this->request->server('REQUEST_METHOD') === 'POST') |
|
149
|
|
|
{ |
|
150
|
|
|
if (!check_form_key($this->form['form_key'])) |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
|
|
$this->errors[] = 'FORM_INVALID'; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$req_mod_input = false; |
|
156
|
|
|
$field_data = $this->get_submitted_data($this->data, $req_mod_input); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $field_data; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @return array |
|
164
|
|
|
*/ |
|
165
|
|
|
public function get_data() |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->data; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return array |
|
172
|
|
|
*/ |
|
173
|
|
|
public function get_errors() |
|
174
|
|
|
{ |
|
175
|
|
|
// Replace "error" strings with their real, localised form |
|
176
|
|
|
return array_map(array($this->language, 'lang'), array_filter($this->errors)); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param array $content_fields |
|
181
|
|
|
* @param bool $req_mod_input |
|
182
|
|
|
* @param string $cp_class ucp|mcp |
|
183
|
|
|
* @return array |
|
184
|
|
|
*/ |
|
185
|
|
|
public function get_submitted_data(array $content_fields, &$req_mod_input, $cp_class = 'ucp') |
|
186
|
|
|
{ |
|
187
|
|
|
$previewing = $this->request->is_set('preview'); |
|
188
|
|
|
|
|
189
|
|
|
$fields_data = array(); |
|
190
|
|
|
foreach ($content_fields as $field => $row) |
|
191
|
|
|
{ |
|
192
|
|
|
$row += $this->get_default_field_data(); |
|
193
|
|
|
$value = $this->get_submitted_field_data($row, $req_mod_input, $cp_class); |
|
194
|
|
|
|
|
195
|
|
|
if ($previewing || empty($row['field_props']['is_db_field'])) |
|
196
|
|
|
{ |
|
197
|
|
|
$fields_data[$field] = $value; |
|
198
|
|
|
} |
|
199
|
|
|
else |
|
200
|
|
|
{ |
|
201
|
|
|
$this->db_fields[$field] = $value; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return array_filter($fields_data); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @param array $topic_data |
|
210
|
|
|
* @param array $content_fields |
|
211
|
|
|
* @return void |
|
212
|
|
|
*/ |
|
213
|
|
|
public function save_db_fields(array $topic_data, array $content_fields) |
|
214
|
|
|
{ |
|
215
|
|
|
foreach ($this->db_fields as $field => $value) |
|
216
|
|
|
{ |
|
217
|
|
|
$field_data = $content_fields[$field]; |
|
218
|
|
|
$obj = $this->fields_factory->get($field_data['field_type']); |
|
219
|
|
|
$field_data['field_props'] += $obj->get_default_props(); |
|
220
|
|
|
$obj->save_field($value, $field_data, $topic_data); |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @param array $row |
|
226
|
|
|
* @param bool $req_mod_input |
|
227
|
|
|
* @param string $cp_class |
|
228
|
|
|
* @return mixed |
|
229
|
|
|
*/ |
|
230
|
|
|
protected function get_submitted_field_data(array &$row, &$req_mod_input, $cp_class) |
|
231
|
|
|
{ |
|
232
|
|
|
$obj = $this->fields_factory->get($row['field_type']); |
|
233
|
|
|
$row['field_props'] += $obj->get_default_props(); |
|
234
|
|
|
$row['field_value'] = $row['field_value'] ?: ''; |
|
235
|
|
|
$field_value = $obj->get_field_value($row); |
|
236
|
|
|
|
|
237
|
|
|
if (!empty($field_value)) |
|
238
|
|
|
{ |
|
239
|
|
|
$this->errors[] = $obj->validate_field($row); |
|
240
|
|
|
} |
|
241
|
|
|
else if ($row['field_required']) |
|
242
|
|
|
{ |
|
243
|
|
|
if (!$row['field_mod_only'] || $cp_class === 'mcp') |
|
244
|
|
|
{ |
|
245
|
|
|
$this->errors[] = $this->language->lang_array('CONTENT_FIELD_REQUIRED', array($row['field_label'])); |
|
246
|
|
|
} |
|
247
|
|
|
else |
|
248
|
|
|
{ |
|
249
|
|
|
$req_mod_input = true; |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
return $field_value; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @return array |
|
258
|
|
|
*/ |
|
259
|
|
|
protected function get_default_field_data() |
|
260
|
|
|
{ |
|
261
|
|
|
return array( |
|
262
|
|
|
'field_label' => '', |
|
263
|
|
|
'field_explain' => '', |
|
264
|
|
|
'field_value' => '', |
|
265
|
|
|
'field_required' => false, |
|
266
|
|
|
'field_props' => array(), |
|
267
|
|
|
); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* @param string $field_type |
|
272
|
|
|
* @return string |
|
273
|
|
|
*/ |
|
274
|
|
|
protected function get_field_key($field_type) |
|
275
|
|
|
{ |
|
276
|
|
|
return ($field_type === 'hidden') ? 'hidden' : 'element'; |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
|
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