Passed
Push — release-3.0.0 ( d75c10...523432 )
by Daniel
05:03
created

fields::set_display_mode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
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;
11
12
class fields extends topic
13
{
14
	/** @var \blitze\content\services\comments\factory */
15
	protected $comments_factory;
16
17
	/** @var \blitze\content\services\form\fields_factory */
18
	protected $fields_factory;
19
20
	/** @var string */
21
	protected $content_type;
22
23
	/** @var \blitze\content\services\comments\comments_inferface */
0 ignored issues
show
Bug introduced by
The type blitze\content\services\...ents\comments_inferface 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 $comments;
25
26
	/** @var string */
27
	protected $board_url = '';
28
29
	/** @var string */
30
	protected $tpl_name = '';
31
32
	/** @var string */
33
	protected $display_mode = 'detail';
34
35
	/** @var string */
36
	protected $view_mode = 'detail';
37
38
	/** @var array */
39
	protected $content_fields = array();
40
41
	/** @var array */
42
	protected $db_fields = array();
43
44
	/** @var array */
45
	protected $form_fields = array();
46
47
	/** @var array */
48
	protected $label = array('', 'label-hidden', 'label-inline', 'label-newline');
49
50
	/**
51
	 * Construct
52
	 *
53
	 * @param \phpbb\config\db										$config					Config object
54
	 * @param \phpbb\controller\helper								$controller_helper		Controller Helper object
55
	 * @param \phpbb\event\dispatcher_interface						$phpbb_dispatcher		Event dispatcher object
56
	 * @param \phpbb\language\language								$language				Language object
57
	 * @param \phpbb\template\template								$template				Template object
58
	 * @param \phpbb\user											$user					User object
59
	 * @param \blitze\content\services\helper						$helper					Content helper object
60
	 * @param \blitze\content\services\comments\factory				$comments_factory		Comments Factory
61
	 * @param \blitze\content\services\form\fields_factory			$fields_factory			Form fields factory
62
	 */
63
	public function __construct(\phpbb\config\db $config, \phpbb\controller\helper $controller_helper, \phpbb\event\dispatcher_interface $phpbb_dispatcher, \phpbb\language\language $language, \phpbb\template\template $template, \phpbb\user $user, \blitze\content\services\helper $helper, \blitze\content\services\comments\factory $comments_factory, \blitze\content\services\form\fields_factory $fields_factory)
64
	{
65
		parent::__construct($config, $controller_helper, $phpbb_dispatcher, $language, $template, $user, $helper);
66
67
		$this->comments_factory = $comments_factory;
68
		$this->fields_factory = $fields_factory;
69
	}
70
71
	/**
72
	 * Set type data needed to display topics. This should only run once
73
	 *
74
	 * @param \blitze\content\model\entity\type $entity
75
	 * @param array $topic_ids
76
	 * @param array $view_mode_fields	array of form array([field_name] => [field_type])
77
	 * @param string $custom_tpl
78
	 * @param string $view_mode			summary|detail
79
	 * @param string $tpl_name
80
	 * @return void
81
	 */
82
	public function prepare_to_show(\blitze\content\model\entity\type $entity, array $topic_ids, array $view_mode_fields, $custom_tpl, $view_mode, $tpl_name = null)
83
	{
84
		$this->reset();
85
		$db_fields = array_fill_keys($topic_ids, array());
86
87
		/**
88
		 * Event to set the values for fields that are stored in the database as opposed to post text e.g. categories
89
		 * We do this here so that we can get all values with one query instead of multiple queries for each field for each topic
90
		 *
91
		 * @event blitze.content.fields.set_values
92
		 * @var string								view_mode			The current view mode (summary|detail)
93
		 * @var	array								view_mode_fields	Array containing fields for current view_mode
94
		 * @var \blitze\content\model\entity\type	entity				Content type entity
95
		 * @var array								db_fields			This array allows extensions that provide fields to list field values for current topic ids.
96
		 *																Ex. array([topic_id] => array([field_name] => [field_value]))
97
		 */
98
		$vars = array('view_mode', 'view_mode_fields', 'entity', 'db_fields');
99
		extract($this->phpbb_dispatcher->trigger_event('blitze.content.fields.set_values', compact($vars)));
100
101
		$this->content_type = $entity->get_content_name();
102
103
		$this->set_view_mode($view_mode);
104
		$this->set_form_fields($view_mode_fields);
105
		$this->set_content_fields($view_mode_fields, $entity->get_content_fields());
106
		$this->set_comments_type($entity->get_comments());
107
108
		$this->board_url = generate_board_url(true);
109
		$this->tpl_name	= ($custom_tpl) ? ($tpl_name ?: $this->content_type . '_' . $this->view_mode) : '';
110
		$this->db_fields = $db_fields;
111
112
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type blitze\content\services\fields which is incompatible with the documented return type void.
Loading history...
113
	}
114
115
	/**
116
	 * @param string $type
117
	 * @param array $topic_data
118
	 * @param array $post_data
119
	 * @param array $users_cache
120
	 * @param array $attachments
121
	 * @param array $update_count
122
	 * @param array $topic_tracking_info
123
	 * @param array $topic_data_overwrite
124
	 * @param string $redirect_url
125
	 * @return array
126
	 */
127
	public function show($type, array &$topic_data, array $post_data, array $users_cache, array &$attachments, array &$update_count, array $topic_tracking_info, array $topic_data_overwrite = array(), $redirect_url = '')
128
	{
129
		$callable = 'get_' . $this->view_mode . '_template_data';
130
		$tpl_data = array_merge(array(
131
				'TOPIC_COMMENTS'	=> !empty($this->comments) ? $this->comments->count($topic_data) : 0,
132
				'S_USER_LOGGED_IN'	=> $this->user->data['is_registered'],
133
			),
134
			$this->{$callable}($type, $topic_data, $post_data, $users_cache, $attachments, $topic_tracking_info, $update_count, $redirect_url),
135
			$topic_data_overwrite
136
		);
137
138
		return $this->build_content($tpl_data);
139
	}
140
141
	/**
142
	 * @param array $tpl_data
143
	 * @return array
144
	 */
145
	public function build_content(array $tpl_data)
146
	{
147
		$fields_data = $this->get_fields_data_for_display($tpl_data);
148
149
		$tpl_data['FIELDS'] = $fields_data;
150
151
		if ($this->tpl_name)
152
		{
153
			$this->template->assign_vars(array_change_key_case(array_merge($tpl_data, (array) $fields_data['all']), CASE_UPPER));
154
			$this->template->set_filenames(array('content' => $this->tpl_name));
155
			$tpl_data['CUSTOM_DISPLAY'] = $this->template->assign_display('content');
156
		}
157
		unset($fields_data);
158
159
		return $tpl_data;
160
	}
161
162
	/**
163
	 * @param string $type
164
	 * @return $this
165
	 */
166
	public function set_content_type($type)
167
	{
168
		$this->content_type = $type;
169
		return $this;
170
	}
171
172
	/**
173
	 * Sets a display mode that is passed on to the form fields for display
174
	 * @param string $mode		print|block|preview
175
	 * @return $this
176
	 */
177
	public function set_display_mode($mode)
178
	{
179
		$this->display_mode = $mode;
180
		return $this;
181
	}
182
183
	/**
184
	 * @param string $service
185
	 * @return $this
186
	 */
187
	public function set_comments_type($service)
188
	{
189
		$this->comments = $this->comments_factory->get($service);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->comments_factory->get($service) can also be of type blitze\content\services\...ents\comments_interface. However, the property $comments is declared as type blitze\content\services\...ents\comments_inferface. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
190
		return $this;
191
	}
192
193
	/**
194
	 * @param array $view_mode_fields	array of form array([field_name] => [field_type])
195
	 * @param array $fields_data
196
	 * @return $this
197
	 */
198
	public function set_content_fields(array $view_mode_fields, array $fields_data)
199
	{
200
		foreach ($view_mode_fields as $name => $field_type)
201
		{
202
			if (isset($this->form_fields[$field_type]))
203
			{
204
				$this->content_fields[$name] = $fields_data[$name];
205
			}
206
		}
207
		return $this;
208
	}
209
210
	/**
211
	 * @param string $field
212
	 * @param array $prop
213
	 * @return $this
214
	 */
215
	public function overwrite_field_data($field, array $data)
216
	{
217
		if (isset($this->content_fields[$field]))
218
		{
219
			$this->content_fields[$field] = array_replace_recursive($this->content_fields[$field], $data);
220
		}
221
		return $this;
222
	}
223
224
	/**
225
	 * @param array $view_mode_fields	array of form array([field_name] => [field_type])
226
	 * @return $this
227
	 */
228
	public function set_form_fields(array $view_mode_fields)
229
	{
230
		$this->form_fields = array_intersect_key($this->fields_factory->get_all(), array_flip($view_mode_fields));
231
		return $this;
232
	}
233
234
	/**
235
	 * @param string $view_mode
236
	 * @return void
237
	 */
238
	protected function set_view_mode($view_mode)
239
	{
240
		$this->view_mode = (in_array($view_mode, array('summary', 'detail'))) ? $view_mode : 'summary';
241
		$this->display_mode = $this->view_mode;
242
	}
243
244
	/**
245
	 * @param array $tpl_data
246
	 * @return array
247
	 */
248
	protected function get_fields_data_for_display(array &$tpl_data)
249
	{
250
		$field_values = $this->get_field_values($tpl_data);
251
		$display_data = array_fill_keys(array('all', 'above', 'body', 'inline', 'footer', 'raw'), array());
252
		$tpl_data['PERMA_LINK'] = $this->board_url . parse_url($tpl_data['TOPIC_URL'], PHP_URL_PATH);
253
254
		foreach ($this->content_fields as $field_name => $field_data)
255
		{
256
			$field_type = $field_data['field_type'];
257
			$field_data['content_type'] = $this->content_type;
258
			$field_data['field_props'] = array_replace_recursive($this->form_fields[$field_type]->get_default_props(), $field_data['field_props']);
259
			$field_data['field_value'] = &$field_values[$field_name];
260
			$field_data['field_value'] = $this->form_fields[$field_type]->get_field_value($field_data);
261
262
			$field_contents	= $this->form_fields[$field_type]->display_field($field_data, $tpl_data, $this->display_mode, $this->view_mode);
263
			$display_data['raw'][$field_name] = $field_data['field_value'];
264
265
			// this essentially hides other fields if the field returns an array
266
			if (is_array($field_contents))
267
			{
268
				$display_data['all'] = $field_contents;
269
				$display_data[$field_data['field_' . $this->view_mode . '_show']] = $field_contents;
270
				break;
271
			}
272
273
			if (!empty($field_contents))
274
			{
275
				$field = $this->get_field_label($field_data['field_' . $this->view_mode . '_ldisp'], $field_data['field_label']) . $field_contents;
276
				$display_data['all'][$field_name] = $field;
277
				$display_data[$field_data['field_' . $this->view_mode . '_show']][$field_name] = $field;
278
			}
279
		}
280
281
		return $display_data;
282
	}
283
284
	/**
285
	 * @param int $label_type
286
	 * @param string $label_text
287
	 * @return string
288
	 */
289
	protected function get_field_label($label_type, $label_text)
290
	{
291
		$html = '';
292
		if ($label_type)
293
		{
294
			$html = '<div class="field-label ' . $this->label[$label_type] . '">' . $label_text . $this->language->lang('COLON') . ' </div>';
295
		}
296
		return $html;
297
	}
298
299
	/**
300
	 * @param array $tpl_data
301
	 * @return array
302
	 */
303
	protected function get_field_values(array &$tpl_data)
304
	{
305
		$message = $tpl_data['MESSAGE'];
306
		unset($tpl_data['MESSAGE']);
307
308
		return array_merge(
309
			isset($this->db_fields[$tpl_data['TOPIC_ID']]) ? $this->db_fields[$tpl_data['TOPIC_ID']] : array(),
310
			$this->get_fields_data_from_post($message)
311
		);
312
	}
313
314
	/**
315
	 * @param string $post_text
316
	 * @return array
317
	 */
318
	protected function get_fields_data_from_post($post_text)
319
	{
320
		$fields_data = array();
321
		$find_fields = join('|', array_keys($this->content_fields));
322
		if (preg_match_all("#<div data-field=\"($find_fields)\">(.*?)</div><br><!-- end field -->#s", $post_text, $matches))
323
		{
324
			$fields_data = array_combine($matches[1], $matches[2]);
325
		}
326
327
		return array_intersect_key($fields_data, $this->content_fields);
0 ignored issues
show
Bug introduced by
It seems like $fields_data can also be of type false; however, parameter $array1 of array_intersect_key() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

327
		return array_intersect_key(/** @scrutinizer ignore-type */ $fields_data, $this->content_fields);
Loading history...
328
	}
329
330
	/**
331
	 * @return void
332
	 */
333
	protected function reset()
334
	{
335
		$this->content_fields = array();
336
		$this->form_fields = array();
337
		$this->tpl_name = '';
338
		$this->view_mode = '';
339
		$this->display_mode = '';
340
	}
341
}
342