From 5bbbe0be91b7f5280c8a36e0eeca5564e0ca2a80 Mon Sep 17 00:00:00 2001 From: defacer Date: Thu, 10 Feb 2005 18:43:18 +0000 Subject: [PATCH] Changing the matching function that decides if a block is applicable for display in a page. Previously it returned the FIRST match; now it returns the BEST match. This allows things like array('all' => true, 'mod' => false, 'mod-quiz' => true) to work correctly regardless of the order the array elements appear in. Also, encapsulated the check into a function (it has definitely become non-trivial). --- lib/blocklib.php | 48 +++++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/lib/blocklib.php b/lib/blocklib.php index 6f89fe750f0..964cf3b93cc 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -106,20 +106,7 @@ function blocks_get_missing(&$page, &$pageblocks) { foreach($allblocks as $block) { if($block->visible && (!blocks_find_block($block->id, $pageblocks) || $block->multiple)) { // And if it's applicable for display in this format... - $formats = block_method_result($block->name, 'applicable_formats'); - $accept = NULL; - foreach($formats as $format => $allowed) { - $thisformat = '^'.str_replace('*', '[^-]*', $format).'.*$'; - if(ereg($thisformat, $pageformat)) { - $accept = $allowed; - break; - } - } - if($accept === NULL) { - // ...or in all pages... - $accept = !empty($formats['all']); - } - if(!empty($accept)) { + if(blocks_name_allowed_in_format($block->name, $pageformat)) { // ...add it to the missing blocks $missingblocks[] = $block->id; } @@ -143,25 +130,32 @@ function blocks_remove_inappropriate($page) { foreach($pageblocks as $position) { foreach($position as $instance) { $block = blocks_get_record($instance->blockid); - $formats = block_method_result($block->name, 'applicable_formats'); - $accept = NULL; - foreach($formats as $format => $allowed) { - $thisformat = '^'.str_replace('*', '[^-]*', $format).'.*$'; - if(ereg($thisformat, $pageformat)) { - $accept = $allowed; - break; - } - } - if($accept === NULL) { - $accept = !empty($formats['all']); - } - if(empty($accept)) { + if(!blocks_name_allowed_in_format($block->name, $pageformat)) { blocks_delete_instance($instance); } } } } +function blocks_name_allowed_in_format($name, $pageformat) { + $formats = block_method_result($name, 'applicable_formats'); + $accept = NULL; + $depth = -1; + foreach($formats as $format => $allowed) { + $thisformat = '^'.str_replace('*', '[^-]*', $format).'.*$'; + if(ereg($thisformat, $pageformat)) { + if(($scount = substr_count($format, '-')) > $depth) { + $depth = $scount; + $accept = $allowed; + } + } + } + if($accept === NULL) { + $accept = !empty($formats['all']); + } + return $accept; +} + function blocks_delete_instance($instance) { global $CFG;