Completed
Push — develop ( 1f2a5d...e6d06f )
by Daniel
21:42 queued 08:26
created

data::get_posters_info()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 11
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\services\forum;
11
12
use blitze\sitemaker\services\forum\query_builder;
13
14
class data extends query_builder
15
{
16
	/** @var \phpbb\auth\auth */
17
	protected $auth;
18
19
	/** @var \phpbb\config\config */
20
	protected $config;
21
22
	/** @var \phpbb\content_visibility */
23
	protected $content_visibility;
24
25
	/** @var \phpbb\db\driver\driver_interface */
26
	protected $db;
27
28
	/** @var \phpbb\language\language */
29
	protected $translator;
30
31
	/** @var \phpbb\user */
32
	protected $user;
33
34
	/** @var string */
35
	protected $phpbb_root_path;
36
37
	/** @var string */
38
	protected $php_ext;
39
40
	/**
41
	 * Constructor
42
	 *
43
	 * @param \phpbb\auth\auth					$auth					Auth object
44
	 * @param \phpbb\config\config				$config					Config object
45
	 * @param \phpbb\content_visibility			$content_visibility		Content visibility
46
	 * @param \phpbb\db\driver\driver_interface	$db     				Database connection
47
	 * @param \phpbb\language\language			$translator				Language object
48
	 * @param \phpbb\user						$user					User object
49
	 * @param string							$phpbb_root_path		Path to the phpbb includes directory.
50
	 * @param string							$php_ext				php file extension
51
	 * @param integer							$cache_time				Cache results for 3 hours by default
52
	 */
53 26
	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 $translator, \phpbb\user $user, $phpbb_root_path, $php_ext, $cache_time = 10800)
54
	{
55 26
		parent::__construct($auth, $config, $content_visibility, $db, $user, $cache_time);
56
57 26
		$this->auth = $auth;
58 26
		$this->config = $config;
59 26
		$this->content_visibility = $content_visibility;
60 26
		$this->db = $db;
61 26
		$this->translator = $translator;
62 26
		$this->user = $user;
63 26
		$this->phpbb_root_path = $phpbb_root_path;
64 26
		$this->php_ext = $php_ext;
65 26
	}
66
67
	/**
68
	 * Get topics count
69
	 *
70
	 * @return integer
71
	 */
72
	public function get_topics_count()
73
	{
74
		$sql_array = array(
75
			'SELECT'	=> 'COUNT(*) AS total_topics',
76
			'FROM'		=> $this->store['sql_array']['FROM'],
77
			'WHERE'		=> $this->store['sql_array']['WHERE'],
78
		);
79
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
80
		$result = $this->db->sql_query($sql);
81
		$total_topics = $this->db->sql_fetchfield('total_topics');
82
		$this->db->sql_freeresult($result);
83
84
		return $total_topics;
85
	}
86
87
	/**
88
	 * Get topic data
89
	 *
90
	 * @param mixed|false $limit
91
	 * @param int $start
92
	 * @return array
93
	 */
94 12
	public function get_topic_data($limit = false, $start = 0)
95
	{
96 12
		$sql = $this->db->sql_build_query('SELECT', $this->store['sql_array']);
97 12
		$result = $this->db->sql_query_limit($sql, $limit, $start, $this->cache_time);
98
99 12
		while ($row = $this->db->sql_fetchrow($result))
100
		{
101 10
			$this->store['topic'][$row['topic_id']] = $row;
102
103 10
			$this->store['tracking'][$row['forum_id']]['topic_list'][] = $row['topic_id'];
104 10
			$this->store['tracking'][$row['forum_id']]['mark_time'] =& $row['forum_mark_time'];
105 10
			$this->store['post_ids']['first'][] = $row['topic_first_post_id'];
106 10
			$this->store['post_ids']['last'][] = $row['topic_last_post_id'];
107 10
		}
108 12
		$this->db->sql_freeresult($result);
109
110 12
		return $this->store['topic'];
111
	}
112
113
	/**
114
	 * Get post data
115
	 *
116
	 * @param mixed|false $topic_first_or_last_post (first|last)
117
	 * @param array $post_ids
118
	 * @param bool|false $limit
119
	 * @param int $start
120
	 * @param array $sql_array
121
	 * @return array
122
	 */
123 10
	public function get_post_data($topic_first_or_last_post = false, $post_ids = array(), $limit = false, $start = 0, $sql_array = array())
124
	{
125 10
		$sql = $this->db->sql_build_query('SELECT_DISTINCT', $this->_get_posts_sql_array($topic_first_or_last_post, $post_ids, $sql_array));
126 10
		$result = $this->db->sql_query_limit($sql, $limit, $start, $this->cache_time);
127
128 10
		$post_data = array();
129 10
		while ($row = $this->db->sql_fetchrow($result))
130
		{
131 9
			$post_data[$row['topic_id']][$row['post_id']] = $row;
132 9
			$this->store['poster_ids'][] = $row['poster_id'];
133 9
			$this->store['poster_ids'][] = $row['post_edit_user'];
134 9
			$this->store['poster_ids'][] = $row['post_delete_user'];
135 9
			$this->store['attachments'][] = $row['post_id'];
136 9
		}
137 10
		$this->db->sql_freeresult($result);
138
139 10
		return $post_data;
140
	}
141
142
	/**
143
	 * Get attachments...
144
	 *
145
	 * @param int $forum_id
146
	 * @param array $allowed_extensions
147
	 * @param bool $exclude_in_message
148
	 * @param string $order_by
149
	 * @return array
150
	 */
151 7
	public function get_attachments($forum_id = 0, $allowed_extensions = array(), $exclude_in_message = true, $order_by = 'filetime DESC, post_msg_id ASC')
152
	{
153 7
		$this->store['attachments'] = array_filter($this->store['attachments']);
154
155 7
		$attachments = array();
156 7
		if ($this->_attachments_allowed($forum_id))
157 7
		{
158 6
			$sql = $this->_get_attachment_sql($allowed_extensions, $exclude_in_message, $order_by);
159 6
			$result = $this->db->sql_query($sql);
160
161 6
			while ($row = $this->db->sql_fetchrow($result))
162
			{
163 6
				$attachments[$row['post_msg_id']][] = $row;
164 6
			}
165 6
			$this->db->sql_freeresult($result);
166 6
		}
167 7
		$this->store['attachments'] = array();
168
169 7
		return $attachments;
170
	}
171
172
	/**
173
	 * Get topic tracking info
174
	 *
175
	 * @param int $forum_id
176
	 * @return array
177
	 */
178 5
	public function get_topic_tracking_info($forum_id = 0)
179
	{
180 5
		if (!sizeof($this->store['tracking']))
181 5
		{
182
			return array();
183
		}
184
185 5
		$tracking_info = $this->_get_tracking_info();
186
187 5
		return ($forum_id) ? (isset($tracking_info[$forum_id]) ? $tracking_info[$forum_id] : array()) : $tracking_info;
188
	}
189
190
	/**
191
	 * Returns an array of topic first post or last post ids
192
	 *
193
	 * @param string $first_or_last_post
194
	 * @return array
195
	 */
196 3
	public function get_topic_post_ids($first_or_last_post = 'first')
197
	{
198 3
		return (isset($this->store['post_ids'][$first_or_last_post])) ? $this->store['post_ids'][$first_or_last_post] : array();
199
	}
200
201
	/**
202
	 * Returns an array of topic first post or last post ids
203
	 */
204
	public function get_posters_info()
205
	{
206
		$this->store['poster_ids'] = array_filter(array_unique($this->store['poster_ids']));
207
208
		if (!sizeof($this->store['poster_ids']))
209
		{
210
			return array();
211
		}
212
213
		return $this->_get_user_data();
214
	}
215
216
	/**
217
	 * @param mixed $topic_first_or_last_post
218
	 * @param array $post_ids
219
	 * @param array $sql_array
220
	 * @return array
221
	 */
222 10
	private function _get_posts_sql_array($topic_first_or_last_post, array $post_ids, array $sql_array)
223
	{
224 10
		$sql_array = array_merge_recursive(
225
			array(
226 10
				'SELECT'	=> array('p.*'),
227 10
				'FROM'		=> array(POSTS_TABLE => 'p'),
228 10
				'WHERE'		=> $this->_get_post_data_where($post_ids, $topic_first_or_last_post),
229 10
				'ORDER_BY'	=> 'p.topic_id, p.post_time ASC',
230 10
			),
231
			$sql_array
232 10
		);
233
234 10
		$sql_array['SELECT'] = join(', ', array_filter($sql_array['SELECT']));
235 10
		$sql_array['WHERE'] = join(' AND ', array_filter($sql_array['WHERE']));
236
237 10
		return $sql_array;
238
	}
239
240
	/**
241
	 * @param array $post_ids
242
	 * @param string $topic_first_or_last_post
243
	 * @return array
244
	 */
245 10
	private function _get_post_data_where(array $post_ids, $topic_first_or_last_post)
246
	{
247 10
		$sql_where = array();
248
249 10
		if (sizeof($post_ids))
250 10
		{
251 2
			$sql_where[] = $this->db->sql_in_set('p.post_id', $post_ids);
252 2
		}
253 8
		else if (sizeof($this->store['topic']))
254 8
		{
255 3
			$this->_limit_posts_by_topic($sql_where, $topic_first_or_last_post);
256 3
		}
257
258 10
		$sql_where[] = $this->content_visibility->get_global_visibility_sql('post', $this->ex_fid_ary, 'p.');
259
260 10
		return $sql_where;
261
	}
262
263
	/**
264
	 * @param array $sql_where
265
	 * @param string $topic_first_or_last_post
266
	 */
267 3
	private function _limit_posts_by_topic(array &$sql_where, $topic_first_or_last_post)
268
	{
269 3
		$sql_where[] = $this->db->sql_in_set('p.topic_id', array_keys($this->store['topic']));
270
271
		if ($topic_first_or_last_post)
272 3
		{
273 3
			$sql_where[] = $this->db->sql_in_set('p.post_id', $this->get_topic_post_ids($topic_first_or_last_post));
274 3
		}
275 3
	}
276
277
	/**
278
	 * @return array
279
	 */
280 5
	private function _get_tracking_info()
281
	{
282 5
		$info = array();
283 5
		if ($this->_can_track_by_lastread())
284 5
		{
285 4
			$info = $this->_build_tracking_info('get_topic_tracking');
286 4
		}
287 1
		else if ($this->_can_track_anonymous())
288 1
		{
289 1
			$info = $this->_build_tracking_info('get_complete_topic_tracking');
290 1
		}
291
292 5
		return $info;
293
	}
294
295
	/**
296
	 * @param string $function
297
	 * @return array
298
	 */
299 5
	private function _build_tracking_info($function)
300
	{
301 5
		$tracking_info = array();
302 5
		foreach ($this->store['tracking'] as $fid => $forum)
303
		{
304 5
			$tracking_info[$fid] = call_user_func_array($function, array($fid, $forum['topic_list'], &$this->store['topic'], array($fid => $forum['mark_time'])));
305 5
		}
306
307 5
		return $tracking_info;
308
	}
309
310
	/**
311
	 * @return bool
312
	 */
313 5
	private function _can_track_by_lastread()
314
	{
315 5
		return ($this->config['load_db_lastread'] && $this->user->data['is_registered']) ? true : false;
316
	}
317
318
	/**
319
	 * @return bool
320
	 */
321 1
	private function _can_track_anonymous()
322
	{
323 1
		return ($this->config['load_anon_lastread'] || $this->user->data['is_registered']) ? true : false;
324
	}
325
326
	/**
327
	 * @param int $forum_id
328
	 * @return bool
329
	 */
330 7
	private function _attachments_allowed($forum_id)
331
	{
332 7
		return ($this->store['attachments'] && $this->_user_can_download_attachments($forum_id)) ? true : false;
333
	}
334
335
	/**
336
	 * @param int $forum_id
337
	 * @return bool
338
	 */
339 6
	private function _user_can_download_attachments($forum_id)
340
	{
341 6
		return ($this->auth->acl_get('u_download') && (!$forum_id || $this->auth->acl_get('f_download', $forum_id))) ? true : false;
342
	}
343
344
	/**
345
	 * @param array $allowed_extensions
346
	 * @param bool $exclude_in_message
347
	 * @param string $order_by
348
	 * @return string
349
	 */
350 6
	private function _get_attachment_sql($allowed_extensions, $exclude_in_message, $order_by)
351
	{
352
		return 'SELECT *
353 6
			FROM ' . ATTACHMENTS_TABLE . '
354 6
			WHERE ' . $this->db->sql_in_set('post_msg_id', $this->store['attachments']) .
355 6
				(($exclude_in_message) ? ' AND in_message = 0' : '') .
356 6
				(sizeof($allowed_extensions) ? ' AND ' . $this->db->sql_in_set('extension', $allowed_extensions) : '') . '
357 6
			ORDER BY ' . $order_by;
358
	}
359
360
	/**
361
	 * @return array
362
	 */
363
	private function _get_user_data()
364
	{
365
		$sql = 'SELECT *
366
			FROM ' . USERS_TABLE . '
367
			WHERE ' . $this->db->sql_in_set('user_id', $this->store['poster_ids']);
368
		$result = $this->db->sql_query($sql);
369
370
		$user_cache = array();
371
		while ($row = $this->db->sql_fetchrow($result))
372
		{
373
			$poster_id = $row['user_id'];
374
375
			$user_cache[$poster_id] = array(
376
				'user_type'					=> $row['user_type'],
377
				'user_inactive_reason'		=> $row['user_inactive_reason'],
378
379
				'joined'			=> $this->user->format_date($row['user_regdate'], 'M d, Y'),
380
				'posts'				=> $row['user_posts'],
381
				'warnings'			=> (isset($row['user_warnings'])) ? $row['user_warnings'] : 0,
382
				'allow_pm'			=> $row['user_allow_pm'],
383
				'avatar'			=> ($this->user->optionget('viewavatars')) ? phpbb_get_user_avatar($row) : '',
384
385
				'contact_user' 		=> $this->translator->lang('CONTACT_USER', get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['username'])),
386
				'search'			=> ($this->auth->acl_get('u_search')) ? append_sid("{$this->phpbb_root_path}search.$this->php_ext", "author_id=$poster_id&amp;sr=posts") : '',
387
388
				'username'			=> get_username_string('username', $poster_id, $row['username'], $row['user_colour']),
389
				'username_full'		=> get_username_string('full', $poster_id, $row['username'], $row['user_colour']),
390
				'user_colour'		=> get_username_string('colour', $poster_id, $row['username'], $row['user_colour']),
391
				'user_profile'		=> get_username_string('profile', $poster_id, $row['username'], $row['user_colour']),
392
			);
393
394
			$user_cache[$poster_id] += $this->_get_user_rank($row);
395
			$user_cache[$poster_id] += $this->_get_user_email($row);
396
		}
397
		$this->db->sql_freeresult($result);
398
399
		return $user_cache;
400
	}
401
402
	/**
403
	 * @param array $row
404
	 */
405
	private function _get_user_rank(array $row)
406
	{
407
		if (!function_exists('phpbb_get_user_rank'))
408
		{
409
			include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
410
		}
411
412
		$user_rank_data = phpbb_get_user_rank($row, $row['user_posts']);
413
414
		if (!empty($user_rank_data))
415
		{
416
			return array(
417
				'rank_title'		=> $user_rank_data['title'],
418
				'rank_image'		=> $user_rank_data['img'],
419
				'rank_image_src'	=> $user_rank_data['img_src'],
420
			);
421
		}
422
		else
423
		{
424
			return array(
425
				'rank_title'		=> '',
426
				'rank_image'		=> '',
427
				'rank_image_src'	=> '',
428
			);
429
		}
430
	}
431
432
	/**
433
	 * @param array $row
434
	 */
435
	private function _get_user_email(array $row)
436
	{
437
		$email = '';
438
		if ((!empty($row['user_allow_viewemail']) && $this->auth->acl_get('u_sendemail')) || $this->auth->acl_get('a_email'))
439
		{
440
			$email = ($this->config['board_email_form'] && $this->config['email_enable']) ? append_sid("{$this->phpbb_root_path}memberlist.$this->php_ext", 'mode=email&amp;u=' . $row['user_id']) : (($this->config['board_hide_emails'] && !$this->auth->acl_get('a_email')) ? '' : 'mailto:' . $row['user_email']);
441
		}
442
443
		return array('email' => $email);
444
	}
445
}
446