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

index::get_topic_status_info()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 4
dl 0
loc 22
ccs 0
cts 18
cp 0
crap 20
rs 8.9197
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\actions\topic;
11
12
use blitze\content\services\actions\action_interface;
13
14
class index extends filter implements action_interface
15
{
16
	/** @var \phpbb\auth\auth */
0 ignored issues
show
Bug introduced by
The type phpbb\auth\auth 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...
17
	protected $auth;
18
19
	/** @var \phpbb\config\config */
0 ignored issues
show
Bug introduced by
The type phpbb\config\config 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...
20
	protected $config;
21
22
	/** @var \phpbb\controller\helper */
0 ignored issues
show
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...
23
	protected $controller_helper;
24
25
	/** @var \phpbb\pagination */
0 ignored issues
show
Bug introduced by
The type phpbb\pagination 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...
26
	protected $pagination;
27
28
	/** @var \phpbb\user */
0 ignored issues
show
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...
29
	protected $user;
30
31
	/** @var \blitze\content\services\types */
32
	protected $content_types;
33
34
	/* @var \blitze\content\services\fields */
35
	protected $fields;
36
37
	/** @var \blitze\sitemaker\services\forum\data */
0 ignored issues
show
Bug introduced by
The type blitze\sitemaker\services\forum\data 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...
38
	protected $forum;
39
40
	/* @var \blitze\content\services\helper */
41
	protected $helper;
42
43
	/** @var string */
44
	protected $phpbb_root_path;
45
46
	/** @var string */
47
	protected $php_ext;
48
49
	/** @var string */
50
	protected $base_url;
51
52
	/**
53
	 * Constructor
54
	 *
55
	 * @param \phpbb\auth\auth								$auth					Auth object
56
	 * @param \phpbb\config\config							$config					Config object
57
	 * @param \phpbb\db\driver\driver_interface				$db						Database connection
58
	 * @param \phpbb\controller\helper						$controller_helper		Controller Helper object
59
	 * @param \phpbb\language\language						$language				Language object
60
	 * @param \phpbb\pagination								$pagination				Pagination object
61
	 * @param \phpbb\request\request_interface				$request				Request object
62
	 * @param \phpbb\template\template						$template				Template object
63
	 * @param \phpbb\user									$user					User object
64
	 * @param \blitze\content\services\types				$content_types			Content types object
65
	 * @param \blitze\content\services\fields				$fields					Content fields object
66
	 * @param \blitze\sitemaker\services\forum\data			$forum					Forum query object
67
	 * @param \blitze\content\services\helper				$helper					Content helper object
68
	 * @param string										$phpbb_root_path		Path to the phpbb includes directory.
69
	 * @param string										$php_ext				php file extension
70
	 */
71
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\controller\helper $controller_helper, \phpbb\language\language $language, \phpbb\pagination $pagination, \phpbb\request\request_interface $request, \phpbb\template\template $template, \phpbb\user $user, \blitze\content\services\types $content_types, \blitze\content\services\fields $fields, \blitze\sitemaker\services\forum\data $forum, \blitze\content\services\helper $helper, $phpbb_root_path, $php_ext)
0 ignored issues
show
Bug introduced by
The type phpbb\db\driver\driver_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...
Bug introduced by
The type phpbb\request\request_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...
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\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...
72
	{
73
		parent::__construct($db, $language, $request, $template);
74
75
		$this->auth = $auth;
76
		$this->config = $config;
77
		$this->controller_helper = $controller_helper;
78
		$this->pagination = $pagination;
79
		$this->user = $user;
80
		$this->content_types = $content_types;
81
		$this->fields = $fields;
82
		$this->forum = $forum;
83
		$this->helper = $helper;
84
		$this->phpbb_root_path = $phpbb_root_path;
85
		$this->php_ext = $php_ext;
86
	}
87
88
	/**
89
	 * @inheritdoc
90
	 */
91
	public function execute($u_action, $mode = '')
92
	{
93
		include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
94
95
		$this->language->add_lang('viewforum');
96
		$this->template->assign_var('MODE', $mode);
97
98
		$content_types = $this->content_types->get_all_types();
99
		$this->content_forums = $this->content_types->get_forum_types();
100
101
		if (sizeof($content_types))
102
		{
103
			$sql_where_array = array();
104
			$this->apply_keyword_filter($sql_where_array);
105
			$filter_topic_status = $this->apply_status_filter($sql_where_array);
106
			$filter_content_type = $this->apply_content_type_filter();
107
108
			$callable = 'init_' . $mode . '_mode';
109
			$this->base_url = $u_action . (sizeof($this->params) ? '&amp;' : '');
110
			$this->$callable($content_types, $sql_where_array);
111
112
			$this->forum->query(true, false)
113
				->fetch_forum(array_keys($this->content_forums))
114
				->set_sorting('t.topic_time')
115
				->fetch_custom(array('WHERE' => $sql_where_array))
116
				->build(true, false, false);
117
118
			$start = $this->generate_pagination($this->base_url);
119
			$this->show_topics($mode, $u_action, $start);
120
121
			$this->generate_content_type_filter($filter_content_type, $content_types, $this->base_url);
122
			$this->generate_topic_status_filter($filter_topic_status, $this->base_url);
123
		}
124
	}
125
126
	/**
127
	 * @param string $mode
128
	 * @param string $u_action
129
	 * @param int $start
130
	 * @return void
131
	 */
132
	protected function show_topics($mode, $u_action, $start)
133
	{
134
		$topics_data = $this->forum->get_topic_data($this->config['topics_per_page'], $start);
135
		$posts_data = $this->forum->get_post_data('first');
136
		$users_cache = $this->forum->get_posters_info();
137
		$topic_tracking_info = $this->forum->get_topic_tracking_info();
138
139
		unset($this->params['type']);
140
		$base_url = join('&amp;', array($u_action, http_build_query($this->params)));
141
142
		$attachments = $update_count = array();
143
		foreach ($topics_data as $topic_id => $topic_row)
144
		{
145
			$post_row = array_shift($posts_data[$topic_id]);
146
			$content_type = $this->content_forums[$topic_row['forum_id']];
147
			$tpl_data = $this->fields->show($content_type, $topic_row, $post_row, $users_cache, $attachments, $update_count, $topic_tracking_info);
148
149
			$this->template->assign_block_vars('topicrow', array_merge($tpl_data,
150
				$this->get_content_type_info($content_type, $base_url),
151
				$this->get_topic_type_info($tpl_data['S_UNREAD_POST'], $tpl_data['TOPIC_COMMENTS'], $topic_row),
152
				$this->get_topic_status_info($tpl_data['S_POST_UNAPPROVED'], $tpl_data['S_TOPIC_DELETED'], $base_url, $topic_row),
153
				$this->get_topic_info($content_type, $u_action, $topic_row),
154
				$this->get_moderator_info($mode, $topic_id, $tpl_data['S_TOPIC_UNAPPROVED'], false, $tpl_data['S_TOPIC_DELETED'])
155
			));
156
		}
157
	}
158
159
	/**
160
	 * @param string $content_type
161
	 * @param string $u_action
162
	 * @param array $row
163
	 * @return array
164
	 */
165
	protected function get_topic_info($content_type, $u_action, array $row)
166
	{
167
		return array(
168
			'ATTACH_ICON_IMG'	=> $this->get_attachment_icon($row),
169
			'U_REVIEW_TOPIC'	=> $u_action . "&amp;do=view&amp;type=$content_type&amp;t=" . $row['topic_id'],
170
		);
171
	}
172
173
	/**
174
	 * @return void
175
	 */
176
	protected function init_mcp_mode()
177
	{
178
		$s_can_approve = (bool) sizeof(array_intersect_key($this->content_forums, $this->auth->acl_getf('m_approve', true)));
179
		$s_can_make_sticky = (bool) sizeof(array_intersect_key($this->content_forums, $this->auth->acl_getf('f_sticky', true)));
180
		$s_can_make_announce = (bool) sizeof(array_intersect_key($this->content_forums, $this->auth->acl_getf('f_announce', true)));
181
		$s_can_delete = (bool) sizeof(array_intersect_key($this->content_forums, $this->auth->acl_getf('m_delete', true)));
182
		$s_can_lock = (bool) sizeof(array_intersect_key($this->content_forums, $this->auth->acl_getf('m_lock', true)));
183
		$user_is_mod = (bool) sizeof(array_intersect_key($this->content_forums, $this->auth->acl_getf('m_', true)));
184
185
		$this->template->assign_vars(array(
186
			'S_CAN_DELETE'			=> $s_can_delete,
187
			'S_CAN_RESTORE'			=> $s_can_approve,
188
			'S_CAN_LOCK'			=> $s_can_lock,
189
			'S_CAN_SYNC'			=> $user_is_mod,
190
			'S_CAN_APPROVE'			=> $s_can_approve,
191
			'S_CAN_MAKE_NORMAL'		=> ($s_can_make_sticky || $s_can_make_announce),
192
			'S_CAN_MAKE_STICKY'		=> $s_can_make_sticky,
193
			'S_CAN_MAKE_ANNOUNCE'	=> $s_can_make_announce,
194
			'U_MCP_ACTION'			=> $this->base_url . '&amp;do=moderate&amp;redirect=' . $this->get_redirect_url($this->base_url),
195
		));
196
	}
197
198
	/**
199
	 * @param array $content_types
200
	 * @param array $sql_where_array
201
	 */
202
	protected function init_ucp_mode(array $content_types, array &$sql_where_array)
203
	{
204
		$sql_where_array[] =  't.topic_poster = ' . (int) $this->user->data['user_id'];
205
206
		// list all content types that the user can post in
207
		$postable_forums = array_intersect_key($this->content_forums, $this->auth->acl_getf('f_post', true));
208
		$postable_types = array_intersect_key($content_types, array_flip($postable_forums));
209
210
		/** @var \blitze\content\model\entity\type $entity */
211
		foreach ($postable_types as $type => $entity)
212
		{
213
			$this->template->assign_block_vars('postable', array(
214
				'TYPE'		=> $entity->get_content_langname(),
215
				'COLOUR'	=> $entity->get_content_colour(),
216
				'U_POST'	=> append_sid("{$this->phpbb_root_path}posting." . $this->php_ext, 'mode=post&amp;f=' . $entity->get_forum_id()),
0 ignored issues
show
Bug introduced by
The function append_sid 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

216
				'U_POST'	=> /** @scrutinizer ignore-call */ append_sid("{$this->phpbb_root_path}posting." . $this->php_ext, 'mode=post&amp;f=' . $entity->get_forum_id()),
Loading history...
217
			));
218
		}
219
	}
220
221
	/**
222
	 * @param string $type
223
	 * @param string $base_url
224
	 * @return array
225
	 */
226
	protected function get_content_type_info($type, $base_url)
227
	{
228
		$entity = $this->content_types->get_type($type);
229
230
		return array(
231
			'CONTENT_TYPE'			=> $entity->get_content_langname(),
232
			'CONTENT_TYPE_COLOR'	=> $entity->get_content_colour(),
233
			'S_COMMENTS'			=> $entity->get_allow_comments(),
234
			'U_CONTENT_TYPE'		=> $base_url . "&amp;type=$type",
235
		);
236
	}
237
238
	/**
239
	 * Get folder img, topic status/type related information
240
	 * @param bool $unread_topic
241
	 * @param int $num_comments
242
	 * @param array $row
243
	 * @return array
244
	 */
245
	protected function get_topic_type_info($unread_topic, $num_comments, $row)
246
	{
247
		$folder_img = $folder_alt = $topic_type = '';
248
		topic_status($row, $num_comments, $unread_topic, $folder_img, $folder_alt, $topic_type);
0 ignored issues
show
Bug introduced by
The function topic_status 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

248
		/** @scrutinizer ignore-call */ 
249
  topic_status($row, $num_comments, $unread_topic, $folder_img, $folder_alt, $topic_type);
Loading history...
249
250
		return array(
251
			'TOPIC_TYPE'				=> $topic_type,
252
			'TOPIC_IMG_STYLE'			=> $folder_img,
253
			'TOPIC_FOLDER_IMG'			=> $this->user->img($folder_img, $folder_alt),
254
		);
255
	}
256
257
	/**
258
	 * @param bool $topic_unapproved
259
	 * @param bool $topic_deleted
260
	 * @param string $base_url
261
	 * @param array $row
262
	 * @return array
263
	 */
264
	protected function get_topic_status_info($topic_unapproved, $topic_deleted, $base_url, array $row)
265
	{
266
		if ($topic_deleted)
267
		{
268
			$topic_status = 'deleted';
269
		}
270
		else if ($topic_unapproved)
271
		{
272
			$topic_status = 'unapproved';
273
		}
274
		else if ($row['topic_time'] > time())
275
		{
276
			$topic_status = 'scheduled';
277
		}
278
		else
279
		{
280
			$topic_status = $this->filter_topic_types_ary[$row['topic_type']];
281
		}
282
283
		return array(
284
			'TOPIC_STATUS'		=> $this->language->lang('TOPIC_' . strtoupper($topic_status)),
285
			'U_TOPIC_STATUS'	=> $base_url . "&amp;status=$topic_status",
286
		);
287
	}
288
289
	/**
290
	 * @param array $row
291
	 * @return string
292
	 */
293
	protected function get_attachment_icon(array $row)
294
	{
295
		return ($this->auth->acl_get('u_download') && $this->auth->acl_get('f_download', $row['forum_id']) && $row['topic_attachment']) ? $this->user->img('icon_topic_attach', $this->language->lang('TOTAL_ATTACHMENTS')) : '';
296
	}
297
298
	/**
299
	 * @param string $mode
300
	 * @param int $topic_id
301
	 * @param bool $topic_unapproved
302
	 * @param bool $posts_unapproved
303
	 * @param bool $topic_deleted
304
	 * @return array
305
	 */
306
	protected function get_moderator_info($mode, $topic_id, $topic_unapproved, $posts_unapproved, $topic_deleted)
307
	{
308
		$u_mcp_queue = '';
309
		if ($mode === 'mcp')
310
		{
311
			$u_mcp_queue = $this->get_mcp_queue_url($topic_unapproved, $posts_unapproved, $topic_id);
312
			$u_mcp_queue = (!$u_mcp_queue && $topic_deleted) ? append_sid("{$this->phpbb_root_path}mcp.$this->php_ext", 'i=queue&amp;mode=deleted_topics&amp;t=' . $topic_id, true, $this->user->session_id) : $u_mcp_queue;
0 ignored issues
show
Bug introduced by
The function append_sid 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

312
			$u_mcp_queue = (!$u_mcp_queue && $topic_deleted) ? /** @scrutinizer ignore-call */ append_sid("{$this->phpbb_root_path}mcp.$this->php_ext", 'i=queue&amp;mode=deleted_topics&amp;t=' . $topic_id, true, $this->user->session_id) : $u_mcp_queue;
Loading history...
313
		}
314
315
		return array(
316
			'U_MCP_QUEUE'	=> $u_mcp_queue,
317
		);
318
	}
319
320
	/**
321
	 * @param bool $topic_unapproved
322
	 * @param bool $posts_unapproved
323
	 * @param int $topic_id
324
	 * @return string
325
	 */
326
	protected function get_mcp_queue_url($topic_unapproved, $posts_unapproved, $topic_id)
327
	{
328
		return ($topic_unapproved || $posts_unapproved) ? append_sid("{$this->phpbb_root_path}mcp.$this->php_ext", 'i=queue&amp;mode=' . (($topic_unapproved) ? 'approve_details' : 'unapproved_posts') . "&amp;t=$topic_id", true, $this->user->session_id) : '';
0 ignored issues
show
Bug introduced by
The function append_sid 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

328
		return ($topic_unapproved || $posts_unapproved) ? /** @scrutinizer ignore-call */ append_sid("{$this->phpbb_root_path}mcp.$this->php_ext", 'i=queue&amp;mode=' . (($topic_unapproved) ? 'approve_details' : 'unapproved_posts') . "&amp;t=$topic_id", true, $this->user->session_id) : '';
Loading history...
329
	}
330
331
	/**
332
	 * @param string $base_url
333
	 * @return string
334
	 */
335
	protected function get_redirect_url($base_url)
336
	{
337
		$base_url .= (sizeof($this->params)) ? '&' . http_build_query($this->params) : '';
338
		return urlencode(str_replace('&amp;', '&', $base_url));
339
	}
340
341
	/**
342
	 * @param string $u_action
343
	 * @return int
344
	 */
345
	protected function generate_pagination($u_action)
346
	{
347
		$start = $this->request->variable('start', 0);
348
349
		$topics_count = $this->forum->get_topics_count();
350
		$u_action .= http_build_query($this->params);
351
352
		$this->template->assign_vars(array(
353
			'TOTAL_TOPICS'		=> $this->language->lang('VIEW_FORUM_TOPICS', $topics_count),
354
		));
355
356
		$start = $this->pagination->validate_start($start, $this->config['topics_per_page'], $topics_count);
357
		$this->pagination->generate_template_pagination($u_action, 'pagination', 'start', $topics_count, $this->config['topics_per_page'], $start);
358
359
		return $start;
360
	}
361
}
362