Passed
Push — develop ( 49e842...20d485 )
by Daniel
03:55
created

form::validate_field()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 4
nop 5
dl 0
loc 19
ccs 0
cts 7
cp 0
crap 30
rs 8.8571
c 0
b 0
f 0
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 */
0 ignored issues
show
Bug introduced by
The type blitze\sitemaker\services\auto_lang was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
	protected $auto_lang;
25
26
	/** @var \blitze\content\services\form\fields_factory */
27
	protected $fields_factory;
28
29
	/** @var \blitze\sitemaker\services\template */
0 ignored issues
show
Bug introduced by
The type blitze\sitemaker\services\template was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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