* @author Romain Quetiez * @author Denis Flaven * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL */ require_once(APPROOT.'/application/webpage.class.inc.php'); require_once(APPROOT.'/application/displayblock.class.inc.php'); class UILinksWidget { protected $m_sClass; protected $m_sAttCode; protected $m_sNameSuffix; protected $m_iInputId; protected $m_aAttributes; protected $m_sExtKeyToRemote; protected $m_sLinkedClass; protected $m_sRemoteClass; protected $m_bDuplicatesAllowed; public function __construct($sClass, $sAttCode, $iInputId, $sNameSuffix = '', $bDuplicatesAllowed = false) { $this->m_sClass = $sClass; $this->m_sAttCode = $sAttCode; $this->m_sNameSuffix = $sNameSuffix; $this->m_iInputId = $iInputId; $this->m_bDuplicatesAllowed = $bDuplicatesAllowed; $this->m_aEditableFields = array(); $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sAttCode); $this->m_sLinkedClass = $oAttDef->GetLinkedClass(); $this->m_sExtKeyToRemote = $oAttDef->GetExtKeyToRemote(); $oLinkingAttDef = MetaModel::GetAttributeDef($this->m_sLinkedClass, $this->m_sExtKeyToRemote); $this->m_sRemoteClass = $oLinkingAttDef->GetTargetClass(); $sExtKeyToMe = $oAttDef->GetExtKeyToMe(); $sStateAttCode = MetaModel::GetStateAttributeCode($this->m_sClass); $sDefaultState = MetaModel::GetDefaultState($this->m_sClass); $this->m_aEditableFields = array(); $this->m_aTableConfig = array(); $this->m_aTableConfig['form::checkbox'] = array( 'label' => "m_sAttCode}{$this->m_sNameSuffix} .selection', this.checked); oWidget".$this->m_iInputId.".OnSelectChange();\">", 'description' => Dict::S('UI:SelectAllToggle+')); foreach(MetaModel::ListAttributeDefs($this->m_sLinkedClass) as $sAttCode=>$oAttDef) { if ($sStateAttCode == $sAttCode) { // State attribute is always hidden from the UI } else if ($oAttDef->IsWritable() && ($sAttCode != $sExtKeyToMe) && ($sAttCode != $this->m_sExtKeyToRemote) && ($sAttCode != 'finalclass')) { $iFlags = MetaModel::GetAttributeFlags($this->m_sLinkedClass, $sDefaultState, $sAttCode); if ( !($iFlags & OPT_ATT_HIDDEN) && !($iFlags & OPT_ATT_READONLY) ) { $this->m_aEditableFields[] = $sAttCode; $this->m_aTableConfig[$sAttCode] = array( 'label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription()); } } } $this->m_aTableConfig['static::key'] = array( 'label' => MetaModel::GetName($this->m_sRemoteClass), 'description' => MetaModel::GetClassDescription($this->m_sRemoteClass)); foreach(MetaModel::GetZListItems($this->m_sRemoteClass, 'list') as $sFieldCode) { // TO DO: check the state of the attribute: hidden or visible ? if ($sFieldCode != 'finalclass') { $oAttDef = MetaModel::GetAttributeDef($this->m_sRemoteClass, $sFieldCode); $this->m_aTableConfig['static::'.$sFieldCode] = array( 'label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription()); } } } /** * A one-row form for editing a link record * @param WebPage $oP Web page used for the ouput * @param DBObject $oLinkedObj The object to which all the elements of the linked set refer to * @param mixed $linkObjOrId Either the object linked or a unique number for new link records to add * @param Hash $aArgs Extra context arguments * @return string The HTML fragment of the one-row form */ protected function GetFormRow(WebPage $oP, DBObject $oLinkedObj, $linkObjOrId = null, $aArgs = array() ) { $sPrefix = "$this->m_sAttCode{$this->m_sNameSuffix}"; $aRow = array(); if(is_object($linkObjOrId)) { $key = $linkObjOrId->GetKey(); $sPrefix .= "[$key]["; $sNameSuffix = "]"; // To make a tabular form $aArgs['prefix'] = $sPrefix; $aRow['form::checkbox'] = "m_iInputId.".OnSelectChange();\" value=\"$key\">"; $aRow['form::checkbox'] .= ""; foreach($this->m_aEditableFields as $sFieldCode) { $sFieldId = $this->m_iInputId.'_'.$sFieldCode.'['.$linkObjOrId->GetKey().']'; $sSafeId = str_replace(array('[',']','-'), '_', $sFieldId); $oAttDef = MetaModel::GetAttributeDef($this->m_sLinkedClass, $sFieldCode); $aRow[$sFieldCode] = cmdbAbstractObject::GetFormElementForField($oP, $this->m_sLinkedClass, $sFieldCode, $oAttDef, $linkObjOrId->Get($sFieldCode), '' /* DisplayValue */, $sSafeId, $sNameSuffix, 0, $aArgs); } } else { // form for creating a new record $sPrefix .= "[$linkObjOrId]["; $sNameSuffix = "]"; // To make a tabular form $aArgs['prefix'] = $sPrefix; $aRow['form::checkbox'] = "m_iInputId.".OnSelectChange();\" value=\"$linkObjOrId\">"; $aRow['form::checkbox'] .= ""; foreach($this->m_aEditableFields as $sFieldCode) { $sFieldId = $this->m_iInputId.'_'.$sFieldCode.'['.$linkObjOrId.']'; $sSafeId = str_replace(array('[',']','-'), '_', $sFieldId); $oAttDef = MetaModel::GetAttributeDef($this->m_sLinkedClass, $sFieldCode); $aRow[$sFieldCode] = cmdbAbstractObject::GetFormElementForField($oP, $this->m_sLinkedClass, $sFieldCode, $oAttDef, '' /* TO DO/ call GetDefaultValue($oObject->ToArgs()) */, '' /* DisplayValue */, $sSafeId /* id */, $sNameSuffix, 0, $aArgs); } } $aRow['static::key'] = $oLinkedObj->GetHyperLink(); foreach(MetaModel::GetZListItems($this->m_sRemoteClass, 'list') as $sFieldCode) { $aRow['static::'.$sFieldCode] = $oLinkedObj->GetAsHTML($sFieldCode); } return $aRow; } /** * Display one row of the whole form * @return none */ protected function DisplayFormRow(WebPage $oP, $aConfig, $aRow, $iRowId) { $sHtml = ''; $sHtml .= "m_sAttCode}{$this->m_sNameSuffix}_row_$iRowId\">\n"; foreach($aConfig as $sName=>$void) { $sHtml .= "".$aRow[$sName]."\n"; } $sHtml .= "\n"; return $sHtml; } /** * Display the table with the form for editing all the links at once * @param WebPage $oP The web page used for the output * @param Hash $aConfig The table's header configuration * @param Hash $aData The tabular data to be displayed * @return string Html fragment representing the form table */ protected function DisplayFormTable(WebPage $oP, $aConfig, $aData) { $sHtml = ''; $sHtml .= "\n"; // Header $sHtml .= "\n"; $sHtml .= "\n"; foreach($aConfig as $sName=>$aDef) { $sHtml .= "\n"; } $sHtml .= "\n"; $sHtml .= "\n"; // Content $sHtml .= "\n"; if (count($aData) == 0) { $sHtml .= "m_sAttCode}{$this->m_sNameSuffix}_empty_row\">"; } else { foreach($aData as $iRowId => $aRow) { $sHtml .= $this->DisplayFormRow($oP, $aConfig, $aRow, $iRowId); } } $sHtml .= "\n"; // Footer $sHtml .= "
".$aDef['label']."
".Dict::S('UI:Message:EmptyList:UseAdd')."m_sAttCode}{$this->m_sNameSuffix}\" value=\"\">
\n"; return $sHtml; } /** * Get the HTML fragment corresponding to the linkset editing widget * @param WebPage $oP The web page used for all the output * @param DBObjectSet The initial value of the linked set * @param Hash $aArgs Extra context arguments * @return string The HTML fragment to be inserted into the page */ public function Display(WebPage $oPage, DBObjectSet $oValue, $aArgs = array()) { $sHtmlValue = ''; $sTargetClass = self::GetTargetClass($this->m_sClass, $this->m_sAttCode); $sHtmlValue .= "
m_sAttCode}{$this->m_sNameSuffix}\">\n"; $oValue->Rewind(); $aForm = array(); while($oCurrentLink = $oValue->Fetch()) { $aRow = array(); $key = $oCurrentLink->GetKey(); $oLinkedObj = MetaModel::GetObject($this->m_sRemoteClass, $oCurrentLink->Get($this->m_sExtKeyToRemote)); $aForm[$key] = $this->GetFormRow($oPage, $oLinkedObj, $oCurrentLink, $aArgs); } $sHtmlValue .= $this->DisplayFormTable($oPage, $this->m_aTableConfig, $aForm); $sDuplicates = ($this->m_bDuplicatesAllowed) ? 'true' : 'false'; $oPage->add_ready_script(<<m_iInputId} = new LinksWidget('{$this->m_sAttCode}{$this->m_sNameSuffix}', '{$this->m_sClass}', '{$this->m_sAttCode}', '{$this->m_iInputId}', '{$this->m_sNameSuffix}', $sDuplicates); oWidget{$this->m_iInputId}.Init(); EOF ); $sHtmlValue .= "     m_sAttCode}{$this->m_sNameSuffix}_btnRemove\" type=\"button\" value=\"".Dict::S('UI:RemoveLinkedObjectsOf_Class')."\" onClick=\"oWidget{$this->m_iInputId}.RemoveSelected();\" >"; $sHtmlValue .= "   m_sAttCode}{$this->m_sNameSuffix}_btnAdd\" type=\"button\" value=\"".Dict::Format('UI:AddLinkedObjectsOf_Class', MetaModel::GetName($this->m_sRemoteClass))."\" onClick=\"oWidget{$this->m_iInputId}.AddObjects();\">\n"; $sHtmlValue .= "

 

\n"; $sHtmlValue .= "
\n"; $oPage->add_at_the_end($this->GetObjectPickerDialog($oPage)); // To prevent adding forms inside the main form return $sHtmlValue; } protected static function GetTargetClass($sClass, $sAttCode) { $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode); $sLinkedClass = $oAttDef->GetLinkedClass(); switch(get_class($oAttDef)) { case 'AttributeLinkedSetIndirect': $oLinkingAttDef = MetaModel::GetAttributeDef($sLinkedClass, $oAttDef->GetExtKeyToRemote()); $sTargetClass = $oLinkingAttDef->GetTargetClass(); break; case 'AttributeLinkedSet': $sTargetClass = $sLinkedClass; break; } return $sTargetClass; } protected function GetObjectPickerDialog($oPage) { $sHtml = "
m_sAttCode}{$this->m_sNameSuffix}\">"; $sHtml .= "
\n"; $oFilter = new DBObjectSearch($this->m_sRemoteClass); $oSet = new CMDBObjectSet($oFilter); $oBlock = new DisplayBlock($oFilter, 'search', false); $sHtml .= $oBlock->GetDisplay($oPage, "SearchFormToAdd_{$this->m_sAttCode}{$this->m_sNameSuffix}", array('open' => true)); $sHtml .= "
m_sAttCode}{$this->m_sNameSuffix}\" OnSubmit=\"return oWidget{$this->m_iInputId}.DoAddObjects(this.id);\">\n"; $sHtml .= "
m_sAttCode}{$this->m_sNameSuffix}\" style=\"vertical-align:top;background: #fff;height:100%;overflow:auto;padding:0;border:0;\">\n"; $sHtml .= "

".Dict::S('UI:Message:EmptyList:UseSearchForm')."

\n"; $sHtml .= "
\n"; $sHtml .= "m_sAttCode}{$this->m_sNameSuffix}').dialog('close');\">  "; $sHtml .= "
\n"; $sHtml .= "\n"; $sHtml .= "
\n"; $oPage->add_ready_script("$('#dlg_{$this->m_sAttCode}{$this->m_sNameSuffix}').dialog({ width: $(window).width()*0.8, height: $(window).height()*0.8, autoOpen: false, modal: true, resizeStop: oWidget{$this->m_iInputId}.UpdateSizes });"); $oPage->add_ready_script("$('#dlg_{$this->m_sAttCode}{$this->m_sNameSuffix}').dialog('option', {title:'".addslashes(Dict::Format('UI:AddObjectsOf_Class_LinkedWith_Class', MetaModel::GetName($this->m_sLinkedClass), MetaModel::GetName($this->m_sClass)))."'});"); $oPage->add_ready_script("$('#SearchFormToAdd_{$this->m_sAttCode}{$this->m_sNameSuffix} form').bind('submit.uilinksWizard', oWidget{$this->m_iInputId}.SearchObjectsToAdd);"); $oPage->add_ready_script("$('#SearchFormToAdd_{$this->m_sAttCode}{$this->m_sNameSuffix}').resize(oWidget{$this->m_iInputId}.UpdateSizes);"); return $sHtml; } /** * Search for objects to be linked to the current object (i.e "remote" objects) * @param WebPage $oP The page used for the output (usually an AjaxWebPage) * @param string $sRemoteClass Name of the "remote" class to perform the search on, must be a derived class of m_sRemoteClass * @param Array $aAlreadyLinkedIds List of IDs of objects of "remote" class already linked, to be filtered out of the search */ public function SearchObjectsToAdd(WebPage $oP, $sRemoteClass = '', $aAlreadyLinkedIds = array()) { if ($sRemoteClass != '') { // assert(MetaModel::IsParentClass($this->m_sRemoteClass, $sRemoteClass)); $oFilter = new DBObjectSearch($sRemoteClass); } else { // No remote class specified use the one defined in the linkedset $oFilter = new DBObjectSearch($this->m_sRemoteClass); } if (!$this->m_bDuplicatesAllowed && count($aAlreadyLinkedIds) > 0) { // Positive IDs correspond to existing link records // negative IDs correspond to "remote" objects to be linked $aLinkIds = array(); $aRemoteObjIds = array(); foreach($aAlreadyLinkedIds as $iId) { if ($iId > 0) { $aLinkIds[] = $iId; } else { $aRemoteObjIds[] = -$iId; } } if (count($aLinkIds) >0) { // Search for the links to find to which "remote" object they are linked $oLinkFilter = new DBObjectSearch($this->m_sLinkedClass); $oLinkFilter->AddCondition('id', $aLinkIds, 'IN'); $oLinkSet = new CMDBObjectSet($oLinkFilter); while($oLink = $oLinkSet->Fetch()) { $aRemoteObjIds[] = $oLink->Get($this->m_sExtKeyToRemote); } } $oFilter->AddCondition('id', $aRemoteObjIds, 'NOTIN'); } $oSet = new CMDBObjectSet($oFilter); $oBlock = new DisplayBlock($oFilter, 'list', false); $oBlock->Display($oP, 'ResultsToAdd', array('menu' => false, 'selection_mode' => true, 'display_limit' => false)); // Don't display the 'Actions' menu on the results } public function DoAddObjects(WebPage $oP, $aLinkedObjectIds = array()) { $aTable = array(); foreach($aLinkedObjectIds as $iObjectId) { $oLinkedObj = MetaModel::GetObject($this->m_sRemoteClass, $iObjectId); if (is_object($oLinkedObj)) { $aRow = $this->GetFormRow($oP, $oLinkedObj, -$iObjectId ); // Not yet created link get negative Ids $oP->add($this->DisplayFormRow($oP, $this->m_aTableConfig, $aRow, -$iObjectId)); } else { $oP->p(Dict::Format('UI:Error:Object_Class_Id_NotFound', $this->m_sLinkedClass, $iObjectId)); } } } } ?>