Completed
Push — develop ( ff34f8...c5327c )
by Daniel
09:18
created

comments::get_post_display_lang()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 2
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\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 */
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_key = $sort_dir = $sort_days = $u_sort_param = '';
97
			$this->set_sorting_options($sort_days, $sort_key, $sort_dir, $u_sort_param);
98
99
			$base_url = append_sid(build_url(array('start', 'p')), (strlen($u_sort_param)) ? $u_sort_param : '');
100
			$this->build_pagination($start, $post_id, $topic_data, $sort_dir, $base_url);
101
102
			$this->forum->query()
103
				->fetch_date_range(time(), $sort_days * 86400, 'post')
104
				->build();
105
			$posts_data = $this->forum->get_post_data(false, array(), $this->config['posts_per_page'], $start, array(
106
				'WHERE'		=> array(
107
					'p.topic_id = ' . (int) $topic_data['topic_id'],
108
					'p.post_id <> ' . (int) $topic_data['topic_first_post_id'],
109
				),
110
				'ORDER_BY'	=> $this->sort_by_sql[$sort_key] . ' ' . $this->sort_dir_sql[$sort_dir],
111
			));
112
113
			$topic_tracking_info = $this->forum->get_topic_tracking_info();
114
			$users_cache = $this->forum->get_posters_info();
115
116
			$this->show_posts($topic_data, array_values(array_shift($posts_data)), $topic_tracking_info, $users_cache, $update_count, $content_type, $start);
117
		}
118
	}
119
120
	/**
121
	 * @param array $topic_data
122
	 * @param array $posts_data
123
	 * @param array $topic_tracking_info
124
	 * @param array $users_cache
125
	 * @param array $update_count
126
	 * @param string $type
127
	 * @param int $start
128
	 * @return void
129
	 */
130
	protected function show_posts(array $topic_data, array $posts_data, array $topic_tracking_info, array $users_cache, array &$update_count, $type, $start)
131
	{
132
		$attachments = $this->forum->get_attachments($topic_data['forum_id']);
133
		$this->set_form_action($topic_data['topic_url'], $start == 0);
0 ignored issues
show
Documentation introduced by
$start == 0 is of type boolean, but the function expects a integer.

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...
134
135
		for ($i = 0, $size = sizeof($posts_data); $i < $size; $i++)
136
		{
137
			$row = $posts_data[$i];
138
			$poster_id = $row['poster_id'];
139
140
			$this->template->assign_block_vars('postrow', array_merge(
141
				$this->topic->get_detail_template_data($type, $topic_data, $row, $users_cache, $attachments, $topic_tracking_info, $update_count),
142
				$this->get_attachments_tpl_data($row['post_id'], $attachments),
143
				array(
144
					'POST_SUBJECT'				=> $row['post_subject'],
145
					'POST_DATE'					=> $this->user->format_date($row['post_time'], false, false),
146
					'POSTER_WARNINGS'			=> $this->get_poster_warnings($users_cache[$poster_id]),
147
					'S_POST_REPORTED'			=> $this->get_report_status($row),
148
					'S_TOPIC_POSTER'			=> ($topic_data['topic_poster'] == $poster_id) ? true : false,
149
					'S_POST_HIDDEN'				=> $row['hide_post'],
150
					'L_POST_DISPLAY'			=> $this->get_post_display_lang($row, $topic_data['topic_url']),
151
				)
152
			));
153
154
			$this->topic->show_attachments($attachments, $row['post_id'], 'postrow.attachment');
155
		}
156
	}
157
158
	/**
159
	 * @param string $view
160
	 * @param array $topic_data
161
	 * @retrun void
162
	 */
163
	protected function find_unread($view, array $topic_data)
164
	{
165
		if ($view === 'unread')
166
		{
167
			$forum_id = (int) $topic_data['forum_id'];
168
			$topic_id = (int) $topic_data['topic_id'];
169
170
			// Get topic tracking info
171
			$topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
172
			$topic_last_read = (isset($topic_tracking_info[$topic_id])) ? $topic_tracking_info[$topic_id] : 0;
173
174
			$sql = 'SELECT post_id, topic_id, forum_id
175
				FROM ' . POSTS_TABLE . "
176
				WHERE topic_id = $topic_id
177
					AND " . $this->content_visibility->get_visibility_sql('post', $forum_id) . "
178
					AND post_time > $topic_last_read
179
					AND forum_id = $forum_id
180
				ORDER BY post_time ASC, post_id ASC";
181
			$result = $this->db->sql_query_limit($sql, 1);
182
			$row = $this->db->sql_fetchrow($result);
183
			$this->db->sql_freeresult($result);
184
185
			if ($row)
186
			{
187
				redirect(append_sid($topic_data['topic_url'], 'p=' . $row['post_id']) . '#p' . $row['post_id']);
188
			}
189
		}
190
	}
191
192
	/**
193
	 * This is for determining where we are (page)
194
	 * @param int $start
195
	 * @param int $post_id
196
	 * @param array $topic_data
197
	 * @param string $sort_dir
198
	 * @param string $base_url
199
	 * @return void
200
	 */
201
	protected function build_pagination(&$start, $post_id, array $topic_data, $sort_dir, $base_url)
202
	{
203
		if ($post_id)
204
		{
205
			$this->check_requested_post_id($topic_data, $base_url);
206
207
			$prev_posts = $this->get_next_posts_count($topic_data, $sort_dir, $post_id);
208
			$start = floor($prev_posts / $this->config['posts_per_page']) * $this->config['posts_per_page'];
209
		}
210
211
		$start = $this->pagination->validate_start($start, $this->config['posts_per_page'], $topic_data['total_comments']);
212
		$this->pagination->generate_template_pagination($base_url, 'pagination', 'start', $topic_data['total_comments'], $this->config['posts_per_page'], $start);
213
214
		$data =& $this->template_context->get_data_ref();
215
		foreach ($data['pagination'] as &$row)
216
		{
217
			$row['PAGE_URL'] .= '#comments';
218
		}
219
	}
220
221
	/**
222
	 * @param array $topic_data
223
	 * @param string $base_url
224
	 * @return void
225
	 */
226
	protected function check_requested_post_id(array $topic_data, $base_url)
227
	{
228
		// are we where we are supposed to be?
229
		if (($topic_data['post_visibility'] == ITEM_UNAPPROVED || $topic_data['post_visibility'] == ITEM_REAPPROVE) && !$this->auth->acl_get('m_approve', $topic_data['forum_id']))
230
		{
231
			// If post_id was submitted, we try at least to display the topic as a last resort...
232
			if ($topic_data['topic_id'])
233
			{
234
				redirect($base_url);
235
			}
236
237
			trigger_error('NO_TOPIC');
238
		}
239
	}
240
241
	/**
242
	 * @param array $topic_data
243
	 * @param string $sort_dir
244
	 * @param int $post_id
245
	 * @return int
246
	 */
247
	protected function get_next_posts_count(array $topic_data, $sort_dir, $post_id)
248
	{
249
		if ($post_id == $topic_data['topic_first_post_id'] || $post_id == $topic_data['topic_last_post_id'])
250
		{
251
			$check_sort = ($post_id == $topic_data['topic_first_post_id']) ? 'd' : 'a';
252
253
			$prev_posts_count = 0;
254
			if ($sort_dir == $check_sort)
255
			{
256
				$prev_posts_count = $this->content_visibility->get_count('topic_posts', $topic_data, $topic_data['forum_id']) - 1;
257
			}
258
			return $prev_posts_count;
259
		}
260
		else
261
		{
262
			return $this->get_prev_posts_count($topic_data['forum_id'], $topic_data['topic_id'], $post_id, $sort_dir) - 1;
263
		}
264
	}
265
266
	/**
267
	 * @param int $forum_id
268
	 * @param int $topic_id
269
	 * @param int $post_id
270
	 * @param string $sort_dir
271
	 * @return int
272
	 */
273
	protected function get_prev_posts_count($forum_id, $topic_id, $post_id, $sort_dir)
274
	{
275
		$sql = 'SELECT post_id, post_time, post_visibility
276
			FROM ' . POSTS_TABLE . " p
277
			WHERE p.topic_id = $topic_id
278
				AND p.post_id = $post_id";
279
		$result = $this->db->sql_query($sql);
280
		$row = $this->db->sql_fetchrow($result);
281
		$this->db->sql_freeresult($result);
282
283
		$sql = 'SELECT COUNT(p.post_id) AS prev_posts
284
			FROM ' . POSTS_TABLE . " p
285
			WHERE p.topic_id = $topic_id
286
				AND " . $this->content_visibility->get_visibility_sql('post', $forum_id, 'p.');
287
288
		if ($sort_dir == 'd')
289
		{
290
			$sql .= " AND (p.post_time > {$row['post_time']} OR (p.post_time = {$row['post_time']} AND p.post_id >= {$row['post_id']}))";
291
		}
292
		else
293
		{
294
			$sql .= " AND (p.post_time < {$row['post_time']} OR (p.post_time = {$row['post_time']} AND p.post_id <= {$row['post_id']}))";
295
		}
296
297
		$result = $this->db->sql_query($sql);
298
		$row = $this->db->sql_fetchrow($result);
299
		$this->db->sql_freeresult($result);
300
301
		return $row['prev_posts'];
302
	}
303
304
	/**
305
	 * @param int $sort_days
306
	 * @param string $sort_key
307
	 * @param string $sort_dir
308
	 * @param string $u_sort_param
309
	 * @return void
310
	 */
311
	protected function set_sorting_options(&$sort_days, &$sort_key, &$sort_dir, &$u_sort_param)
312
	{
313
		$default_sort_days	= (!empty($this->user->data['user_post_show_days'])) ? $this->user->data['user_post_show_days'] : 0;
314
		$default_sort_key	= (!empty($this->user->data['user_post_sortby_type'])) ? $this->user->data['user_post_sortby_type'] : 't';
315
		$default_sort_dir	= (!empty($this->user->data['user_post_sortby_dir'])) ? $this->user->data['user_post_sortby_dir'] : 'a';
316
317
		$sort_days	= $this->request->variable('st', $default_sort_days);
318
		$sort_key	= $this->request->variable('sk', $default_sort_key);
319
		$sort_dir	= $this->request->variable('sd', $default_sort_dir);
320
321
		$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'));
322
		$sort_by_text = array('t' => $this->language->lang('POST_TIME'), 's' => $this->language->lang('SUBJECT'));
323
324
		$s_limit_days = $s_sort_key = $s_sort_dir = '';
325
		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);
326
327
		$this->template->assign_vars(array(
328
			'S_SELECT_SORT_DIR' 	=> $s_sort_dir,
329
			'S_SELECT_SORT_KEY' 	=> $s_sort_key,
330
			'S_SELECT_SORT_DAYS' 	=> $s_limit_days,
331
		));
332
	}
333
334
	/**
335
	 * @param int post_id
336
	 * @param array $attachments
337
	 * @return array
338
	 */
339
	protected function get_attachments_tpl_data($post_id, array $attachments)
340
	{
341
		$has_attachments = $multi_attachments = false;
342
		if (!empty($attachments[$row['post_id']]))
0 ignored issues
show
Bug introduced by
The variable $row seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
343
		{
344
			$has_attachments = true;
345
			$multi_attachments = sizeof($attachments[$post_id]) > 1;
346
		}
347
348
		return array(
349
			'S_HAS_ATTACHMENTS'			=> $has_attachments,
350
			'S_MULTIPLE_ATTACHMENTS'	=> $multi_attachments,
351
		);
352
	}
353
354
	/**
355
	 * @param array $poster_info
356
	 * @return string
357
	 */
358
	protected function get_poster_warnings(array $poster_info)
359
	{
360
		return $this->auth->acl_get('m_warn') ? $poster_info['warnings'] : '';
361
	}
362
363
	/**
364
	 * @param array $row
365
	 * @return bool
366
	 */
367
	protected function get_report_status(array $row)
368
	{
369
		return ($row['post_reported'] && $this->auth->acl_get('m_report', $row['forum_id'])) ? true : false;
370
	}
371
372
	/**
373
	 * @param array $row
374
	 * @param string $topic_url
375
	 * @return string
376
	 */
377
	protected function get_post_display_lang(array $row, $topic_url)
378
	{
379
		return ($row['hide_post']) ? $this->language->lang('POST_DISPLAY', '<a class="display_post" data-post-id="' . $row['post_id'] . '" href="' . append_sid($topic_url, "p={$row['post_id']}&amp;view=show") . "#p{$row['post_id']}" . '">', '</a>') : '';
380
	}
381
382
	/**
383
	 * @param string $topic_url
384
	 * @param int $start
385
	 * @return void
386
	 */
387
	protected function set_form_action($topic_url, $start)
388
	{
389
		$this->template->assign_var('S_TOPIC_ACTION', append_sid($topic_url, (($start == 0) ? '' : "start=$start")) . '#comments');
390
	}
391
}
392