Completed
Push — master ( 3d7558...b8b30a )
by Daniel
09:47
created

query_builder::get_sql_array()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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