Completed
Push — develop ( 755a17...843010 )
by Daniel
07:33
created

form   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 268
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 2
dl 0
loc 268
ccs 0
cts 108
cp 0
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A create() 0 20 1
A add() 0 19 2
A get_form() 0 18 2
A handle_request() 0 16 3
A get_data() 0 4 1
A get_errors() 0 5 1
B get_submitted_data() 0 22 4
A save_db_fields() 0 10 2
B get_submitted_field_data() 0 24 5
A get_default_field_data() 0 10 1
A get_field_key() 0 4 2
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
		$rootref = $this->template_context->get_root_ref();
80
81
		$this->form = array(
82
			'form_name'		=> $form_name,
83
			'form_action'	=> $action,
84
			'form_legend'	=> $legend,
85
			'form_method'	=> $method,
86
			'form_key'		=> $rootref['S_FORM_TOKEN'],
87
		);
88
		unset($rootref);
89
90
		return $this;
91
	}
92
93
	/**
94
	 * @param string $name
95
	 * @param string $type
96
	 * @param array $field_data
97
	 * @param int $forum_id
98
	 * @param int $topic_id
99
	 * @return \blitze\content\services\form\form
100
	 */
101
	public function add($name, $type, array $field_data, $forum_id = 0, $topic_id = 0)
102
	{
103
		$field_data += array('field_id' => 'field-' . $name);
104
		$field_data += $this->get_default_field_data();
105
106
		if ($this->fields_factory->exists($type))
107
		{
108
			$obj = $this->fields_factory->get($type);
109
110
			$field_data['field_type'] = $type;
111
			$field_data['field_props'] += $obj->get_default_props();
112
			$field_data['field_label'] = $this->language->lang($field_data['field_label']);
113
			$field_data['field_view'] = $obj->show_form_field($name, $field_data, $forum_id, $topic_id);
1 ignored issue
show
Unused Code introduced by
The call to field_interface::show_form_field() has too many arguments starting with $forum_id.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
114
115
			$this->data[$name] = $field_data;
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 int $topic_id
212
	 * @param array $content_fields
213
	 * @return void
214
	 */
215
	public function save_db_fields($topic_id, 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($topic_id, $value, $field_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
		$obj = $this->fields_factory->get($row['field_type']);
235
		$row['field_props'] += $obj->get_default_props();
236
		$field_value = $obj->get_field_value($row['field_name'], $row['field_value']);
237
238
		if (!empty($field_value))
239
		{
240
			$this->errors[] = $obj->validate_field($row);
241
		}
242
		else if ($row['field_required'])
243
		{
244
			if (!$row['field_mod_only'] || $cp_class === 'mcp')
245
			{
246
				$this->errors[] = $this->language->lang_array('FIELD_REQUIRED', array($row['field_label']));
247
			}
248
			else
249
			{
250
				$req_mod_input = true;
251
			}
252
		}
253
254
		return $field_value;
255
	}
256
257
	/**
258
	 * @return array
259
	 */
260
	protected function get_default_field_data()
261
	{
262
		return array(
263
			'field_label'		=> '',
264
			'field_explain'		=> '',
265
			'field_value'		=> '',
266
			'field_required'	=> false,
267
			'field_props'		=> array(),
268
		);
269
	}
270
271
	/**
272
	 * @param string $field_type
273
	 * @return string
274
	 */
275
	protected function get_field_key($field_type)
276
	{
277
		return ($field_type === 'hidden') ? 'hidden' : 'element';
278
	}
279
}
280