| Conditions | 45 |
| Paths | > 20000 |
| Total Lines | 278 |
| Code Lines | 173 |
| Lines | 9 |
| Ratio | 3.24 % |
| 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 |
||
| 29 | function ViewModlog() |
||
| 30 | { |
||
| 31 | global $txt, $context, $scripturl, $sourcedir, $smcFunc; |
||
| 32 | |||
| 33 | // Are we looking at the moderation log or the administration log. |
||
| 34 | $context['log_type'] = isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'adminlog' ? 3 : 1; |
||
| 35 | if ($context['log_type'] == 3) |
||
| 36 | isAllowedTo('admin_forum'); |
||
| 37 | |||
| 38 | // These change dependant on whether we are viewing the moderation or admin log. |
||
| 39 | if ($context['log_type'] == 3 || $_REQUEST['action'] == 'admin') |
||
| 40 | $context['url_start'] = '?action=admin;area=logs;sa=' . ($context['log_type'] == 3 ? 'adminlog' : 'modlog') . ';type=' . $context['log_type']; |
||
| 41 | else |
||
| 42 | $context['url_start'] = '?action=moderate;area=modlog;type=' . $context['log_type']; |
||
| 43 | |||
| 44 | $context['can_delete'] = allowedTo('admin_forum'); |
||
| 45 | |||
| 46 | loadLanguage('Modlog'); |
||
| 47 | |||
| 48 | $context['page_title'] = $context['log_type'] == 3 ? $txt['modlog_admin_log'] : $txt['modlog_view']; |
||
| 49 | |||
| 50 | // The number of entries to show per page of log file. |
||
| 51 | $context['displaypage'] = 30; |
||
| 52 | |||
| 53 | // Handle deletion... |
||
| 54 | if (isset($_POST['removeall']) && $context['can_delete']) |
||
| 55 | { |
||
| 56 | checkSession(); |
||
| 57 | validateToken('mod-ml'); |
||
| 58 | |||
| 59 | $smcFunc['db_query']('', ' |
||
| 60 | DELETE FROM {db_prefix}log_actions |
||
| 61 | WHERE id_log = {int:moderate_log}', |
||
| 62 | array( |
||
| 63 | 'moderate_log' => $context['log_type'], |
||
| 64 | ) |
||
| 65 | ); |
||
| 66 | |||
| 67 | $log_type = isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'adminlog' ? 'admin' : 'moderate'; |
||
| 68 | logAction('clearlog_' . $log_type, array(), $log_type); |
||
| 69 | |||
| 70 | } |
||
| 71 | elseif (!empty($_POST['remove']) && isset($_POST['delete']) && $context['can_delete']) |
||
| 72 | { |
||
| 73 | checkSession(); |
||
| 74 | validateToken('mod-ml'); |
||
| 75 | |||
| 76 | // No sneaky removing the 'cleared the log' entries. |
||
| 77 | $smcFunc['db_query']('', ' |
||
| 78 | DELETE FROM {db_prefix}log_actions |
||
| 79 | WHERE id_log = {int:moderate_log} |
||
| 80 | AND id_action IN ({array_string:delete_actions}) |
||
| 81 | AND action NOT LIKE {string:clearlog}', |
||
| 82 | array( |
||
| 83 | 'delete_actions' => array_unique($_POST['delete']), |
||
| 84 | 'moderate_log' => $context['log_type'], |
||
| 85 | 'clearlog' => 'clearlog_%', |
||
| 86 | ) |
||
| 87 | ); |
||
| 88 | } |
||
| 89 | |||
| 90 | // Do the column stuff! |
||
| 91 | $sort_types = array( |
||
| 92 | 'action' =>'lm.action', |
||
| 93 | 'time' => 'lm.log_time', |
||
| 94 | 'member' => 'mem.real_name', |
||
| 95 | 'group' => 'mg.group_name', |
||
| 96 | 'ip' => 'lm.ip', |
||
| 97 | ); |
||
| 98 | |||
| 99 | // Setup the direction stuff... |
||
| 100 | $context['order'] = isset($_REQUEST['sort']) && isset($sort_types[$_REQUEST['sort']]) ? $_REQUEST['sort'] : 'time'; |
||
| 101 | |||
| 102 | // If we're coming from a search, get the variables. |
||
| 103 | View Code Duplication | if (!empty($_REQUEST['params']) && empty($_REQUEST['is_search'])) |
|
|
|
|||
| 104 | { |
||
| 105 | $search_params = base64_decode(strtr($_REQUEST['params'], array(' ' => '+'))); |
||
| 106 | $search_params = $smcFunc['json_decode']($search_params, true); |
||
| 107 | } |
||
| 108 | |||
| 109 | // This array houses all the valid search types. |
||
| 110 | $searchTypes = array( |
||
| 111 | 'action' => array('sql' => 'lm.action', 'label' => $txt['modlog_action']), |
||
| 112 | 'member' => array('sql' => 'mem.real_name', 'label' => $txt['modlog_member']), |
||
| 113 | 'group' => array('sql' => 'mg.group_name', 'label' => $txt['modlog_position']), |
||
| 114 | 'ip' => array('sql' => 'lm.ip', 'label' => $txt['modlog_ip']) |
||
| 115 | ); |
||
| 116 | |||
| 117 | if (!isset($search_params['string']) || (!empty($_REQUEST['search']) && $search_params['string'] != $_REQUEST['search'])) |
||
| 118 | $search_params_string = empty($_REQUEST['search']) ? '' : $_REQUEST['search']; |
||
| 119 | else |
||
| 120 | $search_params_string = $search_params['string']; |
||
| 121 | |||
| 122 | View Code Duplication | if (isset($_REQUEST['search_type']) || empty($search_params['type']) || !isset($searchTypes[$search_params['type']])) |
|
| 123 | $search_params_type = isset($_REQUEST['search_type']) && isset($searchTypes[$_REQUEST['search_type']]) ? $_REQUEST['search_type'] : (isset($searchTypes[$context['order']]) ? $context['order'] : 'member'); |
||
| 124 | else |
||
| 125 | $search_params_type = $search_params['type']; |
||
| 126 | |||
| 127 | $search_params_column = $searchTypes[$search_params_type]['sql']; |
||
| 128 | $search_params = array( |
||
| 129 | 'string' => $search_params_string, |
||
| 130 | 'type' => $search_params_type, |
||
| 131 | ); |
||
| 132 | |||
| 133 | // Setup the search context. |
||
| 134 | $context['search_params'] = empty($search_params['string']) ? '' : base64_encode($smcFunc['json_encode']($search_params)); |
||
| 135 | $context['search'] = array( |
||
| 136 | 'string' => $search_params['string'], |
||
| 137 | 'type' => $search_params['type'], |
||
| 138 | 'label' => $searchTypes[$search_params_type]['label'], |
||
| 139 | ); |
||
| 140 | |||
| 141 | // If they are searching by action, then we must do some manual intervention to search in their language! |
||
| 142 | if ($search_params['type'] == 'action' && !empty($search_params['string'])) |
||
| 143 | { |
||
| 144 | // For the moment they can only search for ONE action! |
||
| 145 | foreach ($txt as $key => $text) |
||
| 146 | { |
||
| 147 | if (substr($key, 0, 10) == 'modlog_ac_' && strpos($text, $search_params['string']) !== false) |
||
| 148 | { |
||
| 149 | $search_params['string'] = substr($key, 10); |
||
| 150 | break; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | require_once($sourcedir . '/Subs-List.php'); |
||
| 156 | |||
| 157 | // This is all the information required for a watched user listing. |
||
| 158 | $listOptions = array( |
||
| 159 | 'id' => 'moderation_log_list', |
||
| 160 | 'width' => '100%', |
||
| 161 | 'items_per_page' => $context['displaypage'], |
||
| 162 | 'no_items_label' => $txt['modlog_' . ($context['log_type'] == 3 ? 'admin_log_' : '') . 'no_entries_found'], |
||
| 163 | 'base_href' => $scripturl . $context['url_start'] . (!empty($context['search_params']) ? ';params=' . $context['search_params'] : ''), |
||
| 164 | 'default_sort_col' => 'time', |
||
| 165 | 'get_items' => array( |
||
| 166 | 'function' => 'list_getModLogEntries', |
||
| 167 | 'params' => array( |
||
| 168 | (!empty($search_params['string']) ? ' INSTR({raw:sql_type}, {string:search_string})' : ''), |
||
| 169 | array('sql_type' => $search_params_column, 'search_string' => $search_params['string']), |
||
| 170 | $context['log_type'], |
||
| 171 | ), |
||
| 172 | ), |
||
| 173 | 'get_count' => array( |
||
| 174 | 'function' => 'list_getModLogEntryCount', |
||
| 175 | 'params' => array( |
||
| 176 | (!empty($search_params['string']) ? ' INSTR({raw:sql_type}, {string:search_string})' : ''), |
||
| 177 | array('sql_type' => $search_params_column, 'search_string' => $search_params['string']), |
||
| 178 | $context['log_type'], |
||
| 179 | ), |
||
| 180 | ), |
||
| 181 | // This assumes we are viewing by user. |
||
| 182 | 'columns' => array( |
||
| 183 | 'action' => array( |
||
| 184 | 'header' => array( |
||
| 185 | 'value' => $txt['modlog_action'], |
||
| 186 | 'class' => 'lefttext', |
||
| 187 | ), |
||
| 188 | 'data' => array( |
||
| 189 | 'db' => 'action_text', |
||
| 190 | 'class' => 'smalltext', |
||
| 191 | ), |
||
| 192 | 'sort' => array( |
||
| 193 | 'default' => 'lm.action', |
||
| 194 | 'reverse' => 'lm.action DESC', |
||
| 195 | ), |
||
| 196 | ), |
||
| 197 | 'time' => array( |
||
| 198 | 'header' => array( |
||
| 199 | 'value' => $txt['modlog_date'], |
||
| 200 | 'class' => 'lefttext', |
||
| 201 | ), |
||
| 202 | 'data' => array( |
||
| 203 | 'db' => 'time', |
||
| 204 | 'class' => 'smalltext', |
||
| 205 | ), |
||
| 206 | 'sort' => array( |
||
| 207 | 'default' => 'lm.log_time DESC', |
||
| 208 | 'reverse' => 'lm.log_time', |
||
| 209 | ), |
||
| 210 | ), |
||
| 211 | 'moderator' => array( |
||
| 212 | 'header' => array( |
||
| 213 | 'value' => $txt['modlog_member'], |
||
| 214 | 'class' => 'lefttext', |
||
| 215 | ), |
||
| 216 | 'data' => array( |
||
| 217 | 'db' => 'moderator_link', |
||
| 218 | 'class' => 'smalltext', |
||
| 219 | ), |
||
| 220 | 'sort' => array( |
||
| 221 | 'default' => 'mem.real_name', |
||
| 222 | 'reverse' => 'mem.real_name DESC', |
||
| 223 | ), |
||
| 224 | ), |
||
| 225 | 'position' => array( |
||
| 226 | 'header' => array( |
||
| 227 | 'value' => $txt['modlog_position'], |
||
| 228 | 'class' => 'lefttext', |
||
| 229 | ), |
||
| 230 | 'data' => array( |
||
| 231 | 'db' => 'position', |
||
| 232 | 'class' => 'smalltext', |
||
| 233 | ), |
||
| 234 | 'sort' => array( |
||
| 235 | 'default' => 'mg.group_name', |
||
| 236 | 'reverse' => 'mg.group_name DESC', |
||
| 237 | ), |
||
| 238 | ), |
||
| 239 | 'ip' => array( |
||
| 240 | 'header' => array( |
||
| 241 | 'value' => $txt['modlog_ip'], |
||
| 242 | 'class' => 'lefttext', |
||
| 243 | ), |
||
| 244 | 'data' => array( |
||
| 245 | 'db' => 'ip', |
||
| 246 | 'class' => 'smalltext', |
||
| 247 | ), |
||
| 248 | 'sort' => array( |
||
| 249 | 'default' => 'lm.ip', |
||
| 250 | 'reverse' => 'lm.ip DESC', |
||
| 251 | ), |
||
| 252 | ), |
||
| 253 | 'delete' => array( |
||
| 254 | 'header' => array( |
||
| 255 | 'value' => '<input type="checkbox" name="all" onclick="invertAll(this, this.form);">', |
||
| 256 | 'class' => 'centercol', |
||
| 257 | ), |
||
| 258 | 'data' => array( |
||
| 259 | 'function' => function ($entry) |
||
| 260 | { |
||
| 261 | return '<input type="checkbox" name="delete[]" value="' . $entry['id'] . '"' . ($entry['editable'] ? '' : ' disabled') . '>'; |
||
| 262 | }, |
||
| 263 | 'class' => 'centercol', |
||
| 264 | ), |
||
| 265 | ), |
||
| 266 | ), |
||
| 267 | 'form' => array( |
||
| 268 | 'href' => $scripturl . $context['url_start'], |
||
| 269 | 'include_sort' => true, |
||
| 270 | 'include_start' => true, |
||
| 271 | 'hidden_fields' => array( |
||
| 272 | $context['session_var'] => $context['session_id'], |
||
| 273 | 'params' => $context['search_params'] |
||
| 274 | ), |
||
| 275 | 'token' => 'mod-ml', |
||
| 276 | ), |
||
| 277 | 'additional_rows' => array( |
||
| 278 | array( |
||
| 279 | 'position' => 'below_table_data', |
||
| 280 | 'value' => ' |
||
| 281 | ' . $txt['modlog_search'] . ' (' . $txt['modlog_by'] . ': ' . $context['search']['label'] . '): |
||
| 282 | <input type="text" name="search" size="18" value="' . $smcFunc['htmlspecialchars']($context['search']['string']) . '"> |
||
| 283 | <input type="submit" name="is_search" value="' . $txt['modlog_go'] . '" class="button_submit" style="float:none"> |
||
| 284 | ' . ($context['can_delete'] ? ' |
||
| 285 | <input type="submit" name="remove" value="' . $txt['modlog_remove'] . '" data-confirm="' . $txt['modlog_remove_selected_confirm'] . '" class="button_submit you_sure"> |
||
| 286 | <input type="submit" name="removeall" value="' . $txt['modlog_removeall'] . '" data-confirm="' . $txt['modlog_remove_all_confirm'] . '" class="button_submit you_sure">' : ''), |
||
| 287 | 'class' => 'floatright', |
||
| 288 | ), |
||
| 289 | ), |
||
| 290 | ); |
||
| 291 | |||
| 292 | createToken('mod-ml'); |
||
| 293 | |||
| 294 | // Create the watched user list. |
||
| 295 | createList($listOptions); |
||
| 296 | |||
| 297 | $context['sub_template'] = 'show_list'; |
||
| 298 | $context['default_list'] = 'moderation_log_list'; |
||
| 299 | |||
| 300 | if (isset($context['moderation_menu_name'])) |
||
| 301 | $context[$context['moderation_menu_name']]['tab_data'] = array( |
||
| 302 | 'title' => $txt['modlog_' . ($context['log_type'] == 3 ? 'admin' : 'moderation') . '_log'], |
||
| 303 | 'help' => $context['log_type'] == 3 ? 'adminlog' : 'modlog', |
||
| 304 | 'description' => $txt['modlog_' . ($context['log_type'] == 3 ? 'admin' : 'moderation') . '_log_desc'] |
||
| 305 | ); |
||
| 306 | } |
||
| 307 | |||
| 651 | ?> |
||
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.