Completed
Push — master ( 9866a1...02e5e5 )
by Daniel
08:18
created

data::_can_track_anonymous()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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