Completed
Push — develop ( 613db1...73b4e9 )
by Daniel
05:02
created

fields::get_field_label()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 6
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 array */
21
	protected $form_fields;
22
23
	/** @var string */
24
	protected $content_type;
25
26
	/** @var array */
27
	protected $content_fields;
28
29
	/** @var string */
30
	protected $board_url = '';
31
32
	/** @var string */
33
	protected $tpl_name = '';
34
35
	/** @var string */
36
	protected $display_mode = '';
37
38
	/** @var string */
39
	protected $view_mode = '';
40
41
	/** @var array */
42
	protected $db_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)
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
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)));
1 ignored issue
show
Bug introduced by
$this->phpbb_dispatcher-...alues', compact($vars)) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
97
98
		$this->board_url = generate_board_url(true);
99
		$this->display_mode = $view_mode;
100
		$this->tpl_name	= ($custom_tpl) ? $tpl_name ?: $this->content_type . '_' . $view_mode : '';
101
		$this->view_mode = (in_array($view_mode, array('summary', 'detail'))) ? $view_mode : 'summary';
102
		$this->db_fields = $db_fields;
103
104
		$this->content_type = $entity->get_content_name();
105
		$this->set_form_fields($view_mode_fields);
106
		$this->set_content_fields($view_mode_fields, $entity->get_content_fields());
107
	}
108
109
	/**
110
	 * @param string $type
111
	 * @param array $topic_data
112
	 * @param array $post_data
113
	 * @param array $users_cache
114
	 * @param array $attachments
115
	 * @param array $update_count
116
	 * @param array $topic_tracking_info
117
	 * @param array $topic_data_overwrite
118
	 * @param string $mode
119
	 * @return array
120
	 */
121
	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 = '')
122
	{
123
		$callable = 'get_' . $this->view_mode . '_template_data';
124
		$tpl_data = array_merge(array(
125
				'TOPIC_COMMENTS'	=> $this->comments->count($topic_data),
126
				'S_USER_LOGGED_IN'	=> $this->user->data['is_registered'],
127
			),
128
			$this->{$callable}($type, $topic_data, $post_data, $users_cache, $attachments, $topic_tracking_info, $update_count, $mode),
129
			$topic_data_overwrite
130
		);
131
132
		$tpl_data['PERMA_LINK'] = $this->board_url . parse_url($tpl_data['TOPIC_URL'], PHP_URL_PATH);
133
134
		return $this->build_content($tpl_data);
135
	}
136
137
	/**
138
	 * @param array $tpl_data
139
	 * @return array
140
	 */
141
	public function build_content(array $tpl_data)
142
	{
143
		$fields_data = $this->get_fields_data_for_display($tpl_data);
144
145
		if ($this->tpl_name)
146
		{
147
			$this->template->assign_vars(array_change_key_case(array_merge($tpl_data, $fields_data['all']), CASE_UPPER));
148
			$this->template->set_filenames(array('content' => $this->tpl_name));
149
			$tpl_data['CUSTOM_DISPLAY'] = $this->template->assign_display('content');
150
		}
151
		else
152
		{
153
			$tpl_data['FIELDS'] = $fields_data;
154
		}
155
		unset($fields_data);
156
157
		return $tpl_data;
158
	}
159
160
	/**
161
	 * @param array $view_mode_fields	array of form array([field_name] => [field_type])
162
	 * @param array $fields_data
163
	 * @return void
164
	 */
165
	public function set_content_fields(array $view_mode_fields, array $fields_data)
166
	{
167
		foreach ($view_mode_fields as $name => $field_type)
168
		{
169
			if (isset($this->form_fields[$field_type]))
170
			{
171
				$this->content_fields[$name] = $fields_data[$name];
172
			}
173
		}
174
	}
175
176
	/**
177
	 * @param array $view_mode_fields	array of form array([field_name] => [field_type])
178
	 * @return void
179
	 */
180
	public function set_form_fields(array $view_mode_fields)
181
	{
182
		$this->form_fields = array_intersect_key($this->fields_factory->get_all(), array_flip($view_mode_fields));
183
	}
184
185
	/**
186
	 * @param string $type
187
	 * @return void
188
	 */
189
	public function set_content_type($type)
190
	{
191
		$this->content_type = $type;
192
	}
193
194
	/**
195
	 * @param array $tpl_data
196
	 * @return array
197
	 */
198
	protected function get_fields_data_for_display(array &$tpl_data)
199
	{
200
		$field_values = $this->get_field_values($tpl_data);
201
		$display_data = array_fill_keys(array('all', 'above', 'body', 'inline', 'footer'), array());
202
203
		foreach ($this->content_fields as $field_name => $field_data)
204
		{
205
			$field_type = $field_data['field_type'];
206
			$field_data['content_type'] = $this->content_type;
207
			$field_data['field_props'] = array_replace_recursive($this->form_fields[$field_type]->get_default_props(), $field_data['field_props']);
208
			$field_data['field_value'] = &$field_values[$field_name];
209
210
			$field_contents	= $this->form_fields[$field_type]->display_field($field_data, $tpl_data, $this->display_mode);
211
212
			// this essentially hides other fields if the field returns an array
213
			if (is_array($field_contents))
214
			{
215
				$display_data['all'] = $field_contents;
216
				$display_data[$field_data['field_' . $this->view_mode . '_show']] = $field_contents;
217
				break;
218
			}
219
220
			if (!empty($field_contents))
221
			{
222
				$field = $this->get_field_label($field_data['field_' . $this->view_mode . '_ldisp'], $field_data['field_label']) . $field_contents;
223
				$display_data['all'][$field_name] = $field;
224
				$display_data[$field_data['field_' . $this->view_mode . '_show']][$field_name] = $field;
225
			}
226
		}
227
228
		return $display_data;
229
	}
230
231
	/**
232
	 * @param int $label_type
233
	 * @param string $label_text
234
	 * @return string
235
	 */
236
	protected function get_field_label($label_type, $label_text)
237
	{
238
		$html = '';
239
		if ($label_type)
240
		{
241
			$html = '<div class="field-label ' . $this->label[$label_type] . '">' . $label_text . $this->language->lang('COLON') . ' </div>';
242
		}
243
		return $html;
244
	}
245
246
	/**
247
	 * @param array $tpl_data
248
	 * @return array
249
	 */
250
	protected function get_field_values(array &$tpl_data)
251
	{
252
		$message = $tpl_data['MESSAGE'];
253
		unset($tpl_data['MESSAGE']);
254
255
		return array_merge(
256
			$this->db_fields[$tpl_data['TOPIC_ID']] ?: array(),
257
			$this->get_fields_data_from_post($message)
258
		);
259
	}
260
261
	/**
262
	 * @param string $post_text
263
	 * @return array
264
	 */
265
	protected function get_fields_data_from_post($post_text)
266
	{
267
		$fields_data = array();
268
		$find_fields = join('|', array_keys($this->content_fields));
269
		if (preg_match_all("#<div data-field=\"($find_fields)\">(.*?)</div><br><!-- end field -->#s", $post_text, $matches))
270
		{
271
			$fields_data = array_combine($matches[1], $matches[2]);
272
		}
273
274
		return array_intersect_key($fields_data, $this->content_fields);
275
	}
276
277
	/**
278
	 * @return void
279
	 */
280
	protected function reset()
281
	{
282
		$this->content_fields = array();
283
		$this->form_fields = array();
284
		$this->tpl_name = '';
285
		$this->view_mode = '';
286
	}
287
}
288