From ba553353b0292c235246794ad68c26c3e8c6f2ba Mon Sep 17 00:00:00 2001 From: Deon George Date: Mon, 17 Aug 2009 16:10:21 +1000 Subject: [PATCH] SF Feature #2837687 - Automatic template selection from one of one valid templates --- lib/PageRender.php | 7 ++++--- lib/Template.php | 10 ++++++++-- lib/TemplateRender.php | 25 +++++++++++++++++++------ lib/xmlTemplates.php | 14 +++++++++++--- 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/lib/PageRender.php b/lib/PageRender.php index 92205ec..470231f 100644 --- a/lib/PageRender.php +++ b/lib/PageRender.php @@ -123,6 +123,7 @@ class PageRender extends Visitor { * May be overloaded in other classes */ protected function getMode() {} + protected function getModeContainer() {} /** * Process our arguments from the templates @@ -297,17 +298,17 @@ class PageRender extends Visitor { return $this->template_id; # If there are no defined templates - } elseif (count($templates->getTemplates($this->getMode())) <= 0) { + } elseif (count($templates->getTemplates($this->getMode(),$this->getModeContainer(),false)) <= 0) { if (DEBUGTMP) printf('%s:%s
',__METHOD__,'Choosing the DEFAULT template, no other template applicable'); # Since getTemplate() returns a default template if the one we want doesnt exist, we can return $templates->getID(), it should be the default. return $template->getID(); # If there is only 1 defined template, and no default available, then that is our template. - } elseif ((count($templates->getTemplates($this->getMode())) == 1) && ! $this->haveDefaultTemplate()) { + } elseif ((count($templates->getTemplates($this->getMode(),$this->getModeContainer(),true)) == 1) && ! $this->haveDefaultTemplate()) { if (DEBUGTMP) printf('%s:%s
',__METHOD__,'AUTOMATIC choosing a template, only 1 template applicable'); - $template = $templates->getTemplates($this->getMode()); + $template = $templates->getTemplates($this->getMode(),$this->getModeContainer(),true); $template = array_shift($template); return $template->getID(); diff --git a/lib/Template.php b/lib/Template.php index 3b4f3a0..b334866 100644 --- a/lib/Template.php +++ b/lib/Template.php @@ -35,6 +35,7 @@ class Template extends xmlTemplate { private $visible = true; # Is this template valid after parsing the XML file private $invalid = false; + private $invalid_admin = false; private $invalid_reason; # The TEMPLATE structural objectclasses protected $structural_oclass = array(); @@ -166,7 +167,7 @@ class Template extends xmlTemplate { $this->$xml_key = $xml_value; if ($xml_key == 'invalid' && $xml_value) - $this->invalid_reason = _('Disabled by XML configuration'); + $this->setInvalid(_('Disabled by XML configuration'),true); } } @@ -909,9 +910,10 @@ class Template extends xmlTemplate { * * @param string Message indicating the reason the template has been invalidated */ - private function setInvalid($msg) { + public function setInvalid($msg,$admin=false) { $this->invalid = true; $this->invalid_reason = $msg; + $this->invalid_admin = $admin; } /** @@ -926,6 +928,10 @@ class Template extends xmlTemplate { return false; } + public function isAdminDisabled() { + return $this->invalid_admin; + } + /** * Set the minimum number of values for an attribute * diff --git a/lib/TemplateRender.php b/lib/TemplateRender.php index caaab9f..802d8e2 100644 --- a/lib/TemplateRender.php +++ b/lib/TemplateRender.php @@ -553,6 +553,24 @@ class TemplateRender extends PageRender { debug_dump_backtrace(sprintf('Unknown mode for %s',__METHOD__),1); } + /** + * Return the container for this mode + */ + protected function getModeContainer() { + switch ($this->getMode()) { + case 'creation': + return $this->container; + break; + + case 'modification': + return $this->dn; + break; + + default: + return null; + } + } + /** * Is the default template enabled? */ @@ -584,7 +602,7 @@ class TemplateRender extends PageRender { } $avail_templates = $this->getTemplates(); - $templates = $avail_templates->getTemplates($this->getMode()); + $templates = $avail_templates->getTemplates($this->getMode(),$this->getModeContainer()); printf('

%s

',$msg); $href_parms = array_to_query_string($_GET,array('meth')); @@ -612,11 +630,6 @@ class TemplateRender extends PageRender { $isInValid = $details->isInValid(); - if (($regexp = $details->getRegExp()) - && (($this->getMode() == 'creation' && ! @preg_match('/'.$regexp.'/i',$this->container)) - || ($this->getMode() == 'modification' && ! @preg_match('/'.$regexp.'/i',$this->dn)))) - $isInValid = _('This template is not valid in this container'); - # Balance the columns properly if (($nb_templates % 2 == 0 && $i == intval($nb_templates / 2)) || ($nb_templates % 2 == 1 && $i == intval($nb_templates / 2) + 1)) { diff --git a/lib/xmlTemplates.php b/lib/xmlTemplates.php index 936c187..a46e9c4f 100644 --- a/lib/xmlTemplates.php +++ b/lib/xmlTemplates.php @@ -177,16 +177,24 @@ abstract class xmlTemplates { * This function should return a sorted list, as the array is built sorted. * * @param string Type of template, eg: creation, modification + * @param boolean Exclude templates purposely disabled. * @return array List of templates of the type */ - public function getTemplates($type=null) { + public function getTemplates($type=null,$container=null,$disabled=false) { $result = array(); if (is_array($this->templates)) - foreach ($this->templates as $template) - if ($template->isVisible()) + foreach ($this->templates as $details) { + + # Clone this, as we'll disable some templates, as a result of the container being requested. + $template = clone $details; + if (! is_null($container) && ($regexp = $template->getRegExp()) && (! @preg_match('/'.$regexp.'/i',$container))) + $template->setInvalid(_('This template is not valid in this container'),true); + + if ($template->isVisible() && (! $disabled || ! $template->isAdminDisabled())) if (is_null($type) || (! is_null($type) && $template->isType($type))) array_push($result,$template); + } return $result; }