Completed
Push — develop ( 7d7ff1...c95879 )
by Daniel
12:59
created

query_builder::fetch_date_range()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

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