Passed
Push — develop ( 49e842...20d485 )
by Daniel
03:55
created

comments::get_post_info()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 10
ccs 0
cts 2
cp 0
crap 2
rs 9.4285
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\comments;
11
12
class comments extends form implements comments_interface
13
{
14
	/** @var \phpbb\content_visibility */
15
	protected $content_visibility;
16
17
	/** @var \phpbb\db\driver\driver_interface */
18
	protected $db;
19
20
	/** @var \phpbb\pagination */
21
	protected $pagination;
22
23
	/** @var \phpbb\request\request_interface */
24
	protected $request;
25
26
	/** @var \phpbb\template\context */
27
	protected $template_context;
28
29
	/** @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...
30
	protected $forum;
31
32
	/** @var \blitze\content\services\topic */
33
	protected $topic;
34
35
	/** @var array */
36
	private $sort_dir_sql = array('a' => 'ASC', 'd' => 'DESC');
37
38
	/** @var array */
39
	private $sort_by_sql = array(
40
		't' => 'p.post_time',
41
		's' => 'p.post_subject, p.post_id',
42
	);
43
44
	/**
45
	 * Constructor
46
	 *
47
	 * @param \phpbb\auth\auth							$auth				Auth object
48
	 * @param \phpbb\config\config						$config				Config object
49
	 * @param \phpbb\content_visibility					$content_visibility	Phpbb Content visibility object
50
	 * @param \phpbb\db\driver\driver_interface			$db					Database object
51
	 * @param \phpbb\language\language					$language			Language Object
52
	 * @param \phpbb\pagination							$pagination			Pagination object
53
	 * @param \phpbb\request\request_interface			$request			Request object
54
	 * @param \phpbb\template\template					$template			Template object
55
	 * @param \phpbb\template\context					$template_context	Template context object
56
	 * @param \phpbb\user								$user				User object
57
	 * @param \blitze\sitemaker\services\forum\data		$forum				Forum Data object
58
	 * @param \blitze\content\services\topic			$topic				Topic object
59
	 * @param string									$root_path			Path to the phpbb directory.
60
	 * @param string									$php_ext			php file extension
61
	*/
62
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\content_visibility $content_visibility, \phpbb\db\driver\driver_interface $db, \phpbb\language\language $language, \phpbb\pagination $pagination, \phpbb\request\request_interface $request, \phpbb\template\template $template, \phpbb\template\context $template_context, \phpbb\user $user, \blitze\sitemaker\services\forum\data $forum, \blitze\content\services\topic $topic, $root_path, $php_ext)
63
	{
64
		parent::__construct($auth, $config, $language, $template, $user, $root_path, $php_ext);
65
66
		$this->content_visibility = $content_visibility;
67
		$this->db = $db;
68
		$this->pagination = $pagination;
69
		$this->request = $request;
70
		$this->template_context = $template_context;
71
		$this->forum = $forum;
72
		$this->topic = $topic;
73
	}
74
75
	/**
76
	 * @inheritdoc
77
	 */
78
	public function count(array $topic_data)
79
	{
80
		return $this->content_visibility->get_count('topic_posts', $topic_data, $topic_data['forum_id']) - 1;
81
	}
82
83
	/**
84
	 * @inheritdoc
85
	 */
86
	public function show_comments($content_type, array $topic_data, array &$update_count)
87
	{
88
		if ($topic_data['total_comments'])
89
		{
90
			$view		= $this->request->variable('view', '');
91
			$start		= $this->request->variable('start', 0);
92
			$post_id	= $this->request->variable('p', 0);
93
94
			$this->find_unread($view, $topic_data);
95
96
			$sort_days = 0;
97
			$sort_key = $sort_dir = $u_sort_param = '';
98
			$this->set_sorting_options($sort_days, $sort_key, $sort_dir, $u_sort_param);
99
100
			$base_url = append_sid(build_url(array('start', 'p')), (strlen($u_sort_param)) ? $u_sort_param : '');
101
			$this->build_pagination($start, $post_id, $topic_data, $sort_dir, $base_url);
102
103
			$this->forum->query()
104
				->fetch_date_range(time(), $sort_days * 86400, 'post')
105
				->build();
106
			$posts_data = $this->forum->get_post_data(false, array(), (int) $this->config['posts_per_page'], $start, array(
107
				'WHERE'		=> array(
108
					'p.topic_id = ' . (int) $topic_data['topic_id'],
109
					'p.post_id <> ' . (int) $topic_data['topic_first_post_id'],
110
				),
111
				'ORDER_BY'	=> $this->sort_by_sql[$sort_key] . ' ' . $this->sort_dir_sql[$sort_dir],
112
			));
113
114
			$topic_tracking_info = $this->forum->get_topic_tracking_info();
115
			$users_cache = $this->forum->get_posters_info();
116
117
			$this->show_posts($topic_data, array_values(array_shift($posts_data)), $topic_tracking_info, $users_cache, $update_count, $content_type, $start);
118
		}
119
	}
120
121
	/**
122
	 * @param array $topic_data
123
	 * @param array $posts_data
124
	 * @param array $topic_tracking_info
125
	 * @param array $users_cache
126
	 * @param array $update_count
127
	 * @param string $type
128
	 * @param int $start
129
	 * @return void
130
	 */
131
	protected function show_posts(array $topic_data, array $posts_data, array $topic_tracking_info, array $users_cache, array &$update_count, $type, $start)
132
	{
133
		$attachments = $this->forum->get_attachments($topic_data['forum_id']);
134
		$this->set_form_action($topic_data['topic_url'], $start);
135
136
		for ($i = 0, $size = sizeof($posts_data); $i < $size; $i++)
137
		{
138
			$row = $posts_data[$i];
139
			$poster_id = $row['poster_id'];
140
141
			$this->template->assign_block_vars('postrow', array_merge(
142
				$this->topic->get_detail_template_data($type, $topic_data, $row, $users_cache, $attachments, $topic_tracking_info, $update_count),
143
				$this->get_attachments_tpl_data($row['post_id'], $attachments),
144
				array(
145
					'POST_SUBJECT'				=> $row['post_subject'],
146
					'POST_DATE'					=> $this->user->format_date($row['post_time'], false, false),
147
					'POSTER_WARNINGS'			=> $this->get_poster_warnings($users_cache[$poster_id]),
148
					'S_POST_REPORTED'			=> $this->get_report_status($row),
149
					'S_TOPIC_POSTER'			=> ($topic_data['topic_poster'] == $poster_id) ? true : false,
150
				)
151
			));
152
153
			$this->topic->show_attachments($attachments, $row['post_id'], 'postrow.attachment');
154
		}
155
	}
156
157
	/**
158
	 * @param string $view
159
	 * @param array $topic_data
160
	 * @retrun void
161
	 */
162
	protected function find_unread($view, array $topic_data)
163
	{
164
		if ($view === 'unread')
165
		{
166
			$forum_id = (int) $topic_data['forum_id'];
167
			$topic_id = (int) $topic_data['topic_id'];
168
169
			// Get topic tracking info
170
			$topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
171
			$topic_last_read = (isset($topic_tracking_info[$topic_id])) ? $topic_tracking_info[$topic_id] : 0;
172
173
			$sql = 'SELECT post_id, topic_id, forum_id
174
				FROM ' . POSTS_TABLE . "
175
				WHERE topic_id = $topic_id
176
					AND " . $this->content_visibility->get_visibility_sql('post', $forum_id) . "
177
					AND post_time > $topic_last_read
178
					AND forum_id = $forum_id
179
				ORDER BY post_time ASC, post_id ASC";
180
			$result = $this->db->sql_query_limit($sql, 1);
181
			$row = $this->db->sql_fetchrow($result);
182
			$this->db->sql_freeresult($result);
183
184
			if ($row)
185
			{
186
				redirect(append_sid($topic_data['topic_url'], 'p=' . $row['post_id']) . '#p' . $row['post_id']);
187
			}
188
		}
189
	}
190
191
	/**
192
	 * This is for determining where we are (page)
193
	 * @param int $start
194
	 * @param int $post_id
195
	 * @param array $topic_data
196
	 * @param string $sort_dir
197
	 * @param string $base_url
198
	 * @return void
199
	 */
200
	protected function build_pagination(&$start, $post_id, array $topic_data, $sort_dir, $base_url)
201
	{
202
		if ($post_id)
203
		{
204
			$post_info = $this->get_post_info($post_id);
205
			$this->check_requested_post_id($post_info, $topic_data, $base_url);
206
207
			$prev_posts = $this->get_next_posts_count($post_info, $topic_data, $sort_dir, $post_id);
208
			$start = (int) floor($prev_posts / $this->config['posts_per_page']) * $this->config['posts_per_page'];
209
		}
210
211
		$start = $this->pagination->validate_start($start, (int) $this->config['posts_per_page'], $topic_data['total_comments']);
212
		$this->pagination->generate_template_pagination($base_url, 'pagination', 'start', $topic_data['total_comments'], (int) $this->config['posts_per_page'], $start);
213
214
		$data = (isset($this->template_context->get_data_ref()['pagination'])) ? $this->template_context->get_data_ref()['pagination'] : array();
215
		foreach ($data as &$row)
216
		{
217
			$row['PAGE_URL'] .= '#comments';
218
		}
219
	}
220
221
	/**
222
	 * @param array $post_info
223
	 * @param array $topic_data
224
	 * @param string $base_url
225
	 * @return void
226
	 */
227
	protected function check_requested_post_id(array $post_info, array $topic_data, $base_url)
228
	{
229
		// are we where we are supposed to be?
230
		if (($post_info['post_visibility'] == ITEM_UNAPPROVED || $post_info['post_visibility'] == ITEM_REAPPROVE) && !$this->auth->acl_get('m_approve', $topic_data['forum_id']))
231
		{
232
			// If post_id was submitted, we try at least to display the topic as a last resort...
233
			if ($topic_data['topic_id'])
234
			{
235
				redirect($base_url);
236
			}
237
238
			trigger_error('NO_TOPIC');
239
		}
240
	}
241
242
	/**
243
	 * @param array $post_info
244
	 * @param array $topic_data
245
	 * @param string $sort_dir
246
	 * @param int $post_id
247
	 * @return int
248
	 */
249
	protected function get_next_posts_count(array $post_info, array $topic_data, $sort_dir, $post_id)
250
	{
251
		if ($post_id == $topic_data['topic_first_post_id'] || $post_id == $topic_data['topic_last_post_id'])
252
		{
253
			$check_sort = ($post_id == $topic_data['topic_first_post_id']) ? 'd' : 'a';
254
255
			$prev_posts_count = 0;
256
			if ($sort_dir == $check_sort)
257
			{
258
				$prev_posts_count = $this->content_visibility->get_count('topic_posts', $topic_data, $topic_data['forum_id']) - 1;
259
			}
260
			return $prev_posts_count;
261
		}
262
		else
263
		{
264
			return $this->get_prev_posts_count($post_info, $topic_data['forum_id'], $topic_data['topic_id'], $post_id, $sort_dir) - 1;
265
		}
266
	}
267
268
	/**
269
	 * @param array $row
270
	 * @param int $forum_id
271
	 * @param int $topic_id
272
	 * @param int $post_id
273
	 * @param string $sort_dir
274
	 * @return int
275
	 */
276
	protected function get_prev_posts_count(array $row, $forum_id, $topic_id, $post_id, $sort_dir)
0 ignored issues
show
Unused Code introduced by
The parameter $post_id is not used and could be removed. ( Ignorable by Annotation )

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

276
	protected function get_prev_posts_count(array $row, $forum_id, $topic_id, /** @scrutinizer ignore-unused */ $post_id, $sort_dir)

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

Loading history...
277
	{
278
		$sql = 'SELECT COUNT(p.post_id) AS prev_posts
279
			FROM ' . POSTS_TABLE . " p
280
			WHERE p.topic_id = $topic_id
281
				AND " . $this->content_visibility->get_visibility_sql('post', $forum_id, 'p.');
282
283
		if ($sort_dir == 'd')
284
		{
285
			$sql .= " AND (p.post_time > {$row['post_time']} OR (p.post_time = {$row['post_time']} AND p.post_id >= {$row['post_id']}))";
286
		}
287
		else
288
		{
289
			$sql .= " AND (p.post_time < {$row['post_time']} OR (p.post_time = {$row['post_time']} AND p.post_id <= {$row['post_id']}))";
290
		}
291
292
		$result = $this->db->sql_query($sql);
293
		$row = $this->db->sql_fetchrow($result);
294
		$this->db->sql_freeresult($result);
295
296
		return $row['prev_posts'];
297
	}
298
299
	/**
300
	 * @param int $post_id
301
	 * @return array
302
	 */
303
	protected function get_post_info($post_id)
304
	{
305
		$sql = 'SELECT post_id, post_time, post_visibility
306
			FROM ' . POSTS_TABLE . ' p
307
			WHERE post_id = ' . (int) $post_id;
308
		$result = $this->db->sql_query($sql);
309
		$row = $this->db->sql_fetchrow($result);
310
		$this->db->sql_freeresult($result);
311
312
		return $row;
313
	}
314
315
	/**
316
	 * @param int $sort_days
317
	 * @param string $sort_key
318
	 * @param string $sort_dir
319
	 * @param string $u_sort_param
320
	 * @return void
321
	 */
322
	protected function set_sorting_options(&$sort_days, &$sort_key, &$sort_dir, &$u_sort_param)
323
	{
324
		$default_sort_days	= (!empty($this->user->data['user_post_show_days'])) ? $this->user->data['user_post_show_days'] : 0;
325
		$default_sort_key	= (!empty($this->user->data['user_post_sortby_type'])) ? $this->user->data['user_post_sortby_type'] : 't';
326
		$default_sort_dir	= (!empty($this->user->data['user_post_sortby_dir'])) ? $this->user->data['user_post_sortby_dir'] : 'a';
327
328
		$sort_days	= $this->request->variable('st', $default_sort_days);
329
		$sort_key	= $this->request->variable('sk', $default_sort_key);
330
		$sort_dir	= $this->request->variable('sd', $default_sort_dir);
331
332
		$limit_days = array(0 => $this->language->lang('ALL_POSTS'), 1 => $this->language->lang('1_DAY'), 7 => $this->language->lang('7_DAYS'), 14 => $this->language->lang('2_WEEKS'), 30 => $this->language->lang('1_MONTH'), 90 => $this->language->lang('3_MONTHS'), 180 => $this->language->lang('6_MONTHS'), 365 => $this->language->lang('1_YEAR'));
333
		$sort_by_text = array('t' => $this->language->lang('POST_TIME'), 's' => $this->language->lang('SUBJECT'));
334
335
		$s_limit_days = $s_sort_key = $s_sort_dir = '';
336
		gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param, $default_sort_days, $default_sort_key, $default_sort_dir);
337
338
		$this->template->assign_vars(array(
339
			'S_SELECT_SORT_DIR' 	=> $s_sort_dir,
340
			'S_SELECT_SORT_KEY' 	=> $s_sort_key,
341
			'S_SELECT_SORT_DAYS' 	=> $s_limit_days,
342
		));
343
	}
344
345
	/**
346
	 * @param int $post_id
347
	 * @param array $attachments
348
	 * @return array
349
	 */
350
	protected function get_attachments_tpl_data($post_id, array $attachments)
351
	{
352
		$has_attachments = $multi_attachments = false;
353
		if (!empty($attachments[$post_id]))
354
		{
355
			$has_attachments = true;
356
			$multi_attachments = sizeof($attachments[$post_id]) > 1;
357
		}
358
359
		return array(
360
			'S_HAS_ATTACHMENTS'			=> $has_attachments,
361
			'S_MULTIPLE_ATTACHMENTS'	=> $multi_attachments,
362
		);
363
	}
364
365
	/**
366
	 * @param array $poster_info
367
	 * @return int
368
	 */
369
	protected function get_poster_warnings(array $poster_info)
370
	{
371
		return ($this->auth->acl_get('m_warn') && !empty($poster_info['warnings'])) ? $poster_info['warnings'] : 0;
372
	}
373
374
	/**
375
	 * @param array $row
376
	 * @return bool
377
	 */
378
	protected function get_report_status(array $row)
379
	{
380
		return ($row['post_reported'] && $this->auth->acl_get('m_report', $row['forum_id'])) ? true : false;
381
	}
382
383
	/**
384
	 * @param string $topic_url
385
	 * @param int $start
386
	 * @return void
387
	 */
388
	protected function set_form_action($topic_url, $start)
389
	{
390
		$this->template->assign_var('S_TOPIC_ACTION', append_sid($topic_url, (($start == 0) ? '' : "start=$start")) . '#comments');
391
	}
392
}
393