Преглед изворни кода

Portal : Added a new mode "apply_stimulus" for forms. This allows to add flags to attributes that are prompt during transitions.

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@4378 a333f486-631f-4898-b8df-5754b55c2be0
glajarige пре 8 година
родитељ
комит
6ebc4c1e1c

+ 10 - 3
datamodels/2.x/itop-portal-base/portal/src/controllers/objectcontroller.class.inc.php

@@ -506,9 +506,16 @@ class ObjectController extends AbstractController
 				->SetMode($sMode)
 				->SetActionRulesToken($sActionRulesToken)
 				->SetRenderer($oFormRenderer)
-				->SetFormProperties($aFormProperties)
-				->Build();
-			
+				->SetFormProperties($aFormProperties);
+
+			if ($sMode === 'apply_stimulus')
+			{
+				$aEditFormProperties = ApplicationHelper::GetLoadedFormFromClass($oApp, $sObjectClass, ObjectFormManager::ENUM_MODE_APPLY_STIMULUS);
+				$oFormManager->MergeFormProperties($aEditFormProperties);
+			}
+
+			$oFormManager->Build();
+
 			// Check the number of editable fields
 			$aFormData['editable_fields_count'] = $oFormManager->GetForm()->GetEditableFieldCount();
 		}

+ 94 - 0
datamodels/2.x/itop-portal-base/portal/src/forms/objectformmanager.class.inc.php

@@ -49,6 +49,7 @@ class ObjectFormManager extends FormManager
 	const ENUM_MODE_VIEW = 'view';
 	const ENUM_MODE_EDIT = 'edit';
 	const ENUM_MODE_CREATE = 'create';
+	const ENUM_MODE_APPLY_STIMULUS = 'apply_stimulus';
 
 	protected $oApp;
 	protected $oObject;
@@ -482,6 +483,18 @@ class ObjectFormManager extends FormManager
 					{
 						$oField->SetReadOnly(true);
 					}
+					// - Else if it's must change, we force it as not readonly and not hidden
+					elseif (($iFieldFlags & OPT_ATT_MUSTCHANGE) === OPT_ATT_MUSTCHANGE)
+					{
+						$oField->SetReadOnly(false);
+						$oField->SetHidden(false);
+					}
+					// - Else if it's must prompt, we force it as not readonly and not hidden
+					elseif (($iFieldFlags & OPT_ATT_MUSTPROMPT) === OPT_ATT_MUSTPROMPT)
+					{
+						$oField->SetReadOnly(false);
+						$oField->SetHidden(false);
+					}
 					else
 					{
 						// Normal field
@@ -658,6 +671,87 @@ class ObjectFormManager extends FormManager
 	}
 
 	/**
+	 * Merging $this->aFormProperties with $aFormPropertiesToMerge. Merge only layout for now
+	 *
+	 * @param array $aFormPropertiesToMerge
+	 * @throws Exception
+	 */
+	public function MergeFormProperties($aFormPropertiesToMerge)
+	{
+		if ($aFormPropertiesToMerge['layout'] !== null)
+		{
+			// Checking if we need to render the template from twig to html in order to parse the fields
+			if ($aFormPropertiesToMerge['layout']['type'] === 'twig')
+			{
+				// Creating sandbox twig env. to load and test the custom form template
+				$oTwig = new \Twig_Environment(new \Twig_Loader_String());
+				$sRendered = $oTwig->render($aFormPropertiesToMerge['layout']['content'], array('oRenderer' => $this->oRenderer, 'oObject' => $this->oObject));
+			}
+			else
+			{
+				$sRendered = $aFormPropertiesToMerge['layout']['content'];
+			}
+
+			// Parsing rendered template to find the fields
+			$oHtmlDocument = new \DOMDocument();
+			$oHtmlDocument->loadHTML('<root>' . $sRendered . '</root>');
+
+			// Adding fields to the list
+			$oXPath = new \DOMXPath($oHtmlDocument);
+			foreach ($oXPath->query('//div[@class="form_field"][@data-field-id]') as $oFieldNode)
+			{
+				$sFieldId = $oFieldNode->getAttribute('data-field-id');
+				$sFieldFlags = $oFieldNode->getAttribute('data-field-flags');
+//				$iFieldFlags = OPT_ATT_NORMAL;
+
+//				// Checking if field has form_path, if not, we add it
+//				if (!$oFieldNode->hasAttribute('data-form-path'))
+//				{
+//					$oFieldNode->setAttribute('data-form-path', $oForm->GetId());
+//				}
+				// Merging only fields that are already in the form
+				if (array_key_exists($sFieldId, $this->aFormProperties['fields']))
+				{
+					// Settings field flags from the data-field-flags attribute
+					foreach (explode(' ', $sFieldFlags) as $sFieldFlag)
+					{
+						if ($sFieldFlag !== '')
+						{
+							$sConst = 'OPT_ATT_' . strtoupper(str_replace('_', '', $sFieldFlag));
+							if (defined($sConst))
+							{
+								switch ($sConst)
+								{
+									case 'OPT_ATT_SLAVE':
+									case 'OPT_ATT_HIDDEN':
+										if (!array_key_exists($sFieldId, $this->aFormProperties['fields']))
+										{
+											$this->aFormProperties['fields'][$sFieldId] = array();
+										}
+										$this->aFormProperties['fields'][$sFieldId]['hidden'] = true;
+										break;
+									case 'OPT_ATT_READONLY':
+										if (!array_key_exists($sFieldId, $this->aFormProperties['fields']))
+										{
+											$this->aFormProperties['fields'][$sFieldId] = array();
+										}
+										$this->aFormProperties['fields'][$sFieldId]['read_only'] = true;
+										break;
+								}
+							}
+							else
+							{
+								IssueLog::Error(__METHOD__ . ' at line ' . __LINE__ . ' : Flag "' . $sFieldFlag . '" is not valid for field [@data-field-id="' . $sFieldId . '"] in form[@id="' . $aFormPropertiesToMerge['id'] . '"]');
+								throw new Exception('Flag "' . $sFieldFlag . '" is not valid for field [@data-field-id="' . $sFieldId . '"] in form[@id="' . $aFormPropertiesToMerge['id'] . '"]');
+							}
+						}
+					}
+				}
+			}
+		}
+	}
+
+	/**
 	 * Calls all form fields OnCancel method in order to delegate them the cleanup;
 	 *
 	 * @param array $aArgs

+ 15 - 0
datamodels/2.x/itop-tickets/datamodel.itop-tickets.xml

@@ -1321,6 +1321,21 @@
 						<mode id="view"/>
 					</modes>
 				</form>
+				<form id="ticket-apply-stimulus">
+					<class>Ticket</class>
+					<fields />
+					<twig>
+						<div>
+							<div class="form_field" data-field-id="team_id" data-field-flags="read_only">
+							</div>
+							<div class="form_field" data-field-id="agent_id" data-field-flags="read_only">
+							</div>
+						</div>
+					</twig>
+					<modes>
+						<mode id="apply_stimulus"/>
+					</modes>
+				</form>
 				<form id="service-view">
 					<class>Service</class>
 					<fields></fields>