Commit 8e275862 authored by Côme Bernigaud's avatar Côme Bernigaud Committed by Benoit Mortier
Browse files

Fixes: #1922 using foreign keys to display references

parent f9560d4c
......@@ -1202,6 +1202,11 @@ class plugin
die("Foreign key was declared but there is no method to handle it!");
}
function foreignKeyCheck ($field, $value, $source)
{
die("Foreign key was declared but there is no method to handle it!");
}
/* \brief Move/Rename complete trees
*
......
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2003 Cajus Pollmeier
Copyright (C) 2011-2013 FusionDirectory
Copyright (C) 2013 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -19,237 +18,130 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class reference extends plugin
class reference extends simplePlugin
{
/* attribute list for save action */
var $attributes = array();
var $objectclasses = array();
var $objectlist = "";
var $obtypes = array();
var $fold = array();
var $objectCache = array();
/*
* \brief Return plugin informations for acl handling
*/
static function plInfo()
{
return array(
"plShortName" => _("References"),
"plDescription" => _("References"),
"plSelfModify" => FALSE,
"plCategory" => array('acl'),
"plObjectType" => array('special'),
'plPriority' => 1,
'plPriority' => 10,
"plProvidedAcls" => array()
);
}
function reference (&$config, $dn = NULL)
static function getAttributesInfo ()
{
/* Include config object */
$this->config = &$config;
$this->dn = $dn;
/* Fill translation array */
$this->obtypes = array(
"gosaAccount" => array(
"text" => _("Generic"),
"image" => "images/penguin.png"
),
"posixAccount" => array(
"text" => _("UNIX"),
"image" => "plugins/users/images/iconMini.png"
),
"gosaMailAccount" => array(
"text" => _("Mail"),
"image" => "images/mailto.png"
),
"sambaSamAccount" => array(
"text" => _("Samba"),
"image" => "images/windows.png"
),
"goFaxAccount" => array(
"text" => _("FAX"),
"image" => "plugins/gofax/images/fax_small.png"
),
"gosaProxyAccount" => array(
"text" => _("Proxy"),
"image" => "images/select_proxy.png"
),
"PureFTPdUser" => array(
"text" => _("FTP"),
"image" => "images/select_proxy.png"
),
"posixGroup" => array(
"text" => _("Group"),
"image" => "images/select_groups.png"
),
"gosaDepartment" => array(
"text" => _("Department"),
"image" => "images/select_department.png"
),
"goFonHardware" => array(
"text" => _("Phone"),
"image" => "plugins/systems/images/select_phone.png"
),
"gosaApplication" => array(
"text" => _("Application"),
"image" => "plugins/systems/images/select_application.png"
),
"goServer" => array(
"text" => _("Server"),
"image" => "plugins/systems/images/select_server.png"
),
"gotoTerminal" => array(
"text" => _("Thin Client"),
"image" => "images/select_terminal.png"
),
"gotoWorkstation" => array(
"text" => _("Workstation"),
"image" => "plugins/systems/images/select_workstation.png"
),
"gosaGroupOfNames" => array(
"text" => _("Object group"),
"image" => "plugins/ogroups/images/select_ogroup.png"
return array(
'main' => array(
'name' => _('References'),
'class' => array('fullwidth'),
'attrs' => array(
new FakeAttribute('refs')
),
'template' => get_template_path('contents.tpl', TRUE, dirname(__FILE__))
),
"gotoPrinter" => array(
"text" => _("Printer"),
"image" => "plugins/systems/images/select_printer.png"
)
);
}
function execute()
function __construct(&$config, $dn = NULL, $object = NULL)
{
/* Call parent execute */
plugin::execute();
parent::__construct($config, $dn, $object);
/* Set government mode */
$smarty = get_smarty();
if (isset($_GET['show'])) {
$dn = base64_decode($_GET['show']);
if (isset($this->fold["$dn"])) {
unset($this->fold["$dn"]);
} else {
$this->fold["$dn"] = "$dn";
}
}
/* Fill array */
$this->reload();
$smarty->assign("objectList", $this->objectlist);
/* Show main page */
return $smarty->fetch (get_template_path('contents.tpl', TRUE, dirname(__FILE__)));
$this->refs = NULL;
}
function reload()
function execute()
{
$ldap = $this->config->get_ldap_link();
$ldap->cd($this->config->current['BASE']);
$dn = LDAP::prepare4filter($this->dn);
$ldap->search(
"(|(gotoHotplugDeviceDN=$dn)(member=$dn)(memberUid=".$this->uid."))",
array('memberUid','member','cn','description','objectClass')
);
$cycle = array("F5F5F5", "ECECEC");
$id = 0;
$this->objectlist = "";
while ($attrs = $ldap->fetch()) {
/* Add more information? */
$addon = "";
if (in_array($attrs['dn'], $this->fold)) {
$memberattr = "";
if (isset($attrs['memberUid'])) {
unset($attrs['memberUid']['count']);
$memberattr = "memberUid";
}
if (isset($attrs['member'])) {
unset($attrs['member']['count']);
$memberattr = "member";
}
if ($memberattr == "") {
continue;
}
foreach ($attrs[$memberattr] as $uid) {
$content = $this->getCacheObject($uid);
if ($content === NULL) {
continue;
}
$addon .= '<tr style="background-color:'.$cycle[$id & 1].';"><td title="'.$attrs['cn'][0].'">&nbsp;&nbsp;&nbsp;'.$content['name'].'</td><td>'.$content['description'].'</td><td>'.$content['type'].'</td> </tr>';
}
}
/* Build final entry */
if (isset($attrs['description'])) {
$description = $attrs['description'][0];
} else {
$description = "-";
}
$content = $this->createContentString($attrs['objectClass']);
if ($addon == "") {
$img = "images/lists/expand.png";
} else {
$img = "images/lists/sort-down.png";
}
$this->objectlist .= '<tr style="background-color:'.$cycle[$id & 1].';"><td class="phonelist" title="'.$attrs['cn'][0].'"><a href="main.php?plug='.$_GET['plug'].'&amp;start=&amp;show='.base64_encode($attrs['dn']).'"><img alt="" src="'.$img.'" border=0>&nbsp;'.$attrs['cn'][0].'</a></td><td>'.$description.'</td><td>'.$content.'</td> </tr>'.$addon;
$id++;
if ($this->refs === NULL) {
$this->fillRefs();
}
$smarty = get_smarty();
$smarty->assign('usePrototype', 'true');
return parent::execute();
}
function getCacheObject($dn)
function fillRefs()
{
if (!isset($this->objectCache[$dn])) {
$ldap = $this->config->get_ldap_link();
if (preg_match('/,/', $dn)) {
$ldap->cat($dn, array("cn", "objectClass", "description","ou"));
} else {
$ldap->search("(uid=$dn)", array("cn", "objectClass", "description","ou"));
}
if ($ldap->count()) {
$attrs = $ldap->fetch();
if (isset($attrs['ou']) && !isset($attrs['cn'])) {
$attrs['cn'] = $attrs['ou'];
}
if (isset($attrs['description'])) {
$description = $attrs['description'][0];
} else {
$description = "-";
if (!isset($this->parent)) {
$this->refs = NULL;
return;
}
$foreignRefs = array();
foreach (array_keys($this->parent->by_object) as $tabclass) {
$infos = pluglist::pluginInfos($tabclass);
foreach ($infos['plForeignRefs'] as $field => $refs) {
foreach ($refs as $ref) {
$class = $ref[0];
$ofield = $ref[1];
$cinfos = pluglist::pluginInfos($class);
foreach ($cinfos['plObjectType'] as $key => $objectType) {
if (!is_numeric($key)) {
$objectType = $key;
}
$foreignRefs[$objectType][$class][$ofield] = array($tabclass,$field);
}
}
$this->objectCache[$dn] = array( 'name' => $attrs['cn'][0],
'description' => "$description",
'type' => $this->createContentString($attrs['objectClass']));
} else {
return NULL;
}
}
return $this->objectCache[$dn];
}
function createContentString($classes)
{
$result = "";
if (!is_null($classes)) {
foreach ($classes as $class) {
if (isset($this->obtypes[$class])) {
$result .= "<a><img alt=\"\" border=0 src='".$this->obtypes[$class]['image']."'>&nbsp;".$this->obtypes[$class]['text']."</a> ";
$refs = array();
// For each concerned objectType
foreach ($foreignRefs as $objectType => $tabRefs) {
// Search objects
$objects = objects::ls($objectType, array('dn' => 'raw'));
// For each object of this type
foreach (array_keys($objects) as $dn) {
// Build the object
$tabobject = objects::open($dn, $objectType);
// For each tab concerned
foreach ($tabRefs as $tab => $fieldRefs) {
// If the tab is activated on this object
if (isset($tabobject->by_object[$tab])) {
// For each field
foreach ($fieldRefs as $ofield => $field_infos) {
list ($tabclass,$field) = $field_infos;
if ($tabobject->by_object[$tab]->foreignKeyCheck(
$ofield,
$this->parent->by_object[$tabclass]->$field,
$field
)) {
if (!isset($refs[$dn])) {
$refs[$dn] = array(
'link' => '',
'tabs' => array(),
);
try {
$refs[$dn]['link'] = objects::link($dn, $objectType);
} catch (Exception $e) {
$refs[$dn]['link'] = $dn;
}
}
if (!isset($refs[$dn]['tabs'][$tab])) {
$refs[$dn]['tabs'][$tab] = array(
'link' => '',
'fields' => array(),
);
try {
$refs[$dn]['tabs'][$tab]['link'] = objects::link($dn, $objectType, "tab_$tab", sprintf(_('Tab "%s"'), $tabobject->by_name[$tab]));
} catch (Exception $e) {
$refs[$dn]['tabs'][$tab]['link'] = $tab;
}
}
$refs[$dn]['tabs'][$tab]['fields'][$ofield] = array(
'tab' => $this->parent->by_name[$tabclass],
'field' => $field
);
}
}
}
}
}
}
return preg_replace('/ $/', '', $result);
$this->refs = $refs;
}
}
?>
{if $objectList ne ""}
<table summary="" style="width:100%; vertical-align:top; text-align:left; border:1px solid #B0B0B0;" cellpadding=4 cellspacing=0 border=0 rules="cols">
<colgroup>
<col width="20%">
<col width="20%">
<col width="60%">
</colgroup>
<tr style="background-color: #E8E8E8; height:26px;font-weight:bold;">
<td>{t}Object name{/t}</td><td>{t}Description{/t}</td><td>{t}Contents{/t}</td>
</tr>
{$objectList}
</table>
{else}
<b>{t}This object has no relationship to other objects.{/t}</b>
{/if}
<div id="{$sectionId}" class="plugin_section{$sectionClasses}">
<span class="legend">
{$section}
</span>
<div>
{if $attributes.refs}
<ul>
{foreach from=$attributes.refs key=key item=object}
<li style="list-style-type:none;">
{$object.link}
<ul>
{foreach from=$object.tabs item=tab}
<li style="list-style-type:none;">{$tab.link}
<ul>
{foreach from=$tab.fields key=ofield item=field}
<li style="list-style-type:disc;">
{t escape=no 1=$ofield 2=$field.tab 3=$field.field}<strong>%1</strong> references our field <strong>%3</strong> from tab <strong>%2</strong>{/t}
</li>
{/foreach}
</ul>
</li>
{/foreach}
</ul>
</li>
{/foreach}
</ul>
{else}
<b>{t}This object has no relationship to other objects.{/t}</b>
{/if}
</div>
</div>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment