Skip to content

Commit a84ea98

Browse files
authored
Patch for issue #49 (#228)
* Patch for issue #49 * remove lib/Template.php.orig
1 parent e31d6c2 commit a84ea98

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

lib/PageRender.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,32 @@ protected function getTemplateChoice() {
360360
else
361361
$this->drawTemplateChoice();
362362

363+
# If mode is modification, use a DefaultForObjectClass template if exaclty 1 matches. Propose the template choice otherwise.
364+
} elseif (($this->getMode() == 'modification') && (! isset($_REQUEST['template']))) {
365+
if (DEBUGTMP) printf('<font size=-2>%s:<u>%s</u></font><br />',__METHOD__,'DEFAULTFOROBJECTCLASS template if only 1 template matches.');
366+
367+
$dn = $this->getModeContainer();
368+
$server = $this->getServer();
369+
$dnojcs = $server->getDNAttrValue($dn,'objectClass');
370+
$matchingtmplIDs = array();
371+
$unmatchingtmplIDs = array();
372+
$tpldefojc = array();
373+
foreach ($templates->getTemplates($this->getMode(),$this->getModeContainer(),true) as $tpl) {
374+
$tpldefojc = $tpl->getDefaultForObjectClass();
375+
if (array_intersect($tpldefojc,$dnojcs)) {
376+
array_push($matchingtmplIDs,$tpl->getID());
377+
} else {
378+
array_push($unmatchingtmplIDs,$tpl->getID());
379+
}
380+
}
381+
if (count($matchingtmplIDs) == 0) # No matching DefaultForObjectClass found,
382+
return 'none'; # so we use the default template.
383+
elseif (count($matchingtmplIDs) == 1) # Exactly 1 matching DefaultForObjectClass found,
384+
return $matchingtmplIDs[0]; # so we use it.
385+
elseif (count($unmatchingtmplIDs) == 0) # No template found at all,
386+
return 'none'; # so we use the default template.
387+
else # Multiple templates found,
388+
$this->drawTemplateChoice(); # so propose the template choice.
363389
} else {
364390
if (DEBUGTMP) printf('<font size=-2>%s:<u>%s</u></font><br />',__METHOD__,'SELECT a template to use.');
365391

lib/Template.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class Template extends xmlTemplate {
3737
private $invalid_reason;
3838
# The TEMPLATE structural objectclasses
3939
protected $structural_oclass = array();
40+
# The objectclasses for which this template is the default
41+
protected $defaultfor_oclass = array();
4042
protected $description = '';
4143
# Is this a read-only template (only valid in modification templates)
4244
private $readonly = false;
@@ -124,6 +126,31 @@ protected function storeTemplate($xmldata) {
124126

125127
break;
126128

129+
# Record our defaultFor object Classes from the Template.
130+
case ('defaultforobjectclasses'):
131+
if (DEBUG_ENABLED)
132+
debug_log('Case [%s]',4,0,__FILE__,__LINE__,__METHOD__,$xml_key);
133+
134+
if (isset($xmldata['template'][$xml_key]['defaultforobjectclass']))
135+
if (is_array($xmldata['template'][$xml_key]['defaultforobjectclass'])) {
136+
foreach ($xmldata['template'][$xml_key]['defaultforobjectclass'] as $index => $details) {
137+
138+
# If we havent recorded this objectclass already, do so now.
139+
if (! in_array($details,$this->defaultfor_oclass))
140+
array_push($this->defaultfor_oclass,$details);
141+
}
142+
143+
} else {
144+
# XML files with only 1 objectClass dont have a numeric index.
145+
$soc = $xmldata['template'][$xml_key]['defaultforobjectclass'];
146+
147+
# If we havent recorded this objectclass already, do so now.
148+
if (! in_array($soc,$this->defaultfor_oclass))
149+
array_push($this->defaultfor_oclass,$soc);
150+
}
151+
152+
break;
153+
127154
# Build our attribute list from the DN and Template.
128155
case ('attributes'):
129156
if (DEBUG_ENABLED)
@@ -179,6 +206,7 @@ protected function storeTemplate($xmldata) {
179206

180207
if ($xml_key == 'invalid' && $xml_value)
181208
$this->setInvalid(_('Disabled by XML configuration'),true);
209+
182210
}
183211
}
184212

@@ -924,6 +952,15 @@ public function setInvisible() {
924952
$this->visible = false;
925953
}
926954

955+
/**
956+
* Get the objectclasses for which this template is the default
957+
*
958+
* @return array The objectclasses
959+
*/
960+
public function getDefaultForObjectClass() {
961+
return $this->defaultfor_oclass;
962+
}
963+
927964
public function getRegExp() {
928965
if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
929966
debug_log('Entered (%%)',5,1,__FILE__,__LINE__,__METHOD__,$fargs,$this->regexp);

0 commit comments

Comments
 (0)