SF Feature #2837687 - Automatic template selection from one of one valid templates

This commit is contained in:
Deon George 2009-08-17 16:10:21 +10:00
parent 803a4c821a
commit ba553353b0
4 changed files with 42 additions and 14 deletions

View File

@ -123,6 +123,7 @@ class PageRender extends Visitor {
* May be overloaded in other classes
*/
protected function getMode() {}
protected function getModeContainer() {}
/**
* Process our <post> 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('<font size=-2>%s:<u>%s</u></font><br />',__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('<font size=-2>%s:<u>%s</u></font><br />',__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();

View File

@ -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
*

View File

@ -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('<center><h3>%s</h3></center>',$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)) {

View File

@ -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;
}