Passed
Push — develop ( cd2eb2...48150f )
by Daniel
02:25
created

fields::set_view_mode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
ccs 0
cts 2
cp 0
crap 6
rs 9.4285
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\comments_interface */
15
	protected $comments;
16
17
	/** @var \blitze\content\services\form\fields_factory */
18
	protected $fields_factory;
19
20
	/** @var string */
21
	protected $content_type;
22
23
	/** @var string */
24
	protected $board_url = '';
25
26
	/** @var string */
27
	protected $tpl_name = '';
28
29
	/** @var string */
30
	protected $display_mode = 'detail';
31
32
	/** @var string */
33
	protected $view_mode = 'detail';
34
35
	/** @var array */
36
	protected $content_fields = array();
37
38
	/** @var array */
39
	protected $db_fields = array();
40
41
	/** @var array */
42
	protected $form_fields = array();
43
44
	/** @var array */
45
	protected $label = array('', 'label-hidden', 'label-inline', 'label-newline');
46
47
	/**
48
	 * Construct
49
	 *
50
	 * @param \phpbb\config\db										$config					Config object
51
	 * @param \phpbb\controller\helper								$controller_helper		Controller Helper object
52
	 * @param \phpbb\event\dispatcher_interface						$phpbb_dispatcher		Event dispatcher object
53
	 * @param \phpbb\language\language								$language				Language object
54
	 * @param \phpbb\template\template								$template				Template object
55
	 * @param \phpbb\user											$user					User object
56
	 * @param \blitze\content\services\helper						$helper					Content helper object
57
	 * @param \blitze\content\services\comments\comments_interface	$comments				Comments object
58
	 * @param \blitze\content\services\form\fields_factory			$fields_factory			Form fields factory
59
	 */
60
	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\comments_interface $comments, \blitze\content\services\form\fields_factory $fields_factory)
0 ignored issues
show
Bug introduced by
The type phpbb\config\db 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...
Bug introduced by
The type phpbb\template\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...
Bug introduced by
The type phpbb\language\language 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...
Bug introduced by
The type phpbb\user 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...
Bug introduced by
The type phpbb\controller\helper 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...
Bug introduced by
The type phpbb\event\dispatcher_interface 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...
61
	{
62
		parent::__construct($config, $controller_helper, $phpbb_dispatcher, $language, $template, $user, $helper);
63
64
		$this->comments = $comments;
65
		$this->fields_factory = $fields_factory;
66
	}
67
68
	/**
69
	 * Set type data needed to display topics. This should only run once
70
	 *
71
	 * @param \blitze\content\model\entity\type $entity
72
	 * @param array $topic_ids
73
	 * @param array $view_mode_fields	array of form array([field_name] => [field_type])
74
	 * @param string $custom_tpl
75
	 * @param string $view_mode		summary|detail|block
76
	 * @param string $tpl_name
77
	 * @return void
78
	 */
79
	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)
80
	{
81
		$this->reset();
82
		$db_fields = array_fill_keys($topic_ids, array());
83
84
		/**
85
		 * Event to set the values for fields that are stored in the database as opposed to post text e.g. categories
86
		 * We do this here so that we can get all values with one query instead of multiple queries for each field for each topic
87
		 *
88
		 * @event blitze.content.fields.set_values
89
		 * @var string								view_mode			The current view mode (summary|detail|block)
90
		 * @var	array								view_mode_fields	Array containing fields for current view_mode
91
		 * @var \blitze\content\model\entity\type	entity				Content type entity
92
		 * @var array								db_fields			This array allows extensions that provide fields to list field values for current topic ids.
93
		 *																Ex. array([topic_id] => array([field_name] => [field_value]))
94
		 */
95
		$vars = array('view_mode', 'view_mode_fields', 'entity', 'db_fields');
96
		extract($this->phpbb_dispatcher->trigger_event('blitze.content.fields.set_values', compact($vars)));
97
98
		$this->board_url = generate_board_url(true);
0 ignored issues
show
Bug introduced by
The function generate_board_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

98
		$this->board_url = /** @scrutinizer ignore-call */ generate_board_url(true);
Loading history...
99
		$this->tpl_name	= ($custom_tpl) ? $tpl_name ?: $this->content_type . '_' . $view_mode : '';
100
		$this->db_fields = $db_fields;
101
102
		$this->content_type = $entity->get_content_name();
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
	}
107
108
	/**
109
	 * @param string $type
110
	 * @param array $topic_data
111
	 * @param array $post_data
112
	 * @param array $users_cache
113
	 * @param array $attachments
114
	 * @param array $update_count
115
	 * @param array $topic_tracking_info
116
	 * @param array $topic_data_overwrite
117
	 * @param string $mode	ucp/mcp
118
	 * @return array
119
	 */
120
	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(), $mode = '')
121
	{
122
		$callable = 'get_' . $this->view_mode . '_template_data';
123
		$tpl_data = array_merge(array(
124
				'TOPIC_COMMENTS'	=> $this->comments->count($topic_data),
125
				'S_USER_LOGGED_IN'	=> $this->user->data['is_registered'],
126
			),
127
			$this->{$callable}($type, $topic_data, $post_data, $users_cache, $attachments, $topic_tracking_info, $update_count, $mode),
128
			$topic_data_overwrite
129
		);
130
131
		$tpl_data['PERMA_LINK'] = $this->board_url . parse_url($tpl_data['TOPIC_URL'], PHP_URL_PATH);
132
133
		return $this->build_content($tpl_data);
134
	}
135
136
	/**
137
	 * @param array $tpl_data
138
	 * @return array
139
	 */
140
	public function build_content(array $tpl_data)
141
	{
142
		$fields_data = $this->get_fields_data_for_display($tpl_data);
143
144
		if ($this->tpl_name)
145
		{
146
			$this->template->assign_vars(array_change_key_case(array_merge($tpl_data, $fields_data['all']), CASE_UPPER));
147
			$this->template->set_filenames(array('content' => $this->tpl_name));
148
			$tpl_data['CUSTOM_DISPLAY'] = $this->template->assign_display('content');
149
		}
150
		else
151
		{
152
			$tpl_data['FIELDS'] = $fields_data;
153
		}
154
		unset($fields_data);
155
156
		return $tpl_data;
157
	}
158
159
	/**
160
	 * @param string $type
161
	 * @return $this
162
	 */
163
	public function set_content_type($type)
164
	{
165
		$this->content_type = $type;
166
		return $this;
167
	}
168
169
	/**
170
	 * @param string $view_mode		summary|detail|block
171
	 * @return $this
172
	 */
173
	public function set_view_mode($view_mode)
174
	{
175
		$this->display_mode = $view_mode;
176
		$this->view_mode = (in_array($view_mode, array('summary', 'detail'))) ? $view_mode : 'summary';
177
		return $this;
178
	}
179
180
	/**
181
	 * @param array $view_mode_fields	array of form array([field_name] => [field_type])
182
	 * @param array $fields_data
183
	 * @return void
184
	 */
185
	public function set_content_fields(array $view_mode_fields, array $fields_data)
186
	{
187
		foreach ($view_mode_fields as $name => $field_type)
188
		{
189
			if (isset($this->form_fields[$field_type]))
190
			{
191
				$this->content_fields[$name] = $fields_data[$name];
192
			}
193
		}
194
		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...
195
	}
196
197
	/**
198
	 * @param array $view_mode_fields	array of form array([field_name] => [field_type])
199
	 * @return $this
200
	 */
201
	public function set_form_fields(array $view_mode_fields)
202
	{
203
		$this->form_fields = array_intersect_key($this->fields_factory->get_all(), array_flip($view_mode_fields));
204
		return $this;
205
	}
206
207
	/**
208
	 * @param array $tpl_data
209
	 * @return array
210
	 */
211
	protected function get_fields_data_for_display(array &$tpl_data)
212
	{
213
		$field_values = $this->get_field_values($tpl_data);
214
		$display_data = array_fill_keys(array('all', 'above', 'body', 'inline', 'footer'), array());
215
216
		foreach ($this->content_fields as $field_name => $field_data)
217
		{
218
			$field_type = $field_data['field_type'];
219
			$field_data['content_type'] = $this->content_type;
220
			$field_data['field_props'] = array_replace_recursive($this->form_fields[$field_type]->get_default_props(), $field_data['field_props']);
221
			$field_data['field_value'] = &$field_values[$field_name];
222
223
			$field_contents	= $this->form_fields[$field_type]->display_field($field_data, $tpl_data, $this->display_mode);
224
225
			// this essentially hides other fields if the field returns an array
226
			if (is_array($field_contents))
227
			{
228
				$display_data['all'] = $field_contents;
229
				$display_data[$field_data['field_' . $this->view_mode . '_show']] = $field_contents;
230
				break;
231
			}
232
233
			if (!empty($field_contents))
234
			{
235
				$field = $this->get_field_label($field_data['field_' . $this->view_mode . '_ldisp'], $field_data['field_label']) . $field_contents;
236
				$display_data['all'][$field_name] = $field;
237
				$display_data[$field_data['field_' . $this->view_mode . '_show']][$field_name] = $field;
238
			}
239
		}
240
241
		return $display_data;
242
	}
243
244
	/**
245
	 * @param int $label_type
246
	 * @param string $label_text
247
	 * @return string
248
	 */
249
	protected function get_field_label($label_type, $label_text)
250
	{
251
		$html = '';
252
		if ($label_type)
253
		{
254
			$html = '<div class="field-label ' . $this->label[$label_type] . '">' . $label_text . $this->language->lang('COLON') . ' </div>';
255
		}
256
		return $html;
257
	}
258
259
	/**
260
	 * @param array $tpl_data
261
	 * @return array
262
	 */
263
	protected function get_field_values(array &$tpl_data)
264
	{
265
		$message = $tpl_data['MESSAGE'];
266
		unset($tpl_data['MESSAGE']);
267
268
		return array_merge(
269
			$this->db_fields[$tpl_data['TOPIC_ID']] ?: array(),
270
			$this->get_fields_data_from_post($message)
271
		);
272
	}
273
274
	/**
275
	 * @param string $post_text
276
	 * @return array
277
	 */
278
	protected function get_fields_data_from_post($post_text)
279
	{
280
		$fields_data = array();
281
		$find_fields = join('|', array_keys($this->content_fields));
282
		if (preg_match_all("#<div data-field=\"($find_fields)\">(.*?)</div><br><!-- end field -->#s", $post_text, $matches))
283
		{
284
			$fields_data = array_combine($matches[1], $matches[2]);
285
		}
286
287
		return array_intersect_key($fields_data, $this->content_fields);
288
	}
289
290
	/**
291
	 * @return void
292
	 */
293
	protected function reset()
294
	{
295
		$this->content_fields = array();
296
		$this->form_fields = array();
297
		$this->tpl_name = '';
298
		$this->view_mode = '';
299
	}
300
}
301