m_sClass = $sClass; $this->m_sAttCode = $sAttCode; $this->m_sNameSuffix = $sNameSuffix; $this->m_iInputId = $iInputId; } public function Display(web_page $oPage, $oCurrentValuesSet = null) { $sHTMLValue = ''; $sTargetClass = self::GetTargetClass($this->m_sClass, $this->m_sAttCode); // #@# todo - add context information, otherwise any value will be authorized for external keys $aAllowedValues = MetaModel::GetAllowedValues_att($this->m_sClass, $this->m_sAttCode, array(), ''); $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sAttCode); $sExtKeyToRemote = $oAttDef->GetExtKeyToRemote(); $sExtKeyToMe = $oAttDef->GetExtKeyToMe(); $sStateAttCode = MetaModel::GetStateAttributeCode($this->m_sClass); $sDefaultState = MetaModel::GetDefaultState($this->m_sClass); $aAttributes = array(); $sLinkedClass = $oAttDef->GetLinkedClass(); foreach(MetaModel::ListAttributeDefs($sLinkedClass) as $sAttCode=>$oAttDef) { if ($sStateAttCode == $sAttCode) { // State attribute is always hidden from the UI } else if (!$oAttDef->IsExternalField() && ($sAttCode != $sExtKeyToMe) && ($sAttCode != $sExtKeyToRemote)) { $iFlags = MetaModel::GetAttributeFlags($this->m_sClass, $sDefaultState, $sAttCode); if ( !($iFlags & OPT_ATT_HIDDEN) && !($iFlags & OPT_ATT_READONLY) ) { $aAttributes[] = $sAttCode; } } } $sAttributes = "['".implode("','", $aAttributes)."']"; if ($oCurrentValuesSet != null) { // Serialize the link set into a JSon object $aCurrentValues = array(); while($oLinkObj = $oCurrentValuesSet->Fetch()) { $sRow = '{'; foreach($aAttributes as $sLinkAttCode) { $sRow.= "\"$sLinkAttCode\": \"".addslashes($oLinkObj->Get($sLinkAttCode))."\", "; } $sRow .= "\"$sExtKeyToRemote\": ".$oLinkObj->Get($sExtKeyToRemote).'}'; $aCurrentValues[] = $sRow; } $sJSON = '['.implode(',', $aCurrentValues).']'; } else { //echo "JSON VA IECH
\n"; } //echo "JASON: $sJSON
\n";; // Many values (or even a unknown list) display an autocomplete if ( (count($aAllowedValues) == 0) || (count($aAllowedValues) > 50) ) { // too many choices, use an autocomplete // The input for the auto complete $sTitle = $oAttDef->GetDescription(); $sHTMLValue .= "\n"; $oPage->add_at_the_end($this->GetObjectPickerDialog($oPage, $sTargetClass, 'oLinkWidget'.$this->m_iInputId.'.OnOk')); // Forms should not be inside forms $oPage->add_at_the_end($this->GetLinkObjectDialog($oPage, $this->m_iInputId)); // Forms should not be inside forms $sHTMLValue .= "m_iInputId}\" size=\"35\" value=\"\" title=\"Type the first 3 characters\"/>"; $sHTMLValue .= "m_iInputId}\" value=\" Add... \" class=\"action\" onClick=\"oLinkWidget{$this->m_iInputId}.AddObject();\"/>"; $sHTMLValue .= " m_iInputId', '$sExtKeyToRemote');\"/>"; // another hidden input to store & pass the object's Id $sHTMLValue .= "m_iInputId}\" onChange=\"EnableAddButton('{$this->m_iInputId}');\"/>\n"; $sHTMLValue .= "m_iInputId}\" name=\"attr_{$this->m_sAttCode}{$this->m_sNameSuffix}\" value=\"\"/>\n"; $oPage->add_ready_script("\$('#{$this->m_iInputId}').val('$sJSON');\noLinkWidget{$this->m_iInputId}.Init();\n\$('#ac_{$this->m_iInputId}').autocomplete('./ajax.render.php', { minChars:3, onItemSelect:selectItem, onFindValue:findValue, formatItem:formatItem, autoFill:true, keyHolder:'#id_ac_{$this->m_iInputId}', extraParams:{operation:'ui.linkswidget', sclass:'{$this->m_sClass}', attCode:'{$this->m_sAttCode}', max:30}});"); $oPage->add_ready_script("\$('#ac_add_{$this->m_iInputId}').attr('disabled', 'disabled');"); $oPage->add_ready_script("\$('#ac_{$this->m_iInputId}').result( function(event, data, formatted) { if (data) { $('#id_ac_{$this->m_iInputId}').val(data[1]); $('#ac_add_{$this->m_iInputId}').attr('disabled', ''); } else { $('#ac_add_{$this->m_iInputId}').attr('disabled', 'disabled'); } } );"); } else { // Few choices, use a normal 'select' $sHTMLValue = "\n"; } $sHTMLValue .= "
m_iInputId}_values\">\n"; if ($oCurrentValuesSet != null) { // transform the DBObjectSet into a CMDBObjectSet !!! $aLinkedObjects = $oCurrentValuesSet->ToArray(false); // Actual values will be displayed asynchronously, no need to display them here //if (count($aLinkedObjects) > 0) //{ // $oSet = CMDBObjectSet::FromArray($sLinkedClass, $aLinkedObjects); // $oDisplayBlock = DisplayBlock::FromObjectSet($oSet, 'list'); // $sHTMLValue .= $oDisplayBlock->GetDisplay($oPage, $this->m_iInputId.'_current', array('linkage' => $sExtKeyToMe, 'menu' => false)); //} } $sHTMLValue .= "
\n"; return $sHTMLValue; } /** * This static function is called by the Ajax Page when there is a need to fill an autocomplete combo * @param $oPage web_page The ajax page used for the put^put (sent back to the browser * @param $oContext UserContext The context of the user (for limiting the search) * @param $sClass string The name of the class of the current object being edited * @param $sAttCode string The name of the attribute being edited * @param $sName string The partial name that was typed by the user * @param $iMaxCount integer The maximum number of items to return * @return void */ static public function Autocomplete(web_page $oPage, UserContext $oContext, $sClass, $sAttCode, $sName, $iMaxCount) { // #@# todo - add context information, otherwise any value will be authorized for external keys $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, array() /* $aArgs */, $sName); if ($aAllowedValues != null) { $iCount = $iMaxCount; foreach($aAllowedValues as $key => $value) { $oPage->add($value."|".$key."\n"); $iCount--; if ($iCount == 0) break; } } else // No limitation to the allowed values { // Search for all the object of the linked class $oAttDef = $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode); $sLinkedClass = $oAttDef->GetLinkedClass(); $sSearchClass = self::GetTargetClass($sClass, $sAttCode); $oFilter = $oContext->NewFilter($sSearchClass); $sSearchAttCode = MetaModel::GetNameAttributeCode($sSearchClass); $oFilter->AddCondition($sSearchAttCode, $sName, 'Begins with'); $oSet = new CMDBObjectSet($oFilter, array($sSearchAttCode => true)); $iCount = 0; while( ($iCount < $iMaxCount) && ($oObj = $oSet->fetch()) ) { $oPage->add($oObj->GetName()."|".$oObj->GetKey()."\n"); $iCount++; } } } /** * This static function is called by the Ajax Page display a set of objects being linked * to the object being created * @param $oPage web_page The ajax page used for the put^put (sent back to the browser * @param $sClass string The name of the 'linking class' which is the class of the objects to display * @param $sSet JSON serialized set of objects * @param $sExtKeyToMe Name of the attribute in sClass that is pointing to a given object * @param $iObjectId The id of the object $sExtKeyToMe is pointing to * @return void */ static public function RenderSet($oPage, $sClass, $sJSONSet, $sExtKeyToMe, $sExtKeyToRemote, $iObjectId) { $aSet = json_decode($sJSONSet, true); // true means hash array instead of object $oSet = CMDBObjectSet::FromScratch($sClass); foreach($aSet as $aObject) { $oObj = MetaModel::NewObject($sClass); foreach($aObject as $sAttCode => $value) { $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode); if ($oAttDef->IsExternalKey()) { $oTargetObj = MetaModel::GetObject($oAttDef->GetTargetClass(), $value); // @@ optimization, don't do & query per object in the set ! $oObj->Set($sAttCode, $oTargetObj); } else { $oObj->Set($sAttCode, $value); } } $oSet->AddObject($oObj); } $aExtraParams = array(); $aExtraParams['link_attr'] = $sExtKeyToMe; $aExtraParams['object_id'] = $iObjectId; $aExtraParams['target_attr'] = $sExtKeyToRemote; $aExtraParams['menu'] = false; $aExtraParams['select'] = false; cmdbAbstractObject::DisplaySet($oPage, $oSet, $aExtraParams); } 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, $sTargetClass, $sOkFunction) { $sHTML = <<< EOF

Selected objects:

Available objects:

     
EOF; $oPage->add_ready_script("$('#ManageObjectsDlg_$this->m_iInputId').jqm({overlay:70, modal:true, toTop:true});"); // jqModal Window //$oPage->add_ready_script("UpdateObjectList('$sClass');"); return $sHTML; } protected function GetLinkObjectDialog($oPage, $sId) { $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sAttCode); $sLinkedClass = $oAttDef->GetLinkedClass(); $sStateAttCode = MetaModel::GetStateAttributeCode($sLinkedClass); $sDefaultState = MetaModel::GetDefaultState($sLinkedClass); $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sAttCode); $sExtKeyToMe = $oAttDef->GetExtKeyToMe(); $sExtKeyToRemote = $oAttDef->GetExtKeyToRemote(); $sHTML = "
\n"; $sHTML .= "
\n"; $sHTML .= "

".MetaModel::GetName($sLinkedClass)." attributes

\n"; $sHTML .= "
\n"; $index = 0; $aAttrsMap = array(); $aDetails = array(); foreach(MetaModel::ListAttributeDefs($sLinkedClass) as $sAttCode=>$oAttDef) { if ($sStateAttCode == $sAttCode) { // State attribute is always hidden from the UI //$sHTMLValue = $this->GetState(); //$aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue); } else if (!$oAttDef->IsExternalField() && ($sAttCode != $sExtKeyToMe) && ($sAttCode != $sExtKeyToRemote)) { $iFlags = MetaModel::GetAttributeFlags($sLinkedClass, $sDefaultState, $sAttCode); if ($iFlags & OPT_ATT_HIDDEN) { // Attribute is hidden, do nothing } else { if ($iFlags & OPT_ATT_READONLY) { // Attribute is read-only $sHTMLValue = $this->GetAsHTML($sAttCode); } else { $sValue = ""; //$this->Get($sAttCode); $sDisplayValue = ""; //$this->GetDisplayValue($sAttCode); $sSubId = $sId.'_'.$index; $aAttrsMap[$sAttCode] = $sSubId; $index++; $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oPage, $sLinkedClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sSubId, $this->m_sAttCode); } $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue); } } } $sHTML .= $oPage->GetDetails($aDetails); $sHTML .= "     \n"; $sHTML .= "
\n"; $sHTML .= "
\n"; $sHTML .= "
\n"; $oPage->add_ready_script("$('#LinkDlg_$sId').jqm({overlay:70, modal:true, toTop:true});"); // jqModal Window //$oPage->add_ready_script("UpdateObjectList('$sClass');"); return $sHTML; } } ?>