| Conditions | 61 |
| Paths | > 20000 |
| Total Lines | 367 |
| Code Lines | 216 |
| Lines | 12 |
| Ratio | 3.27 % |
| 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 |
||
| 26 | public function main($id, $mode) |
||
| 27 | { |
||
| 28 | global $phpbb_container, $db, $user, $phpbb_log, $template, $cache, $request, $table_prefix; |
||
| 29 | |||
| 30 | $this->qte = $phpbb_container->get('ernadoo.qte'); |
||
| 31 | $this->migrator_tool_permission = $phpbb_container->get('migrator.tool.permission'); |
||
| 32 | |||
| 33 | $action = $request->variable('action', ''); |
||
| 34 | $submit = $request->is_set_post('submit'); |
||
| 35 | $attr_id = $request->variable('id', 0); |
||
| 36 | $attr_auth_id = $request->variable('attr_auth_id', 0); |
||
| 37 | |||
| 38 | $error = array(); |
||
| 39 | $clear_dest_perms = false; |
||
| 40 | |||
| 41 | $this->tpl_name = 'acp_attributes'; |
||
|
|
|||
| 42 | $this->page_title = 'QTE_MANAGE_TITLE'; |
||
| 43 | |||
| 44 | $user->add_lang_ext('ernadoo/qte', array('attributes', 'attributes_acp')); |
||
| 45 | |||
| 46 | // Display a warning when a development version is installed or if the database is outdated |
||
| 47 | $this->display_version_warning(); |
||
| 48 | |||
| 49 | add_form_key('acp_attributes'); |
||
| 50 | |||
| 51 | switch ($action) |
||
| 52 | { |
||
| 53 | case 'edit': |
||
| 54 | case 'add': |
||
| 55 | |||
| 56 | $attr_type = $request->variable('attr_type', 0); |
||
| 57 | $attr_name = $request->variable('attr_name', '', true); |
||
| 58 | $attr_img = $request->variable('attr_img', ''); |
||
| 59 | $attr_desc = $request->variable('attr_desc', '', true); |
||
| 60 | $attr_date = $request->variable('attr_date', ''); |
||
| 61 | $attr_colour = $request->variable('attr_colour', ''); |
||
| 62 | $attr_user_colour = $request->variable('attr_user_colour', 0); |
||
| 63 | |||
| 64 | if ($submit) |
||
| 65 | { |
||
| 66 | if (!check_form_key('acp_attributes')) |
||
| 67 | { |
||
| 68 | $error[] = $user->lang['FORM_INVALID']; |
||
| 69 | } |
||
| 70 | |||
| 71 | if (empty($attr_name)) |
||
| 72 | { |
||
| 73 | $error[] = $user->lang['QTE_NAME_ERROR']; |
||
| 74 | } |
||
| 75 | |||
| 76 | if (isset($attr_desc[60])) |
||
| 77 | { |
||
| 78 | $error[] = $user->lang['QTE_DESC_ERROR']; |
||
| 79 | } |
||
| 80 | |||
| 81 | // fully xhtml compatibility : no capital letters |
||
| 82 | if (!empty($attr_colour)) |
||
| 83 | { |
||
| 84 | $attr_colour = strtolower($attr_colour); |
||
| 85 | if (!preg_match('#^([a-f0-9]){6}#i', $attr_colour)) |
||
| 86 | { |
||
| 87 | $error[] = $user->lang['QTE_COLOUR_ERROR']; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | // we don't need user colour when an image is used as attribute |
||
| 92 | if ($attr_type && $attr_user_colour) |
||
| 93 | { |
||
| 94 | $attr_user_colour = false; |
||
| 95 | } |
||
| 96 | |||
| 97 | $attr_name_tmp = $user->lang($attr_name); |
||
| 98 | if ($attr_user_colour) |
||
| 99 | { |
||
| 100 | if (strpos($attr_name_tmp, '%mod%') === false) |
||
| 101 | { |
||
| 102 | $error[] = $user->lang['QTE_USER_COLOUR_ERROR']; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | if (!empty($attr_date)) |
||
| 107 | { |
||
| 108 | if (strpos($attr_name_tmp, '%date%') === false) |
||
| 109 | { |
||
| 110 | $error[] = $user->lang['QTE_DATE_ARGUMENT_ERROR']; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | else |
||
| 114 | { |
||
| 115 | if (strpos($attr_name_tmp, '%date%') !== false) |
||
| 116 | { |
||
| 117 | $error[] = $user->lang['QTE_DATE_FORMAT_ERROR']; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | unset($attr_name_tmp); |
||
| 121 | |||
| 122 | if (!sizeof($error)) |
||
| 123 | { |
||
| 124 | $sql_ary = array( |
||
| 125 | 'attr_type' => $attr_type, |
||
| 126 | 'attr_name' => $attr_name, |
||
| 127 | 'attr_img' => $attr_img, |
||
| 128 | 'attr_desc' => $attr_desc, |
||
| 129 | 'attr_date' => $attr_date, |
||
| 130 | 'attr_colour' => $attr_colour, |
||
| 131 | 'attr_user_colour' => $attr_user_colour, |
||
| 132 | ); |
||
| 133 | |||
| 134 | if ($attr_id) |
||
| 135 | { |
||
| 136 | $sql = 'UPDATE ' . $table_prefix . 'topics_attr |
||
| 137 | SET ' . $db->sql_build_array('UPDATE', $sql_ary) . ' |
||
| 138 | WHERE attr_id = ' . (int) $attr_id; |
||
| 139 | $db->sql_query($sql); |
||
| 140 | |||
| 141 | $clear_dest_perms = true; |
||
| 142 | $message = 'UPDATED'; |
||
| 143 | } |
||
| 144 | else |
||
| 145 | { |
||
| 146 | $sql = 'SELECT MAX(right_id) AS right_id |
||
| 147 | FROM ' . $table_prefix . 'topics_attr'; |
||
| 148 | $result = $db->sql_query($sql); |
||
| 149 | $right_id = (int) $db->sql_fetchfield('right_id'); |
||
| 150 | $db->sql_freeresult($result); |
||
| 151 | |||
| 152 | $sql_ary['left_id'] = ($right_id + 1); |
||
| 153 | $sql_ary['right_id'] = ($right_id + 2); |
||
| 154 | |||
| 155 | $sql = 'INSERT INTO ' . $table_prefix . 'topics_attr ' . $db->sql_build_array('INSERT', $sql_ary); |
||
| 156 | $db->sql_query($sql); |
||
| 157 | $attr_id = $db->sql_nextid(); |
||
| 158 | |||
| 159 | $this->migrator_tool_permission->add('f_qte_attr_'.$attr_id, false); |
||
| 160 | |||
| 161 | $message = 'ADDED'; |
||
| 162 | } |
||
| 163 | |||
| 164 | if ($attr_auth_id) |
||
| 165 | { |
||
| 166 | $this->_copy_permission('f_qte_attr_'.$attr_id, 'f_qte_attr_'.$attr_auth_id, $clear_dest_perms); |
||
| 167 | } |
||
| 168 | |||
| 169 | $cache->destroy('_attr'); |
||
| 170 | |||
| 171 | $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_ATTRIBUTE_' . $message, time(), array($attr_name)); |
||
| 172 | |||
| 173 | trigger_error($user->lang['QTE_' . $message] . adm_back_link($this->u_action)); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | else if ($attr_id) |
||
| 177 | { |
||
| 178 | $attr = $this->_get_attr_info($attr_id); |
||
| 179 | } |
||
| 180 | |||
| 181 | if ($action == 'edit') |
||
| 182 | { |
||
| 183 | $template->assign_vars(array( |
||
| 184 | 'L_QTE_ADD_EDIT' => $user->lang['QTE_EDIT'], |
||
| 185 | 'L_QTE_ADD_EDIT_EXPLAIN' => $user->lang['QTE_EDIT_EXPLAIN'], |
||
| 186 | )); |
||
| 187 | } |
||
| 188 | else |
||
| 189 | { |
||
| 190 | $template->assign_vars(array( |
||
| 191 | 'L_QTE_ADD_EDIT' => $user->lang['QTE_ADD'], |
||
| 192 | 'L_QTE_ADD_EDIT_EXPLAIN' => $user->lang['QTE_ADD_EXPLAIN'], |
||
| 193 | )); |
||
| 194 | } |
||
| 195 | |||
| 196 | $this->qte_attr_select($attr_id); |
||
| 197 | |||
| 198 | if (sizeof($error)) |
||
| 199 | { |
||
| 200 | $template->assign_vars(array( |
||
| 201 | 'S_ERROR' => true, |
||
| 202 | 'ERROR_MSG' => implode('<br />', $error), |
||
| 203 | )); |
||
| 204 | } |
||
| 205 | |||
| 206 | $attr_type_state = ((isset($attr['attr_type']) && $attr['attr_type']) || (isset($attr_type) && $attr_type)); |
||
| 207 | $attr_user_colour_state = ((isset($attr['attr_user_colour']) && $attr['attr_user_colour']) || (isset($attr_user_colour) && $attr_user_colour)); |
||
| 208 | |||
| 209 | $template->assign_vars(array( |
||
| 210 | 'S_EDIT' => true, |
||
| 211 | |||
| 212 | 'U_ACTION' => $this->u_action . '&action=' . (($action == 'add') ? 'add' : 'edit&id=' . (int) $attr_id), |
||
| 213 | 'U_BACK' => $this->u_action, |
||
| 214 | 'U_AJAX' => str_replace('&', '&', $this->u_action), |
||
| 215 | |||
| 216 | 'L_QTE_NAME_EXPLAIN' => $user->lang('QTE_NAME_EXPLAIN', $user->data['username']), |
||
| 217 | |||
| 218 | 'ATTR_ID' => isset($attr['attr_id']) ? $attr['attr_id'] : $attr_id, |
||
| 219 | 'ATTR_NAME' => isset($attr['attr_name']) ? $attr['attr_name'] : $attr_name, |
||
| 220 | 'ATTR_IMG' => isset($attr['attr_img']) ? $attr['attr_img'] : $attr_img, |
||
| 221 | 'ATTR_DESC' => isset($attr['attr_desc']) ? $attr['attr_desc'] : $attr_desc, |
||
| 222 | 'ATTR_DATE' => isset($attr['attr_date']) ? $attr['attr_date'] : $attr_date, |
||
| 223 | 'ATTR_COLOUR' => isset($attr['attr_colour']) ? $attr['attr_colour'] : $attr_colour, |
||
| 224 | |||
| 225 | 'S_TEXT' => $attr_type_state ? true : false, |
||
| 226 | 'S_USER_COLOUR' => $attr_user_colour_state ? true : false, |
||
| 227 | |||
| 228 | )); |
||
| 229 | |||
| 230 | return; |
||
| 231 | |||
| 232 | break; |
||
| 233 | |||
| 234 | case 'delete': |
||
| 235 | |||
| 236 | View Code Duplication | if (!$attr_id) |
|
| 237 | { |
||
| 238 | trigger_error($user->lang['QTE_MUST_SELECT'] . adm_back_link($this->u_action), E_USER_WARNING); |
||
| 239 | } |
||
| 240 | |||
| 241 | if (confirm_box(true)) |
||
| 242 | { |
||
| 243 | $sql = 'SELECT topic_id, topic_attr_id |
||
| 244 | FROM ' . TOPICS_TABLE . ' |
||
| 245 | WHERE topic_attr_id = ' . (int) $attr_id; |
||
| 246 | $result = $db->sql_query($sql); |
||
| 247 | |||
| 248 | $topic_id_ary = array(); |
||
| 249 | while ($row = $db->sql_fetchrow($result)) |
||
| 250 | { |
||
| 251 | $topic_id_ary[] = (int) $row['topic_id']; |
||
| 252 | } |
||
| 253 | $db->sql_freeresult($result); |
||
| 254 | |||
| 255 | if (sizeof($topic_id_ary)) |
||
| 256 | { |
||
| 257 | $fields = array('topic_attr_id' => 0, 'topic_attr_user' => 0, 'topic_attr_time' => 0); |
||
| 258 | |||
| 259 | $sql = 'UPDATE ' . TOPICS_TABLE . ' |
||
| 260 | SET ' . $db->sql_build_array('UPDATE', $fields) . ' |
||
| 261 | WHERE ' . $db->sql_in_set('topic_id', array_map('intval', $topic_id_ary)); |
||
| 262 | $db->sql_query($sql); |
||
| 263 | } |
||
| 264 | |||
| 265 | $sql = 'SELECT attr_name |
||
| 266 | FROM ' . $table_prefix . 'topics_attr |
||
| 267 | WHERE attr_id = ' . (int) $attr_id; |
||
| 268 | $result = $db->sql_query($sql); |
||
| 269 | $attr_name = (string) $db->sql_fetchfield('attr_name'); |
||
| 270 | $db->sql_freeresult($result); |
||
| 271 | |||
| 272 | $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_ATTRIBUTE_REMOVED', time(), array($attr_name)); |
||
| 273 | |||
| 274 | $this->migrator_tool_permission->remove('f_qte_attr_'.$attr_id, false); |
||
| 275 | |||
| 276 | $sql = 'DELETE FROM ' . $table_prefix . 'topics_attr |
||
| 277 | WHERE attr_id = ' . (int) $attr_id; |
||
| 278 | $db->sql_query($sql); |
||
| 279 | |||
| 280 | $cache->destroy('_attr'); |
||
| 281 | |||
| 282 | if ($request->is_ajax()) |
||
| 283 | { |
||
| 284 | $json_response = new \phpbb\json_response; |
||
| 285 | $json_response->send(array( |
||
| 286 | 'success' => 'true', |
||
| 287 | 'MESSAGE_TITLE' => $user->lang['INFORMATION'], |
||
| 288 | 'MESSAGE_TEXT' => $user->lang['QTE_REMOVED'], |
||
| 289 | 'REFRESH_DATA' => array( |
||
| 290 | 'time' => 3, |
||
| 291 | ) |
||
| 292 | )); |
||
| 293 | } |
||
| 294 | else |
||
| 295 | { |
||
| 296 | trigger_error($user->lang['QTE_REMOVED'] . adm_back_link($this->u_action)); |
||
| 297 | } |
||
| 298 | } |
||
| 299 | else |
||
| 300 | { |
||
| 301 | confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array( |
||
| 302 | 'i' => $id, |
||
| 303 | 'mode' => $mode, |
||
| 304 | 'attr_id' => $attr_id, |
||
| 305 | 'action' => 'delete', |
||
| 306 | ))); |
||
| 307 | } |
||
| 308 | |||
| 309 | break; |
||
| 310 | |||
| 311 | case 'move_up': |
||
| 312 | case 'move_down': |
||
| 313 | |||
| 314 | View Code Duplication | if (!$attr_id) |
|
| 315 | { |
||
| 316 | trigger_error($user->lang['QTE_MUST_SELECT'] . adm_back_link($this->u_action), E_USER_WARNING); |
||
| 317 | } |
||
| 318 | |||
| 319 | $sql = 'SELECT * |
||
| 320 | FROM ' . $table_prefix . 'topics_attr |
||
| 321 | WHERE attr_id = ' . (int) $attr_id; |
||
| 322 | $result = $db->sql_query($sql); |
||
| 323 | $row = $db->sql_fetchrow($result); |
||
| 324 | $db->sql_freeresult($result); |
||
| 325 | |||
| 326 | View Code Duplication | if (!$row) |
|
| 327 | { |
||
| 328 | trigger_error($user->lang['QTE_MUST_SELECT'] . adm_back_link($this->u_action), E_USER_WARNING); |
||
| 329 | } |
||
| 330 | |||
| 331 | $move_attr_name = $this->qte_move($row, $action, 1); |
||
| 332 | if ($move_attr_name !== false) |
||
| 333 | { |
||
| 334 | $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_ATTRIBUTE_' . strtoupper($action), time(), array($move_attr_name)); |
||
| 335 | } |
||
| 336 | |||
| 337 | if ($request->is_ajax()) |
||
| 338 | { |
||
| 339 | $json_response = new \phpbb\json_response; |
||
| 340 | $json_response->send(array('success' => true)); |
||
| 341 | } |
||
| 342 | |||
| 343 | break; |
||
| 344 | } |
||
| 345 | |||
| 346 | $template->assign_vars(array('U_ACTION' => $this->u_action)); |
||
| 347 | |||
| 348 | $sql = 'SELECT topic_attr_id, COUNT(topic_id) AS total_topics |
||
| 349 | FROM ' . TOPICS_TABLE . ' |
||
| 350 | GROUP BY topic_attr_id'; |
||
| 351 | $result = $db->sql_query($sql); |
||
| 352 | $stats = array(); |
||
| 353 | $total_topics = 0; |
||
| 354 | while ($row = $db->sql_fetchrow($result)) |
||
| 355 | { |
||
| 356 | $stats[$row['topic_attr_id']] = $row['total_topics']; |
||
| 357 | $total_topics += $row['total_topics']; |
||
| 358 | } |
||
| 359 | $db->sql_freeresult($result); |
||
| 360 | |||
| 361 | $sql = 'SELECT * FROM ' . $table_prefix . 'topics_attr ORDER BY left_id'; |
||
| 362 | $result = $db->sql_query($sql); |
||
| 363 | |||
| 364 | while ($row = $db->sql_fetchrow($result)) |
||
| 365 | { |
||
| 366 | $attribute_name = str_replace(array('%mod%', '%date%'), array($user->lang['QTE_KEY_USERNAME'], $user->lang['QTE_KEY_DATE']), $user->lang($row['attr_name'])); |
||
| 367 | $attribute_count = isset($stats[$row['attr_id']]) ? $stats[$row['attr_id']] : 0; |
||
| 368 | |||
| 369 | $template->assign_block_vars('row', array( |
||
| 370 | 'S_IMAGE' => $row['attr_type'] ? true : false, |
||
| 371 | 'S_COLOUR' => $row['attr_colour'] ? true : false, |
||
| 372 | 'S_DESC' => $row['attr_desc'] ? true : false, |
||
| 373 | 'S_DATE' => $row['attr_date'] ? true : false, |
||
| 374 | 'S_USER_COLOUR' => $row['attr_user_colour'] ? true : false, |
||
| 375 | 'S_CSS' => (!$row['attr_type'] && isset($user->lang[$row['attr_name']]) && empty($row['attr_colour'])) ? true : false, |
||
| 376 | |||
| 377 | 'QTE_TXT' => $attribute_name, |
||
| 378 | 'QTE_DESC' => $user->lang($row['attr_desc']), |
||
| 379 | 'QTE_IMG' => $this->qte->attr_img_key($row['attr_img'], $attribute_name), |
||
| 380 | 'QTE_COLOUR' => $row['attr_colour'], |
||
| 381 | 'QTE_DATE' => $row['attr_date'], |
||
| 382 | 'QTE_COUNT' => (int) $attribute_count, |
||
| 383 | 'QTE_PER_CENT' => empty($total_topics) ? 0 : round(intval($attribute_count) * 100 / $total_topics), |
||
| 384 | |||
| 385 | 'U_EDIT' => $this->u_action . '&action=edit&id=' . $row['attr_id'], |
||
| 386 | 'U_MOVE_UP' => $this->u_action . '&action=move_up&id=' . $row['attr_id'], |
||
| 387 | 'U_MOVE_DOWN' => $this->u_action . '&action=move_down&id=' . $row['attr_id'], |
||
| 388 | 'U_DELETE' => $this->u_action . '&action=delete&id=' . $row['attr_id'], |
||
| 389 | )); |
||
| 390 | } |
||
| 391 | $db->sql_freeresult($result); |
||
| 392 | } |
||
| 393 | |||
| 584 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: