Completed
Push — master ( 8f57ea...eb6780 )
by Daniel
11:35
created

query_builder::fetch_topic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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 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 26
	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 26
		$this->auth = $auth;
51 26
		$this->config = $config;
52 26
		$this->content_visibility = $content_visibility;
53 26
		$this->db = $db;
54 26
		$this->user = $user;
55 26
		$this->cache_time = $cache_time;
56
57 26
		$this->ex_fid_ary = array_unique(array_keys($this->auth->acl_getf('!f_read', true)));
58 26
	}
59
60
	/**
61
	 * Begin query
62
	 *
63
	 * $param bool $track_topics
64
	 * @return $this
65 19
	 */
66
	public function query($track_topics = true)
67 19
	{
68
		$this->_reset();
69 19
70 19
		$this->store['sql_array'] = array(
71
			'SELECT'	=> array('t.*, f.*'),
72 19
73
			'FROM'		=> array(FORUMS_TABLE => 'f'),
74 19
75
			'LEFT_JOIN'	=> array(),
76 19
77
			'WHERE'		=> array(),
78
		);
79
80 19
		// Topics table need to be the last in the chain
81
		$this->store['sql_array']['FROM'][TOPICS_TABLE] = 't';
82 19
83
		if ($track_topics)
84
		{
85
			$this->fetch_tracking_info();
86
		}
87
88
		return $this;
89
	}
90
91 15
	/**
92
	 * Fetch Forum by id(s)
93 15
	 *
94
	 * @param $forum_id
95 15
	 * @return $this
96
	 */
97
	public function fetch_forum($forum_id)
98
	{
99
		$this->_fetch($forum_id, 'f.forum_id');
100
101
		return $this;
102
	}
103
104 3
	/**
105
	 * Fetch Topic by id(s)
106 3
	 *
107
	 * @param mixed $topic_id	Limit by topic id: single id or array of topic ids
108 3
	 * @return $this
109
	 */
110
	public function fetch_topic($topic_id)
111
	{
112
		$this->_fetch($topic_id, 't.topic_id');
113
114
		return $this;
115
	}
116
117 3
	/**
118
	 * Fetch Topic by Poster id(s)
119 3
	 *
120
	 * @param mixed $user_id	User id of topic poster: single id or array of user ids
121 3
	 * @return $this
122
	 */
123
	public function fetch_topic_poster($user_id)
124
	{
125
		$this->_fetch($user_id, 't.topic_poster');
126
127
		return $this;
128
	}
129
130 15
	/**
131
	 * Fetch by Topic Type
132 15
	 *
133 15
	 * @param array $topic_type
134 4
	 * @return $this
135 4
	 */
136
	public function fetch_topic_type(array $topic_type)
137 15
	{
138 15
		if (sizeof($topic_type))
139
		{
140
			$this->store['sql_array']['WHERE'][] = $this->db->sql_in_set('t.topic_type', $topic_type);
141
		}
142 15
143
		if (in_array($topic_type, array(POST_STICKY, POST_ANNOUNCE)))
144
		{
145
			$this->store['sql_array']['WHERE'][] = '(t.topic_time_limit > 0 AND (t.topic_time + t.topic_time_limit) < ' . time() . ')';
146
		}
147
148
		return $this;
149
	}
150
151
	/**
152
	 * Fetch Topic Watch info
153
	 *
154
	 * @return $this
155
	 */
156
	public function fetch_watch_status()
157
	{
158
		if ($this->user->data['is_registered'])
159
		{
160
			$this->store['sql_array']['SELECT'][] = 'tw.notify_status';
161
			$this->store['sql_array']['LEFT_JOIN'][] = array(
162
				'FROM'	=> array(TOPICS_WATCH_TABLE => 'tw'),
163
				'ON'	=> 'tw.user_id = ' . $this->user->data['user_id'] . ' AND t.topic_id = tw.topic_id'
164
			);
165
166
			$this->store['sql_array']['SELECT'][] = 'fw.notify_status';
167
			$this->store['sql_array']['LEFT JOIN'][] = array(
168
				'FROM'	=> array(FORUMS_WATCH_TABLE => 'fw'),
169
				'ON'	=> '(fw.forum_id = f.forum_id AND fw.user_id = ' . $this->user->data['user_id'] . ')',
170
			);
171
		}
172
173
		return $this;
174
	}
175
176
	/**
177
	 * Fetch Topic Bookmark Info
178
	 *
179
	 * @return $this
180
	 */
181
	public function fetch_bookmark_status()
182
	{
183
		if ($this->user->data['is_registered'] && $this->config['allow_bookmarks'])
184
		{
185
			$this->store['sql_array']['SELECT'][] = 'bm.topic_id as bookmarked';
186
			$this->store['sql_array']['LEFT_JOIN'][] = array(
187
				'FROM'	=> array(BOOKMARKS_TABLE => 'bm'),
188
				'ON'	=> 'bm.user_id = ' . $this->user->data['user_id'] . ' AND t.topic_id = bm.topic_id'
189
			);
190
		}
191
192
		return $this;
193
	}
194
195 5
	/**
196
	 * Fetch Topic Tracking Info
197 5
	 *
198 5
	 * @param bool $track
0 ignored issues
show
Bug introduced by
There is no parameter named $track. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
199 3
	 * @return $this
200
	 */
201 3
	public function fetch_tracking_info()
202 3
	{
203 3
		if ($this->user->data['is_registered'] && $this->config['load_db_lastread'])
204 3
		{
205 3
			$this->cache_time = 0;
206
207 3
			$this->store['sql_array']['SELECT'][] = 'tt.mark_time, ft.mark_time as forum_mark_time';
208 3
			$this->store['sql_array']['LEFT_JOIN'][] = array(
209 3
				'FROM'	=> array(TOPICS_TRACK_TABLE => 'tt'),
210 3
				'ON'	=> 'tt.user_id = ' . $this->user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
211 3
			);
212
213 5
			$this->store['sql_array']['LEFT_JOIN'][] = array(
214
				'FROM'	=> array(FORUMS_TRACK_TABLE => 'ft'),
215
				'ON'	=> 'ft.user_id = ' . $this->user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
216
			);
217
		}
218
219
		return $this;
220
	}
221
222
	/**
223
	 * Fetch by Date Range
224 12
	 *
225
	 * @param int $start	Unix start time
226 12
	 * @param int $stop		Unix stop time
227 12
	 * @param string $mode
228 2
	 * @return $this
229 2
	 */
230
	public function fetch_date_range($start, $stop, $mode = 'topic')
231 12
	{
232
		if ($start && $stop)
233
		{
234
			$this->store['sql_array']['WHERE'][] = (($mode == 'topic') ? 't.topic_time' : 'p.post_time') . " BETWEEN $start AND $stop";
235
		}
236
237
		return $this;
238
	}
239
240
	/**
241
	 * Fetch by Custom Query
242
	 *
243
	 * @param array	$sql_array		Array of elements to merge into query
244 7
	 * 										array(
245
	 * 											'SELECT'	=> array('p.*'),
246 7
	 * 											'WHERE'		=> array('p.post_id = 2'),
247
	 * 										)
248 7
	 * @return $this
249
	 */
250
	public function fetch_custom(array $sql_array)
251
	{
252
		$this->store['sql_array'] = array_merge_recursive($this->store['sql_array'], $sql_array);
253
254
		return $this;
255
	}
256
257
	/**
258 12
	 * Set Sorting Order
259
	 *
260 12
	 * @param string $sort_key		The sorting key e.g. t.topic_time
261
	 * @param string $sort_dir		Sort direction: ASC/DESC
262 12
	 * @return $this
263
	 */
264
	public function set_sorting($sort_key, $sort_dir = 'DESC')
265
	{
266
		$this->store['sql_array']['ORDER_BY'] = $sort_key . ' ' . $sort_dir;
267
268
		return $this;
269
	}
270
271
	/**
272
	 * Build the query
273 19
	 *
274
	 * @param bool|true $check_visibility		Should we only return data from forums the user is allowed to see?
275 19
	 * @param bool|true $enable_caching			Should the query be cached where possible?
276 19
	 * @param bool|true $displayed_on_index		Only get forums that are displayed on index?
277
	 * @return $this
278
	 */
279 19
	public function build($check_visibility = true, $enable_caching = true, $displayed_on_index = true)
280 19
	{
281 19
		$this->_set_cache_time($enable_caching);
282
		$this->_set_topic_visibility($check_visibility);
283 19
284 19
		if ($displayed_on_index)
285
		{
286 19
			$this->store['sql_array']['WHERE'][] = 'f.display_on_index <> 0';
287 19
		}
288
289 19
		$this->store['sql_array']['WHERE'][] = 'f.forum_id = t.forum_id';
290
		$this->store['sql_array']['WHERE'][] = 't.topic_moved_id = 0';
291
292
		$this->store['sql_array']['SELECT'] = join(', ', array_filter($this->store['sql_array']['SELECT']));
293
		$this->store['sql_array']['WHERE'] = join(' AND ', array_filter($this->store['sql_array']['WHERE']));
294
295
		return $this;
296
	}
297 7
298
	/**
299 7
	 * Get the query array
300
	 *
301
	 * @return array	The sql array that can be used with sql_build_query
302
	 */
303
	public function get_sql_array()
304
	{
305 19
		return $this->store['sql_array'];
306
	}
307 19
308 19
	/**
309 4
	 * @param bool $enable_caching
310 4
	 */
311 19
	protected function _set_cache_time($enable_caching)
312
	{
313
		if ($enable_caching === false)
314
		{
315
			$this->cache_time = 0;
316
		}
317 15
	}
318
319 15
	/**
320 15
	 * @param int $column_id
321 10
	 * @param string $column
322 10
	 */
323 15
	private function _fetch($column_id, $column)
324
	{
325
		if (!empty($column_id))
326
		{
327
			$this->store['sql_array']['WHERE'][] = (is_array($column_id)) ? $this->db->sql_in_set($column, $column_id) : $column . ' = ' . (int) $column_id;
328 19
		}
329
	}
330
331 19
	/**
332 19
	 * @param bool $check_visibility
333 19
	 */
334 19
	private function _set_topic_visibility($check_visibility)
335 19
	{
336
		if ($check_visibility)
337
		{
338
			$this->store['sql_array']['WHERE'][] = 't.topic_time <= ' . time();
339
			$this->store['sql_array']['WHERE'][] = $this->content_visibility->get_global_visibility_sql('topic', $this->ex_fid_ary, 't.');
340 19
		}
341
	}
342 19
343 19
	/**
344 19
	 * Reset data
345 19
	 */
346 19
	private function _reset()
347 19
	{
348 19
		$this->store = array(
349
			'attachments'	=> array(),
350 19
			'post_ids'		=> array(),
351
			'poster_ids'	=> array(),
352
			'sql_array'		=> array(),
353
			'topic'			=> array(),
354
			'tracking'		=> array(),
355
		);
356
	}
357
}
358