Completed
Push — develop ( ddbdd6...7d7ff1 )
by Daniel
09:09
created

data::get_post_data()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4.0032

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 16
cts 17
cp 0.9412
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 15
nc 3
nop 5
crap 4.0032
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
	/** @var \blitze\sitemaker\services\users\data */
30
	protected $user_data;
31
32
	/**
33
	 * Constructor
34
	 *
35
	 * @param \phpbb\auth\auth						$auth					Auth object
36
	 * @param \phpbb\config\config					$config					Config object
37
	 * @param \phpbb\content_visibility				$content_visibility		Content visibility
38
	 * @param \phpbb\db\driver\driver_interface		$db     				Database connection
39
	 * @param \phpbb\user							$user					User object
40
	 * @param \blitze\sitemaker\services\users\data	$user_data				Sitemaker User data object
41
	 * @param integer								$cache_time				Cache results for 3 hours by default
42
	 */
43 31
	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, \blitze\sitemaker\services\users\data $user_data, $cache_time)
44
	{
45 31
		parent::__construct($auth, $config, $content_visibility, $db, $user, $cache_time);
46
47 31
		$this->auth = $auth;
48 31
		$this->config = $config;
49 31
		$this->content_visibility = $content_visibility;
50 31
		$this->db = $db;
51 31
		$this->user = $user;
52 31
		$this->user_data = $user_data;
53 31
	}
54
55
	/**
56
	 * Get topics count
57
	 *
58
	 * @return integer
59
	 */
60 1
	public function get_topics_count()
61
	{
62
		$sql_array = array(
63 1
			'SELECT'	=> 'COUNT(*) AS total_topics',
64 1
			'FROM'		=> $this->store['sql_array']['FROM'],
65 1
			'WHERE'		=> $this->store['sql_array']['WHERE'],
66 1
		);
67 1
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
68 1
		$result = $this->db->sql_query($sql);
69 1
		$total_topics = $this->db->sql_fetchfield('total_topics');
70 1
		$this->db->sql_freeresult($result);
71
72 1
		return $total_topics;
73
	}
74
75
	/**
76
	 * Get topic data
77
	 *
78
	 * @param int|false $limit
79
	 * @param int $start
80
	 * @return array
81
	 */
82 13
	public function get_topic_data($limit = false, $start = 0)
83
	{
84
		// Topics table need to be the last in the chain
85 13
		$this->store['sql_array']['FROM'][TOPICS_TABLE] = 't';
86
87 13
		$sql = $this->db->sql_build_query('SELECT', $this->store['sql_array']);
88 13
		$result = $this->db->sql_query_limit($sql, $limit, $start, $this->cache_time);
89
90 13
		while ($row = $this->db->sql_fetchrow($result))
91
		{
92 11
			$this->store['topic'][$row['topic_id']] = $row;
93
94 11
			$this->store['tracking'][$row['forum_id']]['topic_list'][] = $row['topic_id'];
95 11
			$this->store['tracking'][$row['forum_id']]['mark_time'] =& $row['forum_mark_time'];
96 11
			$this->store['post_ids']['first'][] = $row['topic_first_post_id'];
97 11
			$this->store['post_ids']['last'][] = $row['topic_last_post_id'];
98 11
			$this->store['poster_ids'][] = $row['topic_poster'];
99 11
			$this->store['poster_ids'][] = $row['topic_last_poster_id'];
100 11
		}
101 13
		$this->db->sql_freeresult($result);
102
103 13
		return $this->store['topic'];
104
	}
105
106
	/**
107
	 * Get post data
108
	 *
109
	 * @param mixed|false $topic_first_or_last_post (first|last)
110
	 * @param array $post_ids
111
	 * @param bool|false $limit
112
	 * @param int $start
113
	 * @param array $sql_array
114
	 * @return array
115
	 */
116 11
	public function get_post_data($topic_first_or_last_post = false, $post_ids = array(), $limit = false, $start = 0, $sql_array = array())
117
	{
118 11
		$post_data = array();
119 11
		if ($topic_first_or_last_post && !sizeof($this->store['topic']))
120 11
		{
121
			return $post_data;
122
		}
123
124 11
		$sql_array = $this->_get_posts_sql_array($topic_first_or_last_post, $post_ids, $sql_array);
125 11
		$sql = $this->db->sql_build_query('SELECT_DISTINCT', $sql_array);
126 11
		$result = $this->db->sql_query_limit($sql, $limit, $start, $this->cache_time);
127
128 11
		while ($row = $this->db->sql_fetchrow($result))
129
		{
130 10
			$post_data[$row['topic_id']][$row['post_id']] = $row;
131 10
			$this->store['poster_ids'][] = $row['poster_id'];
132 10
			$this->store['poster_ids'][] = $row['post_edit_user'];
133 10
			$this->store['poster_ids'][] = $row['post_delete_user'];
134 10
			$this->store['attachments'][] = $row['post_id'];
135 10
		}
136 11
		$this->db->sql_freeresult($result);
137
138 11
		return $post_data;
139
	}
140
141
	/**
142
	 * Get attachments...
143
	 *
144
	 * @param int $forum_id
145
	 * @param array $allowed_extensions
146
	 * @param mixed|bool $limit
147
	 * @param bool $exclude_in_message
148
	 * @param string $order_by
149
	 * @return array
150
	 */
151 8
	public function get_attachments($forum_id = 0, $allowed_extensions = array(), $limit = false, $exclude_in_message = true, $order_by = 'filetime DESC')
152
	{
153 8
		$data = array();
154 8
		if (sizeof($this->store['attachments']))
155 8
		{
156 7
			$attachments = new attachments($this->auth, $this->db);
157 7
			$data = $attachments->get_attachments($forum_id, $this->store['attachments'], $allowed_extensions, $limit, $exclude_in_message, $order_by);
158 7
		}
159
160 8
		return $data;
161
	}
162
163
	/**
164
	 * Get topic tracking info
165
	 *
166
	 * @param int $forum_id
167
	 * @return array
168
	 */
169 6
	public function get_topic_tracking_info($forum_id = 0)
170
	{
171 6
		$tracking_info = array();
172 6
		if (sizeof($this->store['tracking']))
173 6
		{
174 6
			$tracker = new tracker($this->config, $this->user);
175 6
			$tracking_info = $tracker->get_tracking_info($this->store['tracking'], $this->store['topic']);
176 6
		}
177
178 6
		return ($forum_id) ? (isset($tracking_info[$forum_id]) ? $tracking_info[$forum_id] : array()) : $tracking_info;
179
	}
180
181
	/**
182
	 * Returns an array of topic first post or last post ids
183
	 *
184
	 * @param string $first_or_last_post
185
	 * @return array
186
	 */
187 3
	public function get_topic_post_ids($first_or_last_post = 'first')
188
	{
189 3
		return (isset($this->store['post_ids'][$first_or_last_post])) ? $this->store['post_ids'][$first_or_last_post] : array();
190
	}
191
192
	/**
193
	 * Returns an array of topic first post or last post ids
194
	 */
195 6
	public function get_posters_info()
196
	{
197 6
		$this->store['poster_ids'] = array_filter(array_keys(array_flip($this->store['poster_ids'])));
198
199 6
		$info = array();
200 6
		if (sizeof($this->store['poster_ids']))
201 6
		{
202 6
			$info = $this->user_data->get_users($this->store['poster_ids']);
203 6
		}
204
205 6
		return $info;
206
	}
207
208
	/**
209
	 * @param mixed $topic_first_or_last_post
210
	 * @param array $post_ids
211
	 * @param array $sql_array
212
	 * @return array
213
	 */
214 11
	private function _get_posts_sql_array($topic_first_or_last_post, array $post_ids, array $sql_array)
215
	{
216 11
		$sql_array = array_merge_recursive(
217
			array(
218 11
				'SELECT'	=> array('p.*'),
219 11
				'FROM'		=> array(POSTS_TABLE => 'p'),
220 11
				'WHERE'		=> $this->_get_post_data_where($post_ids, $topic_first_or_last_post),
221 11
			),
222
			$sql_array
223 11
		);
224
225 11
		$sql_array['SELECT'] = (is_array($sql_array['SELECT'])) ? join(', ', array_filter($sql_array['SELECT'])) : $sql_array['SELECT'];
226 11
		$sql_array['WHERE'] = (is_array($sql_array['WHERE'])) ? join(' AND ', array_filter($sql_array['WHERE'])) : $sql_array['WHERE'];
227 11
		$sql_array['ORDER_BY'] = ($sql_array['ORDER_BY']) ?: 'p.post_time DESC';
228
229 11
		return $sql_array;
230
	}
231
232
	/**
233
	 * @param array $post_ids
234
	 * @param string $topic_first_or_last_post
235
	 * @return array
236
	 */
237 11
	private function _get_post_data_where(array $post_ids, $topic_first_or_last_post)
238
	{
239 11
		$sql_where = array();
240
241 11
		if (sizeof($post_ids))
242 11
		{
243 2
			return $this->db->sql_in_set('p.post_id', array_map('intval', $post_ids));
244
		}
245 9
		else if (sizeof($this->store['topic']))
246 9
		{
247 3
			$this->_limit_posts_by_topic($sql_where, $topic_first_or_last_post);
248 3
		}
249
250 9
		$sql_where[] = $this->content_visibility->get_global_visibility_sql('post', $this->ex_fid_ary, 'p.');
251
252 9
		return $sql_where;
253
	}
254
255
	/**
256
	 * @param array $sql_where
257
	 * @param string $topic_first_or_last_post
258
	 */
259 3
	private function _limit_posts_by_topic(array &$sql_where, $topic_first_or_last_post)
260
	{
261 3
		$sql_where[] = $this->db->sql_in_set('p.topic_id', array_keys($this->store['topic']));
262
263
		if ($topic_first_or_last_post)
264 3
		{
265 3
			$sql_where[] = $this->db->sql_in_set('p.post_id', array_map('intval', $this->get_topic_post_ids($topic_first_or_last_post)));
266 3
		}
267 3
	}
268
}
269