'.Dict::S('UI:UndefinedObject').''; // Objects built in memory have negative IDs
$oAppContext = new ApplicationContext();
$sExtClassNameAtt = MetaModel::GetNameAttributeCode($sObjClass);
$sPage = self::ComputeUIPage($sObjClass);
$sAbsoluteUrl = utils::GetAbsoluteUrl(false); // False => Don't get the query string
$sAbsoluteUrl = substr($sAbsoluteUrl, 0, 1+strrpos($sAbsoluteUrl, '/')); // remove the current page, keep just the path, up to the last /
// Use the "name" of the target class as the label of the hyperlink
// unless it's not available in the external attributes...
if (isset($aAvailableFields[$sExtClassNameAtt]))
{
$sLabel = $aAvailableFields[$sExtClassNameAtt];
}
else
{
$sLabel = implode(' / ', $aAvailableFields);
}
// Safety belt
//
if (empty($sLabel))
{
// Developer's note:
// This is doing the job for you, but that is just there in case
// the external fields associated to the external key are blanks
// The ultimate solution will be to query the name automatically
// and independantly from the data model (automatic external field)
// AND make the name be a mandatory field
//
$sObject = MetaModel::GetObject($sObjClass, $sObjKey);
$sLabel = $sObject->GetDisplayName();
}
// Safety net
//
if (empty($sLabel))
{
$sLabel = MetaModel::GetName($sObjClass)." #$sObjKey";
}
$sHint = MetaModel::GetName($sObjClass)."::$sObjKey";
return "GetForLink()."\" title=\"$sHint\">$sLabel";
}
function DisplayBareHeader(WebPage $oPage)
{
// Standard Header with name, actions menu and history block
//
$oPage->add("
\n");
// action menu
$oSingletonFilter = new DBObjectSearch(get_class($this));
$oSingletonFilter->AddCondition('id', array($this->GetKey()));
$oBlock = new MenuBlock($oSingletonFilter, 'popup', false);
$oBlock->Display($oPage, -1);
$oPage->add("
\n";
}
}
}
return $sHtml;
}
function DisplayDetails(WebPage $oPage)
{
$sTemplate = Utils::ReadFromFile(MetaModel::GetDisplayTemplate(get_class($this)));
if (!empty($sTemplate))
{
$oTemplate = new DisplayTemplate($sTemplate);
$sNameAttCode = MetaModel::GetNameAttributeCode(get_class($this));
// Note: to preserve backward compatibility with home-made templates, the placeholder '$pkey$' has been preserved
// but the preferred method is to use '$id$'
$oTemplate->Render($oPage, array('class_name'=> MetaModel::GetName(get_class($this)),'class'=> get_class($this), 'pkey'=> $this->GetKey(), 'id'=> $this->GetKey(), 'name' => $this->Get($sNameAttCode)));
}
else
{
// Object's details
// template not found display the object using the *old style*
$this->DisplayBareHeader($oPage);
$this->DisplayBareDetails($oPage);
$this->DisplayBareRelations($oPage);
}
}
function DisplayPreview(WebPage $oPage)
{
$aDetails = array();
$sClass = get_class($this);
$aList = MetaModel::GetZListItems($sClass, 'preview');
foreach($aList as $sAttCode)
{
$aDetails[] = array('label' => MetaModel::GetLabel($sClass, $sAttCode), 'value' =>$this->GetAsHTML($sAttCode));
}
$oPage->details($aDetails);
}
// Comment by Rom: this helper may be used to display objects of class DBObject
// -> I am using this to display the changes history
public static function DisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
{
$oPage->add(self::GetDisplaySet($oPage, $oSet, $aExtraParams));
}
//public static function GetDisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $sLinkageAttribute = '', $bDisplayMenu = true, $bSelectMode = false)
public static function GetDisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
{
static $iListId = 0;
$iListId++;
// Initialize and check the parameters
$sLinkageAttribute = isset($aExtraParams['link_attr']) ? $aExtraParams['link_attr'] : '';
$iLinkedObjectId = isset($aExtraParams['object_id']) ? $aExtraParams['object_id'] : 0;
$sTargetAttr = isset($aExtraParams['target_attr']) ? $aExtraParams['target_attr'] : '';
if (!empty($sLinkageAttribute))
{
if($iLinkedObjectId == 0)
{
// if 'links' mode is requested the id of the object to link to must be specified
throw new ApplicationException(Dict::S('UI:Error:MandatoryTemplateParameter_object_id'));
}
if($sTargetAttr == '')
{
// if 'links' mode is requested the d of the object to link to must be specified
throw new ApplicationException(Dict::S('UI:Error:MandatoryTemplateParameter_target_attr'));
}
}
$bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
$bSelectMode = isset($aExtraParams['selection_mode']) ? $aExtraParams['selection_mode'] == true : false;
$bSingleSelectMode = isset($aExtraParams['selection_type']) ? ($aExtraParams['selection_type'] == 'single') : false;
$sHtml = '';
$oAppContext = new ApplicationContext();
$sClassName = $oSet->GetFilter()->GetClass();
$aAttribs = array();
$aList = MetaModel::GetZListItems($sClassName, 'list');
if (!empty($sLinkageAttribute))
{
// The set to display is in fact a set of links between the object specified in the $sLinkageAttribute
// and other objects...
// The display will then group all the attributes related to the link itself:
// | Link_attr1 | link_attr2 | ... || Object_attr1 | Object_attr2 | Object_attr3 | .. | Object_attr_n |
$aAttDefs = MetaModel::ListAttributeDefs($sClassName);
assert(isset($aAttDefs[$sLinkageAttribute]));
$oAttDef = $aAttDefs[$sLinkageAttribute];
assert($oAttDef->IsExternalKey());
// First display all the attributes specific to the link record
foreach($aList as $sLinkAttCode)
{
$oLinkAttDef = $aAttDefs[$sLinkAttCode];
if ( (!$oLinkAttDef->IsExternalKey()) && (!$oLinkAttDef->IsExternalField()) )
{
$aDisplayList[] = $sLinkAttCode;
}
}
// Then display all the attributes neither specific to the link record nor to the 'linkage' object (because the latter are constant)
foreach($aList as $sLinkAttCode)
{
$oLinkAttDef = $aAttDefs[$sLinkAttCode];
if (($oLinkAttDef->IsExternalKey() && ($sLinkAttCode != $sLinkageAttribute))
|| ($oLinkAttDef->IsExternalField() && ($oLinkAttDef->GetKeyAttCode()!=$sLinkageAttribute)) )
{
$aDisplayList[] = $sLinkAttCode;
}
}
// First display all the attributes specific to the link
// Then display all the attributes linked to the other end of the relationship
$aList = $aDisplayList;
}
if ($bSelectMode)
{
if (!$bSingleSelectMode)
{
$aAttribs['form::select'] = array('label' => "", 'description' => Dict::S('UI:SelectAllToggle+'));
}
else
{
$aAttribs['form::select'] = array('label' => "", 'description' => '');
}
}
$aAttribs['key'] = array('label' => '', 'description' => Dict::S('UI:ClickToDisplay+'));
foreach($aList as $sAttCode)
{
$aAttribs[$sAttCode] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
}
$aValues = array();
$oSet->Seek(0);
$bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
$iMaxObjects = -1;
if ($bDisplayLimit)
{
if ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit())
{
$iMaxObjects = utils::GetConfig()->GetMinDisplayLimit();
}
}
while (($oObj = $oSet->Fetch()) && ($iMaxObjects != 0))
{
$aRow = array();
$aRow['key'] = $oObj->GetKey();
if ($bSelectMode)
{
if ($bSingleSelectMode)
{
$aRow['form::select'] = "GetKey()."\">";
}
else
{
$aRow['form::select'] = "GetKey()."\">";
}
}
$aRow['key'] = $oObj->GetKey();
foreach($aList as $sAttCode)
{
$aRow[$sAttCode] = $oObj->GetAsHTML($sAttCode);
}
$aValues[] = $aRow;
$iMaxObjects--;
}
$sHtml .= '
';
$sColspan = '';
if ($bDisplayMenu)
{
$oMenuBlock = new MenuBlock($oSet->GetFilter());
$sColspan = 'colspan="2"';
$aMenuExtraParams = $aExtraParams;
if (!empty($sLinkageAttribute))
{
//$aMenuExtraParams['linkage'] = $sLinkageAttribute;
$aMenuExtraParams = $aExtraParams;
}
if ($bDisplayLimit && ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit()))
{
// list truncated
$divId = $aExtraParams['block_id'];
$sFilter = $oSet->GetFilter()->serialize();
$aExtraParams['display_limit'] = false; // To expand the full list
$sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
$sHtml .= '
';
return $sHtml;
}
public static function GetDisplayExtendedSet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
{
static $iListId = 0;
$iListId++;
$aList = array();
// Initialize and check the parameters
$bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
// Check if there is a list of aliases to limit the display to...
$aDisplayAliases = isset($aExtraParams['display_aliases']) ? explode(',', $aExtraParams['display_aliases']) : array();
$sHtml = '';
$oAppContext = new ApplicationContext();
$aClasses = $oSet->GetFilter()->GetSelectedClasses();
$aAuthorizedClasses = array();
foreach($aClasses as $sAlias => $sClassName)
{
if ((UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES) &&
( (count($aDisplayAliases) == 0) || (in_array($sAlias, $aDisplayAliases))) )
{
$aAuthorizedClasses[$sAlias] = $sClassName;
}
}
$aAttribs = array();
foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
{
$aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'list');
$aAttribs['key_'.$sAlias] = array('label' => '', 'description' => Dict::S('UI:ClickToDisplay+'));
foreach($aList[$sClassName] as $sAttCode)
{
$aAttribs[$sAttCode.'_'.$sAlias] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
}
}
$aValues = array();
$oSet->Seek(0);
$bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
$iMaxObjects = -1;
if ($bDisplayLimit)
{
if ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit())
{
$iMaxObjects = utils::GetConfig()->GetMinDisplayLimit();
}
}
while (($aObjects = $oSet->FetchAssoc()) && ($iMaxObjects != 0))
{
$aRow = array();
foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
{
$aRow['key_'.$sAlias] = $aObjects[$sAlias]->GetKey();
foreach($aList[$sClassName] as $sAttCode)
{
$aRow[$sAttCode.'_'.$sAlias] = $aObjects[$sAlias]->GetAsHTML($sAttCode);
}
}
$aValues[] = $aRow;
$iMaxObjects--;
}
$sHtml .= '
';
$sColspan = '';
if ($bDisplayMenu)
{
$oMenuBlock = new MenuBlock($oSet->GetFilter());
$sColspan = 'colspan="2"';
$aMenuExtraParams = $aExtraParams;
if (!empty($sLinkageAttribute))
{
$aMenuExtraParams = $aExtraParams;
}
if ($bDisplayLimit && ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit()))
{
// list truncated
$divId = $aExtraParams['block_id'];
$sFilter = $oSet->GetFilter()->serialize();
$aExtraParams['display_limit'] = false; // To expand the full list
$sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
$sHtml .= '
\n";
}
// Check if the current class has some sub-classes
if (isset($aExtraParams['baseClass']))
{
$sRootClass = $aExtraParams['baseClass'];
}
else
{
$sRootClass = $sClassName;
}
$aSubClasses = MetaModel::GetSubclasses($sRootClass);
if (count($aSubClasses) > 0)
{
$aOptions = array();
$aOptions[MetaModel::GetName($sRootClass)] = "
\n";
}
// OQL query builder
$sHtml .= "
\n";
$sHtml .= "
".Dict::S('UI:OQLQueryBuilderTitle')."
\n";
$sHtml .= "\n";
$sHtml .= "
\n";
return $sHtml;
}
public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '', $iFlags = 0, $aArgs = array())
{
static $iInputId = 0;
if (isset($aArgs[$sAttCode]) && empty($value))
{
// default value passed by the context (either the app context of the operation)
$value = $aArgs[$sAttCode];
}
if (!empty($iId))
{
$iInputId = $iId;
}
else
{
$iInputId++;
}
if (!$oAttDef->IsExternalField())
{
$aCSSClasses = array();
$bMandatory = 0;
if ( (!$oAttDef->IsNullAllowed()) || ($iFlags & OPT_ATT_MANDATORY))
{
$aCSSClasses[] = 'mandatory';
$bMandatory = 1;
}
$sCSSClasses = self::GetCSSClasses($aCSSClasses);
$sValidationField = "";
switch($oAttDef->GetEditClass())
{
case 'Date':
case 'DateTime':
$aCSSClasses[] = 'date-pick';
$sCSSClasses = self::GetCSSClasses($aCSSClasses);
$sHTMLValue = "$sValidationField";
break;
case 'Password':
$sHTMLValue = "$sValidationField";
break;
case 'Text':
$sHTMLValue = "$sValidationField";
break;
case 'List':
$oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sNameSuffix);
$sHTMLValue = $oWidget->Display($oPage, $value);
break;
case 'Document':
$oDocument = $value; // Value is an ormDocument object
$sFileName = '';
if (is_object($oDocument))
{
$sFileName = $oDocument->GetFileName();
}
$iMaxFileSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
$sHTMLValue = "\n";
$sHTMLValue .= "\n";
$sHTMLValue .= "$sFileName \n";
$sHTMLValue .= "\n";
break;
case 'String':
default:
// #@# todo - add context information (depending on dimensions)
$aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
if ($aAllowedValues !== null)
{
//Enum field or external key, display a combo
//if (count($aAllowedValues) == 0)
//{
// $sHTMLValue = "";
//}
//else if (count($aAllowedValues) > 50)
if (count($aAllowedValues) > 50)
{
// too many choices, use an autocomplete
// The input for the auto complete
$sHTMLValue = "$sValidationField";
// another hidden input to store & pass the object's Id
$sHTMLValue .= "\n";
$oPage->add_ready_script("\$('#label_$iInputId').autocomplete('./ajax.render.php', { scroll:true, minChars:3, onItemSelect:selectItem, onFindValue:findValue, formatItem:formatItem, autoFill:true, keyHolder:'#$iInputId', extraParams:{operation:'autocomplete', sclass:'$sClass',attCode:'".$sAttCode."'}});");
$oPage->add_ready_script("\$('#label_$iInputId').result( function(event, data, formatted) { if (data) { $('#{$iInputId}').val(data[1]); } } );");
// Prepopulate with a default value -- but no display value...
//if (!empty($value))
//{
// $oPage->add_ready_script("\$('#label_$iInputId').search( 'domino.combodo.com' );");
//}
}
else
{
// Few choices, use a normal 'select'
// In case there are no valid values, the select will be empty, thus blocking the user from validating the form
$sHTMLValue = "$sValidationField\n";
}
}
else
{
$sHTMLValue = "$sValidationField";
}
break;
}
$sPattern = addslashes($oAttDef->GetValidationPattern()); //'^([0-9]+)$';
$oPage->add_ready_script("$('#$iInputId').bind('validate blur', function(evt, sFormId) { return ValidateField('$iInputId', '$sPattern', $bMandatory, sFormId) } );"); // Bind to a custom event: validate
$aDependencies = MetaModel::GetDependentAttributes($sClass, $sAttCode); // List of attributes that depend on the current one
if (count($aDependencies) > 0)
{
$oPage->add_ready_script("$('#$iInputId').bind('change', function(evt, sFormId) { return UpdateDependentFields(['".implode("','", $aDependencies)."']) } );"); // Bind to a custom event: validate
}
}
return $sHTMLValue;
}
public function DisplayModifyForm(WebPage $oPage, $aExtraParams = array())
{
static $iFormId = 0;
$iFormId++;
$sClass = get_class($this);
$oAppContext = new ApplicationContext();
$sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
$iKey = $this->GetKey();
$aDetails = array();
$aFieldsMap = array();
$oPage->add("\n");
$iFieldsCount = count($aFieldsMap);
$sJsonFieldsMap = json_encode($aFieldsMap);
$oPage->add_script(
<<add("\n");
}
protected static function GetCSSClasses($aCSSClasses)
{
$sCSSClasses = '';
if (!empty($aCSSClasses))
{
$sCSSClasses = ' class="'.implode(' ', $aCSSClasses).'" ';
}
return $sCSSClasses;
}
}
?>