Explorar o código

Properly handle restrictions (e.g. AllowedValues) on objects that are used as n:n relationships managed via LinksWidget.

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@1682 a333f486-631f-4898-b8df-5754b55c2be0
dflaven %!s(int64=13) %!d(string=hai) anos
pai
achega
d2c11f6be7

+ 7 - 3
application/cmdbabstract.class.inc.php

@@ -1565,8 +1565,8 @@ EOF
 				case 'LinkedSet':
 					$aEventsList[] ='validate';
 					$aEventsList[] ='change';
-					$oWidget = new UILinksWidget($sClass, $sAttCode, $iId, $sNameSuffix, $oAttDef->DuplicatesAllowed());
-					$sHTMLValue = $oWidget->Display($oPage, $value);
+					$oWidget = new UILinksWidget($sClass, $sAttCode, $iId, $sNameSuffix, $oAttDef->DuplicatesAllowed(), $aArgs);
+					$sHTMLValue = $oWidget->Display($oPage, $value, array(), '', $this);
 				break;
 							
 				case 'Document':
@@ -1733,7 +1733,7 @@ EOF
 		if ($iKey > 0)
 		{
 			// The object already exists in the database, it's a modification
-			$sButtons = "<input type=\"hidden\" name=\"id\" value=\"$iKey\">\n";
+			$sButtons = "<input id=\"{$sPrefix}_id\" type=\"hidden\" name=\"id\" value=\"$iKey\">\n";
 			$sButtons .= "<input type=\"hidden\" name=\"operation\" value=\"{$sOperation}\">\n";			
 			$sButtons .= "<button type=\"button\" class=\"action cancel\"><span>".Dict::S('UI:Button:Cancel')."</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n";
 			$sButtons .= "<button type=\"submit\" class=\"action\"><span>{$sApplyButton}</span></button>\n";
@@ -1819,6 +1819,10 @@ EOF
 		$oPage->SetCurrentTab(Dict::S('UI:PropertiesTab'));
 
 		$aFieldsMap = $this->DisplayBareProperties($oPage, true, $sPrefix, $aExtraParams);
+		if ($iKey > 0)
+		{
+			$aFieldsMap['id'] = $sPrefix.'_id';
+		}
 		// Now display the relations, one tab per relation
 		if (!isset($aExtraParams['noRelations']))
 		{

+ 17 - 9
application/ui.linkswidget.class.inc.php

@@ -34,6 +34,7 @@ class UILinksWidget
 	protected $m_iInputId;
 	protected $m_aAttributes;
 	protected $m_sExtKeyToRemote;
+	protected $m_sExtKeyToMe;
 	protected $m_sLinkedClass;
 	protected $m_sRemoteClass;
 	protected $m_bDuplicatesAllowed;
@@ -50,6 +51,7 @@ class UILinksWidget
 		$oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sAttCode);
 		$this->m_sLinkedClass = $oAttDef->GetLinkedClass();
 		$this->m_sExtKeyToRemote = $oAttDef->GetExtKeyToRemote();
+		$this->m_sExtKeyToMe = $oAttDef->GetExtKeyToMe();
 		$oLinkingAttDef = 	MetaModel::GetAttributeDef($this->m_sLinkedClass, $this->m_sExtKeyToRemote);
 		$this->m_sRemoteClass = $oLinkingAttDef->GetTargetClass();
 		$sExtKeyToMe = $oAttDef->GetExtKeyToMe();
@@ -97,7 +99,7 @@ class UILinksWidget
 	 * @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() )
+	protected function GetFormRow(WebPage $oP, DBObject $oLinkedObj, $linkObjOrId = null, $aArgs = array(), $oCurrentObj )
 	{
 		$sPrefix = "$this->m_sAttCode{$this->m_sNameSuffix}";
 		$aRow = array();
@@ -107,6 +109,7 @@ class UILinksWidget
 			$sPrefix .= "[$key][";
 			$sNameSuffix = "]"; // To make a tabular form
 			$aArgs['prefix'] = $sPrefix;
+			$aArgs['this'] = $linkObjOrId;
 			$aRow['form::checkbox'] = "<input class=\"selection\" type=\"checkbox\" onClick=\"oWidget".$this->m_iInputId.".OnSelectChange();\" value=\"$key\">";
 			$aRow['form::checkbox'] .= "<input type=\"hidden\" name=\"attr_{$sPrefix}id{$sNameSuffix}\" value=\"$key\">";
 			foreach($this->m_aEditableFields as $sFieldCode)
@@ -122,10 +125,12 @@ class UILinksWidget
 			// form for creating a new record
 			$sPrefix .= "[$linkObjOrId][";
 			$oNewLinkObj = MetaModel::NewObject($this->m_sLinkedClass);
-			$oNewLinkObj->Set($this->m_sExtKeyToRemote, -$linkObjOrId);
-			$oNewLinkObj->ComputeValues();
+			$oRemoteObj = MetaModel::GetObject($this->m_sRemoteClass, -$linkObjOrId);
+			$oNewLinkObj->Set($this->m_sExtKeyToRemote, $oRemoteObj); // Setting the extkey with the object alsoo fills the related external fields
+			$oNewLinkObj->Set($this->m_sExtKeyToMe, $oCurrentObj); // Setting the extkey with the object alsoo fills the related external fields
 			$sNameSuffix = "]"; // To make a tabular form
 			$aArgs['prefix'] = $sPrefix;
+			$aArgs['this'] = $oNewLinkObj;
 			$aRow['form::checkbox'] = "<input class=\"selection\" type=\"checkbox\" onClick=\"oWidget".$this->m_iInputId.".OnSelectChange();\" value=\"$linkObjOrId\">";
 			$aRow['form::checkbox'] .= "<input type=\"hidden\" name=\"attr_{$sPrefix}id{$sNameSuffix}\" value=\"\">";
 			foreach($this->m_aEditableFields as $sFieldCode)
@@ -210,9 +215,11 @@ class UILinksWidget
 	 * @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
+	 * @param string $sFormPrefix prefix of the fields in the current form
+	 * @param DBObject $oCurrentObj the current object to which the linkset is related
 	 * @return string The HTML fragment to be inserted into the page
 	 */
-	public function Display(WebPage $oPage, DBObjectSet $oValue, $aArgs = array())
+	public function Display(WebPage $oPage, DBObjectSet $oValue, $aArgs = array(), $sFormPrefix, $oCurrentObj)
 	{
 		$sHtmlValue = '';
 		$sTargetClass = self::GetTargetClass($this->m_sClass, $this->m_sAttCode);
@@ -226,19 +233,20 @@ class UILinksWidget
 			if ($oCurrentLink->IsNew())
 			{
 				$key = -$oLinkedObj->GetKey();
-				$aForm[$key] = $this->GetFormRow($oPage, $oLinkedObj, $key, $aArgs);
+				$aForm[$key] = $this->GetFormRow($oPage, $oLinkedObj, $key, $aArgs, $oCurrentObj);
 			}
 			else
 			{
 				$key = $oCurrentLink->GetKey();
-				$aForm[$key] = $this->GetFormRow($oPage, $oLinkedObj, $oCurrentLink, $aArgs);
+				$aForm[$key] = $this->GetFormRow($oPage, $oLinkedObj, $oCurrentLink, $aArgs, $oCurrentObj);
 			}
 
 		}
 		$sHtmlValue .= $this->DisplayFormTable($oPage, $this->m_aTableConfig, $aForm);
 		$sDuplicates = ($this->m_bDuplicatesAllowed) ? 'true' : 'false';
+		$sWizHelper = 'oWizardHelper'.$this->m_sFormPrefix;
 		$oPage->add_ready_script(<<<EOF
-		oWidget{$this->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} = new LinksWidget('{$this->m_sAttCode}{$this->m_sNameSuffix}', '{$this->m_sClass}', '{$this->m_sAttCode}', '{$this->m_iInputId}', '{$this->m_sNameSuffix}', $sDuplicates, $sWizHelper);
 		oWidget{$this->m_iInputId}.Init();
 EOF
 );
@@ -347,7 +355,7 @@ EOF
 		$oBlock->Display($oP, "ResultsToAdd_{$this->m_sAttCode}", array('menu' => false, 'cssCount'=> '#count_'.$this->m_sAttCode.$this->m_sNameSuffix , 'selection_mode' => true)); // Don't display the 'Actions' menu on the results
 	}
 	
-	public function DoAddObjects(WebPage $oP, $oFullSetFilter)
+	public function DoAddObjects(WebPage $oP, $oFullSetFilter, $oCurrentObj)
 	{
 		$aLinkedObjectIds = utils::ReadMultipleSelection($oFullSetFilter);
 
@@ -356,7 +364,7 @@ EOF
 			$oLinkedObj = MetaModel::GetObject($this->m_sRemoteClass, $iObjectId);
 			if (is_object($oLinkedObj))
 			{
-				$aRow = $this->GetFormRow($oP, $oLinkedObj, -$iObjectId ); // Not yet created link get negative Ids
+				$aRow = $this->GetFormRow($oP, $oLinkedObj, -$iObjectId, array(), $oCurrentObj ); // Not yet created link get negative Ids
 				$oP->add($this->DisplayFormRow($oP, $this->m_aTableConfig, $aRow, -$iObjectId)); 
 			}
 			else

+ 12 - 1
js/linkswidget.js

@@ -1,5 +1,5 @@
 // JavaScript Document
-function LinksWidget(id, sClass, sAttCode, iInputId, sSuffix, bDuplicates)
+function LinksWidget(id, sClass, sAttCode, iInputId, sSuffix, bDuplicates, oWizHelper)
 {
 	this.id = id;
 	this.iInputId = iInputId;
@@ -7,6 +7,7 @@ function LinksWidget(id, sClass, sAttCode, iInputId, sSuffix, bDuplicates)
 	this.sAttCode = sAttCode;
 	this.sSuffix = sSuffix;
 	this.bDuplicates = bDuplicates;
+	this.oWizardHelper = oWizHelper;
 	var me = this;
 	this.Init = function()
 	{
@@ -202,6 +203,16 @@ function LinksWidget(id, sClass, sAttCode, iInputId, sSuffix, bDuplicates)
 //		}
 		
 		theMap['operation'] = 'doAddObjects';
+		if (me.oWizardHelper == null)
+		{
+			theMap['json'] = '';
+		}
+		else
+		{
+			// Not inside a "search form", updating a real object
+			me.oWizardHelper.UpdateWizard();
+			theMap['json'] = me.oWizardHelper.ToJSON();
+		}
 		$('#busy_'+me.iInputId).html('&nbsp;<img src="../images/indicator.gif"/>');
 		// Run the query and display the results
 		$.post( GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', theMap, 

+ 4 - 1
pages/ajax.render.php

@@ -297,6 +297,9 @@ try
 		$sSuffix = utils::ReadParam('sSuffix', '');
 		$sRemoteClass = utils::ReadParam('sRemoteClass', $sClass, false, 'class');
 		$bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
+		$sJson = utils::ReadParam('json', '', false, 'raw_data');
+		$oWizardHelper = WizardHelper::FromJSON($sJson);
+		$oObj = $oWizardHelper->GetTargetObject();
 		$oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
 		if ($sFilter != '')
 		{
@@ -306,7 +309,7 @@ try
 		{
 			$oFullSetFilter = new DBObjectSearch($sRemoteClass);		
 		}
-		$oWidget->DoAddObjects($oPage, $oFullSetFilter);	
+		$oWidget->DoAddObjects($oPage, $oFullSetFilter, $oObj);	
 		break;
 			
 		case 'wizard_helper_preview':