Completed
Push — master ( 4a8b55...d86878 )
by Daniel
11:13
created

data::get_attachments()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

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