index::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 24
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 34
ccs 0
cts 23
cp 0
crap 6
rs 9.536
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 */
17
	protected $auth;
18
19
	/** @var \phpbb\config\config */
20
	protected $config;
21
22
	/** @var \phpbb\controller\helper */
23
	protected $controller_helper;
24
25
	/** @var \phpbb\pagination */
26
	protected $pagination;
27
28
	/** @var \phpbb\user */
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
	/** @var string */
53
	protected $redirect_url;
54
55
	/** @var int */
56
	protected $topics_per_page;
57
58
	/**
59
	 * Constructor
60
	 *
61
	 * @param \phpbb\auth\auth								$auth					Auth object
62
	 * @param \phpbb\config\config							$config					Config object
63
	 * @param \phpbb\db\driver\driver_interface				$db						Database connection
64
	 * @param \phpbb\controller\helper						$controller_helper		Controller Helper object
65
	 * @param \phpbb\pagination								$pagination				Pagination object
66
	 * @param \phpbb\request\request_interface				$request				Request object
67
	 * @param \phpbb\template\template						$template				Template object
68
	 * @param \phpbb\user									$user					User object
69
	 * @param \blitze\content\services\types				$content_types			Content types object
70
	 * @param \blitze\content\services\fields				$fields					Content fields object
71
	 * @param \blitze\sitemaker\services\forum\data			$forum					Forum query object
72
	 * @param \blitze\content\services\helper				$helper					Content helper object
73
	 * @param string										$phpbb_root_path		Path to the phpbb includes directory.
74
	 * @param string										$php_ext				php file extension
75
	 */
76
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\controller\helper $controller_helper, \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)
77
	{
78
		parent::__construct($db, $request, $template);
79
80
		$this->auth = $auth;
81
		$this->config = $config;
82
		$this->controller_helper = $controller_helper;
83
		$this->pagination = $pagination;
84
		$this->user = $user;
85
		$this->content_types = $content_types;
86
		$this->fields = $fields;
87
		$this->forum = $forum;
88
		$this->helper = $helper;
89
		$this->phpbb_root_path = $phpbb_root_path;
90
		$this->php_ext = $php_ext;
91
	}
92
93
	/**
94
	 * @inheritdoc
95
	 */
96
	public function execute($u_action, $mode = '')
97
	{
98
		include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
99
100
		$this->user->add_lang('viewforum');
0 ignored issues
show
Deprecated Code introduced by
The function phpbb\user::add_lang() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

100
		/** @scrutinizer ignore-deprecated */ $this->user->add_lang('viewforum');
Loading history...
101
		$this->template->assign_var('MODE', $mode);
102
103
		$content_types = $this->content_types->get_all_types();
104
		$this->content_forums = $this->content_types->get_forum_types();
105
		$this->topics_per_page = (int) $this->config['topics_per_page'];
106
		$this->redirect_url = build_url();
107
108
		if (sizeof($content_types))
109
		{
110
			$sql_where_array = array();
111
			$search_info = $this->apply_search_filter($sql_where_array);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $search_info is correct as $this->apply_search_filter($sql_where_array) targeting blitze\content\services\...::apply_search_filter() 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...
112
			$filter_topic_status = $this->apply_topic_status_filter($sql_where_array);
113
			$filter_content_type = $this->apply_content_type_filter();
114
115
			$this->generate_search_filter($search_info, $u_action);
0 ignored issues
show
Bug introduced by
$search_info of type void is incompatible with the type array expected by parameter $search_info of blitze\content\services\...enerate_search_filter(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
			$this->generate_search_filter(/** @scrutinizer ignore-type */ $search_info, $u_action);
Loading history...
116
			$this->generate_content_type_filter($filter_content_type, $content_types, $u_action);
117
			$this->generate_topic_status_filter($filter_topic_status, $u_action);
118
119
			$this->forum->query()
120
				->fetch_forum(array_keys($this->content_forums))
121
				->set_sorting('t.topic_time')
122
				->fetch_custom(array('WHERE' => $sql_where_array));
123
124
			$this->base_url = $u_action;
125
			$callable = 'init_' . $mode . '_mode';
126
			$this->$callable($content_types);
127
128
			$start = $this->generate_pagination($u_action);
129
			$this->show_topics($mode, $u_action, $start);
130
		}
131
	}
132
133
	/**
134
	 * @return void
135
	 */
136
	protected function init_mcp_mode()
137
	{
138
		$this->forum->build(true, false, false);
139
140
		$s_can_approve = (bool) sizeof(array_intersect_key($this->content_forums, $this->auth->acl_getf('m_approve', true)));
141
		$s_can_make_sticky = (bool) sizeof(array_intersect_key($this->content_forums, $this->auth->acl_getf('f_sticky', true)));
142
		$s_can_make_announce = (bool) sizeof(array_intersect_key($this->content_forums, $this->auth->acl_getf('f_announce', true)));
143
		$s_can_delete = (bool) sizeof(array_intersect_key($this->content_forums, $this->auth->acl_getf('m_delete', true)));
144
		$s_can_lock = (bool) sizeof(array_intersect_key($this->content_forums, $this->auth->acl_getf('m_lock', true)));
145
		$user_is_mod = (bool) sizeof(array_intersect_key($this->content_forums, $this->auth->acl_getf('m_', true)));
146
147
		$this->template->assign_vars(array(
148
			'S_CAN_DELETE'			=> $s_can_delete,
149
			'S_CAN_RESTORE'			=> $s_can_approve,
150
			'S_CAN_LOCK'			=> $s_can_lock,
151
			'S_CAN_SYNC'			=> $user_is_mod,
152
			'S_CAN_APPROVE'			=> $s_can_approve,
153
			'S_CAN_MAKE_NORMAL'		=> ($s_can_make_sticky || $s_can_make_announce),
154
			'S_CAN_MAKE_STICKY'		=> $s_can_make_sticky,
155
			'S_CAN_MAKE_ANNOUNCE'	=> $s_can_make_announce,
156
			'U_MCP_ACTION'			=> $this->base_url . '&amp;do=moderate&amp;redirect=' . $this->get_redirect_url($this->base_url),
157
		));
158
	}
159
160
	/**
161
	 * @param array $content_types
162
	 */
163
	protected function init_ucp_mode(array $content_types)
164
	{
165
		$sql_where_array['WHERE'] = 't.topic_poster = ' . (int) $this->user->data['user_id'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$sql_where_array was never initialized. Although not strictly required by PHP, it is generally a good practice to add $sql_where_array = array(); before regardless.
Loading history...
166
		$this->forum->fetch_custom($sql_where_array)
167
			->build(false, false, false);
168
169
		// list all content types that the user can post in
170
		$postable_forums = array_intersect_key($this->content_forums, $this->auth->acl_getf('f_post', true));
171
		$postable_types = array_intersect_key($content_types, array_flip($postable_forums));
172
		$redirect = '&amp;redirect=' . urlencode($this->redirect_url);
173
174
		/** @var \blitze\content\model\entity\type $entity */
175
		foreach ($postable_types as $type => $entity)
176
		{
177
			$this->template->assign_block_vars('postable', array(
178
				'TYPE'		=> $entity->get_content_langname(),
179
				'COLOUR'	=> $entity->get_content_colour(),
180
				'U_POST'	=> append_sid("{$this->phpbb_root_path}posting." . $this->php_ext, 'mode=post&amp;f=' . $entity->get_forum_id() . $redirect),
181
			));
182
		}
183
	}
184
185
	/**
186
	 * @param string $mode
187
	 * @param string $u_action
188
	 * @param int $start
189
	 * @return void
190
	 */
191
	protected function show_topics($mode, $u_action, $start)
192
	{
193
		$topics_data = $this->forum->get_topic_data($this->topics_per_page, $start);
194
		$posts_data = $this->forum->get_post_data('first');
195
		$users_cache = $this->forum->get_posters_info();
196
		$topic_tracking_info = $this->forum->get_topic_tracking_info();
197
198
		$attachments = $update_count = array();
199
		foreach ($topics_data as $topic_id => $topic_row)
200
		{
201
			$post_row = array_shift($posts_data[$topic_id]);
202
			$content_type = $this->content_forums[$topic_row['forum_id']];
203
			$tpl_data = $this->fields->show($content_type, $topic_row, $post_row, $users_cache, $attachments, $update_count, $topic_tracking_info, array(), $this->redirect_url);
204
205
			$this->template->assign_block_vars('topicrow', array_merge($tpl_data,
206
				$this->get_content_type_info($content_type),
207
				$this->get_topic_type_info($tpl_data['S_UNREAD_POST'], $tpl_data['TOPIC_COMMENTS'], $topic_row),
208
				$this->get_topic_status_info($topic_row),
209
				$this->get_topic_info($content_type, $u_action, $topic_row),
210
				$this->get_moderator_info($mode, $topic_id, $tpl_data['S_POST_UNAPPROVED'], false, $tpl_data['S_TOPIC_DELETED'])
211
			));
212
		}
213
	}
214
215
	/**
216
	 * @param string $content_type
217
	 * @param string $u_action
218
	 * @param array $row
219
	 * @return array
220
	 */
221
	protected function get_topic_info($content_type, $u_action, array $row)
222
	{
223
		return array(
224
			'ATTACH_ICON_IMG'	=> $this->get_attachment_icon($row),
225
			'U_REVIEW_TOPIC'	=> $u_action . "&amp;do=view&amp;type=$content_type&amp;t={$row['topic_id']}&amp;redirect=" . $this->redirect_url,
226
		);
227
	}
228
229
	/**
230
	 * @param string $type
231
	 * @return array
232
	 */
233
	protected function get_content_type_info($type)
234
	{
235
		$entity = $this->content_types->get_type($type);
236
		$comments_type = $entity->get_comments();
237
238
		$this->fields->set_comments_type($comments_type);
239
240
		return array(
241
			'CONTENT_TYPE'			=> $entity->get_content_langname(),
242
			'CONTENT_TYPE_COLOR'	=> $entity->get_content_colour(),
243
			'S_COMMENTS'			=> (bool) $comments_type,
244
			'U_CONTENT_TYPE'		=> $this->content_type_base_url . "&amp;type=$type",
245
		);
246
	}
247
248
	/**
249
	 * Get folder img, topic status/type related information
250
	 * @param bool $unread_topic
251
	 * @param int $num_comments
252
	 * @param array $row
253
	 * @return array
254
	 */
255
	protected function get_topic_type_info($unread_topic, $num_comments, $row)
256
	{
257
		$folder_img = $folder_alt = $topic_type = '';
258
		topic_status($row, $num_comments, $unread_topic, $folder_img, $folder_alt, $topic_type);
259
260
		return array(
261
			'TOPIC_TYPE'				=> $topic_type,
262
			'TOPIC_IMG_STYLE'			=> $folder_img,
263
			'TOPIC_FOLDER_IMG'			=> $this->user->img($folder_img, $folder_alt),
264
		);
265
	}
266
267
	/**
268
	 * @param array $row
269
	 * @return array
270
	 */
271
	protected function get_topic_status_info(array $row)
272
	{
273
		$unapproved = true;
274
		$topic_status = $this->get_topic_status_filters()[$row['topic_visibility']];
275
276
		if ($row['topic_visibility'] == ITEM_APPROVED)
277
		{
278
			$unapproved = false;
279
			$topic_status = $this->get_topic_type($row);
280
		}
281
282
		return array(
283
			'TOPIC_STATUS'		=> $topic_status,
284
			'S_POST_UNAPPROVED'	=> $unapproved,
285
			'U_TOPIC_STATUS'	=> $this->topic_status_base_url . '&amp;status=' . $topic_status,
286
		);
287
	}
288
289
	/**
290
	 * @param array $row
291
	 * @return string
292
	 */
293
	protected function get_topic_type(array $row)
294
	{
295
		return $this->get_topic_types_filters()[$row['topic_type']];
296
	}
297
298
	/**
299
	 * @param array $row
300
	 * @return string
301
	 */
302
	protected function get_attachment_icon(array $row)
303
	{
304
		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')) : '';
0 ignored issues
show
Bug Best Practice introduced by
The property language does not exist on blitze\content\services\actions\topic\index. Did you maybe forget to declare it?
Loading history...
305
	}
306
307
	/**
308
	 * @param string $mode
309
	 * @param int $topic_id
310
	 * @param bool $topic_unapproved
311
	 * @param bool $posts_unapproved
312
	 * @param bool $topic_deleted
313
	 * @return array
314
	 */
315
	protected function get_moderator_info($mode, $topic_id, $topic_unapproved, $posts_unapproved, $topic_deleted)
316
	{
317
		$u_mcp_queue = '';
318
		if ($mode === 'mcp')
319
		{
320
			$u_mcp_queue = $this->get_mcp_queue_url($topic_unapproved, $posts_unapproved, $topic_id);
321
			$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;
322
		}
323
324
		return array(
325
			'U_MCP_QUEUE'	=> $u_mcp_queue,
326
		);
327
	}
328
329
	/**
330
	 * @param bool $topic_unapproved
331
	 * @param bool $posts_unapproved
332
	 * @param int $topic_id
333
	 * @return string
334
	 */
335
	protected function get_mcp_queue_url($topic_unapproved, $posts_unapproved, $topic_id)
336
	{
337
		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) : '';
338
	}
339
340
	/**
341
	 * @param string $base_url
342
	 * @return string
343
	 */
344
	protected function get_redirect_url($base_url)
345
	{
346
		$base_url .= (sizeof($this->params)) ? '&' . http_build_query($this->params) : '';
347
		return urlencode(str_replace('&amp;', '&', $base_url));
348
	}
349
350
	/**
351
	 * @param string $u_action
352
	 * @return int
353
	 */
354
	protected function generate_pagination($u_action)
355
	{
356
		$start = $this->request->variable('start', 0);
357
358
		$topics_count = $this->forum->get_topics_count();
359
		$page_url = $this->get_filter_type_base_url($u_action);
360
361
		$this->template->assign_vars(array(
362
			'TOTAL_TOPICS'		=> $this->user->lang('VIEW_FORUM_TOPICS', $topics_count),
0 ignored issues
show
Deprecated Code introduced by
The function phpbb\user::lang() has been deprecated: 3.2.0-dev (To be removed 4.0.0) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

362
			'TOTAL_TOPICS'		=> /** @scrutinizer ignore-deprecated */ $this->user->lang('VIEW_FORUM_TOPICS', $topics_count),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
363
		));
364
365
		$start = $this->pagination->validate_start($start, $this->topics_per_page, $topics_count);
366
		$this->pagination->generate_template_pagination($page_url, 'pagination', 'start', $topics_count, $this->topics_per_page, $start);
367
368
		return $start;
369
	}
370
}
371