diff --git a/application/ui.linkswidget.class.inc.php b/application/ui.linkswidget.class.inc.php index b7d3ec4e7..8fb88aa3f 100644 --- a/application/ui.linkswidget.class.inc.php +++ b/application/ui.linkswidget.class.inc.php @@ -251,10 +251,10 @@ class UILinksWidget 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 .= "   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();\">m_sAttCode}{$this->m_sNameSuffix}_indicatorAdd\">\n"; $sHtmlValue .= "

 

\n"; $sHtmlValue .= "\n"; - $oPage->add_at_the_end($this->GetObjectPickerDialog($oPage), "dlg_{$this->m_sAttCode}{$this->m_sNameSuffix}"); // To prevent adding forms inside the main form + $oPage->add_at_the_end("
m_sAttCode}{$this->m_sNameSuffix}\">
"); // To prevent adding forms inside the main form return $sHtmlValue; } @@ -277,12 +277,11 @@ EOF return $sTargetClass; } - protected function GetObjectPickerDialog($oPage) + public function GetObjectPickerDialog($oPage, $oCurrentObj) { - $sHtml = "
m_sAttCode}{$this->m_sNameSuffix}\">"; $sHtml .= "
\n"; $oFilter = new DBObjectSearch($this->m_sRemoteClass); - $oSet = new CMDBObjectSet($oFilter); + $this->SetSearchDefaultFromContext($oCurrentObj, $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"; @@ -293,12 +292,11 @@ EOF $sHtml .= "m_sAttCode}{$this->m_sNameSuffix}').dialog('close');\">  m_sAttCode}{$this->m_sNameSuffix}\" disabled=\"disabled\" type=\"submit\" value=\"".Dict::S('UI:Button:Add')."\">"; $sHtml .= "
\n"; $sHtml .= "\n"; - $sHtml .= "
\n"; + $oPage->add($sHtml); $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; } /** @@ -373,5 +371,47 @@ EOF } } } + + /** + * Initializes the default search parameters based on 1) a 'current' object and 2) the silos defined by the context + * @param DBObject $oSourceObj + * @param DBObjectSearch $oSearch + */ + protected function SetSearchDefaultFromContext($oSourceObj, &$oSearch) + { + $oAppContext = new ApplicationContext(); + $sSrcClass = get_class($oSourceObj); + $sDestClass = $oSearch->GetClass(); + foreach($oAppContext->GetNames() as $key) + { + // Find the value of the object corresponding to each 'context' parameter + $aCallSpec = array($sSrcClass, 'MapContextParam'); + $sAttCode = ''; + if (is_callable($aCallSpec)) + { + $sAttCode = call_user_func($aCallSpec, $key); // Returns null when there is no mapping for this parameter + } + + if (MetaModel::IsValidAttCode($sSrcClass, $sAttCode)) + { + $oAttDef = MetaModel::GetAttributeDef($sSrcClass, $sAttCode); + $defaultValue = $oSourceObj->Get($sAttCode); + + // Find the attcode for the same 'context' parameter in the destination class + // and sets its value as the default value for the search condition + $aCallSpec = array($sDestClass, 'MapContextParam'); + $sAttCode = ''; + if (is_callable($aCallSpec)) + { + $sAttCode = call_user_func($aCallSpec, $key); // Returns null when there is no mapping for this parameter + } + + if (MetaModel::IsValidAttCode($sDestClass, $sAttCode) && !empty($defaultValue)) + { + $oSearch->AddCondition($sAttCode, $defaultValue); + } + } + } + } } ?> diff --git a/core/dbobjectset.class.php b/core/dbobjectset.class.php index 8953cfa56..91fcf8f51 100644 --- a/core/dbobjectset.class.php +++ b/core/dbobjectset.class.php @@ -716,7 +716,7 @@ class DBObjectSet protected function ExpandArgs($aArgs) { - $aScalarArgs = array(); + $aScalarArgs = $this->m_oFilter->GetInternalParams(); foreach($this->m_aArgs as $sArgName => $value) { if (MetaModel::IsValidObject($value)) diff --git a/js/linkswidget.js b/js/linkswidget.js index ef7980bb6..8377f9114 100644 --- a/js/linkswidget.js +++ b/js/linkswidget.js @@ -64,9 +64,28 @@ function LinksWidget(id, sClass, sAttCode, iInputId, sSuffix, bDuplicates, oWizH this.AddObjects = function() { - $('#dlg_'+me.id).dialog('open'); - this.UpdateSizes(null, null); - this.SearchObjectsToAdd(); + var me = this; + $('#'+me.id+'_indicatorAdd').html(' '); + me.oWizardHelper.UpdateWizard(); + var theMap = { sAttCode: me.sAttCode, + iInputId: me.iInputId, + sSuffix: me.sSuffix, + bDuplicates: me.bDuplicates, + 'class' : me.sClass, + operation: 'addObjects', + json: me.oWizardHelper.ToJSON() + }; + $.post( GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', theMap, + function(data) + { + $('#dlg_'+me.id).html(data); + $('#dlg_'+me.id).dialog('open'); + me.UpdateSizes(null, null); + me.SearchObjectsToAdd(); + $('#'+me.id+'_indicatorAdd').html(''); + }, + 'html' + ); } this.SearchObjectsToAdd = function() diff --git a/pages/ajax.render.php b/pages/ajax.render.php index f86616811..8bcdd456a 100644 --- a/pages/ajax.render.php +++ b/pages/ajax.render.php @@ -73,7 +73,6 @@ try $sSelectMode = utils::ReadParam('select_mode', ''); $bDisplayKey = utils::ReadParam('display_key', 'true') == 'true'; $aList = utils::ReadParam('display_list', array()); - $sClassName = $oFilter->GetClass(); //$aList = cmdbAbstractObject::FlattenZList(MetaModel::GetZListItems($sClassName, 'list')); @@ -169,6 +168,28 @@ try } break; + // ui.linkswidget + case 'addObjects': + $oPage->SetContentType('text/html'); + $sAttCode = utils::ReadParam('sAttCode', ''); + $iInputId = utils::ReadParam('iInputId', ''); + $sSuffix = utils::ReadParam('sSuffix', ''); + $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true; + $sJson = utils::ReadParam('json', '', false, 'raw_data'); + if (!empty($sJson)) + { + $oWizardHelper = WizardHelper::FromJSON($sJson); + $oObj = $oWizardHelper->GetTargetObject(); + } + else + { + // Search form: no current object + $oObj = null; + } + $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates); + $oWidget->GetObjectPickerDialog($oPage, $oObj); + break; + // ui.linkswidget case 'searchObjectsToAdd': $oPage->SetContentType('text/html');