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

comments::build_pagination()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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