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