1 | <?php |
||
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) |
|
59 | |||
60 | /** |
||
61 | * Begin query |
||
62 | * |
||
63 | * @param bool $track_topics |
||
64 | * @param bool $add_forum_data |
||
65 | * @return $this |
||
66 | */ |
||
67 | 22 | public function query($track_topics = true, $add_forum_data = true) |
|
68 | { |
||
69 | 22 | $this->_reset(); |
|
70 | |||
71 | 22 | $this->store['sql_array'] = array_fill_keys(array('SELECT', 'FROM', 'LEFT_JOIN', 'WHERE'), array()); |
|
72 | |||
73 | if ($add_forum_data) |
||
74 | 22 | { |
|
75 | 21 | $this->store['sql_array']['SELECT'][] = 'f.*'; |
|
76 | 21 | $this->store['sql_array']['FROM'][FORUMS_TABLE] = 'f'; |
|
77 | 21 | } |
|
78 | |||
79 | 22 | $this->store['sql_array']['SELECT'][] = 't.*'; |
|
80 | |||
81 | if ($track_topics) |
||
82 | 22 | { |
|
83 | 7 | $this->fetch_tracking_info(); |
|
84 | 7 | } |
|
85 | |||
86 | 22 | return $this; |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Fetch Forum by id(s) |
||
91 | * |
||
92 | * @param $forum_id |
||
93 | * @return $this |
||
94 | */ |
||
95 | 17 | public function fetch_forum($forum_id) |
|
96 | { |
||
97 | 17 | $this->store['sql_array']['FROM'][FORUMS_TABLE] = 'f'; |
|
98 | 17 | $this->_fetch($forum_id, 'f.forum_id'); |
|
99 | |||
100 | 17 | return $this; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Fetch Topic by id(s) |
||
105 | * |
||
106 | * @param mixed $topic_id Limit by topic id: single id or array of topic ids |
||
107 | * @return $this |
||
108 | */ |
||
109 | 11 | public function fetch_topic($topic_id) |
|
110 | { |
||
111 | 11 | $this->_fetch($topic_id, 't.topic_id'); |
|
112 | |||
113 | 11 | return $this; |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * Fetch Topic by Poster id(s) |
||
118 | * |
||
119 | * @param mixed $user_id User id of topic poster: single id or array of user ids |
||
120 | * @return $this |
||
121 | */ |
||
122 | 3 | public function fetch_topic_poster($user_id) |
|
123 | { |
||
124 | 3 | $this->_fetch($user_id, 't.topic_poster'); |
|
125 | |||
126 | 3 | return $this; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * Fetch by Topic Type |
||
131 | * |
||
132 | * @param array $topic_type |
||
133 | * @return $this |
||
134 | */ |
||
135 | 17 | public function fetch_topic_type(array $topic_type) |
|
136 | { |
||
137 | 17 | if (sizeof($topic_type)) |
|
138 | 17 | { |
|
139 | 5 | $this->store['sql_array']['WHERE'][] = $this->db->sql_in_set('t.topic_type', $topic_type); |
|
140 | 5 | } |
|
141 | |||
142 | 17 | return $this; |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * Fetch Topic Watch info |
||
147 | * |
||
148 | * @param $type |
||
149 | * @return $this |
||
150 | */ |
||
151 | public function fetch_watch_status($type = 'topic') |
||
152 | { |
||
153 | if ($this->user->data['is_registered']) |
||
154 | { |
||
155 | $keys = array( |
||
156 | 'forum' => array( |
||
157 | 'table' => FORUMS_WATCH_TABLE, |
||
158 | 'cond' => 'ws.forum_id = f.forum_id', |
||
159 | ), |
||
160 | 'topic' => array( |
||
161 | 'table' => TOPICS_WATCH_TABLE, |
||
162 | 'cond' => 'ws.topic_id = t.topic_id', |
||
163 | ), |
||
164 | ); |
||
165 | |||
166 | $this->store['sql_array']['SELECT'][] = 'ws.notify_status'; |
||
167 | $this->store['sql_array']['LEFT_JOIN'][] = array( |
||
168 | 'FROM' => array($keys[$type]['table'] => 'ws'), |
||
169 | 'ON' => $keys[$type]['cond'] . ' AND ws.user_id = ' . (int) $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() |
||
194 | |||
195 | /** |
||
196 | * Fetch Topic Tracking Info |
||
197 | * |
||
198 | * @return $this |
||
199 | */ |
||
200 | 7 | public function fetch_tracking_info() |
|
220 | |||
221 | /** |
||
222 | * Fetch by Date Range |
||
223 | * |
||
224 | * @param int $unix_start_time |
||
225 | * @param int $unix_stop_time |
||
226 | * @param string $mode |
||
227 | * @return $this |
||
228 | */ |
||
229 | 14 | public function fetch_date_range($unix_start_time, $unix_stop_time, $mode = 'topic') |
|
238 | |||
239 | /** |
||
240 | * Fetch by Custom Query |
||
241 | * |
||
242 | * @param array $sql_array Array of elements to merge into query |
||
243 | * array( |
||
244 | * 'SELECT' => array('p.*'), |
||
245 | * 'WHERE' => array('p.post_id = 2'), |
||
246 | * ) |
||
247 | * @param array $overwrite_keys Array of query keys to overwrite with yours instead of merging |
||
248 | * e.g array('SELECT') will overwrite the 'SELECT' key with whatever is provided in $sql_array |
||
249 | * @return $this |
||
250 | */ |
||
251 | 8 | public function fetch_custom(array $sql_array, $overwrite_keys = array()) |
|
262 | |||
263 | /** |
||
264 | * Set Sorting Order |
||
265 | * |
||
266 | * @param string $sort_key The sorting key e.g. t.topic_time |
||
267 | * @param string $sort_dir Sort direction: ASC/DESC |
||
268 | * @return $this |
||
269 | */ |
||
270 | 13 | public function set_sorting($sort_key, $sort_dir = 'DESC') |
|
276 | |||
277 | /** |
||
278 | * Build the query |
||
279 | * |
||
280 | * @param bool|true $check_visibility Should we only return data from forums the user is allowed to see? |
||
281 | * @param bool|true $enable_caching Should the query be cached where possible? |
||
282 | * @param bool|true $exclude_hidden_forums Leave out hidden forums? |
||
283 | * @return $this |
||
284 | */ |
||
285 | 22 | public function build($check_visibility = true, $enable_caching = true, $exclude_hidden_forums = true) |
|
300 | |||
301 | /** |
||
302 | * Get the query array |
||
303 | * |
||
304 | * @return array The sql array that can be used with sql_build_query |
||
305 | */ |
||
306 | 8 | public function get_sql_array() |
|
310 | |||
311 | /** |
||
312 | * @param bool $enable_caching |
||
313 | * @return void |
||
314 | */ |
||
315 | 22 | protected function _set_cache_time($enable_caching) |
|
322 | |||
323 | /** |
||
324 | * @param int $column_id |
||
325 | * @param string $column |
||
326 | * @return void |
||
327 | */ |
||
328 | 17 | private function _fetch($column_id, $column) |
|
335 | |||
336 | /** |
||
337 | * @param bool $check_visibility |
||
338 | * @return void |
||
339 | */ |
||
340 | 22 | private function _set_topic_visibility($check_visibility) |
|
348 | |||
349 | /** |
||
350 | * @param bool $exclude_hidden_forums |
||
351 | * @return void |
||
352 | */ |
||
353 | 22 | private function _set_forum_table($exclude_hidden_forums) |
|
366 | |||
367 | /** |
||
368 | * Reset data |
||
369 | * @return void |
||
370 | */ |
||
371 | 22 | private function _reset() |
|
382 | } |
||
383 |