| Conditions | 33 |
| Paths | > 20000 |
| Total Lines | 210 |
| Code Lines | 102 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 17 |
| CRAP Score | 718.6814 |
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 |
||
| 34 | 1 | public function run() |
|
| 35 | { |
||
| 36 | 1 | global $scripturl, $txt; |
|
| 37 | |||
| 38 | 1 | $db = database(); |
|
| 39 | |||
| 40 | // Grab all the items awaiting approval and sort type then board - clear up any things that are no longer relevant. |
||
| 41 | 1 | $request = $db->query('', ' |
|
| 42 | SELECT aq.id_msg, aq.id_attach, aq.id_event, m.id_topic, m.id_board, m.subject, t.id_first_msg, |
||
| 43 | b.id_profile |
||
| 44 | FROM {db_prefix}approval_queue AS aq |
||
| 45 | INNER JOIN {db_prefix}messages AS m ON (m.id_msg = aq.id_msg) |
||
| 46 | INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic) |
||
| 47 | 1 | INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)', |
|
| 48 | array( |
||
| 49 | 1 | ) |
|
| 50 | 1 | ); |
|
| 51 | 1 | $notices = array(); |
|
| 52 | 1 | $profiles = array(); |
|
| 53 | 1 | while ($row = $db->fetch_assoc($request)) |
|
| 54 | { |
||
| 55 | // If this is no longer around we'll ignore it. |
||
| 56 | if (empty($row['id_topic'])) |
||
| 57 | continue; |
||
| 58 | |||
| 59 | // What type is it? |
||
| 60 | if ($row['id_first_msg'] && $row['id_first_msg'] == $row['id_msg']) |
||
| 61 | $type = 'topic'; |
||
| 62 | elseif ($row['id_attach']) |
||
| 63 | $type = 'attach'; |
||
| 64 | else |
||
| 65 | $type = 'msg'; |
||
| 66 | |||
| 67 | // Add it to the array otherwise. |
||
| 68 | $notices[$row['id_board']][$type][] = array( |
||
| 69 | 'subject' => $row['subject'], |
||
| 70 | 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'], |
||
| 71 | ); |
||
| 72 | |||
| 73 | // Store the profile for a bit later. |
||
| 74 | $profiles[$row['id_board']] = $row['id_profile']; |
||
| 75 | } |
||
| 76 | 1 | $db->free_result($request); |
|
| 77 | |||
| 78 | // Delete it all! |
||
| 79 | 1 | $db->query('', ' |
|
| 80 | 1 | DELETE FROM {db_prefix}approval_queue', |
|
| 81 | array( |
||
| 82 | 1 | ) |
|
| 83 | 1 | ); |
|
| 84 | |||
| 85 | // If nothing quit now. |
||
| 86 | 1 | if (empty($notices)) |
|
| 87 | 1 | return true; |
|
| 88 | |||
| 89 | // Now we need to think about finding out *who* can approve - this is hard! |
||
| 90 | // First off, get all the groups with this permission and sort by board. |
||
| 91 | $request = $db->query('', ' |
||
| 92 | SELECT id_group, id_profile, add_deny |
||
| 93 | FROM {db_prefix}board_permissions |
||
| 94 | WHERE permission = {string:approve_posts} |
||
| 95 | AND id_profile IN ({array_int:profile_list})', |
||
| 96 | array( |
||
| 97 | 'profile_list' => $profiles, |
||
| 98 | 'approve_posts' => 'approve_posts', |
||
| 99 | ) |
||
| 100 | ); |
||
| 101 | $perms = array(); |
||
| 102 | $addGroups = array(1); |
||
| 103 | while ($row = $db->fetch_assoc($request)) |
||
| 104 | { |
||
| 105 | // Sorry guys, but we have to ignore guests AND members - it would be too many otherwise. |
||
| 106 | if ($row['id_group'] < 2) |
||
| 107 | continue; |
||
| 108 | |||
| 109 | $perms[$row['id_profile']][$row['add_deny'] ? 'add' : 'deny'][] = $row['id_group']; |
||
| 110 | |||
| 111 | // Anyone who can access has to be considered. |
||
| 112 | if ($row['add_deny']) |
||
| 113 | $addGroups[] = $row['id_group']; |
||
| 114 | } |
||
| 115 | $db->free_result($request); |
||
| 116 | |||
| 117 | // Grab the moderators if they have permission! |
||
| 118 | $mods = array(); |
||
| 119 | $members = array(); |
||
| 120 | if (in_array(2, $addGroups)) |
||
| 121 | { |
||
| 122 | require_once(SUBSDIR . '/Boards.subs.php'); |
||
| 123 | $all_mods = allBoardModerators(true); |
||
| 124 | |||
| 125 | // Make sure they get included in the big loop. |
||
| 126 | $members = array_keys($all_mods); |
||
| 127 | foreach ($all_mods as $rows) |
||
| 128 | foreach ($rows as $row) |
||
| 129 | $mods[$row['id_member']][$row['id_board']] = true; |
||
| 130 | } |
||
| 131 | |||
| 132 | // Come along one and all... until we reject you ;) |
||
| 133 | $request = $db->query('', ' |
||
| 134 | SELECT id_member, real_name, email_address, lngfile, id_group, additional_groups, mod_prefs |
||
| 135 | FROM {db_prefix}members |
||
| 136 | WHERE id_group IN ({array_int:additional_group_list}) |
||
| 137 | OR FIND_IN_SET({raw:additional_group_list_implode}, additional_groups) != 0' . (empty($members) ? '' : ' |
||
| 138 | OR id_member IN ({array_int:member_list})') . ' |
||
| 139 | ORDER BY lngfile', |
||
| 140 | array( |
||
| 141 | 'additional_group_list' => $addGroups, |
||
| 142 | 'member_list' => $members, |
||
| 143 | 'additional_group_list_implode' => implode(', additional_groups) != 0 OR FIND_IN_SET(', $addGroups), |
||
| 144 | ) |
||
| 145 | ); |
||
| 146 | $members = array(); |
||
| 147 | while ($row = $db->fetch_assoc($request)) |
||
| 148 | { |
||
| 149 | // Check whether they are interested. |
||
| 150 | if (!empty($row['mod_prefs'])) |
||
| 151 | { |
||
| 152 | list (,, $pref_binary) = explode('|', $row['mod_prefs']); |
||
| 153 | if (!($pref_binary & 4)) |
||
| 154 | continue; |
||
| 155 | } |
||
| 156 | |||
| 157 | $members[$row['id_member']] = array( |
||
| 158 | 'id' => $row['id_member'], |
||
| 159 | 'groups' => array_merge(explode(',', $row['additional_groups']), array($row['id_group'])), |
||
| 160 | 'language' => $row['lngfile'], |
||
| 161 | 'email' => $row['email_address'], |
||
| 162 | 'name' => $row['real_name'], |
||
| 163 | ); |
||
| 164 | } |
||
| 165 | $db->free_result($request); |
||
| 166 | |||
| 167 | // Get the mailing stuff. |
||
| 168 | require_once(SUBSDIR . '/Mail.subs.php'); |
||
| 169 | |||
| 170 | // Need the below for loadLanguage to work! |
||
| 171 | loadEssentialThemeData(); |
||
| 172 | |||
| 173 | $current_language = ''; |
||
| 174 | |||
| 175 | // Finally, loop through each member, work out what they can do, and send it. |
||
| 176 | foreach ($members as $id => $member) |
||
| 177 | { |
||
| 178 | $emailbody = ''; |
||
| 179 | |||
| 180 | // Load the language file as required. |
||
| 181 | if (empty($current_language) || $current_language != $member['language']) |
||
| 182 | $current_language = loadLanguage('EmailTemplates', $member['language'], false); |
||
| 183 | |||
| 184 | // Loop through each notice... |
||
| 185 | foreach ($notices as $board => $notice) |
||
| 186 | { |
||
| 187 | $access = false; |
||
| 188 | |||
| 189 | // Can they mod in this board? |
||
| 190 | if (isset($mods[$id][$board])) |
||
| 191 | $access = true; |
||
| 192 | |||
| 193 | // Do the group check... |
||
| 194 | if (!$access && isset($perms[$profiles[$board]]['add'])) |
||
| 195 | { |
||
| 196 | // They can access?! |
||
| 197 | if (array_intersect($perms[$profiles[$board]]['add'], $member['groups'])) |
||
| 198 | $access = true; |
||
| 199 | |||
| 200 | // If they have deny rights don't consider them! |
||
| 201 | if (isset($perms[$profiles[$board]]['deny'])) |
||
| 202 | if (array_intersect($perms[$profiles[$board]]['deny'], $member['groups'])) |
||
| 203 | $access = false; |
||
| 204 | } |
||
| 205 | |||
| 206 | // Finally, fix it for admins! |
||
| 207 | if (in_array(1, $member['groups'])) |
||
| 208 | $access = true; |
||
| 209 | |||
| 210 | // If they can't access it then give it a break! |
||
| 211 | if (!$access) |
||
| 212 | continue; |
||
| 213 | |||
| 214 | foreach ($notice as $type => $items) |
||
| 215 | { |
||
| 216 | // Build up the top of this section. |
||
| 217 | $emailbody .= $txt['scheduled_approval_email_' . $type] . "\n" . |
||
| 218 | '------------------------------------------------------' . "\n"; |
||
| 219 | |||
| 220 | foreach ($items as $item) |
||
| 221 | $emailbody .= $item['subject'] . ' - ' . $item['href'] . "\n"; |
||
| 222 | |||
| 223 | $emailbody .= "\n"; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | if ($emailbody == '') |
||
| 228 | continue; |
||
| 229 | |||
| 230 | $replacements = array( |
||
| 231 | 'REALNAME' => $member['name'], |
||
| 232 | 'BODY' => $emailbody, |
||
| 233 | ); |
||
| 234 | |||
| 235 | $emaildata = loadEmailTemplate('scheduled_approval', $replacements, $current_language); |
||
| 236 | |||
| 237 | // Send the actual email. |
||
| 238 | sendmail($member['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 2); |
||
| 239 | } |
||
| 240 | |||
| 241 | // All went well! |
||
| 242 | return true; |
||
| 243 | } |
||
| 244 | } |