Completed
Push — develop ( 755a17...843010 )
by Daniel
07:33
created

base_view::display_topic()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 0
cts 28
cp 0
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 23
nc 2
nop 5
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\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 string */
39
	protected $phpbb_root_path;
40
41
	/** @var string */
42
	protected $php_ext;
43
44
	/**
45
	 * Constructor
46
	 *
47
	 * @param \phpbb\event\dispatcher_interface			$phpbb_dispatcher	Event dispatcher object
48
	 * @param \phpbb\language\language					$language			Language Object
49
	 * @param \phpbb\pagination							$pagination			Pagination object
50
	 * @param \phpbb\template\template					$template			Template object
51
	 * @param \blitze\content\services\fields			$fields				Content fields object
52
	 * @param \blitze\sitemaker\services\forum\data		$forum				Forum Data object
53
	 * @param \blitze\content\services\helper			$helper				Content helper object
54
	 * @param \blitze\content\services\quickmod			$quickmod			Quick moderator tools
55
	 * @param string									$phpbb_root_path	Path to the phpbb includes directory.
56
	 * @param string									$php_ext			php file extension
57
	*/
58
	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, $phpbb_root_path, $php_ext)
59
	{
60
		$this->phpbb_dispatcher = $phpbb_dispatcher;
61
		$this->language = $language;
62
		$this->pagination = $pagination;
63
		$this->template = $template;
64
		$this->fields = $fields;
65
		$this->forum = $forum;
66
		$this->helper = $helper;
67
		$this->quickmod = $quickmod;
68
		$this->phpbb_root_path = $phpbb_root_path;
69
		$this->php_ext = $php_ext;
70
	}
71
72
	/**
73
	 * {@inheritdoc}
74
	 */
75
	public function get_detail_template()
76
	{
77
		return 'views/content_detail.html';
78
	}
79
80
	/**
81
	 * {@inheritdoc}
82
	 */
83
	public function build_index_query($filter_type, $filter_value, $forum_id = '')
84
	{
85
		$sql_array = array();
86
87
		/**
88
		 * Event to filter topics by field value e.g category/food
89
		 *
90
		 * @event blitze.content.view.filter
91
		 * @var mixed								forum_id		Forum id, if available
92
		 * @var string								filter_type		Filter type e.g category|tag
93
		 * @var string								filter_value	The filter value e.g food
94
		 * @var array								sql_array		Array to modify sql query to get topics
95
		 */
96
		$vars = array('forum_id', 'filter_type', 'filter_value', 'sql_array');
97
		extract($this->phpbb_dispatcher->trigger_event('blitze.content.view.filter', compact($vars)));
98
99
		$this->forum->query()
100
			->fetch_forum($forum_id)
101
			->fetch_custom($sql_array)
102
			->set_sorting('t.forum_id, t.topic_time')
103
			->build(true, true, false);
104
	}
105
106
	/**
107
	 * {@inheritdoc}
108
	 */
109
	public function render_index(\blitze\content\model\entity\type $entity, $page, $filter_type, $filter_value, array $topic_data_overwrite = array())
110
	{
111
		$content_type = $entity->get_content_name();
112
		$items_per_page = $entity->get_items_per_page();
113
		$start = ($page - 1) * $items_per_page;
114
115
		$this->build_index_query($filter_type, $filter_value, $entity->get_forum_id());
116
117
		if ($entity->get_show_pagination())
118
		{
119
			$total_topics = $this->forum->get_topics_count();
120
			$this->generate_pagination('summary', $total_topics, $start, $items_per_page, array(
121
				'type'			=> $content_type,
122
				'filter_type'	=> $filter_type,
123
				'filter_value'	=> $filter_value,
124
			));
125
		}
126
127
		$this->display_topics($entity, $items_per_page, $start, $topic_data_overwrite);
128
	}
129
130
	/**
131
	 * @param \blitze\content\model\entity\type $entity
132
	 * @param int $items_per_page
133
	 * @param int $start
134
	 * @param array $topic_data_overwrite
135
	 * @return void
136
	 */
137
	protected function display_topics(\blitze\content\model\entity\type $entity, $items_per_page = 1, $start = 0, array $topic_data_overwrite = array())
138
	{
139
		$content_type = $entity->get_content_name();
140
		$topics_data = $this->forum->get_topic_data($items_per_page, $start);
141
		$posts_data = $this->forum->get_post_data('first');
142
		$topic_tracking_info = $this->forum->get_topic_tracking_info($entity->get_forum_id());
143
		$users_cache = $this->forum->get_posters_info();
144
		$attachments = $this->forum->get_attachments($entity->get_forum_id());
145
146
		$this->fields->prepare_to_show($entity, array_keys($topics_data), $entity->get_summary_tags(), $entity->get_summary_tpl(), 'summary');
0 ignored issues
show
Documentation introduced by
$entity->get_summary_tags() is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
147
148
		$update_count = array();
149
		foreach ($posts_data as $topic_id => $posts)
150
		{
151
			$post_data	= array_shift($posts);
152
			$topic_data	= $topics_data[$topic_id];
153
154
			$this->template->assign_block_vars('topicrow', array_merge(
155
				$this->fields->show($content_type, $topic_data, $post_data, $users_cache, $attachments, $update_count, $topic_tracking_info),
156
				$topic_data_overwrite
157
			));
158
		}
159
		unset($topics_data, $posts_data, $users_cache, $attachments, $topic_tracking_info);
160
	}
161
162
	/**
163
	 * {@inheritdoc}
164
	 */
165
	public function render_detail(\blitze\content\model\entity\type $entity, $topic_id, array &$update_count, $mode = '', array $topic_data_overwrite = array())
166
	{
167
		$this->language->add_lang('viewtopic');
168
		$this->language->add_lang('content', 'blitze/content');
169
170
		$this->forum->query()
171
			->fetch_topic($topic_id)
172
			->build(true, true, false);
173
174
		return $this->display_topic($mode, $topic_id, $entity, $update_count, $topic_data_overwrite);
175
	}
176
177
	/**
178
	 * @param string $mode
179
	 * @param int $topic_id
180
	 * @param array $update_count
181
	 * @param array $topic_data_overwrite
182
	 * @return array
183
	 * @throws \Exception
184
	 */
185
	protected function display_topic($mode, $topic_id, \blitze\content\model\entity\type $entity, array &$update_count, array $topic_data_overwrite)
0 ignored issues
show
Unused Code introduced by
The parameter $mode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
186
	{
187
		$forum_id = $entity->get_forum_id();
188
		$content_type = $entity->get_content_name();
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_tags(), $entity->get_detail_tpl(), 'detail');
0 ignored issues
show
Documentation introduced by
$entity->get_detail_tags() is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
202
203
		$topic_data = array_shift($topics_data);
204
		$post_data = array_shift($post_data[$topic_id]);
205
		$tpl_data = array_merge($topic_data, $this->fields->show($content_type, $topic_data, $post_data, $users_cache, $attachments, $update_count, $topic_tracking_info, $topic_data_overwrite));
206
207
		$this->template->assign_vars(array_change_key_case($tpl_data, CASE_UPPER));
208
		$this->fields->show_attachments($attachments, $post_data['post_id']);
209
		$this->show_author_info($forum_id, $post_data['poster_id'], $content_langname, $users_cache[$post_data['poster_id']], $entity->get_show_poster_info());
0 ignored issues
show
Bug introduced by
The variable $content_langname does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
210
		$this->show_author_contents($topic_data, $content_type, $content_langname, $entity->get_show_poster_contents());
211
		$this->quickmod->show_tools($topic_data);
212
213
		return array_merge($topic_data, array(
214
			'topic_title'		=> $tpl_data['TOPIC_TITLE'],
215
			'total_comments'	=> $tpl_data['TOPIC_COMMENTS'],
216
			'topic_url'			=> $tpl_data['TOPIC_URL'],
217
		));
218
	}
219
220
	/**
221
	 * @param array $attachments
222
	 * @param int $post_id
223
	 * @return void
224
	 */
225
	 protected function show_attachments(array $attachments, $post_id)
226
	 {
227
		if (!empty($attachments[$post_id]))
228
		{
229
			foreach ($attachments[$post_id] as $attachment)
230
			{
231
				$this->template->assign_block_vars('attachment', array(
232
					'DISPLAY_ATTACHMENT'	=> $attachment)
233
				);
234
			}
235
		}
236
	 }
237
238
	/**
239
	 * @param string $view_mode
240
	 * @param int $total_topics
241
	 * @param int $start
242
	 * @param int $items_per_page
243
	 * @param array $params
244
	 */
245
	protected function generate_pagination($view_mode, $total_topics, &$start, $items_per_page, array $params)
246
	{
247
		$route = ($view_mode === 'summary') ? 'index' : 'show';
248
		$start = $this->pagination->validate_start($start, $items_per_page, $total_topics);
249
		$this->pagination->generate_template_pagination(
250
			array(
251
				'routes' => array(
252
					'blitze_content_' . $route,
253
					'blitze_content_' . $route . '_page',
254
				),
255
				'params' => $params,
256
			),
257
			'pagination', 'page', $total_topics, $items_per_page, $start
258
		);
259
	}
260
261
	/**
262
	 * @param int $forum_id
263
	 * @param int $poster_id
264
	 * @param string $content_langname
265
	 * @param array $user_cache
266
	 * @param bool $show_author_info
267
	 * @return void
268
	 */
269
	protected function show_author_info($forum_id, $poster_id, $content_langname, array $user_cache, $show_author_info)
270
	{
271
		if ($show_author_info)
272
		{
273
			$this->forum->query()
274
				->fetch_forum($forum_id)
275
				->fetch_topic_poster($poster_id)
276
				->build(true, true, false);
277
			$user_content_topics = $this->forum->get_topics_count();
278
279
			$this->template->assign_vars(array_merge($user_cache, array(
280
				'S_USER_INFO'			=> true,
281
				'L_USER_ABOUT'			=> $this->language->lang('AUTHOR_INFO_EXPLAIN', $user_cache['username_full'], $user_cache['joined'], $user_content_topics, $content_langname, $user_cache['posts']),
282
				'L_USER_VIEW_ALL'		=> $this->language->lang('VIEW_AUTHOR_CONTENTS', $content_langname, $user_cache['username']),
283
				'L_SEARCH_USER_POSTS'	=> $this->language->lang('SEARCH_USER_POSTS', $user_cache['username']),
284
				'U_SEARCH_CONTENTS'		=> append_sid("{$this->phpbb_root_path}search.{$this->php_ext}", "author={$user_cache['username']}&amp;" . urlencode('fid[]') . "=$forum_id&amp;sc=0&amp;sf=titleonly&amp;sr=topics")
285
			)));
286
		}
287
	}
288
289
	/**
290
	 * @param array $topic_data
291
	 * @param string $content_type
292
	 * @param string $content_langname
293
	 * @param bool $show_author_contents
294
	 * @return void
295
	 */
296
	protected function show_author_contents($topic_data, $content_type, $content_langname, $show_author_contents)
297
	{
298
		if ($show_author_contents)
299
		{
300
			$this->forum->query()
301
				->fetch_forum($topic_data['forum_id'])
302
				->fetch_topic_poster($topic_data['topic_poster'])
303
				->fetch_custom(array(
304
					'WHERE' => array('t.topic_id <> ' . (int) $topic_data['topic_id'])
305
				))->build(true, true, false);
306
307
			$topics_data = $this->forum->get_topic_data(5);
308
			$topic_tracking_info = $this->forum->get_topic_tracking_info($topic_data['forum_id']);
309
310
			$this->template->assign_var('AUTHOR_CONTENT', $this->language->lang('AUTHOR_CONTENTS', $content_langname, $topic_data['topic_first_poster_name']));
311
312
			foreach ($topics_data as $row)
313
			{
314
				$this->template->assign_block_vars('content', $this->fields->get_min_topic_info($content_type, $row, $topic_tracking_info));
315
			}
316
		}
317
	}
318
}
319