| Conditions | 90 |
| Paths | > 20000 |
| Total Lines | 437 |
| Code Lines | 217 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 8 | ||
| 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 |
||
| 212 | public function action_edit() |
||
| 213 | { |
||
| 214 | global $txt, $context, $modSettings, $boards; |
||
| 215 | |||
| 216 | // Just in case, the admin could be doing something silly like editing a SP block while SP is disabled. ;) |
||
| 217 | require_once(SUBSDIR . '/spblocks/SPAbstractBlock.class.php'); |
||
| 218 | |||
| 219 | $context['SPortal']['is_new'] = empty($_REQUEST['block_id']); |
||
| 220 | |||
| 221 | // Convert HTML to BBC if needed |
||
| 222 | $this->_fixBBC(); |
||
| 223 | |||
| 224 | // Passing the selected type via $_GET instead of $_POST? |
||
| 225 | $start_parameters = $this->_getStartParameters(); |
||
| 226 | |||
| 227 | // Want use a block on the portal? |
||
| 228 | if ($context['SPortal']['is_new'] && empty($_POST['selected_type']) && empty($_POST['add_block'])) |
||
| 229 | { |
||
| 230 | // Gather the blocks we have available |
||
| 231 | $context['SPortal']['block_types'] = getFunctionInfo(); |
||
| 232 | |||
| 233 | // Create a list of the blocks in use |
||
| 234 | $in_use = getBlockInfo(); |
||
| 235 | foreach ($in_use as $block) |
||
| 236 | { |
||
| 237 | $context['SPortal']['block_inuse'][$block['type']] = array('state' => $block['state'], 'column' => $block['column']); |
||
| 238 | } |
||
| 239 | |||
| 240 | $context['location'] = array(1 => $txt['sp-positionLeft'], $txt['sp-positionTop'], $txt['sp-positionBottom'], $txt['sp-positionRight'], $txt['sp-positionHeader'], $txt['sp-positionFooter']); |
||
| 241 | |||
| 242 | if (!empty($_REQUEST['col'])) |
||
| 243 | { |
||
| 244 | $context['SPortal']['block']['column'] = $_REQUEST['col']; |
||
| 245 | } |
||
| 246 | |||
| 247 | $context['sub_template'] = 'block_select_type'; |
||
| 248 | $context['page_title'] = $txt['sp-blocksAdd']; |
||
| 249 | } |
||
| 250 | // Selecting a block to place in to service, load some initial values |
||
| 251 | elseif ($context['SPortal']['is_new'] && !empty($_POST['selected_type'])) |
||
| 252 | { |
||
| 253 | $block = sp_instantiate_block($_POST['selected_type'][0]); |
||
| 254 | $context['SPortal']['block'] = array( |
||
| 255 | 'id' => 0, |
||
| 256 | 'label' => $txt['sp-blocksDefaultLabel'], |
||
| 257 | 'type' => $_POST['selected_type'][0], |
||
| 258 | 'type_text' => !empty($txt['sp_function_' . $_POST['selected_type'][0] . '_label']) ? $txt['sp_function_' . $_POST['selected_type'][0] . '_label'] : $txt['sp_function_unknown_label'], |
||
| 259 | 'column' => !empty($_POST['block_column']) ? $_POST['block_column'] : 0, |
||
| 260 | 'row' => 0, |
||
| 261 | 'permissions' => 3, |
||
| 262 | 'styles' => 4, |
||
| 263 | 'visibility' => 14, |
||
| 264 | 'state' => 1, |
||
| 265 | 'force_view' => 0, |
||
| 266 | 'parameters' => !empty($start_parameters) ? $start_parameters : array(), |
||
| 267 | 'options' => $block->parameters(), |
||
| 268 | 'list_blocks' => !empty($_POST['block_column']) ? getBlockInfo((int) $_POST['block_column']) : array(), |
||
| 269 | ); |
||
| 270 | } |
||
| 271 | // Saving the block to one of the zones |
||
| 272 | elseif (!$context['SPortal']['is_new'] && empty($_POST['add_block'])) |
||
| 273 | { |
||
| 274 | $_REQUEST['block_id'] = (int) $_REQUEST['block_id']; |
||
| 275 | $context['SPortal']['block'] = current(getBlockInfo(null, $_REQUEST['block_id'])); |
||
| 276 | |||
| 277 | $block = sp_instantiate_block($context['SPortal']['block']['type']); |
||
| 278 | $context['SPortal']['block'] += array( |
||
| 279 | 'options' => $block->parameters(), |
||
| 280 | 'list_blocks' => getBlockInfo($context['SPortal']['block']['column']), |
||
| 281 | ); |
||
| 282 | } |
||
| 283 | |||
| 284 | // Want to take a look at how this block will appear, well we try our best |
||
| 285 | if (!empty($_POST['preview_block']) || isset($_SESSION['sp_error'])) |
||
| 286 | { |
||
| 287 | // An error was generated on save, lets set things up like a preview and return to the preview |
||
| 288 | if (isset($_SESSION['sp_error'])) |
||
| 289 | { |
||
| 290 | $context['SPortal']['error'] = $_SESSION['sp_error']; |
||
| 291 | $_POST = $_SESSION['sp_error_post']; |
||
| 292 | $_POST['preview_block'] = true; |
||
| 293 | |||
| 294 | // Clean up |
||
| 295 | unset($_SESSION['sp_error'], $_SESSION['sp_error_post'], $_POST['add_block']); |
||
| 296 | } |
||
| 297 | |||
| 298 | // Just in case, the admin could be doing something silly like editing a SP block while SP is disabled. ;) |
||
| 299 | require_once(BOARDDIR . '/SSI.php'); |
||
| 300 | sportal_init_headers(); |
||
| 301 | loadTemplate('Portal'); |
||
| 302 | |||
| 303 | // Start the block and get its parameters for display |
||
| 304 | $block = sp_instantiate_block((string) $_POST['block_type']); |
||
| 305 | $type_parameters = $block->parameters(); |
||
| 306 | |||
| 307 | if (!empty($_POST['parameters']) && is_array($_POST['parameters']) && !empty($type_parameters)) |
||
| 308 | { |
||
| 309 | foreach ($type_parameters as $name => $type) |
||
| 310 | { |
||
| 311 | if (isset($_POST['parameters'][$name])) |
||
| 312 | { |
||
| 313 | $this->_prepare_parameters($type, $name); |
||
| 314 | } |
||
| 315 | } |
||
| 316 | } |
||
| 317 | else |
||
| 318 | { |
||
| 319 | $_POST['parameters'] = array(); |
||
| 320 | } |
||
| 321 | |||
| 322 | // Prepare a preview with the form parameters |
||
| 323 | $block->setup($_POST['parameters'], false); |
||
| 324 | |||
| 325 | // Create all the information we know about this block |
||
| 326 | $context['SPortal']['block'] = array( |
||
| 327 | 'id' => $_POST['block_id'], |
||
| 328 | 'label' => Util::htmlspecialchars($_POST['block_name'], ENT_QUOTES), |
||
| 329 | 'type' => $_POST['block_type'], |
||
| 330 | 'type_text' => !empty($txt['sp_function_' . $_POST['block_type'] . '_label']) ? $txt['sp_function_' . $_POST['block_type'] . '_label'] : $txt['sp_function_unknown_label'], |
||
| 331 | 'column' => $_POST['block_column'], |
||
| 332 | 'row' => !empty($_POST['block_row']) ? $_POST['block_row'] : 0, |
||
| 333 | 'placement' => !empty($_POST['placement']) && in_array($_POST['placement'], array('before', 'after')) ? $_POST['placement'] : '', |
||
| 334 | 'permissions' => $_POST['permissions'], |
||
| 335 | 'styles' => $_POST['styles'], |
||
| 336 | 'visibility' => $_POST['visibility'], |
||
| 337 | 'state' => !empty($_POST['block_active']), |
||
| 338 | 'force_view' => !empty($_POST['block_force']), |
||
| 339 | 'parameters' => !empty($_POST['parameters']) ? $_POST['parameters'] : array(), |
||
| 340 | 'options' => $block->parameters(), |
||
| 341 | 'list_blocks' => getBlockInfo($_POST['block_column']), |
||
| 342 | 'collapsed' => false, |
||
| 343 | 'instance' => $block, |
||
| 344 | ); |
||
| 345 | |||
| 346 | if (strpos($modSettings['leftwidth'], '%') !== false || strpos($modSettings['leftwidth'], 'px') !== false) |
||
| 347 | { |
||
| 348 | $context['widths'][1] = $modSettings['leftwidth']; |
||
| 349 | } |
||
| 350 | else |
||
| 351 | { |
||
| 352 | $context['widths'][1] = $modSettings['leftwidth'] . 'px'; |
||
| 353 | } |
||
| 354 | |||
| 355 | if (strpos($modSettings['rightwidth'], '%') !== false || strpos($modSettings['rightwidth'], 'px') !== false) |
||
| 356 | { |
||
| 357 | $context['widths'][4] = $modSettings['rightwidth']; |
||
| 358 | } |
||
| 359 | else |
||
| 360 | { |
||
| 361 | $context['widths'][4] = $modSettings['rightwidth'] . 'px'; |
||
| 362 | } |
||
| 363 | |||
| 364 | if (strpos($context['widths'][1], '%') !== false) |
||
| 365 | { |
||
| 366 | $context['widths'][2] = $context['widths'][3] = 100 - ((int) $context['widths'][1] + (int) $context['widths'][4]) . '%'; |
||
| 367 | $context['widths'][5] = $context['widths'][6] = '100%'; |
||
| 368 | } |
||
| 369 | elseif (strpos($context['widths'][1], 'px') !== false) |
||
| 370 | { |
||
| 371 | $context['widths'][2] = $context['widths'][3] = 960 - ((int) $context['widths'][1] + (int) $context['widths'][4]) . 'px'; |
||
| 372 | $context['widths'][5] = $context['widths'][6] = '960px'; |
||
| 373 | } |
||
| 374 | |||
| 375 | $context['SPortal']['preview'] = true; |
||
| 376 | } |
||
| 377 | |||
| 378 | if (!empty($_POST['selected_type']) || !empty($_POST['preview_block']) || (!$context['SPortal']['is_new'] && empty($_POST['add_block']))) |
||
| 379 | { |
||
| 380 | // Only the admin can use PHP blocks |
||
| 381 | if ($context['SPortal']['block']['type'] === 'sp_php' && !allowedTo('admin_forum')) |
||
| 382 | { |
||
| 383 | throw new Elk_Exception('cannot_admin_forum', false); |
||
| 384 | } |
||
| 385 | |||
| 386 | loadLanguage('SPortalHelp'); |
||
| 387 | |||
| 388 | // Load up the permissions |
||
| 389 | $context['SPortal']['block']['permission_profiles'] = sportal_get_profiles(null, 1, 'name'); |
||
| 390 | if (empty($context['SPortal']['block']['permission_profiles'])) |
||
| 391 | { |
||
| 392 | throw new Elk_Exception('error_sp_no_permission_profiles', false); |
||
| 393 | } |
||
| 394 | |||
| 395 | // Load in the style profiles |
||
| 396 | $context['SPortal']['block']['style_profiles'] = sportal_get_profiles(null, 2, 'name'); |
||
| 397 | if (empty($context['SPortal']['block']['style_profiles'])) |
||
| 398 | { |
||
| 399 | throw new Elk_Exception('error_sp_no_style_profiles', false); |
||
| 400 | } |
||
| 401 | |||
| 402 | // Load in the display profiles |
||
| 403 | $context['SPortal']['block']['visibility_profiles'] = sportal_get_profiles(null, 3, 'name'); |
||
| 404 | if (empty($context['SPortal']['block']['visibility_profiles'])) |
||
| 405 | { |
||
| 406 | throw new Elk_Exception('error_sp_no_visibility_profiles', false); |
||
| 407 | } |
||
| 408 | |||
| 409 | $context['SPortal']['block']['style'] = sportal_select_style($context['SPortal']['block']['styles']); |
||
| 410 | |||
| 411 | // Prepare the Textcontent for BBC, only the first bbc will be detected correctly! |
||
| 412 | $firstBBCFound = false; |
||
| 413 | foreach ($context['SPortal']['block']['options'] as $name => $type) |
||
| 414 | { |
||
| 415 | // Selectable Boards :D |
||
| 416 | if ($type === 'board_select' || $type === 'boards') |
||
| 417 | { |
||
| 418 | if (empty($boards)) |
||
| 419 | { |
||
| 420 | require_once(SUBSDIR . '/Boards.subs.php'); |
||
| 421 | getBoardTree(); |
||
| 422 | } |
||
| 423 | |||
| 424 | // Merge the array ;) |
||
| 425 | if (!isset($context['SPortal']['block']['parameters'][$name])) |
||
| 426 | { |
||
| 427 | $context['SPortal']['block']['parameters'][$name] = array(); |
||
| 428 | } |
||
| 429 | elseif (!empty($context['SPortal']['block']['parameters'][$name]) && is_array($context['SPortal']['block']['parameters'][$name])) |
||
| 430 | { |
||
| 431 | $context['SPortal']['block']['parameters'][$name] = implode('|', $context['SPortal']['block']['parameters'][$name]); |
||
| 432 | } |
||
| 433 | |||
| 434 | $context['SPortal']['block']['board_options'][$name] = array(); |
||
| 435 | $config_variable = !empty($context['SPortal']['block']['parameters'][$name]) ? $context['SPortal']['block']['parameters'][$name] : array(); |
||
| 436 | $config_variable = !is_array($config_variable) ? explode('|', $config_variable) : $config_variable; |
||
| 437 | $context['SPortal']['block']['board_options'][$name] = array(); |
||
| 438 | |||
| 439 | // Create the list for this Item |
||
| 440 | foreach ($boards as $board) |
||
| 441 | { |
||
| 442 | // Ignore the redirected boards :) |
||
| 443 | if (!empty($board['redirect'])) |
||
| 444 | { |
||
| 445 | continue; |
||
| 446 | } |
||
| 447 | |||
| 448 | $context['SPortal']['block']['board_options'][$name][$board['id']] = array( |
||
| 449 | 'value' => $board['id'], |
||
| 450 | 'text' => $board['name'], |
||
| 451 | 'selected' => in_array($board['id'], $config_variable), |
||
| 452 | ); |
||
| 453 | } |
||
| 454 | } |
||
| 455 | // Prepare the Text content for BBC, only the first bbc will be correct detected! |
||
| 456 | elseif ($type === 'bbc') |
||
| 457 | { |
||
| 458 | // ELK onlys supports one bbc correctly, multiple bbc do not work at the moment |
||
| 459 | if (!$firstBBCFound) |
||
| 460 | { |
||
| 461 | $firstBBCFound = true; |
||
| 462 | |||
| 463 | // Start Elk BBC System :) |
||
| 464 | require_once(SUBSDIR . '/Editor.subs.php'); |
||
| 465 | |||
| 466 | // Prepare the output :D |
||
| 467 | $form_message = !empty($context['SPortal']['block']['parameters'][$name]) ? $context['SPortal']['block']['parameters'][$name] : ''; |
||
| 468 | |||
| 469 | // But if it's in HTML world, turn them into htmlspecialchar's so they can be edited! |
||
| 470 | if (strpos($form_message, '[html]') !== false) |
||
| 471 | { |
||
| 472 | $parts = preg_split('~(\[/code\]|\[code(?:=[^\]]+)?\])~i', $form_message, -1, PREG_SPLIT_DELIM_CAPTURE); |
||
| 473 | for ($i = 0, $n = count($parts); $i < $n; $i++) |
||
| 474 | { |
||
| 475 | // It goes 0 = outside, 1 = begin tag, 2 = inside, 3 = close tag, repeat. |
||
| 476 | if ($i % 4 == 0) |
||
| 477 | { |
||
| 478 | $parts[$i] = preg_replace_callback('~\[html\](.+?)\[/html\]~is', function ($m) { |
||
| 479 | return '[html]' . preg_replace('~<br\s?/?>~i', '<br /><br />', $m[1]) . '[/html]'; |
||
| 480 | }, $parts[$i]); |
||
| 481 | } |
||
| 482 | } |
||
| 483 | |||
| 484 | $form_message = implode('', $parts); |
||
| 485 | } |
||
| 486 | $form_message = preg_replace('~<br(?: /)?' . '>~i', "\n", $form_message); |
||
| 487 | |||
| 488 | // Prepare the data before i want them inside the textarea |
||
| 489 | $form_message = str_replace(array('"', '<', '>', ' '), array('"', '<', '>', ' '), $form_message); |
||
| 490 | $context['SPortal']['bbc'] = 'bbc_' . $name; |
||
| 491 | $message_data = array( |
||
| 492 | 'id' => $context['SPortal']['bbc'], |
||
| 493 | 'width' => '95%', |
||
| 494 | 'height' => '200px', |
||
| 495 | 'value' => $form_message, |
||
| 496 | 'form' => 'sp_block', |
||
| 497 | ); |
||
| 498 | |||
| 499 | // Run the ELK bbc editor routine |
||
| 500 | create_control_richedit($message_data); |
||
| 501 | |||
| 502 | // Store the updated data on the parameters |
||
| 503 | $context['SPortal']['block']['parameters'][$name] = $form_message; |
||
| 504 | } |
||
| 505 | else |
||
| 506 | { |
||
| 507 | $context['SPortal']['block']['options'][$name] = 'textarea'; |
||
| 508 | } |
||
| 509 | } |
||
| 510 | } |
||
| 511 | |||
| 512 | loadJavascriptFile('portal.js?sp100rc1'); |
||
| 513 | $context['sub_template'] = 'block_edit'; |
||
| 514 | $context['page_title'] = $context['SPortal']['is_new'] ? $txt['sp-blocksAdd'] : $txt['sp-blocksEdit']; |
||
| 515 | } |
||
| 516 | |||
| 517 | // Want to add / edit a block on the portal |
||
| 518 | if (!empty($_POST['add_block'])) |
||
| 519 | { |
||
| 520 | checkSession(); |
||
| 521 | |||
| 522 | // Only the admin can do php here |
||
| 523 | if ($_POST['block_type'] === 'sp_php' && !allowedTo('admin_forum')) |
||
| 524 | { |
||
| 525 | throw new Elk_Exception('cannot_admin_forum', false); |
||
| 526 | } |
||
| 527 | |||
| 528 | // Make sure the block name is something safe |
||
| 529 | if (!isset($_POST['block_name']) || Util::htmltrim(Util::htmlspecialchars($_POST['block_name'], ENT_QUOTES)) === '') |
||
| 530 | { |
||
| 531 | throw new Elk_Exception('error_sp_name_empty', false); |
||
| 532 | } |
||
| 533 | |||
| 534 | if ($_POST['block_type'] === 'sp_php' && !empty($_POST['parameters']['content']) && empty($modSettings['sp_disable_php_validation'])) |
||
| 535 | { |
||
| 536 | require_once(SUBSDIR . '/DataValidator.class.php'); |
||
| 537 | |||
| 538 | $validator = new Data_Validator(); |
||
| 539 | $validator->validation_rules(array('content' => 'php_syntax')); |
||
| 540 | $validator->validate(array('content' => $_POST['parameters']['content'])); |
||
| 541 | $error = $validator->validation_errors(); |
||
| 542 | |||
| 543 | if ($error) |
||
| 544 | { |
||
| 545 | $_SESSION['sp_error'] = $error[0]; |
||
| 546 | $_SESSION['sp_error_post'] = $_POST; |
||
| 547 | redirectexit('action=admin;area=portalblocks;sa=' . $_REQUEST['sa'] . (!empty($_REQUEST['block_id']) ? ';block_id=' . $_REQUEST['block_id'] : '')); |
||
| 548 | } |
||
| 549 | } |
||
| 550 | |||
| 551 | // If we have a block ID passed, we must be editing, so load the blocks current data |
||
| 552 | if (!empty($_REQUEST['block_id'])) |
||
| 553 | { |
||
| 554 | $current_data = current(getBlockInfo(null, $_REQUEST['block_id'])); |
||
| 555 | } |
||
| 556 | |||
| 557 | // Where are we going to place this new block, before, after, no change |
||
| 558 | if (!empty($_POST['placement']) && (($_POST['placement'] === 'before') || ($_POST['placement'] === 'after'))) |
||
| 559 | { |
||
| 560 | if (!empty($current_data)) |
||
| 561 | { |
||
| 562 | $current_row = $current_data['row']; |
||
| 563 | } |
||
| 564 | else |
||
| 565 | { |
||
| 566 | $current_row = null; |
||
| 567 | } |
||
| 568 | |||
| 569 | // Before or after the chosen block |
||
| 570 | if ($_POST['placement'] === 'before') |
||
| 571 | { |
||
| 572 | $row = (int) $_POST['block_row']; |
||
| 573 | } |
||
| 574 | else |
||
| 575 | { |
||
| 576 | $row = (int) $_POST['block_row'] + 1; |
||
| 577 | } |
||
| 578 | |||
| 579 | if (!empty($current_row) && ($row > $current_row)) |
||
| 580 | { |
||
| 581 | sp_update_block_row($current_row, $row - 1, $_POST['block_column']); |
||
| 582 | } |
||
| 583 | else |
||
| 584 | { |
||
| 585 | sp_update_block_row($current_row, $row, $_POST['block_column'], false); |
||
| 586 | } |
||
| 587 | } |
||
| 588 | elseif (!empty($_POST['placement']) && $_POST['placement'] === 'nochange') |
||
| 589 | { |
||
| 590 | $row = 0; |
||
| 591 | } |
||
| 592 | else |
||
| 593 | { |
||
| 594 | $block_id = !empty($_REQUEST['block_id']) ? (int) $_REQUEST['block_id'] : 0; |
||
| 595 | $row = sp_block_nextrow($_POST['block_column'], $block_id); |
||
| 596 | } |
||
| 597 | |||
| 598 | $block = sp_instantiate_block((string) $_POST['block_type']); |
||
| 599 | $type_parameters = $block->parameters(); |
||
| 600 | |||
| 601 | if (!empty($_POST['parameters']) && is_array($_POST['parameters']) && !empty($type_parameters)) |
||
| 602 | { |
||
| 603 | foreach ($type_parameters as $name => $type) |
||
| 604 | { |
||
| 605 | // Prepare BBC Content for ELK |
||
| 606 | if (isset($_POST['parameters'][$name])) |
||
| 607 | { |
||
| 608 | $this->_prepare_parameters($type, $name); |
||
| 609 | } |
||
| 610 | } |
||
| 611 | } |
||
| 612 | else |
||
| 613 | { |
||
| 614 | $_POST['parameters'] = array(); |
||
| 615 | } |
||
| 616 | |||
| 617 | $blockInfo = array( |
||
| 618 | 'id' => (int) $_POST['block_id'], |
||
| 619 | 'label' => Util::htmlspecialchars($_POST['block_name'], ENT_QUOTES), |
||
| 620 | 'type' => $_POST['block_type'], |
||
| 621 | 'col' => $_POST['block_column'], |
||
| 622 | 'row' => $row, |
||
| 623 | 'permissions' => (int) $_POST['permissions'], |
||
| 624 | 'styles' => (int) $_POST['styles'], |
||
| 625 | 'visibility' => (int) $_POST['visibility'], |
||
| 626 | 'state' => !empty($_POST['block_active']) ? 1 : 0, |
||
| 627 | 'force_view' => !empty($_POST['block_force']) ? 1 : 0, |
||
| 628 | ); |
||
| 629 | |||
| 630 | // Insert a new block in to the portal |
||
| 631 | if ($context['SPortal']['is_new']) |
||
| 632 | { |
||
| 633 | unset($blockInfo['id']); |
||
| 634 | $blockInfo['id'] = sp_block_insert($blockInfo); |
||
| 635 | } |
||
| 636 | // Update one that is there |
||
| 637 | else |
||
| 638 | { |
||
| 639 | sp_block_update($blockInfo); |
||
| 640 | } |
||
| 641 | |||
| 642 | // Save any parameters for the block |
||
| 643 | if (!empty($_POST['parameters'])) |
||
| 644 | { |
||
| 645 | sp_block_insert_parameters($_POST['parameters'], $blockInfo['id']); |
||
| 646 | } |
||
| 647 | |||
| 648 | redirectexit('action=admin;area=portalblocks'); |
||
| 649 | } |
||
| 1019 |