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

base_view::show_topic_blocks()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 4
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\views\driver;
11
12
abstract class base_view implements views_interface
13
{
14
	/** @var \phpbb\event\dispatcher_interface */
15
	protected $phpbb_dispatcher;
16
17
	/** @var\phpbb\language\language */
18
	protected $language;
19
20
	/** @var \phpbb\pagination */
21
	protected $pagination;
22
23
	/** @var \phpbb\template\template */
24
	protected $template;
25
26
	/* @var \blitze\content\services\fields */
27
	protected $fields;
28
29
	/** @var \blitze\sitemaker\services\forum\data */
30
	protected $forum;
31
32
	/* @var \blitze\content\services\helper */
33
	protected $helper;
34
35
	/* @var \blitze\content\services\quickmod */
36
	protected $quickmod;
37
38
	/** @var \blitze\content\services\topic\blocks_factory */
39
	protected $topic_blocks_factory;
40
41
	/**
42
	 * Constructor
43
	 *
44
	 * @param \phpbb\event\dispatcher_interface					$phpbb_dispatcher		Event dispatcher object
45
	 * @param \phpbb\language\language							$language				Language Object
46
	 * @param \phpbb\pagination									$pagination				Pagination object
47
	 * @param \phpbb\template\template							$template				Template object
48
	 * @param \blitze\content\services\fields					$fields					Content fields object
49
	 * @param \blitze\sitemaker\services\forum\data				$forum					Forum Data object
50
	 * @param \blitze\content\services\helper					$helper					Content helper object
51
	 * @param \blitze\content\services\quickmod					$quickmod				Quick moderator tools
52
	 * @param \blitze\content\services\topic\blocks_factory		$topic_blocks_factory	Topic blocks factory object
53
	*/
54
	public function __construct(\phpbb\event\dispatcher_interface $phpbb_dispatcher, \phpbb\language\language $language, \phpbb\pagination $pagination, \phpbb\template\template $template, \blitze\content\services\fields $fields, \blitze\sitemaker\services\forum\data $forum, \blitze\content\services\helper $helper, \blitze\content\services\quickmod $quickmod, \blitze\content\services\topic\blocks_factory $topic_blocks_factory)
55
	{
56
		$this->phpbb_dispatcher = $phpbb_dispatcher;
57
		$this->language = $language;
58
		$this->pagination = $pagination;
59
		$this->template = $template;
60
		$this->fields = $fields;
61
		$this->forum = $forum;
62
		$this->helper = $helper;
63
		$this->quickmod = $quickmod;
64
		$this->topic_blocks_factory = $topic_blocks_factory;
65
	}
66
67
	/**
68
	 * {@inheritdoc}
69
	 */
70
	public function get_detail_template()
71
	{
72
		return 'views/content_detail.html';
73
	}
74
75
	/**
76
	 * {@inheritdoc}
77
	 */
78
	public function build_index_query(array $filters, \blitze\content\model\entity\type $entity = null)
79
	{
80
		$forum_id = $entity ? $entity->get_forum_id() : 0;
81
		$sql_array = $this->get_filter_sql($filters, $forum_id);
82
83
		$this->forum->query()
84
			->fetch_forum($forum_id)
85
			->fetch_custom($sql_array)
86
			->set_sorting('t.topic_time')
87
			->build(true, false, false);
88
	}
89
90
	/**
91
	 * {@inheritdoc}
92
	 * @param array $topic_data_overwrite
93
	 */
94
	public function render_index(\blitze\content\model\entity\type $entity, $page, array $filters, array $topic_data_overwrite = array())
95
	{
96
		$content_type = $entity->get_content_name();
97
		$items_per_page = $entity->get_items_per_page();
98
		$start = ($page - 1) * $items_per_page;
99
100
		$this->build_index_query($filters, $entity);
101
		$this->set_mcp_url($entity->get_forum_id());
102
103
		if ($entity->get_show_pagination())
104
		{
105
			list($filter_type, $filter_value) = each($filters);
106
			$filter_value = (array) $filter_value;
107
108
			$total_topics = $this->forum->get_topics_count();
109
			$this->generate_pagination('summary', $total_topics, $start, $items_per_page, array(
110
				'type'			=> $content_type,
111
				'filter_type'	=> $filter_type,
112
				'filter_value'	=> current($filter_value),
113
			));
114
		}
115
116
		return $this->display_topics($entity, $items_per_page, $start, $topic_data_overwrite);
117
	}
118
119
	/**
120
	 * @param \blitze\content\model\entity\type $entity
121
	 * @param int $items_per_page
122
	 * @param int $start
123
	 * @param array $topic_data_overwrite
124
	 * @return int
125
	 */
126
	protected function display_topics(\blitze\content\model\entity\type $entity, $items_per_page = 1, $start = 0, array $topic_data_overwrite = array())
127
	{
128
		$content_type = $entity->get_content_name();
129
		$topics_data = $this->forum->get_topic_data($items_per_page, $start);
130
		$posts_data = $this->forum->get_post_data('first');
131
		$topic_tracking_info = $this->forum->get_topic_tracking_info($entity->get_forum_id());
132
		$users_cache = $this->forum->get_posters_info();
133
		$attachments = $this->forum->get_attachments($entity->get_forum_id());
134
135
		$this->fields->prepare_to_show($entity, array_keys($topics_data), $entity->get_summary_fields(), $entity->get_summary_tpl(), 'summary');
136
137
		$update_count = array();
138
		$max_update_time = 0;
139
140
		foreach ($posts_data as $topic_id => $posts)
141
		{
142
			$post_data	= array_shift($posts);
143
			$topic_data	= $topics_data[$topic_id];
144
			$topic_data = array_merge(
145
				$this->fields->show($content_type, $topic_data, $post_data, $users_cache, $attachments, $update_count, $topic_tracking_info),
146
				$topic_data_overwrite
147
			);
148
149
			$this->template->assign_block_vars('topicrow', $topic_data);
150
			$max_update_time = max($max_update_time, $topic_data['UPDATED']);
151
		}
152
		unset($topics_data, $posts_data, $users_cache, $attachments, $topic_tracking_info);
153
154
		return $max_update_time;
155
	}
156
157
	/**
158
	 * {@inheritdoc}
159
	 * @param array $topic_data_overwrite
160
	 */
161
	public function render_detail(\blitze\content\model\entity\type $entity, $topic_id, array &$update_count, array $topic_data_overwrite = array())
162
	{
163
		$this->language->add_lang('viewtopic');
164
		$this->language->add_lang('content', 'blitze/content');
165
		$this->set_mcp_url($entity->get_forum_id(), $topic_id);
166
167
		$this->forum->query()
168
			->fetch_topic($topic_id)
169
			->fetch_watch_status()
170
			->fetch_bookmark_status()
171
			->build(true, true, false);
172
173
		return $this->display_topic($topic_id, $entity, $update_count, $topic_data_overwrite);
174
	}
175
176
	/**
177
	 * @param int $topic_id
178
	 * @param \blitze\content\model\entity\type $entity
179
	 * @param array $update_count
180
	 * @param array $topic_data_overwrite
181
	 * @return array
182
	 * @throws \Exception
183
	 */
184
	protected function display_topic($topic_id, \blitze\content\model\entity\type $entity, array &$update_count, array $topic_data_overwrite)
185
	{
186
		$forum_id = $entity->get_forum_id();
187
		$content_type = $entity->get_content_name();
188
		$content_langname = $entity->get_content_name();
0 ignored issues
show
Unused Code introduced by
$content_langname is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
189
190
		$topics_data = $this->forum->get_topic_data();
191
		$post_data = $this->forum->get_post_data('first');
192
		$topic_tracking_info = $this->forum->get_topic_tracking_info($forum_id);
193
		$users_cache = $this->forum->get_posters_info();
194
		$attachments = $this->forum->get_attachments($forum_id);
195
196
		if (!sizeof($post_data))
197
		{
198
			throw new \Exception($this->language->lang('CONTENT_NO_EXIST'));
199
		}
200
201
		$this->fields->prepare_to_show($entity, array_keys($topics_data), $entity->get_detail_fields(), $entity->get_detail_tpl(), 'detail');
202
203
		$topic_data = array_shift($topics_data);
204
		$post_data = array_shift($post_data[$topic_id]);
205
		$tpl_data = array_merge($topic_data,
206
			$this->fields->show($content_type, $topic_data, $post_data, $users_cache, $attachments, $update_count, $topic_tracking_info, $topic_data_overwrite),
207
			$this->fields->get_topic_tools_data($topic_data)
208
		);
209
210
		$this->template->assign_vars(array_change_key_case($tpl_data, CASE_UPPER));
211
		$this->fields->show_attachments($attachments, $post_data['post_id']);
212
		$this->show_topic_blocks($entity, $topic_data, $post_data, array_shift($users_cache));
213
		$this->quickmod->show_tools($topic_data);
214
		$this->set_meta_tags($entity->get_detail_fields(), $tpl_data);
215
216
		return array_merge($topic_data, array(
217
			'topic_title'		=> $tpl_data['TOPIC_TITLE'],
218
			'total_comments'	=> $tpl_data['TOPIC_COMMENTS'],
219
			'topic_url'			=> $tpl_data['TOPIC_URL'],
220
		));
221
	}
222
223
	/**
224
	 * @param array $attachments
225
	 * @param int $post_id
226
	 * @return void
227
	 */
228
	 protected function show_attachments(array $attachments, $post_id)
229
	 {
230
		if (!empty($attachments[$post_id]))
231
		{
232
			foreach ($attachments[$post_id] as $attachment)
233
			{
234
				$this->template->assign_block_vars('attachment', array(
235
					'DISPLAY_ATTACHMENT'	=> $attachment)
236
				);
237
			}
238
		}
239
	 }
240
241
	/**
242
	 * @param string $view_mode
243
	 * @param int $total_topics
244
	 * @param int $start
245
	 * @param int $items_per_page
246
	 * @param array $params
247
	 */
248
	protected function generate_pagination($view_mode, $total_topics, &$start, $items_per_page, array $params)
249
	{
250
		$params = array_filter($params);
251
		$route_type = $this->get_route_type($view_mode, $params);
252
		$start = $this->pagination->validate_start($start, $items_per_page, $total_topics);
253
		$this->pagination->generate_template_pagination(
254
			array(
255
				'routes' => array(
256
					'blitze_content_' . $route_type,
257
					'blitze_content_' . $route_type . '_page',
258
				),
259
				'params' => $params,
260
			),
261
			'pagination', 'page', $total_topics, $items_per_page, $start
262
		);
263
	}
264
265
	/**
266
	 * @param string $view_mode
267
	 * @param array $params
268
	 * @return string
269
	 */
270
	protected function get_route_type($view_mode, array $params)
271
	{
272
		$types = array(
273
			'show'		=> 'show',
274
			'summary'	=> join('_', array_filter(array(
275
				(!empty($params['type'])) ? 'type' : '',
276
				(!empty($params['filters'])) ? 'multi' : '',
277
				(!empty($params['filter_type'])) ? 'filter' : '',
278
			))),
279
		);
280
281
		return $types[$view_mode];
282
	}
283
284
	/**
285
	 * {@inheritdoc}
286
	 */
287
	protected function get_filter_sql(array $filters, $forum_id)
288
	{
289
		$sql_array = array();
290
291
		/**
292
		 * Event to filter topics by field value e.g category/food
293
		 *
294
		 * @event blitze.content.view.filter
295
		 * @var mixed								forum_id		Forum id, if available
296
		 * @var array								filters			Filters
297
		 * @var array								sql_array		Array to modify sql query to get topics
298
		 */
299
		$vars = array('forum_id', 'filters', 'sql_array');
300
		extract($this->phpbb_dispatcher->trigger_event('blitze.content.view.filter', compact($vars)));
1 ignored issue
show
Bug introduced by
$this->phpbb_dispatcher-...ilter', compact($vars)) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
301
302
		return $sql_array;
303
	}
304
305
	/**
306
	 * @param \blitze\content\model\entity\type $entity
307
	 * @param array $topic_data
308
	 * @param array $post_data
309
	 * @param array $user_cache
310
	 * @return void
311
	 */
312
	protected function show_topic_blocks(\blitze\content\model\entity\type $entity, array $topic_data, array $post_data, array $user_cache)
313
	{
314
		$topic_blocks = $entity->get_topic_blocks();
315
		foreach ($topic_blocks as $service_name)
316
		{
317
			if ($block = $this->topic_blocks_factory->get($service_name))
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $block is correct as $this->topic_blocks_factory->get($service_name) (which targets blitze\content\services\...c\blocks_factory::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
318
			{
319
				$block->show_block($entity, $topic_data, $post_data, $user_cache);
320
			}
321
		}
322
	}
323
324
	/**
325
	 * @param int $forum_id
326
	 * @param int $topic_id
327
	 * @return void
328
	 */
329
	protected function set_mcp_url($forum_id, $topic_id = 0)
330
	{
331
		$this->template->assign_var('U_MCP', $this->helper->get_mcp_url($forum_id, $topic_id));
332
	}
333
334
	/**
335
	 * @param array $field_types
336
	 * @param array $topic_data
337
	 * @return array
338
	 */
339
	protected function set_meta_tags(array $field_types, array $topic_data)
340
	{
341
		$image_url = $this->get_topic_image_url($field_types, $topic_data['FIELDS']['all']);
342
		$description = $this->get_topic_description($field_types, $topic_data['FIELDS']['all']);
343
344
		$meta = "<meta name=\"description\" content=\"$description\" />\n";
345
		$meta .= "<meta name=\"twitter:card\" value=\"summary\">\n";
346
		$meta .= "<meta property=\"og:title\" content=\"{$topic_data['TOPIC_TITLE']}\" />\n";
347
		$meta .= "<meta property=\"og:type\" content=\"article\" />\n";
348
		$meta .= "<meta property=\"og:url\" content=\"{$topic_data['PERMA_LINK']}\" />\n";
349
		$meta .= "<meta property=\"og:image\" content=\"$image_url\" />\n";
350
		$meta .= "<meta property=\"og:description\" content=\"$description\" />";
351
352
		$this->template->assign_var('META', $meta);
353
	}
354
355
	/**
356
	 * @param array $field_types
357
	 * @param array $fields_data
358
	 * @return string
359
	 */
360
	protected function get_topic_image_url(array $field_types, array $fields_data)
361
	{
362
		$image_url = '';
363
364
		$image_field = array_shift(array_keys($field_types, 'image'));
0 ignored issues
show
Bug introduced by
array_keys($field_types, 'image') cannot be passed to array_shift() as the parameter $array expects a reference.
Loading history...
365
		if (null !== ($figure = $fields_data[$image_field]) && preg_match('/src="(.*?)"/i', $figure, $matches))
366
		{
367
			$image_url = $matches[1];
368
		}
369
370
		return $image_url;
371
	}
372
373
	/**
374
	 * @param array $field_types
375
	 * @param array $fields_data
376
	 * @return string
377
	 */
378
	protected function get_topic_description(array $field_types, array $fields_data)
379
	{
380
		$desc_field = array_shift(array_keys($field_types, 'textarea'));
0 ignored issues
show
Bug introduced by
array_keys($field_types, 'textarea') cannot be passed to array_shift() as the parameter $array expects a reference.
Loading history...
381
382
		if (null !== ($description = $fields_data[$desc_field]))
383
		{
384
			$description = implode(' ', array_slice(explode(' ', strip_tags($description)), 1, 20));
385
		}
386
387
		return $description;
388
	}
389
}
390