data::get_topic_data()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

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