| Conditions | 62 |
| Paths | > 20000 |
| Total Lines | 260 |
| Code Lines | 137 |
| Lines | 42 |
| Ratio | 16.15 % |
| 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 |
||
| 53 | function UnapprovedPosts() |
||
| 54 | { |
||
| 55 | global $txt, $scripturl, $context, $user_info, $smcFunc, $options, $modSettings; |
||
| 56 | |||
| 57 | $context['current_view'] = isset($_GET['sa']) && $_GET['sa'] == 'topics' ? 'topics' : 'replies'; |
||
| 58 | $context['page_title'] = $txt['mc_unapproved_posts']; |
||
| 59 | |||
| 60 | // Work out what boards we can work in! |
||
| 61 | $approve_boards = boardsAllowedTo('approve_posts'); |
||
| 62 | |||
| 63 | // If we filtered by board remove ones outside of this board. |
||
| 64 | // @todo Put a message saying we're filtered? |
||
| 65 | if (isset($_REQUEST['brd'])) |
||
| 66 | { |
||
| 67 | $filter_board = array((int) $_REQUEST['brd']); |
||
| 68 | $approve_boards = $approve_boards == array(0) ? $filter_board : array_intersect($approve_boards, $filter_board); |
||
| 69 | } |
||
| 70 | |||
| 71 | View Code Duplication | if ($approve_boards == array(0)) |
|
|
|
|||
| 72 | $approve_query = ''; |
||
| 73 | elseif (!empty($approve_boards)) |
||
| 74 | $approve_query = ' AND m.id_board IN (' . implode(',', $approve_boards) . ')'; |
||
| 75 | // Nada, zip, etc... |
||
| 76 | else |
||
| 77 | $approve_query = ' AND 1=0'; |
||
| 78 | |||
| 79 | // We also need to know where we can delete topics and/or replies to. |
||
| 80 | if ($context['current_view'] == 'topics') |
||
| 81 | { |
||
| 82 | $delete_own_boards = boardsAllowedTo('remove_own'); |
||
| 83 | $delete_any_boards = boardsAllowedTo('remove_any'); |
||
| 84 | $delete_own_replies = array(); |
||
| 85 | } |
||
| 86 | else |
||
| 87 | { |
||
| 88 | $delete_own_boards = boardsAllowedTo('delete_own'); |
||
| 89 | $delete_any_boards = boardsAllowedTo('delete_any'); |
||
| 90 | $delete_own_replies = boardsAllowedTo('delete_own_replies'); |
||
| 91 | } |
||
| 92 | |||
| 93 | $toAction = array(); |
||
| 94 | // Check if we have something to do? |
||
| 95 | View Code Duplication | if (isset($_GET['approve'])) |
|
| 96 | $toAction[] = (int) $_GET['approve']; |
||
| 97 | // Just a deletion? |
||
| 98 | elseif (isset($_GET['delete'])) |
||
| 99 | $toAction[] = (int) $_GET['delete']; |
||
| 100 | // Lots of approvals? |
||
| 101 | elseif (isset($_POST['item'])) |
||
| 102 | foreach ($_POST['item'] as $item) |
||
| 103 | $toAction[] = (int) $item; |
||
| 104 | |||
| 105 | // What are we actually doing. |
||
| 106 | View Code Duplication | if (isset($_GET['approve']) || (isset($_POST['do']) && $_POST['do'] == 'approve')) |
|
| 107 | $curAction = 'approve'; |
||
| 108 | elseif (isset($_GET['delete']) || (isset($_POST['do']) && $_POST['do'] == 'delete')) |
||
| 109 | $curAction = 'delete'; |
||
| 110 | |||
| 111 | // Right, so we have something to do? |
||
| 112 | if (!empty($toAction) && isset($curAction)) |
||
| 113 | { |
||
| 114 | checkSession('request'); |
||
| 115 | |||
| 116 | // Handy shortcut. |
||
| 117 | $any_array = $curAction == 'approve' ? $approve_boards : $delete_any_boards; |
||
| 118 | |||
| 119 | // Now for each message work out whether it's actually a topic, and what board it's on. |
||
| 120 | $request = $smcFunc['db_query']('', ' |
||
| 121 | SELECT m.id_msg, m.id_member, m.id_board, m.subject, t.id_topic, t.id_first_msg, t.id_member_started |
||
| 122 | FROM {db_prefix}messages AS m |
||
| 123 | INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic) |
||
| 124 | LEFT JOIN {db_prefix}boards AS b ON (t.id_board = b.id_board) |
||
| 125 | WHERE m.id_msg IN ({array_int:message_list}) |
||
| 126 | AND m.approved = {int:not_approved} |
||
| 127 | AND {query_see_board}', |
||
| 128 | array( |
||
| 129 | 'message_list' => $toAction, |
||
| 130 | 'not_approved' => 0, |
||
| 131 | ) |
||
| 132 | ); |
||
| 133 | $toAction = array(); |
||
| 134 | $details = array(); |
||
| 135 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 136 | { |
||
| 137 | // If it's not within what our view is ignore it... |
||
| 138 | if (($row['id_msg'] == $row['id_first_msg'] && $context['current_view'] != 'topics') || ($row['id_msg'] != $row['id_first_msg'] && $context['current_view'] != 'replies')) |
||
| 139 | continue; |
||
| 140 | |||
| 141 | $can_add = false; |
||
| 142 | // If we're approving this is simple. |
||
| 143 | if ($curAction == 'approve' && ($any_array == array(0) || in_array($row['id_board'], $any_array))) |
||
| 144 | { |
||
| 145 | $can_add = true; |
||
| 146 | } |
||
| 147 | // Delete requires more permission checks... |
||
| 148 | View Code Duplication | elseif ($curAction == 'delete') |
|
| 149 | { |
||
| 150 | // Own post is easy! |
||
| 151 | if ($row['id_member'] == $user_info['id'] && ($delete_own_boards == array(0) || in_array($row['id_board'], $delete_own_boards))) |
||
| 152 | $can_add = true; |
||
| 153 | // Is it a reply to their own topic? |
||
| 154 | elseif ($row['id_member'] == $row['id_member_started'] && $row['id_msg'] != $row['id_first_msg'] && ($delete_own_replies == array(0) || in_array($row['id_board'], $delete_own_replies))) |
||
| 155 | $can_add = true; |
||
| 156 | // Someone elses? |
||
| 157 | elseif ($row['id_member'] != $user_info['id'] && ($delete_any_boards == array(0) || in_array($row['id_board'], $delete_any_boards))) |
||
| 158 | $can_add = true; |
||
| 159 | } |
||
| 160 | |||
| 161 | if ($can_add) |
||
| 162 | $anItem = $context['current_view'] == 'topics' ? $row['id_topic'] : $row['id_msg']; |
||
| 163 | $toAction[] = $anItem; |
||
| 164 | |||
| 165 | // All clear. What have we got now, what, what? |
||
| 166 | $details[$anItem] = array(); |
||
| 167 | $details[$anItem]["subject"] = $row['subject']; |
||
| 168 | $details[$anItem]["topic"] = $row['id_topic']; |
||
| 169 | $details[$anItem]["member"] = ($context['current_view'] == 'topics') ? $row['id_member_started'] : $row['id_member']; |
||
| 170 | $details[$anItem]["board"] = $row['id_board']; |
||
| 171 | } |
||
| 172 | $smcFunc['db_free_result']($request); |
||
| 173 | |||
| 174 | // If we have anything left we can actually do the approving (etc). |
||
| 175 | if (!empty($toAction)) |
||
| 176 | { |
||
| 177 | if ($curAction == 'approve') |
||
| 178 | { |
||
| 179 | approveMessages($toAction, $details, $context['current_view']); |
||
| 180 | } |
||
| 181 | else |
||
| 182 | { |
||
| 183 | removeMessages($toAction, $details, $context['current_view']); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | // How many unapproved posts are there? |
||
| 189 | $request = $smcFunc['db_query']('', ' |
||
| 190 | SELECT COUNT(*) |
||
| 191 | FROM {db_prefix}messages AS m |
||
| 192 | INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic AND t.id_first_msg != m.id_msg) |
||
| 193 | INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board) |
||
| 194 | WHERE m.approved = {int:not_approved} |
||
| 195 | AND {query_see_board} |
||
| 196 | ' . $approve_query, |
||
| 197 | array( |
||
| 198 | 'not_approved' => 0, |
||
| 199 | ) |
||
| 200 | ); |
||
| 201 | list ($context['total_unapproved_posts']) = $smcFunc['db_fetch_row']($request); |
||
| 202 | $smcFunc['db_free_result']($request); |
||
| 203 | |||
| 204 | // What about topics? Normally we'd use the table alias t for topics but lets use m so we don't have to redo our approve query. |
||
| 205 | $request = $smcFunc['db_query']('', ' |
||
| 206 | SELECT COUNT(m.id_topic) |
||
| 207 | FROM {db_prefix}topics AS m |
||
| 208 | INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board) |
||
| 209 | WHERE m.approved = {int:not_approved} |
||
| 210 | AND {query_see_board} |
||
| 211 | ' . $approve_query, |
||
| 212 | array( |
||
| 213 | 'not_approved' => 0, |
||
| 214 | ) |
||
| 215 | ); |
||
| 216 | list ($context['total_unapproved_topics']) = $smcFunc['db_fetch_row']($request); |
||
| 217 | $smcFunc['db_free_result']($request); |
||
| 218 | |||
| 219 | // Limit to how many? (obey the user setting) |
||
| 220 | $limit = !empty($options['messages_per_page']) ? $options['messages_per_page'] : $modSettings['defaultMaxMessages']; |
||
| 221 | |||
| 222 | $context['page_index'] = constructPageIndex($scripturl . '?action=moderate;area=postmod;sa=' . $context['current_view'] . (isset($_REQUEST['brd']) ? ';brd=' . (int) $_REQUEST['brd'] : ''), $_GET['start'], $context['current_view'] == 'topics' ? $context['total_unapproved_topics'] : $context['total_unapproved_posts'], $limit); |
||
| 223 | $context['start'] = $_GET['start']; |
||
| 224 | |||
| 225 | // We have enough to make some pretty tabs! |
||
| 226 | $context[$context['moderation_menu_name']]['tab_data'] = array( |
||
| 227 | 'title' => $txt['mc_unapproved_posts'], |
||
| 228 | 'help' => 'postmod', |
||
| 229 | 'description' => $txt['mc_unapproved_posts_desc'], |
||
| 230 | ); |
||
| 231 | |||
| 232 | // Update the tabs with the correct number of posts. |
||
| 233 | $context['menu_data_' . $context['moderation_menu_id']]['sections']['posts']['areas']['postmod']['subsections']['posts']['label'] .= ' (' . $context['total_unapproved_posts'] . ')'; |
||
| 234 | $context['menu_data_' . $context['moderation_menu_id']]['sections']['posts']['areas']['postmod']['subsections']['topics']['label'] .= ' (' . $context['total_unapproved_topics'] . ')'; |
||
| 235 | |||
| 236 | // If we are filtering some boards out then make sure to send that along with the links. |
||
| 237 | if (isset($_REQUEST['brd'])) |
||
| 238 | { |
||
| 239 | $context['menu_data_' . $context['moderation_menu_id']]['sections']['posts']['areas']['postmod']['subsections']['posts']['add_params'] = ';brd=' . (int) $_REQUEST['brd']; |
||
| 240 | $context['menu_data_' . $context['moderation_menu_id']]['sections']['posts']['areas']['postmod']['subsections']['topics']['add_params'] = ';brd=' . (int) $_REQUEST['brd']; |
||
| 241 | } |
||
| 242 | |||
| 243 | // Get all unapproved posts. |
||
| 244 | $request = $smcFunc['db_query']('', ' |
||
| 245 | SELECT m.id_msg, m.id_topic, m.id_board, m.subject, m.body, m.id_member, |
||
| 246 | COALESCE(mem.real_name, m.poster_name) AS poster_name, m.poster_time, m.smileys_enabled, |
||
| 247 | t.id_member_started, t.id_first_msg, b.name AS board_name, c.id_cat, c.name AS cat_name |
||
| 248 | FROM {db_prefix}messages AS m |
||
| 249 | INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic) |
||
| 250 | INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board) |
||
| 251 | LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member) |
||
| 252 | LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat) |
||
| 253 | WHERE m.approved = {int:not_approved} |
||
| 254 | AND t.id_first_msg ' . ($context['current_view'] == 'topics' ? '=' : '!=') . ' m.id_msg |
||
| 255 | AND {query_see_board} |
||
| 256 | ' . $approve_query . ' |
||
| 257 | LIMIT {int:start}, {int:limit}', |
||
| 258 | array( |
||
| 259 | 'not_approved' => 0, |
||
| 260 | 'start' => $context['start'], |
||
| 261 | 'limit' => $limit, |
||
| 262 | ) |
||
| 263 | ); |
||
| 264 | $context['unapproved_items'] = array(); |
||
| 265 | for ($i = 1; $row = $smcFunc['db_fetch_assoc']($request); $i++) |
||
| 266 | { |
||
| 267 | // Can delete is complicated, let's solve it first... is it their own post? |
||
| 268 | View Code Duplication | if ($row['id_member'] == $user_info['id'] && ($delete_own_boards == array(0) || in_array($row['id_board'], $delete_own_boards))) |
|
| 269 | $can_delete = true; |
||
| 270 | // Is it a reply to their own topic? |
||
| 271 | elseif ($row['id_member'] == $row['id_member_started'] && $row['id_msg'] != $row['id_first_msg'] && ($delete_own_replies == array(0) || in_array($row['id_board'], $delete_own_replies))) |
||
| 272 | $can_delete = true; |
||
| 273 | // Someone elses? |
||
| 274 | elseif ($row['id_member'] != $user_info['id'] && ($delete_any_boards == array(0) || in_array($row['id_board'], $delete_any_boards))) |
||
| 275 | $can_delete = true; |
||
| 276 | else |
||
| 277 | $can_delete = false; |
||
| 278 | |||
| 279 | $context['unapproved_items'][] = array( |
||
| 280 | 'id' => $row['id_msg'], |
||
| 281 | 'counter' => $context['start'] + $i, |
||
| 282 | 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'], |
||
| 283 | 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '">' . $row['subject'] . '</a>', |
||
| 284 | 'subject' => $row['subject'], |
||
| 285 | 'body' => parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']), |
||
| 286 | 'time' => timeformat($row['poster_time']), |
||
| 287 | 'poster' => array( |
||
| 288 | 'id' => $row['id_member'], |
||
| 289 | 'name' => $row['poster_name'], |
||
| 290 | 'link' => $row['id_member'] ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>' : $row['poster_name'], |
||
| 291 | 'href' => $scripturl . '?action=profile;u=' . $row['id_member'], |
||
| 292 | ), |
||
| 293 | 'topic' => array( |
||
| 294 | 'id' => $row['id_topic'], |
||
| 295 | ), |
||
| 296 | 'board' => array( |
||
| 297 | 'id' => $row['id_board'], |
||
| 298 | 'name' => $row['board_name'], |
||
| 299 | 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>', |
||
| 300 | ), |
||
| 301 | 'category' => array( |
||
| 302 | 'id' => $row['id_cat'], |
||
| 303 | 'name' => $row['cat_name'], |
||
| 304 | 'link' => '<a href="' . $scripturl . '#c' . $row['id_cat'] . '">' . $row['cat_name'] . '</a>', |
||
| 305 | ), |
||
| 306 | 'can_delete' => $can_delete, |
||
| 307 | ); |
||
| 308 | } |
||
| 309 | $smcFunc['db_free_result']($request); |
||
| 310 | |||
| 311 | $context['sub_template'] = 'unapproved_posts'; |
||
| 312 | } |
||
| 313 | |||
| 813 | ?> |
||
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.