Completed
Push — master ( 871ee2...b5dc49 )
by Daniel
08:08
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 10
CRAP Score 1

Importance

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