Conditions | 24 |
Paths | 4668 |
Total Lines | 160 |
Code Lines | 85 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
164 | function TopicNotify() |
||
165 | { |
||
166 | global $smcFunc, $user_info, $topic, $sourcedir, $context, $scripturl, $txt; |
||
167 | |||
168 | require_once($sourcedir . '/Subs-Notify.php'); |
||
169 | |||
170 | if (isset($_REQUEST['u']) && isset($_REQUEST['token'])) |
||
171 | { |
||
172 | $member_info = getMemberWithToken('topic'); |
||
173 | $skipCheckSession = true; |
||
174 | } |
||
175 | else |
||
176 | { |
||
177 | is_not_guest(); |
||
178 | $member_info = $user_info; |
||
179 | } |
||
180 | |||
181 | // Make sure the topic has been specified. |
||
182 | if (empty($topic)) |
||
183 | fatal_lang_error('not_a_topic', false); |
||
184 | |||
185 | // sa=on/off is used to toggle email notifications |
||
186 | if (!isset($_GET['mode']) && isset($_GET['sa'])) |
||
187 | { |
||
188 | $_GET['mode'] = $_GET['sa'] == 'on' ? 3 : -1; |
||
189 | unset($_GET['sa']); |
||
190 | } |
||
191 | |||
192 | // What do we do? Better ask if they didn't say.. |
||
193 | if (!isset($_GET['mode']) && !isset($_GET['xml'])) |
||
194 | { |
||
195 | // Load the template, but only if it is needed. |
||
196 | loadTemplate('Notify'); |
||
197 | |||
198 | // Find out if they have notification set for this topic already. |
||
199 | $request = $smcFunc['db_query']('', ' |
||
200 | SELECT id_member |
||
201 | FROM {db_prefix}log_notify |
||
202 | WHERE id_member = {int:current_member} |
||
203 | AND id_topic = {int:current_topic} |
||
204 | LIMIT 1', |
||
205 | array( |
||
206 | 'current_member' => $member_info['id'], |
||
207 | 'current_topic' => $topic, |
||
208 | ) |
||
209 | ); |
||
210 | $context['notification_set'] = $smcFunc['db_num_rows']($request) != 0; |
||
211 | $smcFunc['db_free_result']($request); |
||
212 | |||
213 | if ($member_info['id'] !== $user_info['id']) |
||
214 | $context['notify_info'] = array( |
||
215 | 'u' => $member_info['id'], |
||
216 | 'token' => $_REQUEST['token'], |
||
217 | ); |
||
218 | |||
219 | // Set the template variables... |
||
220 | $context['topic_href'] = $scripturl . '?topic=' . $topic . '.' . $_REQUEST['start']; |
||
221 | $context['start'] = $_REQUEST['start']; |
||
222 | $context['page_title'] = $txt['notification']; |
||
223 | |||
224 | return; |
||
225 | } |
||
226 | elseif (isset($_GET['mode'])) |
||
227 | { |
||
228 | if (empty($skipCheckSession)) |
||
229 | checkSession('get'); |
||
230 | |||
231 | $mode = (int) $_GET['mode']; |
||
232 | |||
233 | // Turn off email notifications while leaving the alert pref alone. |
||
234 | if ($mode == -1) |
||
235 | $mode = min(2, getNotifyPrefs($member_info['id'], array('topic_notify_' . $topic), true)); |
||
236 | |||
237 | $alertPref = $mode <= 1 ? 0 : ($mode == 2 ? 1 : 3); |
||
238 | |||
239 | $request = $smcFunc['db_query']('', ' |
||
240 | SELECT id_member, id_topic, id_msg, unwatched |
||
241 | FROM {db_prefix}log_topics |
||
242 | WHERE id_member = {int:current_user} |
||
243 | AND id_topic = {int:current_topic}', |
||
244 | array( |
||
245 | 'current_user' => $member_info['id'], |
||
246 | 'current_topic' => $topic, |
||
247 | ) |
||
248 | ); |
||
249 | $log = $smcFunc['db_fetch_assoc']($request); |
||
250 | $smcFunc['db_free_result']($request); |
||
251 | if (empty($log)) |
||
252 | { |
||
253 | $insert = true; |
||
254 | $log = array( |
||
255 | 'id_member' => $member_info['id'], |
||
256 | 'id_topic' => $topic, |
||
257 | 'id_msg' => 0, |
||
258 | 'unwatched' => empty($mode) ? 1 : 0, |
||
259 | ); |
||
260 | } |
||
261 | else |
||
262 | { |
||
263 | $insert = false; |
||
264 | $log['unwatched'] = empty($mode) ? 1 : 0; |
||
265 | } |
||
266 | |||
267 | $smcFunc['db_insert']($insert ? 'insert' : 'replace', |
||
268 | '{db_prefix}log_topics', |
||
269 | array( |
||
270 | 'id_member' => 'int', 'id_topic' => 'int', 'id_msg' => 'int', 'unwatched' => 'int', |
||
271 | ), |
||
272 | $log, |
||
273 | array('id_member', 'id_topic') |
||
274 | ); |
||
275 | |||
276 | setNotifyPrefs((int) $member_info['id'], array('topic_notify_' . $log['id_topic'] => $alertPref)); |
||
277 | |||
278 | if ($mode > 1) |
||
279 | { |
||
280 | // Turn notification on. (note this just blows smoke if it's already on.) |
||
281 | $smcFunc['db_insert']('ignore', |
||
282 | '{db_prefix}log_notify', |
||
283 | array('id_member' => 'int', 'id_topic' => 'int', 'id_board' => 'int'), |
||
284 | array($user_info['id'], $log['id_topic'], 0), |
||
285 | array('id_member','id_topic', 'id_board') |
||
286 | ); |
||
287 | } |
||
288 | else |
||
289 | $smcFunc['db_query']('', ' |
||
290 | DELETE FROM {db_prefix}log_notify |
||
291 | WHERE id_topic = {int:topic} |
||
292 | AND id_member = {int:member}', |
||
293 | array( |
||
294 | 'topic' => $log['id_topic'], |
||
295 | 'member' => $member_info['id'], |
||
296 | ) |
||
297 | ); |
||
298 | } |
||
299 | |||
300 | if (isset($_GET['xml'])) |
||
301 | { |
||
302 | $context['xml_data']['errors'] = array( |
||
303 | 'identifier' => 'error', |
||
304 | 'children' => array( |
||
305 | array( |
||
306 | 'value' => 0, |
||
307 | ), |
||
308 | ), |
||
309 | ); |
||
310 | $context['sub_template'] = 'generic_xml'; |
||
311 | } |
||
312 | // Probably followed an unsubscribe link, so just show a confirmation message. |
||
313 | elseif (!empty($skipCheckSession) && isset($mode)) |
||
314 | { |
||
315 | loadTemplate('Notify'); |
||
316 | $context['page_title'] = $txt['notification']; |
||
317 | $context['sub_template'] = 'notify_pref_changed'; |
||
318 | $context['notify_success_msg'] = sprintf($txt['notify_topic' . ($mode == 3 ? '_subscribed' : '_unsubscribed')], $member_info['email']); |
||
319 | return; |
||
320 | } |
||
321 | // Back to the topic. |
||
322 | else |
||
323 | redirectexit('topic=' . $topic . '.' . $_REQUEST['start']); |
||
324 | } |
||
386 | ?> |