diff --git a/application/cmdbabstract.class.inc.php b/application/cmdbabstract.class.inc.php index 4e6a00e74..30331316d 100644 --- a/application/cmdbabstract.class.inc.php +++ b/application/cmdbabstract.class.inc.php @@ -2321,12 +2321,7 @@ EOF if ($oObjectToClone == null) { - $oObj = MetaModel::NewObject($sClass); - if (!empty($sStateAttCode)) - { - $sTargetState = MetaModel::GetDefaultState($sClass); - $oObj->Set($sStateAttCode, $sTargetState); - } + $oObj = DBObject::MakeDefaultInstance($sClass); } else { diff --git a/application/ui.extkeywidget.class.inc.php b/application/ui.extkeywidget.class.inc.php index 34f7ed506..5ec9acd5d 100644 --- a/application/ui.extkeywidget.class.inc.php +++ b/application/ui.extkeywidget.class.inc.php @@ -508,7 +508,7 @@ EOF /** * Get the form to create a new object of the 'target' class */ - public function GetObjectCreationForm(WebPage $oPage, $oCurrObject) + public function GetObjectCreationForm(WebPage $oPage, $oCurrObject, $aPrefillFormParam) { // Set all the default values in an object and clone this "default" object $oNewObj = MetaModel::NewObject($this->sTargetClass); @@ -516,7 +516,7 @@ EOF // 1st - set context values $oAppContext = new ApplicationContext(); $oAppContext->InitObjectFromContext($oNewObj); - + $oNewObj->PrefillForm('creation_from_extkey', $aPrefillFormParam); // 2nd set the default values from the constraint on the external key... if any if ( ($oCurrObject != null) && ($this->sAttCode != '')) { diff --git a/application/ui.linksdirectwidget.class.inc.php b/application/ui.linksdirectwidget.class.inc.php index b38db95c3..bbbd09745 100644 --- a/application/ui.linksdirectwidget.class.inc.php +++ b/application/ui.linksdirectwidget.class.inc.php @@ -179,7 +179,7 @@ class UILinksWidgetDirect * @param WebPage $oPage * @param string $sProposedRealClass */ - public function GetObjectCreationDlg(WebPage $oPage, $sProposedRealClass = '') + public function GetObjectCreationDlg(WebPage $oPage, $sProposedRealClass = '', $oSourceObj = null) { // For security reasons: check that the "proposed" class is actually a subclass of the linked class // and that the current user is allowed to create objects of this class @@ -211,7 +211,10 @@ class UILinksWidgetDirect $oLinksetDef = MetaModel::GetAttributeDef($this->sClass, $this->sAttCode); $sExtKeyToMe = $oLinksetDef->GetExtKeyToMe(); $aFieldFlags = array( $sExtKeyToMe => OPT_ATT_HIDDEN); - cmdbAbstractObject::DisplayCreationForm($oPage, $sRealClass, null, array(), array('formPrefix' => $this->sInputid, 'noRelations' => true, 'fieldsFlags' => $aFieldFlags)); + $oObj = DBObject::MakeDefaultInstance($sRealClass); + $aPrefillParam = array('source_obj' => $oSourceObj); + $oObj->PrefillForm('creation_from_editinplace', $aPrefillParam); + cmdbAbstractObject::DisplayCreationForm($oPage, $sRealClass, $oObj, array(), array('formPrefix' => $this->sInputid, 'noRelations' => true, 'fieldsFlags' => $aFieldFlags)); } else { diff --git a/application/ui.linkswidget.class.inc.php b/application/ui.linkswidget.class.inc.php index deef928b2..9055a248c 100644 --- a/application/ui.linkswidget.class.inc.php +++ b/application/ui.linkswidget.class.inc.php @@ -393,7 +393,7 @@ EOF * @throws \DictExceptionMissingString * @throws \Exception */ - public function GetObjectPickerDialog($oPage, $oCurrentObj, $sJson, $aAlreadyLinkedIds = array()) + public function GetObjectPickerDialog($oPage, $oCurrentObj, $sJson, $aAlreadyLinkedIds = array(), $aPrefillFormParam = array()) { $bOpen = MetaModel::GetConfig()->Get('legacy_search_drawer_open'); $sHtml = "
\n"; @@ -411,9 +411,12 @@ EOF } $oFilter = new DBObjectSearch($this->m_sRemoteClass); - if ($oCurrentObj != null) + + if(!empty($oCurrentObj)) { $this->SetSearchDefaultFromContext($oCurrentObj, $oFilter); + $aPrefillFormParam['filter'] = $oFilter; + $oCurrentObj->PrefillForm('search', $aPrefillFormParam); } $oBlock = new DisplayBlock($oFilter, 'search', false); $sHtml .= $oBlock->GetDisplay($oPage, "SearchFormToAdd_{$this->m_sAttCode}{$this->m_sNameSuffix}", diff --git a/core/dbobject.class.php b/core/dbobject.class.php index c5fefa728..5f40cc25b 100644 --- a/core/dbobject.class.php +++ b/core/dbobject.class.php @@ -3657,5 +3657,75 @@ abstract class DBObject implements iDisplay $this->m_aCurrValues['archive_date'] = null; $this->m_aOrigValues['archive_date'] = null; } + + + + /** + * @param string $sClass Needs to be an instanciable class + * @returns $oObj + **/ + public static function MakeDefaultInstance($sClass) + { + $sStateAttCode = MetaModel::GetStateAttributeCode($sClass); + $oObj = MetaModel::NewObject($sClass); + if (!empty($sStateAttCode)) + { + $sTargetState = MetaModel::GetDefaultState($sClass); + $oObj->Set($sStateAttCode, $sTargetState); + } + return $oObj; + } + + /** + * Complete a new object with data from context + * @param array $aContextParam Context used for creation form prefilling + * + */ + public function PrefillCreationForm(&$aContextParam) + { + } + + /** + * Complete an object after a state transition with data from context + * @param array $aContextParam Context used for creation form prefilling + * + */ + public function PrefillTransitionForm(&$aContextParam) + { + } + + /** + * Complete a filter ($aContextParam['filter']) data from context + * @param array $aContextParam Context used for creation form prefilling + * + */ + public function PrefillSearchForm(&$aContextParam) + { + } + + /** + * Prefill a creation / stimulus change / search form according to context, current state of an object, stimulus.. $sOperation + * @param string $sOperation Operation identifier + * @param array $aContextParam Context used for creation form prefilling + * + */ + public function PrefillForm($sOperation, &$aContextParam) + { + switch($sOperation){ + case 'creation_from_0': + case 'creation_from_extkey': + case 'creation_from_editinplace': + $this->PrefillCreationForm($aContextParam); + break; + case 'state_change': + $this->PrefillTransitionForm($aContextParam); + break; + case 'search': + $this->PrefillSearchForm($aContextParam); + break; + default: + break; + } + } } diff --git a/datamodels/2.x/itop-portal-base/portal/src/controllers/objectcontroller.class.inc.php b/datamodels/2.x/itop-portal-base/portal/src/controllers/objectcontroller.class.inc.php index c9ee55552..0af740729 100644 --- a/datamodels/2.x/itop-portal-base/portal/src/controllers/objectcontroller.class.inc.php +++ b/datamodels/2.x/itop-portal-base/portal/src/controllers/objectcontroller.class.inc.php @@ -458,6 +458,9 @@ class ObjectController extends AbstractController // Retrieve action rules information to auto-fill the form if available // Preparing object $oApp['context_manipulator']->PrepareObject($aActionRules, $oObject); + $aPrefillFormParam = array( 'user' => $_SESSION["auth_user"], + 'origin' => 'portal'); + $oObject->PrefillForm('creation_from_0', $aPrefillFormParam); } else { @@ -515,6 +518,13 @@ class ObjectController extends AbstractController $aFormData['buttons']['submit']['label'] = Dict::S('Portal:Button:Apply'); } } + else + { + $aPrefillFormParam = array('user' => $_SESSION["auth_user"], + 'origin' => 'portal', + 'stimulus' => $oRequestParams->get('apply_stimulus')['code']); + $oObject->PrefillForm('state_change', $aPrefillFormParam); + } // Preparing callback urls $aCallbackUrls = $oApp['context_manipulator']->GetCallbackUrls($oApp, $aActionRules, $oObject, $bModal); diff --git a/js/linksdirectwidget.js b/js/linksdirectwidget.js index b143a1938..0f1fc58a7 100644 --- a/js/linksdirectwidget.js +++ b/js/linksdirectwidget.js @@ -154,6 +154,11 @@ $(function() oParams.att_code = this.options.att_code; oParams.iInputId = this.id; var me = this; + if (this.options.oWizardHelper) + { + this.options.oWizardHelper.UpdateWizard(); + oParams.json = this.options.oWizardHelper.ToJSON(); + } $.post(this.options.submit_to, oParams, function(data){ me.oDlg = $('
'); $('body').append(me.oDlg); diff --git a/pages/UI.php b/pages/UI.php index 2debf6183..db6ff4a52 100644 --- a/pages/UI.php +++ b/pages/UI.php @@ -346,7 +346,6 @@ try $oP = new iTopWebPage(Dict::S('UI:WelcomeToITop'), $bPrintable); $oP->SetMessage($sLoginMessage); - // All the following actions use advanced forms that require more javascript to be loaded switch($operation) { @@ -787,9 +786,14 @@ EOF // 1st - set context values $oAppContext->InitObjectFromContext($oObjToClone); - // 2nd - set values from the page argument 'default' $oObjToClone->UpdateObjectFromArg('default'); + $aPrefillFormParam = array( 'user' => $_SESSION["auth_user"], + 'context' => $oAppContext->GetAsHash(), + 'default' => utils::ReadParam('default', array(), '', 'raw_data'), + 'origin' => 'console' + ); + $oObjToClone->PrefillForm('creation_from_0',$aPrefillFormParam); cmdbAbstractObject::DisplayCreationForm($oP, $sRealClass, $oObjToClone, array()); $oP->add("
\n"); @@ -1447,6 +1451,12 @@ EOF $oObj = MetaModel::GetObject($sClass, $id, false); if ($oObj != null) { + $aPrefillFormParam = array( 'user' => $_SESSION["auth_user"], + 'context' => $oAppContext->GetAsHash(), + 'stimulus' => $sStimulus, + 'origin' => 'console' + ); + $oObj->PrefillForm('state_change', $aPrefillFormParam); $oObj->DisplayStimulusForm($oP, $sStimulus); } else diff --git a/pages/ajax.render.php b/pages/ajax.render.php index 8f8b3fdd6..c396b1488 100644 --- a/pages/ajax.render.php +++ b/pages/ajax.render.php @@ -299,10 +299,22 @@ try $oObj = null; } $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates); + $oAppContext = new ApplicationContext(); + $aPrefillFormParam = array( 'user' => $_SESSION["auth_user"], + 'context' => $oAppContext->GetAsHash(), + 'att_code' => $sAttCode, + 'origin' => 'console' + ); + if (!empty($sJson)) + { + $oWizardHelper = WizardHelper::FromJSON($sJson); + $oSourceObj = $oWizardHelper->GetTargetObject(); + $aPrefillFormParam['source_obj'] = $oSourceObj; + } $aAlreadyLinked = utils::ReadParam('aAlreadyLinked', array()); - $oWidget->GetObjectPickerDialog($oPage, $oObj, $sJson, $aAlreadyLinked); + $oWidget->GetObjectPickerDialog($oPage, $oObj, $sJson, $aAlreadyLinked, $aPrefillFormParam); break; - + // ui.linkswidget case 'searchObjectsToAdd': $oPage->SetContentType('text/html'); @@ -324,10 +336,17 @@ try $sAttCode = utils::ReadParam('att_code', ''); $iInputId = utils::ReadParam('iInputId', ''); $oPage->SetContentType('text/html'); + $sJson = utils::ReadParam('json', '', false, 'raw_data'); + if (!empty($sJson)) + { + $oWizardHelper = WizardHelper::FromJSON($sJson); + $oObj = $oWizardHelper->GetTargetObject(); + } + $oObj = $oWizardHelper->GetTargetObject(); $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iInputId); - $oWidget->GetObjectCreationDlg($oPage, $sRealClass); - break; - + $oWidget->GetObjectCreationDlg($oPage, $sRealClass, $oObj); + break; + // ui.linksdirectwidget case 'getLinksetRow': $oPage->SetContentType('text/html'); @@ -498,31 +517,39 @@ try $oPage->SetContentType('text/html'); // Retrieving parameters $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class'); - $iInputId = utils::ReadParam('iInputId', ''); - $sAttCode = utils::ReadParam('sAttCode', ''); - $sJson = utils::ReadParam('json', '', false, 'raw_data'); + $iInputId = utils::ReadParam('iInputId', ''); + $sAttCode = utils::ReadParam('sAttCode', ''); + $sJson = utils::ReadParam('json', '', false, 'raw_data'); // Building form, if target class is abstract we ask the user for the desired leaf class - $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, false); - if (MetaModel::IsAbstract($sTargetClass)) - { - $oWidget->GetClassSelectionForm($oPage); - } - else - { - if (!empty($sJson)) - { - $oWizardHelper = WizardHelper::FromJSON($sJson); - $oObj = $oWizardHelper->GetTargetObject(); - } - else - { - // Search form: no current object - $oObj = null; - } - $oWidget->GetObjectCreationForm($oPage, $oObj); - } + $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, false); + if(MetaModel::IsAbstract($sTargetClass)) + { + $oWidget->GetClassSelectionForm($oPage); + } + else + { + $aPrefillFormParam = array(); + if (!empty($sJson)) + { + $oWizardHelper = WizardHelper::FromJSON($sJson); + $oObj = $oWizardHelper->GetTargetObject(); + $oAppContext = new ApplicationContext(); + $aPrefillFormParam = array( 'user' => $_SESSION["auth_user"], + 'context' => $oAppContext->GetAsHash(), + 'att_code' => $sAttCode, + 'source_obj' => $oObj, + 'origin' => 'console' + ); + } + else + { + // Search form: no current object + $oObj = null; + } + $oWidget->GetObjectCreationForm($oPage, $oObj, $aPrefillFormParam); + } break; - + // ui.extkeywidget case 'doCreateObject': $oPage->SetContentType('application/json'); @@ -2567,7 +2594,7 @@ EOF $oPage->add_header("Pragma: cache"); // Reset the value set .... where ? $oPage->add(file_get_contents(Utils::GetCachePath().$sSignature.'.js')); break; - + default: $oPage->p("Invalid query."); }