Conditions | 49 |
Paths | > 20000 |
Total Lines | 366 |
Code Lines | 202 |
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 |
||
67 | function issueWarning($memID) |
||
68 | { |
||
69 | global $txt, $scripturl, $modSettings, $user_info, $mbname; |
||
70 | global $context, $cur_profile, $smcFunc, $sourcedir; |
||
71 | |||
72 | // Get all the actual settings. |
||
73 | list ($modSettings['warning_enable'], $modSettings['user_limit']) = explode(',', $modSettings['warning_settings']); |
||
74 | |||
75 | // This stores any legitimate errors. |
||
76 | $issueErrors = array(); |
||
77 | |||
78 | // Doesn't hurt to be overly cautious. |
||
79 | if (empty($modSettings['warning_enable']) || ($context['user']['is_owner'] && !$cur_profile['warning']) || !allowedTo('issue_warning')) |
||
80 | fatal_lang_error('no_access', false); |
||
81 | |||
82 | // Get the base (errors related) stuff done. |
||
83 | loadLanguage('Errors'); |
||
84 | $context['custom_error_title'] = $txt['profile_warning_errors_occured']; |
||
85 | |||
86 | // Make sure things which are disabled stay disabled. |
||
87 | $modSettings['warning_watch'] = !empty($modSettings['warning_watch']) ? $modSettings['warning_watch'] : 110; |
||
88 | $modSettings['warning_moderate'] = !empty($modSettings['warning_moderate']) && !empty($modSettings['postmod_active']) ? $modSettings['warning_moderate'] : 110; |
||
89 | $modSettings['warning_mute'] = !empty($modSettings['warning_mute']) ? $modSettings['warning_mute'] : 110; |
||
90 | |||
91 | $context['warning_limit'] = allowedTo('admin_forum') ? 0 : $modSettings['user_limit']; |
||
92 | $context['member']['warning'] = $cur_profile['warning']; |
||
93 | $context['member']['name'] = $cur_profile['real_name']; |
||
94 | |||
95 | // What are the limits we can apply? |
||
96 | $context['min_allowed'] = 0; |
||
97 | $context['max_allowed'] = 100; |
||
98 | if ($context['warning_limit'] > 0) |
||
99 | { |
||
100 | // Make sure we cannot go outside of our limit for the day. |
||
101 | $request = $smcFunc['db_query']('', ' |
||
102 | SELECT SUM(counter) |
||
103 | FROM {db_prefix}log_comments |
||
104 | WHERE id_recipient = {int:selected_member} |
||
105 | AND id_member = {int:current_member} |
||
106 | AND comment_type = {string:warning} |
||
107 | AND log_time > {int:day_time_period}', |
||
108 | array( |
||
109 | 'current_member' => $user_info['id'], |
||
110 | 'selected_member' => $memID, |
||
111 | 'day_time_period' => time() - 86400, |
||
112 | 'warning' => 'warning', |
||
113 | ) |
||
114 | ); |
||
115 | list ($current_applied) = $smcFunc['db_fetch_row']($request); |
||
116 | $smcFunc['db_free_result']($request); |
||
117 | |||
118 | $context['min_allowed'] = max(0, $cur_profile['warning'] - $current_applied - $context['warning_limit']); |
||
119 | $context['max_allowed'] = min(100, $cur_profile['warning'] - $current_applied + $context['warning_limit']); |
||
120 | } |
||
121 | |||
122 | // Defaults. |
||
123 | $context['warning_data'] = array( |
||
124 | 'reason' => '', |
||
125 | 'notify' => '', |
||
126 | 'notify_subject' => '', |
||
127 | 'notify_body' => '', |
||
128 | ); |
||
129 | |||
130 | // Are we saving? |
||
131 | if (isset($_POST['save'])) |
||
132 | { |
||
133 | // Security is good here. |
||
134 | checkSession(); |
||
135 | |||
136 | // This cannot be empty! |
||
137 | $_POST['warn_reason'] = isset($_POST['warn_reason']) ? trim($_POST['warn_reason']) : ''; |
||
138 | if ($_POST['warn_reason'] == '' && !$context['user']['is_owner']) |
||
139 | $issueErrors[] = 'warning_no_reason'; |
||
140 | $_POST['warn_reason'] = $smcFunc['htmlspecialchars']($_POST['warn_reason']); |
||
141 | |||
142 | $_POST['warning_level'] = (int) $_POST['warning_level']; |
||
143 | $_POST['warning_level'] = max(0, min(100, $_POST['warning_level'])); |
||
144 | if ($_POST['warning_level'] < $context['min_allowed']) |
||
145 | $_POST['warning_level'] = $context['min_allowed']; |
||
146 | elseif ($_POST['warning_level'] > $context['max_allowed']) |
||
147 | $_POST['warning_level'] = $context['max_allowed']; |
||
148 | |||
149 | // Do we actually have to issue them with a PM? |
||
150 | $id_notice = 0; |
||
151 | if (!empty($_POST['warn_notify']) && empty($issueErrors)) |
||
152 | { |
||
153 | $_POST['warn_sub'] = trim($_POST['warn_sub']); |
||
154 | $_POST['warn_body'] = trim($_POST['warn_body']); |
||
155 | if (empty($_POST['warn_sub']) || empty($_POST['warn_body'])) |
||
156 | $issueErrors[] = 'warning_notify_blank'; |
||
157 | // Send the PM? |
||
158 | else |
||
159 | { |
||
160 | require_once($sourcedir . '/Subs-Post.php'); |
||
161 | $from = array( |
||
162 | 'id' => 0, |
||
163 | 'name' => $context['forum_name_html_safe'], |
||
164 | 'username' => $context['forum_name_html_safe'], |
||
165 | ); |
||
166 | sendpm(array('to' => array($memID), 'bcc' => array()), $_POST['warn_sub'], $_POST['warn_body'], false, $from); |
||
167 | |||
168 | // Log the notice! |
||
169 | $id_notice = $smcFunc['db_insert']('', |
||
170 | '{db_prefix}log_member_notices', |
||
171 | array( |
||
172 | 'subject' => 'string-255', 'body' => 'string-65534', |
||
173 | ), |
||
174 | array( |
||
175 | $smcFunc['htmlspecialchars']($_POST['warn_sub']), $smcFunc['htmlspecialchars']($_POST['warn_body']), |
||
176 | ), |
||
177 | array('id_notice'), |
||
178 | 1 |
||
179 | ); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | // Just in case - make sure notice is valid! |
||
184 | $id_notice = (int) $id_notice; |
||
185 | |||
186 | // What have we changed? |
||
187 | $level_change = $_POST['warning_level'] - $cur_profile['warning']; |
||
188 | |||
189 | // No errors? Proceed! Only log if you're not the owner. |
||
190 | if (empty($issueErrors)) |
||
191 | { |
||
192 | // Log what we've done! |
||
193 | if (!$context['user']['is_owner']) |
||
194 | $smcFunc['db_insert']('', |
||
195 | '{db_prefix}log_comments', |
||
196 | array( |
||
197 | 'id_member' => 'int', 'member_name' => 'string', 'comment_type' => 'string', 'id_recipient' => 'int', 'recipient_name' => 'string-255', |
||
198 | 'log_time' => 'int', 'id_notice' => 'int', 'counter' => 'int', 'body' => 'string-65534', |
||
199 | ), |
||
200 | array( |
||
201 | $user_info['id'], $user_info['name'], 'warning', $memID, $cur_profile['real_name'], |
||
202 | time(), $id_notice, $level_change, $_POST['warn_reason'], |
||
203 | ), |
||
204 | array('id_comment') |
||
205 | ); |
||
206 | |||
207 | // Make the change. |
||
208 | updateMemberData($memID, array('warning' => $_POST['warning_level'])); |
||
209 | |||
210 | // Leave a lovely message. |
||
211 | $context['profile_updated'] = $context['user']['is_owner'] ? $txt['profile_updated_own'] : $txt['profile_warning_success']; |
||
212 | } |
||
213 | else |
||
214 | { |
||
215 | // Try to remember some bits. |
||
216 | $context['warning_data'] = array( |
||
217 | 'reason' => $_POST['warn_reason'], |
||
218 | 'notify' => !empty($_POST['warn_notify']), |
||
219 | 'notify_subject' => isset($_POST['warn_sub']) ? $_POST['warn_sub'] : '', |
||
220 | 'notify_body' => isset($_POST['warn_body']) ? $_POST['warn_body'] : '', |
||
221 | ); |
||
222 | } |
||
223 | |||
224 | // Show the new improved warning level. |
||
225 | $context['member']['warning'] = $_POST['warning_level']; |
||
226 | } |
||
227 | |||
228 | if (isset($_POST['preview'])) |
||
229 | { |
||
230 | $warning_body = !empty($_POST['warn_body']) ? trim(censorText($_POST['warn_body'])) : ''; |
||
231 | $context['preview_subject'] = !empty($_POST['warn_sub']) ? trim($smcFunc['htmlspecialchars']($_POST['warn_sub'])) : ''; |
||
232 | if (empty($_POST['warn_sub']) || empty($_POST['warn_body'])) |
||
233 | $issueErrors[] = 'warning_notify_blank'; |
||
234 | |||
235 | if (!empty($_POST['warn_body'])) |
||
236 | { |
||
237 | require_once($sourcedir . '/Subs-Post.php'); |
||
238 | |||
239 | preparsecode($warning_body); |
||
240 | $warning_body = parse_bbc($warning_body, true); |
||
241 | } |
||
242 | |||
243 | // Try to remember some bits. |
||
244 | $context['warning_data'] = array( |
||
245 | 'reason' => $_POST['warn_reason'], |
||
246 | 'notify' => !empty($_POST['warn_notify']), |
||
247 | 'notify_subject' => isset($_POST['warn_sub']) ? $_POST['warn_sub'] : '', |
||
248 | 'notify_body' => isset($_POST['warn_body']) ? $_POST['warn_body'] : '', |
||
249 | 'body_preview' => $warning_body, |
||
250 | ); |
||
251 | } |
||
252 | |||
253 | if (!empty($issueErrors)) |
||
254 | { |
||
255 | // Fill in the suite of errors. |
||
256 | $context['post_errors'] = array(); |
||
257 | foreach ($issueErrors as $error) |
||
258 | $context['post_errors'][] = $txt[$error]; |
||
259 | } |
||
260 | |||
261 | $context['page_title'] = $txt['profile_issue_warning']; |
||
262 | |||
263 | // Let's use a generic list to get all the current warnings |
||
264 | require_once($sourcedir . '/Subs-List.php'); |
||
265 | |||
266 | // Work our the various levels. |
||
267 | $context['level_effects'] = array( |
||
268 | 0 => $txt['profile_warning_effect_none'], |
||
269 | $modSettings['warning_watch'] => $txt['profile_warning_effect_watch'], |
||
270 | $modSettings['warning_moderate'] => $txt['profile_warning_effect_moderation'], |
||
271 | $modSettings['warning_mute'] => $txt['profile_warning_effect_mute'], |
||
272 | ); |
||
273 | $context['current_level'] = 0; |
||
274 | foreach ($context['level_effects'] as $limit => $dummy) |
||
275 | if ($context['member']['warning'] >= $limit) |
||
276 | $context['current_level'] = $limit; |
||
277 | |||
278 | $listOptions = array( |
||
279 | 'id' => 'view_warnings', |
||
280 | 'title' => $txt['profile_viewwarning_previous_warnings'], |
||
281 | 'items_per_page' => $modSettings['defaultMaxListItems'], |
||
282 | 'no_items_label' => $txt['profile_viewwarning_no_warnings'], |
||
283 | 'base_href' => $scripturl . '?action=profile;area=issuewarning;sa=user;u=' . $memID, |
||
284 | 'default_sort_col' => 'log_time', |
||
285 | 'get_items' => array( |
||
286 | 'function' => 'list_getUserWarnings', |
||
287 | 'params' => array( |
||
288 | $memID, |
||
289 | ), |
||
290 | ), |
||
291 | 'get_count' => array( |
||
292 | 'function' => 'list_getUserWarningCount', |
||
293 | 'params' => array( |
||
294 | $memID, |
||
295 | ), |
||
296 | ), |
||
297 | 'columns' => array( |
||
298 | 'issued_by' => array( |
||
299 | 'header' => array( |
||
300 | 'value' => $txt['profile_warning_previous_issued'], |
||
301 | 'style' => 'width: 20%;', |
||
302 | ), |
||
303 | 'data' => array( |
||
304 | 'function' => function($warning) |
||
305 | { |
||
306 | return $warning['issuer']['link']; |
||
307 | }, |
||
308 | ), |
||
309 | 'sort' => array( |
||
310 | 'default' => 'lc.member_name DESC', |
||
311 | 'reverse' => 'lc.member_name', |
||
312 | ), |
||
313 | ), |
||
314 | 'log_time' => array( |
||
315 | 'header' => array( |
||
316 | 'value' => $txt['profile_warning_previous_time'], |
||
317 | 'style' => 'width: 30%;', |
||
318 | ), |
||
319 | 'data' => array( |
||
320 | 'db' => 'time', |
||
321 | ), |
||
322 | 'sort' => array( |
||
323 | 'default' => 'lc.log_time DESC', |
||
324 | 'reverse' => 'lc.log_time', |
||
325 | ), |
||
326 | ), |
||
327 | 'reason' => array( |
||
328 | 'header' => array( |
||
329 | 'value' => $txt['profile_warning_previous_reason'], |
||
330 | ), |
||
331 | 'data' => array( |
||
332 | 'function' => function($warning) use ($scripturl, $txt) |
||
333 | { |
||
334 | $ret = ' |
||
335 | <div class="floatleft"> |
||
336 | ' . $warning['reason'] . ' |
||
337 | </div>'; |
||
338 | |||
339 | if (!empty($warning['id_notice'])) |
||
340 | $ret .= ' |
||
341 | <div class="floatright"> |
||
342 | <a href="' . $scripturl . '?action=moderate;area=notice;nid=' . $warning['id_notice'] . '" onclick="window.open(this.href, \'\', \'scrollbars=yes,resizable=yes,width=400,height=250\');return false;" target="_blank" rel="noopener" title="' . $txt['profile_warning_previous_notice'] . '"><span class="main_icons filter centericon"></span></a> |
||
343 | </div>'; |
||
344 | |||
345 | return $ret; |
||
346 | }, |
||
347 | ), |
||
348 | ), |
||
349 | 'level' => array( |
||
350 | 'header' => array( |
||
351 | 'value' => $txt['profile_warning_previous_level'], |
||
352 | 'style' => 'width: 6%;', |
||
353 | ), |
||
354 | 'data' => array( |
||
355 | 'db' => 'counter', |
||
356 | ), |
||
357 | 'sort' => array( |
||
358 | 'default' => 'lc.counter DESC', |
||
359 | 'reverse' => 'lc.counter', |
||
360 | ), |
||
361 | ), |
||
362 | ), |
||
363 | ); |
||
364 | |||
365 | // Create the list for viewing. |
||
366 | require_once($sourcedir . '/Subs-List.php'); |
||
367 | createList($listOptions); |
||
368 | |||
369 | // Are they warning because of a message? |
||
370 | if (isset($_REQUEST['msg']) && 0 < (int) $_REQUEST['msg']) |
||
371 | { |
||
372 | $request = $smcFunc['db_query']('', ' |
||
373 | SELECT m.subject |
||
374 | FROM {db_prefix}messages AS m |
||
375 | WHERE m.id_msg = {int:message} |
||
376 | AND {query_see_message_board} |
||
377 | LIMIT 1', |
||
378 | array( |
||
379 | 'message' => (int) $_REQUEST['msg'], |
||
380 | ) |
||
381 | ); |
||
382 | if ($smcFunc['db_num_rows']($request) != 0) |
||
383 | { |
||
384 | $context['warning_for_message'] = (int) $_REQUEST['msg']; |
||
385 | list ($context['warned_message_subject']) = $smcFunc['db_fetch_row']($request); |
||
386 | } |
||
387 | $smcFunc['db_free_result']($request); |
||
388 | } |
||
389 | |||
390 | // Didn't find the message? |
||
391 | if (empty($context['warning_for_message'])) |
||
392 | { |
||
393 | $context['warning_for_message'] = 0; |
||
394 | $context['warned_message_subject'] = ''; |
||
395 | } |
||
396 | |||
397 | // Any custom templates? |
||
398 | $context['notification_templates'] = array(); |
||
399 | |||
400 | $request = $smcFunc['db_query']('', ' |
||
401 | SELECT recipient_name AS template_title, body |
||
402 | FROM {db_prefix}log_comments |
||
403 | WHERE comment_type = {literal:warntpl} |
||
404 | AND (id_recipient = {int:generic} OR id_recipient = {int:current_member})', |
||
405 | array( |
||
406 | 'generic' => 0, |
||
407 | 'current_member' => $user_info['id'], |
||
408 | ) |
||
409 | ); |
||
410 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
411 | { |
||
412 | // If we're not warning for a message skip any that are. |
||
413 | if (!$context['warning_for_message'] && strpos($row['body'], '{MESSAGE}') !== false) |
||
414 | continue; |
||
415 | |||
416 | $context['notification_templates'][] = array( |
||
417 | 'title' => $row['template_title'], |
||
418 | 'body' => $row['body'], |
||
419 | ); |
||
420 | } |
||
421 | $smcFunc['db_free_result']($request); |
||
422 | |||
423 | // Setup the "default" templates. |
||
424 | foreach (array('spamming', 'offence', 'insulting') as $type) |
||
425 | $context['notification_templates'][] = array( |
||
426 | 'title' => $txt['profile_warning_notify_title_' . $type], |
||
427 | 'body' => sprintf($txt['profile_warning_notify_template_outline' . (!empty($context['warning_for_message']) ? '_post' : '')], $txt['profile_warning_notify_for_' . $type]), |
||
428 | ); |
||
429 | |||
430 | // Replace all the common variables in the templates. |
||
431 | foreach ($context['notification_templates'] as $k => $name) |
||
432 | $context['notification_templates'][$k]['body'] = strtr($name['body'], array('{MEMBER}' => un_htmlspecialchars($context['member']['name']), '{MESSAGE}' => '[url=' . $scripturl . '?msg=' . $context['warning_for_message'] . ']' . un_htmlspecialchars($context['warned_message_subject']) . '[/url]', '{SCRIPTURL}' => $scripturl, '{FORUMNAME}' => $mbname, '{REGARDS}' => sprintf($txt['regards_team'], $context['forum_name']))); |
||
433 | } |
||
970 | ?> |