| Conditions | 12 |
| Paths | 16 |
| Total Lines | 114 |
| Code Lines | 62 |
| 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 |
||
| 25 | public function execute() |
||
| 26 | { |
||
| 27 | global $smcFunc, $sourcedir, $modSettings, $language, $scripturl; |
||
| 28 | |||
| 29 | // Anyone with moderate_forum can see this report |
||
| 30 | require_once($sourcedir . '/Subs-Members.php'); |
||
| 31 | $members = membersAllowedTo('moderate_forum'); |
||
| 32 | |||
| 33 | // And don't send it to them if they're the one who reported it. |
||
| 34 | $members = array_diff($members, array($this->_details['sender_id'])); |
||
| 35 | |||
| 36 | // Having successfully figured this out, now let's get the preferences of everyone. |
||
| 37 | require_once($sourcedir . '/Subs-Notify.php'); |
||
| 38 | $prefs = getNotifyPrefs($members, 'member_report', true); |
||
| 39 | |||
| 40 | // So now we find out who wants what. |
||
| 41 | $alert_bits = array( |
||
| 42 | 'alert' => self::RECEIVE_NOTIFY_ALERT, |
||
| 43 | 'email' => self::RECEIVE_NOTIFY_EMAIL, |
||
| 44 | ); |
||
| 45 | $notifies = array(); |
||
| 46 | |||
| 47 | foreach ($prefs as $member => $pref_option) |
||
| 48 | { |
||
| 49 | foreach ($alert_bits as $type => $bitvalue) |
||
| 50 | if ($pref_option['member_report'] & $bitvalue) |
||
| 51 | $notifies[$type][] = $member; |
||
| 52 | } |
||
| 53 | |||
| 54 | // Firstly, anyone who wants alerts. |
||
| 55 | if (!empty($notifies['alert'])) |
||
| 56 | { |
||
| 57 | // Alerts are relatively easy. |
||
| 58 | $insert_rows = array(); |
||
| 59 | foreach ($notifies['alert'] as $member) |
||
| 60 | { |
||
| 61 | $insert_rows[] = array( |
||
| 62 | 'alert_time' => $this->_details['time'], |
||
| 63 | 'id_member' => $member, |
||
| 64 | 'id_member_started' => $this->_details['sender_id'], |
||
| 65 | 'member_name' => $this->_details['sender_name'], |
||
| 66 | 'content_type' => 'member', |
||
| 67 | 'content_id' => $this->_details['user_id'], |
||
| 68 | 'content_action' => 'report', |
||
| 69 | 'is_read' => 0, |
||
| 70 | 'extra' => $smcFunc['json_encode']( |
||
| 71 | array( |
||
| 72 | 'report_link' => '?action=moderate;area=reportedmembers;sa=details;rid=' . $this->_details['report_id'], // We don't put $scripturl in these! |
||
| 73 | 'user_name' => $this->_details['user_name'], |
||
| 74 | ) |
||
| 75 | ), |
||
| 76 | ); |
||
| 77 | } |
||
| 78 | |||
| 79 | $smcFunc['db_insert']('insert', |
||
| 80 | '{db_prefix}user_alerts', |
||
| 81 | array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int', |
||
| 82 | 'member_name' => 'string', 'content_type' => 'string', 'content_id' => 'int', |
||
| 83 | 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'), |
||
| 84 | $insert_rows, |
||
| 85 | array('id_alert') |
||
| 86 | ); |
||
| 87 | |||
| 88 | // And update the count of alerts for those people. |
||
| 89 | updateMemberData($notifies['alert'], array('alerts' => '+')); |
||
| 90 | } |
||
| 91 | |||
| 92 | // Secondly, anyone who wants emails. |
||
| 93 | if (!empty($notifies['email'])) |
||
| 94 | { |
||
| 95 | // Emails are a bit complicated. We have to do language stuff. |
||
| 96 | require_once($sourcedir . '/Subs-Post.php'); |
||
| 97 | require_once($sourcedir . '/ScheduledTasks.php'); |
||
| 98 | loadEssentialThemeData(); |
||
| 99 | |||
| 100 | // First, get everyone's language and details. |
||
| 101 | $emails = array(); |
||
| 102 | $request = $smcFunc['db_query']('', ' |
||
| 103 | SELECT id_member, lngfile, email_address |
||
| 104 | FROM {db_prefix}members |
||
| 105 | WHERE id_member IN ({array_int:members})', |
||
| 106 | array( |
||
| 107 | 'members' => $notifies['email'], |
||
| 108 | ) |
||
| 109 | ); |
||
| 110 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 111 | { |
||
| 112 | if (empty($row['lngfile'])) |
||
| 113 | $row['lngfile'] = $language; |
||
| 114 | $emails[$row['lngfile']][$row['id_member']] = $row['email_address']; |
||
| 115 | } |
||
| 116 | $smcFunc['db_free_result']($request); |
||
| 117 | |||
| 118 | // Iterate through each language, load the relevant templates and set up sending. |
||
| 119 | foreach ($emails as $this_lang => $recipients) |
||
| 120 | { |
||
| 121 | $replacements = array( |
||
| 122 | 'MEMBERNAME' => $this->_details['user_name'], |
||
| 123 | 'REPORTERNAME' => $this->_details['sender_name'], |
||
| 124 | 'PROFILELINK' => $scripturl . '?action=profile;u=' . $this->_details['user_id'], |
||
| 125 | 'REPORTLINK' => $scripturl . '?action=moderate;area=reportedmembers;sa=details;rid=' . $this->_details['report_id'], |
||
| 126 | 'COMMENT' => $this->_details['comment'], |
||
| 127 | ); |
||
| 128 | |||
| 129 | $emaildata = loadEmailTemplate('report_member_profile', $replacements, empty($modSettings['userLanguage']) ? $language : $this_lang); |
||
| 130 | |||
| 131 | // And do the actual sending... |
||
| 132 | foreach ($recipients as $id_member => $email_address) |
||
| 133 | sendmail($email_address, $emaildata['subject'], $emaildata['body'], null, 'ureport' . $this->_details['report_id'], $emaildata['is_html'], 2); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | // And now we're all done. |
||
| 138 | return true; |
||
| 139 | } |
||
| 142 | ?> |