Conditions | 17 |
Paths | 63 |
Total Lines | 134 |
Code Lines | 71 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
26 | public function execute() |
||
27 | { |
||
28 | global $sourcedir, $smcFunc, $language, $modSettings; |
||
29 | |||
30 | // Get the details of all the members concerned... |
||
31 | $request = $smcFunc['db_query']('', ' |
||
32 | SELECT lgr.id_request, lgr.id_member, lgr.id_group, mem.email_address, |
||
33 | mem.lngfile, mem.member_name, mg.group_name |
||
34 | FROM {db_prefix}log_group_requests AS lgr |
||
35 | INNER JOIN {db_prefix}members AS mem ON (mem.id_member = lgr.id_member) |
||
36 | INNER JOIN {db_prefix}membergroups AS mg ON (mg.id_group = lgr.id_group) |
||
37 | WHERE lgr.id_request IN ({array_int:request_list}) |
||
38 | ORDER BY mem.lngfile', |
||
39 | array( |
||
40 | 'request_list' => $this->_details['request_list'], |
||
41 | ) |
||
42 | ); |
||
43 | $affected_users = array(); |
||
44 | $members = array(); |
||
45 | $alert_rows = array(); |
||
46 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
47 | { |
||
48 | $members[] = $row['id_member']; |
||
49 | $row['lngfile'] = empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']; |
||
50 | |||
51 | // If we are approving, add them! |
||
52 | if ($this->_details['status'] == 'approve') |
||
53 | { |
||
54 | // Hack in blank permissions so that allowedTo() will fail. |
||
55 | require_once($sourcedir . '/Security.php'); |
||
56 | $user_info['permissions'] = array(); |
||
57 | |||
58 | // For the moddlog |
||
59 | $user_info['id'] = $this->_details['member_id']; |
||
60 | $user_info['ip'] = $this->_details['member_ip']; |
||
61 | |||
62 | require_once($sourcedir . '/Subs-Membergroups.php'); |
||
63 | addMembersToGroup($row['id_member'], $row['id_group'], 'auto', true); |
||
64 | } |
||
65 | |||
66 | // Build the required information array |
||
67 | $affected_users[$row['id_member']] = array( |
||
68 | 'rid' => $row['id_request'], |
||
69 | 'member_id' => $row['id_member'], |
||
70 | 'member_name' => $row['member_name'], |
||
71 | 'group_id' => $row['id_group'], |
||
72 | 'group_name' => $row['group_name'], |
||
73 | 'email' => $row['email_address'], |
||
74 | 'language' => $row['lngfile'], |
||
75 | ); |
||
76 | } |
||
77 | $smcFunc['db_free_result']($request); |
||
78 | |||
79 | // Ensure everyone who is online gets their changes right away. |
||
80 | updateSettings(array('settings_updated' => time())); |
||
81 | |||
82 | if (!empty($affected_users)) |
||
83 | { |
||
84 | require_once($sourcedir . '/Subs-Notify.php'); |
||
85 | $prefs = getNotifyPrefs($members, array('groupr_approved', 'groupr_rejected'), true); |
||
86 | |||
87 | // They are being approved? |
||
88 | if ($this->_details['status'] == 'approve') |
||
89 | { |
||
90 | $pref_name = 'approved'; |
||
91 | $email_template_name = 'mc_group_approve'; |
||
92 | $email_message_id_prefix = 'grpapp'; |
||
93 | } |
||
94 | // Otherwise, they are getting rejected (With or without a reason). |
||
95 | else |
||
96 | { |
||
97 | $pref_name = 'rejected'; |
||
98 | $email_template_name = empty($custom_reason) ? 'mc_group_reject' : 'mc_group_reject_reason'; |
||
|
|||
99 | $email_message_id_prefix = 'grprej'; |
||
100 | } |
||
101 | |||
102 | // Same as for approving, kind of. |
||
103 | foreach ($affected_users as $user) |
||
104 | { |
||
105 | $pref = !empty($prefs[$user['member_id']]['groupr_' . $pref_name]) ? $prefs[$user['member_id']]['groupr_' . $pref_name] : 0; |
||
106 | $custom_reason = isset($this->_details['reason']) && isset($this->_details['reason'][$user['rid']]) ? $this->_details['reason'][$user['rid']] : ''; |
||
107 | |||
108 | if ($pref & self::RECEIVE_NOTIFY_ALERT) |
||
109 | { |
||
110 | $alert_rows[] = array( |
||
111 | 'alert_time' => time(), |
||
112 | 'id_member' => $user['member_id'], |
||
113 | 'content_type' => 'groupr', |
||
114 | 'content_id' => 0, |
||
115 | 'content_action' => $pref_name, |
||
116 | 'is_read' => 0, |
||
117 | 'extra' => $smcFunc['json_encode'](array('group_name' => $user['group_name'], 'reason' => !empty($custom_reason) ? '<br><br>' . $custom_reason : '')), |
||
118 | ); |
||
119 | } |
||
120 | |||
121 | if ($pref & self::RECEIVE_NOTIFY_EMAIL) |
||
122 | { |
||
123 | // Emails are a bit complicated. We have to do language stuff. |
||
124 | require_once($sourcedir . '/Subs-Post.php'); |
||
125 | require_once($sourcedir . '/ScheduledTasks.php'); |
||
126 | loadEssentialThemeData(); |
||
127 | |||
128 | $replacements = array( |
||
129 | 'USERNAME' => $user['member_name'], |
||
130 | 'GROUPNAME' => $user['group_name'], |
||
131 | ); |
||
132 | |||
133 | if (!empty($custom_reason)) |
||
134 | $replacements['REASON'] = $custom_reason; |
||
135 | |||
136 | $emaildata = loadEmailTemplate($email_template_name, $replacements, $user['language']); |
||
137 | |||
138 | sendmail($user['email'], $emaildata['subject'], $emaildata['body'], null, $email_message_id_prefix . $user['rid'], $emaildata['is_html'], 2); |
||
139 | } |
||
140 | } |
||
141 | |||
142 | // Insert the alerts if any |
||
143 | if (!empty($alert_rows)) |
||
144 | { |
||
145 | $smcFunc['db_insert']('', |
||
146 | '{db_prefix}user_alerts', |
||
147 | array( |
||
148 | 'alert_time' => 'int', 'id_member' => 'int', 'content_type' => 'string', |
||
149 | 'content_id' => 'int', 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string', |
||
150 | ), |
||
151 | $alert_rows, |
||
152 | array() |
||
153 | ); |
||
154 | |||
155 | updateMemberData(array_keys($affected_users), array('alerts' => '+')); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | return true; |
||
160 | } |
||
163 | ?> |