Completed
Push — master ( 73b87b...be40cf )
by Daniel
10:47
created

data::get_topics_count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
ccs 0
cts 13
cp 0
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
crap 2
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
class data extends query_builder
13
{
14
	/** @var \phpbb\auth\auth */
15
	protected $auth;
16
17
	/** @var \phpbb\config\config */
18
	protected $config;
19
20
	/** @var \phpbb\content_visibility */
21
	protected $content_visibility;
22
23
	/** @var \phpbb\db\driver\driver_interface */
24
	protected $db;
25
26
	/** @var \phpbb\user */
27
	protected $user;
28
29
	/**
30
	 * Constructor
31
	 *
32
	 * @param \phpbb\auth\auth					$auth					Auth object
33
	 * @param \phpbb\config\config				$config					Config object
34
	 * @param \phpbb\content_visibility			$content_visibility		Content visibility
35
	 * @param \phpbb\db\driver\driver_interface	$db     				Database connection
36
	 * @param \phpbb\user						$user					User object
37
	 * @param integer							$cache_time				Cache results for 3 hours by default
38
	 */
39
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\content_visibility $content_visibility, \phpbb\db\driver\driver_interface $db, \phpbb\user $user, $cache_time = 10800)
40
	{
41
		parent::__construct($auth, $config, $content_visibility, $db, $user, $cache_time);
42
43
		$this->auth = $auth;
44
		$this->config = $config;
45
		$this->content_visibility = $content_visibility;
46
		$this->db = $db;
47
		$this->user = $user;
48
	}
49
50
	/**
51
	 * Get topics count
52
	 *
53
	 * @return integer
54
	 */
55
	public function get_topics_count()
56
	{
57
		$sql_array = array(
58
			'SELECT'	=> 'COUNT(*) AS total_topics',
59
			'FROM'		=> $this->store['sql_array']['FROM'],
60
			'WHERE'		=> $this->store['sql_array']['WHERE'],
61
		);
62
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
63
		$result = $this->db->sql_query($sql);
64
		$total_topics = $this->db->sql_fetchfield('total_topics');
65
		$this->db->sql_freeresult($result);
66
67
		return $total_topics;
68
	}
69
70
	/**
71
	 * Get topic data
72
	 *
73
	 * @param mixed|false $limit
74
	 * @param int $start
75
	 * @return array
76
	 */
77
	public function get_topic_data($limit = false, $start = 0)
78
	{
79
		$sql = $this->db->sql_build_query('SELECT', $this->store['sql_array']);
80
		$result = $this->db->sql_query_limit($sql, $limit, $start, $this->cache_time);
81
82
		while ($row = $this->db->sql_fetchrow($result))
83
		{
84
			$this->store['topic'][$row['topic_id']] = $row;
85
86
			$this->store['tracking'][$row['forum_id']]['topic_list'][] = $row['topic_id'];
87
			$this->store['tracking'][$row['forum_id']]['mark_time'] =& $row['forum_mark_time'];
88
			$this->store['post_ids']['first'][] = $row['topic_first_post_id'];
89
			$this->store['post_ids']['last'][] = $row['topic_last_post_id'];
90
		}
91
		$this->db->sql_freeresult($result);
92
93
		return $this->store['topic'];
94
	}
95
96
	/**
97
	 * Get post data
98
	 *
99
	 * @param mixed|false $topic_first_or_last_post (first|last)
100
	 * @param array $post_ids
101
	 * @param bool|false $limit
102
	 * @param int $start
103
	 * @param array $sql_array
104
	 * @return array
105
	 */
106
	public function get_post_data($topic_first_or_last_post = false, $post_ids = array(), $limit = false, $start = 0, $sql_array = array())
107
	{
108
		$sql = $this->db->sql_build_query('SELECT_DISTINCT', $this->_get_posts_sql_array($topic_first_or_last_post, $post_ids, $sql_array));
109
		$result = $this->db->sql_query_limit($sql, $limit, $start, $this->cache_time);
110
111
		$post_data = array();
112
		while ($row = $this->db->sql_fetchrow($result))
113
		{
114
			$post_data[$row['topic_id']][$row['post_id']] = $row;
115
			$this->store['poster_ids'][] = $row['poster_id'];
116
			$this->store['poster_ids'][] = $row['post_edit_user'];
117
			$this->store['poster_ids'][] = $row['post_delete_user'];
118
			$this->store['attachments'][] = $row['post_id'];
119
		}
120
		$this->db->sql_freeresult($result);
121
122
		return $post_data;
123
	}
124
125
	/**
126
	 * Get attachments...
127
	 *
128
	 * @param int $forum_id
129
	 * @param array $allowed_extensions
130
	 * @param mixed|bool $limit
131
	 * @param bool $exclude_in_message
132
	 * @param string $order_by
133
	 * @return array
134
	 */
135
	public function get_attachments($forum_id = 0, $allowed_extensions = array(), $limit = false, $exclude_in_message = true, $order_by = 'filetime DESC')
136
	{
137
		$this->store['attachments'] = array_filter($this->store['attachments']);
138
139
		$attachments = array();
140
		if ($this->_attachments_allowed($forum_id))
141
		{
142
			$sql = $this->_get_attachment_sql($allowed_extensions, $exclude_in_message, $order_by);
143
			$result = $this->db->sql_query_limit($sql, $limit);
144
145
			while ($row = $this->db->sql_fetchrow($result))
146
			{
147
				$attachments[$row['post_msg_id']][] = $row;
148
			}
149
			$this->db->sql_freeresult($result);
150
		}
151
		$this->store['attachments'] = array();
152
153
		return $attachments;
154
	}
155
156
	/**
157
	 * Get topic tracking info
158
	 *
159
	 * @param int $forum_id
160
	 * @return array
161
	 */
162
	public function get_topic_tracking_info($forum_id = 0)
163
	{
164
		if (!sizeof($this->store['tracking']))
165
		{
166
			return array();
167
		}
168
169
		$tracking_info = $this->_get_tracking_info();
170
171
		return ($forum_id) ? (isset($tracking_info[$forum_id]) ? $tracking_info[$forum_id] : array()) : $tracking_info;
172
	}
173
174
	/**
175
	 * Returns an array of topic first post or last post ids
176
	 *
177
	 * @param string $first_or_last_post
178
	 * @return array
179
	 */
180
	public function get_topic_post_ids($first_or_last_post = 'first')
181
	{
182
		return (isset($this->store['post_ids'][$first_or_last_post])) ? $this->store['post_ids'][$first_or_last_post] : array();
183
	}
184
185
	/**
186
	 * @param mixed $topic_first_or_last_post
187
	 * @param array $post_ids
188
	 * @param array $sql_array
189
	 * @return array
190
	 */
191
	private function _get_posts_sql_array($topic_first_or_last_post, array $post_ids, array $sql_array)
192
	{
193
		$sql_array = array_merge_recursive(
194
			array(
195
				'SELECT'	=> array('p.*'),
196
				'FROM'		=> array(POSTS_TABLE => 'p'),
197
				'WHERE'		=> $this->_get_post_data_where($post_ids, $topic_first_or_last_post),
198
				'ORDER_BY'	=> 'p.post_time DESC',
199
			),
200
			$sql_array
201
		);
202
203
		$sql_array['SELECT'] = join(', ', array_filter($sql_array['SELECT']));
204
		$sql_array['WHERE'] = join(' AND ', array_filter($sql_array['WHERE']));
205
206
		return $sql_array;
207
	}
208
209
	/**
210
	 * @param array $post_ids
211
	 * @param string $topic_first_or_last_post
212
	 * @return array
213
	 */
214
	private function _get_post_data_where(array $post_ids, $topic_first_or_last_post)
215
	{
216
		$sql_where = array();
217
218
		if (sizeof($post_ids))
219
		{
220
			$sql_where[] = $this->db->sql_in_set('p.post_id', $post_ids);
221
		}
222
		else if (sizeof($this->store['topic']))
223
		{
224
			$this->_limit_posts_by_topic($sql_where, $topic_first_or_last_post);
225
		}
226
227
		$sql_where[] = $this->content_visibility->get_global_visibility_sql('post', $this->ex_fid_ary, 'p.');
228
229
		return $sql_where;
230
	}
231
232
	/**
233
	 * @param array $sql_where
234
	 * @param string $topic_first_or_last_post
235
	 */
236
	private function _limit_posts_by_topic(array &$sql_where, $topic_first_or_last_post)
237
	{
238
		$sql_where[] = $this->db->sql_in_set('p.topic_id', array_keys($this->store['topic']));
239
240
		if ($topic_first_or_last_post)
241
		{
242
			$sql_where[] = $this->db->sql_in_set('p.post_id', $this->get_topic_post_ids($topic_first_or_last_post));
243
		}
244
	}
245
246
	/**
247
	 * @return array
248
	 */
249
	private function _get_tracking_info()
250
	{
251
		$info = array();
252
		if ($this->_can_track_by_lastread())
253
		{
254
			$info = $this->_build_tracking_info('get_topic_tracking');
255
		}
256
		else if ($this->_can_track_anonymous())
257
		{
258
			$info = $this->_build_tracking_info('get_complete_topic_tracking');
259
		}
260
261
		return $info;
262
	}
263
264
	/**
265
	 * @param string $function
266
	 * @return array
267
	 */
268
	private function _build_tracking_info($function)
269
	{
270
		$tracking_info = array();
271
		foreach ($this->store['tracking'] as $fid => $forum)
272
		{
273
			$tracking_info[$fid] = call_user_func_array($function, array($fid, $forum['topic_list'], &$this->store['topic'], array($fid => $forum['mark_time'])));
274
		}
275
276
		return $tracking_info;
277
	}
278
279
	/**
280
	 * @return bool
281
	 */
282
	private function _can_track_by_lastread()
283
	{
284
		return ($this->config['load_db_lastread'] && $this->user->data['is_registered']) ? true : false;
285
	}
286
287
	/**
288
	 * @return bool
289
	 */
290
	private function _can_track_anonymous()
291
	{
292
		return ($this->config['load_anon_lastread'] || $this->user->data['is_registered']) ? true : false;
293
	}
294
295
	/**
296
	 * @param int $forum_id
297
	 * @return bool
298
	 */
299
	private function _attachments_allowed($forum_id)
300
	{
301
		return ($this->store['attachments'] && $this->_user_can_download_attachments($forum_id)) ? true : false;
302
	}
303
304
	/**
305
	 * @param int $forum_id
306
	 * @return bool
307
	 */
308
	private function _user_can_download_attachments($forum_id)
309
	{
310
		return ($this->auth->acl_get('u_download') && (!$forum_id || $this->auth->acl_get('f_download', $forum_id))) ? true : false;
311
	}
312
313
	/**
314
	 * @param array $allowed_extensions
315
	 * @param bool $exclude_in_message
316
	 * @param string $order_by
317
	 * @return string
318
	 */
319
	private function _get_attachment_sql($allowed_extensions, $exclude_in_message, $order_by)
320
	{
321
		return 'SELECT *
322
			FROM ' . ATTACHMENTS_TABLE . '
323
			WHERE ' . $this->db->sql_in_set('post_msg_id', $this->store['attachments']) .
324
				(($exclude_in_message) ? ' AND in_message = 0' : '') .
325
				(sizeof($allowed_extensions) ? ' AND ' . $this->db->sql_in_set('extension', $allowed_extensions) : '') . '
326
			ORDER BY ' . $order_by;
327
	}
328
}
329