Completed
Push — master ( c96b51...1deced )
by Daniel
08:48
created

data::fetch_tracking_info()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

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