Completed
Push — develop ( 7acac6...1f96ef )
by Daniel
10:51
created

data::get_post_data()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 18
ccs 13
cts 13
cp 1
rs 9.4285
cc 2
eloc 12
nc 2
nop 5
crap 2
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 29
	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 = 10800)
44
	{
45 29
		parent::__construct($auth, $config, $content_visibility, $db, $user, $cache_time);
46
47 29
		$this->auth = $auth;
48 29
		$this->config = $config;
49 29
		$this->content_visibility = $content_visibility;
50 29
		$this->db = $db;
51 29
		$this->user = $user;
52 29
		$this->user_data = $user_data;
53 29
	}
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 mixed|false $limit
79
	 * @param int $start
80
	 * @return array
81
	 */
82 12
	public function get_topic_data($limit = false, $start = 0)
83
	{
84 12
		$sql = $this->db->sql_build_query('SELECT', $this->store['sql_array']);
85 12
		$result = $this->db->sql_query_limit($sql, $limit, $start, $this->cache_time);
86
87 12
		while ($row = $this->db->sql_fetchrow($result))
88
		{
89 10
			$this->store['topic'][$row['topic_id']] = $row;
90
91 10
			$this->store['tracking'][$row['forum_id']]['topic_list'][] = $row['topic_id'];
92 10
			$this->store['tracking'][$row['forum_id']]['mark_time'] =& $row['forum_mark_time'];
93 10
			$this->store['post_ids']['first'][] = $row['topic_first_post_id'];
94 10
			$this->store['post_ids']['last'][] = $row['topic_last_post_id'];
95 10
			$this->store['poster_ids'][] = $row['topic_poster'];
96 10
			$this->store['poster_ids'][] = $row['topic_last_poster_id'];
97 10
		}
98 12
		$this->db->sql_freeresult($result);
99
100 12
		return $this->store['topic'];
101
	}
102
103
	/**
104
	 * Get post data
105
	 *
106
	 * @param mixed|false $topic_first_or_last_post (first|last)
107
	 * @param array $post_ids
108
	 * @param bool|false $limit
109
	 * @param int $start
110
	 * @param array $sql_array
111
	 * @return array
112
	 */
113 9
	public function get_post_data($topic_first_or_last_post = false, $post_ids = array(), $limit = false, $start = 0, $sql_array = array())
114
	{
115 9
		$sql = $this->db->sql_build_query('SELECT_DISTINCT', $this->_get_posts_sql_array($topic_first_or_last_post, $post_ids, $sql_array));
116 9
		$result = $this->db->sql_query_limit($sql, $limit, $start, $this->cache_time);
117
118 9
		$post_data = array();
119 9
		while ($row = $this->db->sql_fetchrow($result))
120
		{
121 8
			$post_data[$row['topic_id']][$row['post_id']] = $row;
122 8
			$this->store['poster_ids'][] = $row['poster_id'];
123 8
			$this->store['poster_ids'][] = $row['post_edit_user'];
124 8
			$this->store['poster_ids'][] = $row['post_delete_user'];
125 8
			$this->store['attachments'][] = $row['post_id'];
126 8
		}
127 9
		$this->db->sql_freeresult($result);
128
129 9
		return $post_data;
130
	}
131
132
	/**
133
	 * Get attachments...
134
	 *
135
	 * @param int $forum_id
136
	 * @param array $allowed_extensions
137
	 * @param mixed|bool $limit
138
	 * @param bool $exclude_in_message
139
	 * @param string $order_by
140
	 * @return array
141
	 */
142 7
	public function get_attachments($forum_id = 0, $allowed_extensions = array(), $limit = false, $exclude_in_message = true, $order_by = 'filetime DESC')
143
	{
144 7
		$attach_ids = array_filter($this->store['attachments']);
145
146 7
		$data = array();
147 7
		if (sizeof($attach_ids))
148 7
		{
149 6
			$attachments = new attachments($this->auth, $this->db);
150 6
			$data = $attachments->get_attachments($forum_id, $attach_ids, $allowed_extensions, $limit, $exclude_in_message, $order_by);
151 6
		}
152
153 7
		return $data;
154
	}
155
156
	/**
157
	 * Get topic tracking info
158
	 *
159
	 * @param int $forum_id
160
	 * @return array
161
	 */
162 5
	public function get_topic_tracking_info($forum_id = 0)
163
	{
164 5
		$tracking_info = array();
165 5
		if (sizeof($this->store['tracking']))
166 5
		{
167 5
			$tracker = new tracker($this->config, $this->user);
168 5
			$tracking_info = $tracker->get_tracking_info($this->store['tracking'], $this->store['topic']);
169 5
		}
170
171 5
		return ($forum_id) ? (isset($tracking_info[$forum_id]) ? $tracking_info[$forum_id] : array()) : $tracking_info;
172
	}
173
174
	/**
175
	 * Returns an array of topic first post or last post ids
176
	 *
177
	 * @param string $first_or_last_post
178
	 * @return array
179
	 */
180 2
	public function get_topic_post_ids($first_or_last_post = 'first')
181
	{
182 2
		return (isset($this->store['post_ids'][$first_or_last_post])) ? $this->store['post_ids'][$first_or_last_post] : array();
183
	}
184
185
	/**
186
	 * Returns an array of topic first post or last post ids
187
	 */
188 5
	public function get_posters_info()
189
	{
190 5
		$this->store['poster_ids'] = array_filter(array_keys(array_flip($this->store['poster_ids'])));
191
192 5
		$info = array();
193 5
		if (sizeof($this->store['poster_ids']))
194 5
		{
195 5
			$info = $this->user_data->get_users($this->store['poster_ids']);
196 5
		}
197
198 5
		return $info;
199
	}
200
201
	/**
202
	 * @param mixed $topic_first_or_last_post
203
	 * @param array $post_ids
204
	 * @param array $sql_array
205
	 * @return array
206
	 */
207 9
	private function _get_posts_sql_array($topic_first_or_last_post, array $post_ids, array $sql_array)
208
	{
209 9
		$sql_array = array_merge_recursive(
210
			array(
211 9
				'SELECT'	=> array('p.*'),
212 9
				'FROM'		=> array(POSTS_TABLE => 'p'),
213 9
				'WHERE'		=> $this->_get_post_data_where($post_ids, $topic_first_or_last_post),
214 9
				'ORDER_BY'	=> 'p.post_time DESC',
215 9
			),
216
			$sql_array
217 9
		);
218
219 9
		$sql_array['SELECT'] = join(', ', array_filter($sql_array['SELECT']));
220 9
		$sql_array['WHERE'] = join(' AND ', array_filter($sql_array['WHERE']));
221
222 9
		return $sql_array;
223
	}
224
225
	/**
226
	 * @param array $post_ids
227
	 * @param string $topic_first_or_last_post
228
	 * @return array
229
	 */
230 9
	private function _get_post_data_where(array $post_ids, $topic_first_or_last_post)
231
	{
232 9
		$sql_where = array();
233
234 9
		if (sizeof($post_ids))
235 9
		{
236 2
			$sql_where[] = $this->db->sql_in_set('p.post_id', $post_ids);
237 2
		}
238 7
		else if (sizeof($this->store['topic']))
239 7
		{
240 2
			$this->_limit_posts_by_topic($sql_where, $topic_first_or_last_post);
241 2
		}
242
243 9
		$sql_where[] = $this->content_visibility->get_global_visibility_sql('post', $this->ex_fid_ary, 'p.');
244
245 9
		return $sql_where;
246
	}
247
248
	/**
249
	 * @param array $sql_where
250
	 * @param string $topic_first_or_last_post
251
	 */
252 2
	private function _limit_posts_by_topic(array &$sql_where, $topic_first_or_last_post)
253
	{
254 2
		$sql_where[] = $this->db->sql_in_set('p.topic_id', array_keys($this->store['topic']));
255
256
		if ($topic_first_or_last_post)
257 2
		{
258 2
			$sql_where[] = $this->db->sql_in_set('p.post_id', $this->get_topic_post_ids($topic_first_or_last_post));
259 2
		}
260 2
	}
261
}
262