Conditions | 20 |
Paths | 1212 |
Total Lines | 125 |
Code Lines | 64 |
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 |
||
29 | function BoardNotify() |
||
30 | { |
||
31 | global $board, $user_info, $context, $smcFunc, $sourcedir, $scripturl, $txt; |
||
32 | |||
33 | require_once($sourcedir . '/Subs-Notify.php'); |
||
34 | |||
35 | // Subscribing or unsubscribing with a token. |
||
36 | if (isset($_REQUEST['u']) && isset($_REQUEST['token'])) |
||
37 | { |
||
38 | $member_info = getMemberWithToken('board'); |
||
39 | $skipCheckSession = true; |
||
40 | } |
||
41 | // No token, so try with the current user. |
||
42 | else |
||
43 | { |
||
44 | // Permissions are an important part of anything ;). |
||
45 | is_not_guest(); |
||
46 | $member_info = $user_info; |
||
47 | } |
||
48 | |||
49 | // You have to specify a board to turn notifications on! |
||
50 | if (empty($board)) |
||
51 | fatal_lang_error('no_board', false); |
||
52 | |||
53 | // sa=on/off is used for email subscribe/unsubscribe links |
||
54 | if (!isset($_GET['mode']) && isset($_GET['sa'])) |
||
55 | { |
||
56 | $_GET['mode'] = $_GET['sa'] == 'on' ? 3 : -1; |
||
57 | unset($_GET['sa']); |
||
58 | } |
||
59 | |||
60 | // No mode: find out what to do. |
||
61 | if (!isset($_GET['mode']) && !isset($_GET['xml'])) |
||
62 | { |
||
63 | // We're gonna need the notify template... |
||
64 | loadTemplate('Notify'); |
||
65 | |||
66 | // Find out if they have notification set for this board already. |
||
67 | $request = $smcFunc['db_query']('', ' |
||
68 | SELECT id_member |
||
69 | FROM {db_prefix}log_notify |
||
70 | WHERE id_member = {int:current_member} |
||
71 | AND id_board = {int:current_board} |
||
72 | LIMIT 1', |
||
73 | array( |
||
74 | 'current_board' => $board, |
||
75 | 'current_member' => $member_info['id'], |
||
76 | ) |
||
77 | ); |
||
78 | $context['notification_set'] = $smcFunc['db_num_rows']($request) != 0; |
||
79 | $smcFunc['db_free_result']($request); |
||
80 | |||
81 | if ($member_info['id'] !== $user_info['id']) |
||
82 | $context['notify_info'] = array( |
||
83 | 'u' => $member_info['id'], |
||
84 | 'token' => $_REQUEST['token'], |
||
85 | ); |
||
86 | |||
87 | // Set the template variables... |
||
88 | $context['board_href'] = $scripturl . '?board=' . $board . '.' . $_REQUEST['start']; |
||
89 | $context['start'] = $_REQUEST['start']; |
||
90 | $context['page_title'] = $txt['notification']; |
||
91 | $context['sub_template'] = 'notify_board'; |
||
92 | |||
93 | return; |
||
94 | } |
||
95 | elseif (isset($_GET['mode'])) |
||
96 | { |
||
97 | if (empty($skipCheckSession)) |
||
98 | checkSession('get'); |
||
99 | |||
100 | $mode = (int) $_GET['mode']; |
||
101 | |||
102 | // -1 is used to turn off email notifications while leaving the alert pref unchanged. |
||
103 | if ($mode == -1) |
||
104 | $mode = min(2, getNotifyPrefs($member_info['id'], array('board_notify_' . $board), true)); |
||
105 | |||
106 | $alertPref = $mode <= 1 ? 0 : ($mode == 2 ? 1 : 3); |
||
107 | |||
108 | setNotifyPrefs((int) $member_info['id'], array('board_notify_' . $board => $alertPref)); |
||
109 | |||
110 | if ($mode > 1) |
||
111 | // Turn notification on. (note this just blows smoke if it's already on.) |
||
112 | $smcFunc['db_insert']('ignore', |
||
113 | '{db_prefix}log_notify', |
||
114 | array('id_member' => 'int', 'id_topic' => 'int', 'id_board' => 'int'), |
||
115 | array($user_info['id'], 0, $board), |
||
116 | array('id_member', 'id_topic', 'id_board') |
||
117 | ); |
||
118 | else |
||
119 | $smcFunc['db_query']('', ' |
||
120 | DELETE FROM {db_prefix}log_notify |
||
121 | WHERE id_member = {int:current_member} |
||
122 | AND id_board = {int:current_board}', |
||
123 | array( |
||
124 | 'current_board' => $board, |
||
125 | 'current_member' => $member_info['id'], |
||
126 | ) |
||
127 | ); |
||
128 | } |
||
129 | |||
130 | if (isset($_GET['xml'])) |
||
131 | { |
||
132 | $context['xml_data']['errors'] = array( |
||
133 | 'identifier' => 'error', |
||
134 | 'children' => array( |
||
135 | array( |
||
136 | 'value' => 0, |
||
137 | ), |
||
138 | ), |
||
139 | ); |
||
140 | $context['sub_template'] = 'generic_xml'; |
||
141 | } |
||
142 | // Probably followed an unsubscribe link, so just show a confirmation message. |
||
143 | elseif (!empty($skipCheckSession) && isset($mode)) |
||
144 | { |
||
145 | loadTemplate('Notify'); |
||
146 | $context['page_title'] = $txt['notification']; |
||
147 | $context['sub_template'] = 'notify_pref_changed'; |
||
148 | $context['notify_success_msg'] = sprintf($txt['notify_board' . ($mode == 3 ? '_subscribed' : '_unsubscribed')], $member_info['email']); |
||
149 | return; |
||
150 | } |
||
151 | // Back to the board! |
||
152 | else |
||
153 | redirectexit('board=' . $board . '.' . $_REQUEST['start']); |
||
154 | } |
||
386 | ?> |