Completed
Push — develop ( 414e20...ddbdd6 )
by Daniel
09:18
created

query_builder   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 346
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 79.84%

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 0
dl 0
loc 346
ccs 103
cts 129
cp 0.7984
rs 9.3999
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B query() 0 24 2
A fetch_forum() 0 6 1
A fetch_topic() 0 6 1
A fetch_topic_poster() 0 6 1
A fetch_topic_type() 0 9 2
B fetch_watch_status() 0 24 2
A fetch_bookmark_status() 0 13 3
A fetch_tracking_info() 0 20 3
A fetch_date_range() 0 9 4
A fetch_custom() 0 6 1
A set_sorting() 0 6 1
A build() 0 18 2
A get_sql_array() 0 4 1
A _set_cache_time() 0 7 2
A _fetch() 0 7 3
A _set_topic_visibility() 0 8 2
A _reset() 0 11 1
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 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 array */
30
	protected $store;
31
32
	/** @var array */
33
	protected $ex_fid_ary;
34
35
	/** @var integer */
36
	protected $cache_time;
37
38
	/**
39
	 * Constructor
40
	 *
41
	 * @param \phpbb\auth\auth					$auth					Auth object
42
	 * @param \phpbb\config\config				$config					Config object
43
	 * @param \phpbb\content_visibility			$content_visibility		Content visibility
44
	 * @param \phpbb\db\driver\driver_interface	$db     				Database connection
45
	 * @param \phpbb\user						$user					User object
46
	 * @param integer							$cache_time				Cache results for 3 hours by default
47
	 */
48 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, $cache_time = 10800)
49
	{
50 31
		$this->auth = $auth;
51 31
		$this->config = $config;
52 31
		$this->content_visibility = $content_visibility;
53 31
		$this->db = $db;
54 31
		$this->user = $user;
55 31
		$this->cache_time = $cache_time;
56
57 31
		$this->ex_fid_ary = array_unique(array_keys($this->auth->acl_getf('!f_read', true)));
58 31
	}
59
60
	/**
61
	 * Begin query
62
	 *
63
	 * @param bool $track_topics
64
	 * @return $this
65
	 */
66 22
	public function query($track_topics = true)
67
	{
68 22
		$this->_reset();
69
70 22
		$this->store['sql_array'] = array(
71 22
			'SELECT'	=> array('t.*, f.*'),
72
73 22
			'FROM'		=> array(FORUMS_TABLE => 'f'),
74
75 22
			'LEFT_JOIN'	=> array(),
76
77 22
			'WHERE'		=> array(),
78
		);
79
80
		// Topics table need to be the last in the chain
81 22
		$this->store['sql_array']['FROM'][TOPICS_TABLE] = 't';
82
83
		if ($track_topics)
84 22
		{
85 7
			$this->fetch_tracking_info();
86 7
		}
87
88 22
		return $this;
89
	}
90
91
	/**
92
	 * Fetch Forum by id(s)
93
	 *
94
	 * @param $forum_id
95
	 * @return $this
96
	 */
97 17
	public function fetch_forum($forum_id)
98
	{
99 17
		$this->_fetch($forum_id, 'f.forum_id');
100
101 17
		return $this;
102
	}
103
104
	/**
105
	 * Fetch Topic by id(s)
106
	 *
107
	 * @param mixed $topic_id	Limit by topic id: single id or array of topic ids
108
	 * @return $this
109
	 */
110 11
	public function fetch_topic($topic_id)
111
	{
112 11
		$this->_fetch($topic_id, 't.topic_id');
113
114 11
		return $this;
115
	}
116
117
	/**
118
	 * Fetch Topic by Poster id(s)
119
	 *
120
	 * @param mixed $user_id	User id of topic poster: single id or array of user ids
121
	 * @return $this
122
	 */
123 3
	public function fetch_topic_poster($user_id)
124
	{
125 3
		$this->_fetch($user_id, 't.topic_poster');
126
127 3
		return $this;
128
	}
129
130
	/**
131
	 * Fetch by Topic Type
132
	 *
133
	 * @param array $topic_type
134
	 * @return $this
135
	 */
136 17
	public function fetch_topic_type(array $topic_type)
137
	{
138 17
		if (sizeof($topic_type))
139 17
		{
140 5
			$this->store['sql_array']['WHERE'][] = $this->db->sql_in_set('t.topic_type', $topic_type);
141 5
		}
142
143 17
		return $this;
144
	}
145
146
	/**
147
	 * Fetch Topic Watch info
148
	 *
149
	 * @param $type
150
	 * @return $this
151
	 */
152
	public function fetch_watch_status($type = 'topic')
153
	{
154
		if ($this->user->data['is_registered'])
155
		{
156
			$keys = array(
157
				'forum'	=> array(
158
					'table'	=> FORUMS_WATCH_TABLE,
159
					'cond'	=> 'ws.forum_id = f.forum_id',
160
				),
161
				'topic'	=> array(
162
					'table'	=> TOPICS_WATCH_TABLE,
163
					'cond'	=> 'ws.topic_id = t.topic_id',
164
				),
165
			);
166
167
			$this->store['sql_array']['SELECT'][] = 'ws.notify_status';
168
			$this->store['sql_array']['LEFT_JOIN'][] = array(
169
				'FROM'	=> array($keys[$type]['table'] => 'ws'),
170
				'ON'	=> $keys[$type]['cond'] . ' AND ws.user_id = ' . (int) $this->user->data['user_id'],
171
			);
172
		}
173
174
		return $this;
175
	}
176
177
	/**
178
	 * Fetch Topic Bookmark Info
179
	 *
180
	 * @return $this
181
	 */
182
	public function fetch_bookmark_status()
183
	{
184
		if ($this->user->data['is_registered'] && $this->config['allow_bookmarks'])
185
		{
186
			$this->store['sql_array']['SELECT'][] = 'bm.topic_id as bookmarked';
187
			$this->store['sql_array']['LEFT_JOIN'][] = array(
188
				'FROM'	=> array(BOOKMARKS_TABLE => 'bm'),
189
				'ON'	=> 'bm.user_id = ' . (int) $this->user->data['user_id'] . ' AND t.topic_id = bm.topic_id'
190
			);
191
		}
192
193
		return $this;
194
	}
195
196
	/**
197
	 * Fetch Topic Tracking Info
198
	 *
199
	 * @return $this
200
	 */
201 7
	public function fetch_tracking_info()
202
	{
203 7
		if ($this->user->data['is_registered'] && $this->config['load_db_lastread'])
204 7
		{
205 6
			$this->cache_time = 0;
206
207 6
			$this->store['sql_array']['SELECT'][] = 'tt.mark_time, ft.mark_time as forum_mark_time';
208 6
			$this->store['sql_array']['LEFT_JOIN'][] = array(
209 6
				'FROM'	=> array(TOPICS_TRACK_TABLE => 'tt'),
210 6
				'ON'	=> 'tt.user_id = ' . (int) $this->user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
211 6
			);
212
213 6
			$this->store['sql_array']['LEFT_JOIN'][] = array(
214 6
				'FROM'	=> array(FORUMS_TRACK_TABLE => 'ft'),
215 6
				'ON'	=> 'ft.user_id = ' . (int) $this->user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
216 6
			);
217 6
		}
218
219 7
		return $this;
220
	}
221
222
	/**
223
	 * Fetch by Date Range
224
	 *
225
	 * @param int $unix_start_time
226
	 * @param int $unix_stop_time
227
	 * @param string $mode
228
	 * @return $this
229
	 */
230 14
	public function fetch_date_range($unix_start_time, $unix_stop_time, $mode = 'topic')
231
	{
232 14
		if ($unix_start_time && $unix_stop_time)
233 14
		{
234 2
			$this->store['sql_array']['WHERE'][] = (($mode == 'topic') ? 't.topic_time' : 'p.post_time') . " BETWEEN $unix_start_time AND $unix_stop_time";
235 2
		}
236
237 14
		return $this;
238
	}
239
240
	/**
241
	 * Fetch by Custom Query
242
	 *
243
	 * @param array	$sql_array		Array of elements to merge into query
244
	 * 										array(
245
	 * 											'SELECT'	=> array('p.*'),
246
	 * 											'WHERE'		=> array('p.post_id = 2'),
247
	 * 										)
248
	 * @return $this
249
	 */
250 8
	public function fetch_custom(array $sql_array)
251
	{
252 8
		$this->store['sql_array'] = array_merge_recursive($this->store['sql_array'], $sql_array);
253
254 8
		return $this;
255
	}
256
257
	/**
258
	 * Set Sorting Order
259
	 *
260
	 * @param string $sort_key		The sorting key e.g. t.topic_time
261
	 * @param string $sort_dir		Sort direction: ASC/DESC
262
	 * @return $this
263
	 */
264 13
	public function set_sorting($sort_key, $sort_dir = 'DESC')
265
	{
266 13
		$this->store['sql_array']['ORDER_BY'] = $sort_key . ' ' . $sort_dir;
267
268 13
		return $this;
269
	}
270
271
	/**
272
	 * Build the query
273
	 *
274
	 * @param bool|true $check_visibility		Should we only return data from forums the user is allowed to see?
275
	 * @param bool|true $enable_caching			Should the query be cached where possible?
276
	 * @param bool|true $exclude_hidden_forums	Leave out hidden forums?
277
	 * @return $this
278
	 */
279 22
	public function build($check_visibility = true, $enable_caching = true, $exclude_hidden_forums = true)
280
	{
281 22
		$this->_set_cache_time($enable_caching);
282 22
		$this->_set_topic_visibility($check_visibility);
283
284
		if ($exclude_hidden_forums)
285 22
		{
286 11
			$this->store['sql_array']['WHERE'][] = 'f.hidden_forum = 0';
287 11
		}
288
289 22
		$this->store['sql_array']['WHERE'][] = 'f.forum_id = t.forum_id';
290 22
		$this->store['sql_array']['WHERE'][] = 't.topic_moved_id = 0';
291
292 22
		$this->store['sql_array']['SELECT'] = join(', ', array_filter($this->store['sql_array']['SELECT']));
293 22
		$this->store['sql_array']['WHERE'] = join(' AND ', array_filter($this->store['sql_array']['WHERE']));
294
295 22
		return $this;
296
	}
297
298
	/**
299
	 * Get the query array
300
	 *
301
	 * @return array	The sql array that can be used with sql_build_query
302
	 */
303 8
	public function get_sql_array()
304
	{
305 8
		return $this->store['sql_array'];
306
	}
307
308
	/**
309
	 * @param bool $enable_caching
310
	 */
311 22
	protected function _set_cache_time($enable_caching)
312
	{
313 22
		if ($enable_caching === false)
314 22
		{
315 5
			$this->cache_time = 0;
316 5
		}
317 22
	}
318
319
	/**
320
	 * @param int $column_id
321
	 * @param string $column
322
	 */
323 17
	private function _fetch($column_id, $column)
324
	{
325 17
		if (!empty($column_id))
326 17
		{
327 11
			$this->store['sql_array']['WHERE'][] = (is_array($column_id)) ? $this->db->sql_in_set($column, $column_id) : $column . ' = ' . (int) $column_id;
328 11
		}
329 17
	}
330
331
	/**
332
	 * @param bool $check_visibility
333
	 */
334 22
	private function _set_topic_visibility($check_visibility)
335
	{
336
		if ($check_visibility)
337 22
		{
338 22
			$this->store['sql_array']['WHERE'][] = 't.topic_time <= ' . time();
339 22
			$this->store['sql_array']['WHERE'][] = $this->content_visibility->get_global_visibility_sql('topic', $this->ex_fid_ary, 't.');
340 22
		}
341 22
	}
342
343
	/**
344
	 * Reset data
345
	 */
346 22
	private function _reset()
347
	{
348 22
		$this->store = array(
349 22
			'attachments'	=> array(),
350 22
			'post_ids'		=> array(),
351 22
			'poster_ids'	=> array(),
352 22
			'sql_array'		=> array(),
353 22
			'topic'			=> array(),
354 22
			'tracking'		=> array(),
355
		);
356 22
	}
357
}
358