Completed
Push — develop ( ff34f8...c5327c )
by Daniel
09:18
created

fields::prepare_to_show()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 14
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 5
crap 12
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 $tpl_name = '';
31
32
	/** @var string */
33
	protected $display_mode = '';
34
35
	/** @var string */
36
	protected $view_mode = '';
37
38
	/** @var array */
39
	protected $tags = array();
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
70
	 *
71
	 * @param \blitze\content\model\entity\type $entity
72
	 * @param array $topic_ids
73
	 * @param array $view_mode_fields
74
	 * @param string $custom_tpl
75
	 * @param string $view_mode
76
	 * @return void
77
	 */
78
	public function prepare_to_show(\blitze\content\model\entity\type $entity, array $topic_ids, array $view_mode_fields, $custom_tpl, $view_mode)
79
	{
80
		$this->reset();
81
		$db_fields = array_fill_keys($topic_ids, array());
82
83
		/**
84
		 * Event to set the values for fields that are stored in the database, as opposed to post text
85
		 *
86
		 * @event blitze.content.fields.set_values
87
		 * @var string								view_mode			The current view mode (summary|detail|block)
88
		 * @var	array								view_mode_fields	Array containing fields for current view_mode
89
		 * @var \blitze\content\model\entity\type	entity				Content type entity
90
		 * @var array								db_fields			This array allows extensions that provide fields to list field values for current topic ids.
91
		 *																Extensions should merge and not overwrite/replace these entries, unless it is necessary to do so
92
		 *																Ex. array([topic_id] => array([field_name] => [field_value]))
93
		 */
94
		$vars = array('view_mode', 'view_mode_fields', 'entity', 'db_fields');
95
		extract($this->phpbb_dispatcher->trigger_event('blitze.content.fields.set_values', compact($vars)));
96
97
		$this->display_mode = $view_mode;
98
		$this->content_type = $entity->get_content_name();
99
		$this->tpl_name	= ($custom_tpl) ? $this->content_type . '_' . $view_mode : '';
100
		$this->view_mode = (in_array($view_mode, array('summary', 'detail'))) ? $view_mode : 'summary';
101
		$this->form_fields = array_intersect_key($this->fields_factory->get_all(), array_flip($view_mode_fields));
102
		$this->db_fields = $db_fields;
103
		$this->set_content_fields($view_mode_fields, $entity->get_content_fields());
104
	}
105
106
	/**
107
	 * @param string $type
108
	 * @param array $topic_data
109
	 * @param array $post_data
110
	 * @param array $users_cache
111
	 * @param array $attachments
112
	 * @param array $update_count
113
	 * @param array $topic_tracking_info
114
	 * @param array $topic_data_overwrite
115
	 * @param string $mode
116
	 * @return array
117
	 */
118
	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 = '')
119
	{
120
		$callable = 'get_' . $this->view_mode . '_template_data';
121
		$tpl_data = array_merge(array(
122
				'TOPIC_COMMENTS'	=> $this->comments->count($topic_data),
123
				'S_USER_LOGGED_IN'	=> $this->user->data['is_registered'],
124
			),
125
			$this->{$callable}($type, $topic_data, $post_data, $users_cache, $attachments, $topic_tracking_info, $update_count, $mode),
126
			$topic_data_overwrite
127
		);
128
129
		return $this->build_content($tpl_data);
130
	}
131
132
	/**
133
	 * @param array $tpl_data
134
	 * @return array
135
	 */
136
	public function build_content(array $tpl_data)
137
	{
138
		$fields_data = $this->get_fields_data_for_display($tpl_data);
139
140
		if ($this->tpl_name)
141
		{
142
			$this->template->assign_vars(array_change_key_case(array_merge($tpl_data, $fields_data['all']), CASE_UPPER));
143
			$this->template->set_filenames(array('content' => $this->tpl_name));
144
			$tpl_data['CUSTOM_DISPLAY'] = $this->template->assign_display('content');
145
		}
146
		else
147
		{
148
			$tpl_data['FIELDS'] = $fields_data;
149
		}
150
		unset($fields_data);
151
152
		return $tpl_data;
153
	}
154
155
	/**
156
	 * @param array $view_mode_fields
157
	 * @param array $fields_data
158
	 * @return void
159
	 */
160
	protected function set_content_fields(array $view_mode_fields, array $fields_data)
161
	{
162
		foreach ($view_mode_fields as $name => $field_type)
163
		{
164
			if (isset($this->form_fields[$field_type]))
165
			{
166
				$this->tags[$name] = $name;
167
				$this->content_fields[$name] = $fields_data[$name];
168
			}
169
		}
170
	}
171
172
	/**
173
	 * @param array $tpl_data
174
	 * @return array
175
	 */
176
	protected function get_fields_data_for_display(array &$tpl_data)
177
	{
178
		$field_values = $this->get_field_values($tpl_data);
179
		$display_data = array_fill_keys(array('all', 'above', 'body', 'inline', 'footer'), array());
180
181
		foreach ($this->content_fields as $field_name => $field_data)
182
		{
183
			$field_type = $field_data['field_type'];
184
			$field_data['field_props'] = array_replace_recursive($this->form_fields[$field_type]->get_default_props(), $field_data['field_props']);
185
			$field_data['field_value'] = &$field_values[$field_name];
186
187
			$field_contents	= $this->form_fields[$field_type]->display_field($field_data, $this->display_mode, $tpl_data, $this->content_type);
188
189
			// this essentially hides other fields if the field returns an array
190
			if (is_array($field_contents))
191
			{
192
				$display_data['all'] = $field_contents;
193
				$display_data[$field_data['field_' . $this->view_mode . '_show']] = $field_contents;
194
				break;
195
			}
196
197
			if (!empty($field_contents))
198
			{
199
				$field = '<div class="field-label ' . $this->label[$field_data['field_' . $this->view_mode . '_ldisp']] . '">' . $field_data['field_label'] . $this->language->lang('COLON') . ' </div>' . $field_contents;
200
				$display_data['all'][$field_name] = $field;
201
				$display_data[$field_data['field_' . $this->view_mode . '_show']][$field_name] = $field;
202
			}
203
		}
204
205
		return $display_data;
206
	}
207
208
	/**
209
	 * @param array $tpl_data
210
	 * @return array
211
	 */
212
	protected function get_field_values(array &$tpl_data)
213
	{
214
		$message = $tpl_data['MESSAGE'];
215
		unset($tpl_data['MESSAGE']);
216
	
217
		return array_merge(
218
			$this->db_fields[$tpl_data['TOPIC_ID']] ?: array(),
219
			$this->get_fields_data_from_post($message)
220
		);
221
	}
222
223
	/**
224
	 * @param string $post_text
225
	 * @return array
226
	 */
227
	protected function get_fields_data_from_post($post_text)
228
	{
229
		$fields_data = array();
230
		$find_tags = join('|', $this->tags);
231
		if (preg_match_all("#<div data-field=\"($find_tags)\">(.*?)</div><br><!-- end field -->#s", $post_text, $matches))
232
		{
233
			$fields_data = array_combine($matches[1], $matches[2]);
234
		}
235
236
		return array_intersect_key($fields_data, $this->tags);
237
	}
238
239
	/**
240
	 * @return void
241
	 */
242
	protected function reset()
243
	{
244
		$this->tags = array();
245
		$this->content_fields = array();
246
		$this->form_fields = array();
247
		$this->tpl_name = '';
248
		$this->view_mode = '';
249
	}
250
}
251