Browse Source

New attribute 'display_style' for Strings, Enums and ExternalKeys: when the list is short, display it as a set of radio buttons instead of a drop-down list. Possible values: 'radio', 'radio_vertical' and 'radio_horizontal'. ('radio' == 'radio_vertical').

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@1426 a333f486-631f-4898-b8df-5754b55c2be0
dflaven 14 years ago
parent
commit
e4f4e5a489

+ 29 - 14
application/cmdbabstract.class.inc.php

@@ -1520,24 +1520,39 @@ EOF
 					$iFieldSize = $oAttDef->GetMaxSize();
 					if ($aAllowedValues !== null)
 					{
-						// Discrete list of values, use a SELECT
-						$sHTMLValue = "<select title=\"$sHelpText\" name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" id=\"$iId\">\n";
-						$sHTMLValue .= "<option value=\"\">".Dict::S('UI:SelectOne')."</option>\n";
-						foreach($aAllowedValues as $key => $display_value)
+						// Discrete list of values, use a SELECT or RADIO buttons depending on the config
+						$sDisplayStyle = $oAttDef->GetDisplayStyle();
+						switch($sDisplayStyle)
 						{
-							if ((count($aAllowedValues) == 1) && ($bMandatory == 'true') )
-							{
-								// When there is only once choice, select it by default
-								$sSelected = ' selected';
-							}
-							else
+							case 'radio':
+							case 'radio_horizontal':
+							case 'radio_vertical':
+							$sHTMLValue = '';
+							$bVertical = ($sDisplayStyle != 'radio_horizontal');
+							$sHTMLValue = $oPage->GetRadioButtons($aAllowedValues, $value, $iId, "{$sFieldPrefix}{$sAttCode}{$sNameSuffix}", $bMandatory, $bVertical, $sValidationField);
+							$aEventsList[] ='change';
+							break;
+							
+							case 'select':
+							default:
+							$sHTMLValue = "<select title=\"$sHelpText\" name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" id=\"$iId\">\n";
+							$sHTMLValue .= "<option value=\"\">".Dict::S('UI:SelectOne')."</option>\n";
+							foreach($aAllowedValues as $key => $display_value)
 							{
-								$sSelected = ($value == $key) ? ' selected' : '';
+								if ((count($aAllowedValues) == 1) && ($bMandatory == 'true') )
+								{
+									// When there is only once choice, select it by default
+									$sSelected = ' selected';
+								}
+								else
+								{
+									$sSelected = ($value == $key) ? ' selected' : '';
+								}
+								$sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
 							}
-							$sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
+							$sHTMLValue .= "</select>&nbsp;{$sValidationField}\n";
+							$aEventsList[] ='change';
 						}
-						$sHTMLValue .= "</select>&nbsp;{$sValidationField}\n";
-						$aEventsList[] ='change';
 					}
 					else
 					{

+ 55 - 32
application/ui.extkeywidget.class.inc.php

@@ -73,8 +73,9 @@ class UIExtKeyWidget
 		$sTargetClass = $oAttDef->GetTargetClass();
 		$iMaxComboLength = $oAttDef->GetMaximumComboLength();
 		$bAllowTargetCreation = $oAttDef->AllowTargetCreation();
+		$sDisplayStyle = $oAttDef->GetDisplayStyle();
 		$oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
-		return $oWidget->Display($oPage, $iMaxComboLength, $bAllowTargetCreation, $sTitle, $oAllowedValues, $value, $iInputId, $bMandatory, $sFieldName, $sFormPrefix, $aArgs, $bSearchMode);
+		return $oWidget->Display($oPage, $iMaxComboLength, $bAllowTargetCreation, $sTitle, $oAllowedValues, $value, $iInputId, $bMandatory, $sFieldName, $sFormPrefix, $aArgs, $bSearchMode, $sDisplayStyle);
 	}
 
 	public function __construct($sTargetClass, $iInputId)
@@ -89,13 +90,14 @@ class UIExtKeyWidget
 	 * @param Hash $aArgs Extra context arguments
 	 * @return string The HTML fragment to be inserted into the page
 	 */
-	public function Display(WebPage $oPage, $iMaxComboLength, $bAllowTargetCreation, $sTitle, $oAllowedValues, $value, $iInputId, $bMandatory, $sFieldName, $sFormPrefix = '', $aArgs = array(), $bSearchMode = false)
+	public function Display(WebPage $oPage, $iMaxComboLength, $bAllowTargetCreation, $sTitle, $oAllowedValues, $value, $iInputId, $bMandatory, $sFieldName, $sFormPrefix = '', $aArgs = array(), $bSearchMode = false, $sDisplayStyle = 'select')
 	{
 		$sTitle = addslashes($sTitle);	
 		$oPage->add_linked_script('../js/extkeywidget.js');
 		$oPage->add_linked_script('../js/forms-json-utils.js');
 		
 		$bCreate = (!$bSearchMode) && (!MetaModel::IsAbstract($this->sTargetClass)) && (UserRights::IsActionAllowed($this->sTargetClass, UR_ACTION_BULK_MODIFY) && $bAllowTargetCreation);
+		$bExtensions = true;
 		$sMessage = Dict::S('UI:Message:EmptyList:UseSearchForm');
 		$sAttrFieldPrefix = ($bSearchMode) ? '' : 'attr_';
 
@@ -117,40 +119,60 @@ class UIExtKeyWidget
 		}
 		elseif ($oAllowedValues->Count() < $iMaxComboLength)
 		{
-			// Few choices, use a normal 'select'
-			$sSelectMode = 'true';
-			
-			$sHelpText = ''; //$this->oAttDef->GetHelpOnEdition();
-			
-			$sHTMLValue = "<select title=\"$sHelpText\" name=\"{$sAttrFieldPrefix}{$sFieldName}\" id=\"$this->iId\">\n";
-			if ($bSearchMode)
+			// Discrete list of values, use a SELECT or RADIO buttons depending on the config
+			switch($sDisplayStyle)
 			{
-				$sDisplayValue = isset($aArgs['sDefaultValue']) ? $aArgs['sDefaultValue'] : Dict::S('UI:SearchValue:Any');
-				$sHTMLValue .= "<option value=\"\">$sDisplayValue</option>\n";			
-			}
-			else
-			{
-				$sHTMLValue .= "<option value=\"\">".Dict::S('UI:SelectOne')."</option>\n";
-			}
-			$oAllowedValues->Rewind();
-			while($oObj = $oAllowedValues->Fetch())
-			{
-				$key = $oObj->GetKey();
-				$display_value = $oObj->Get('friendlyname');
+				case 'radio':
+				case 'radio_horizontal':
+				case 'radio_vertical':
+				$sHTMLValue = '';
+				$bVertical = ($sDisplayStyle != 'radio_horizontal');
+				$bExtensions = false;
+				$oAllowedValues->Rewind();
+				$aAllowedValues = array();
+				while($oObj = $oAllowedValues->Fetch())
+				{
+					$aAllowedValues[$oObj->GetKey()] = $oObj->Get('friendlyname');
+				}				
+				$sHTMLValue = $oPage->GetRadioButtons($aAllowedValues, $value, $this->iId, "{$sAttrFieldPrefix}{$sFieldName}", $bMandatory, $bVertical, '' /* TODO: manage validation field */);
+				$aEventsList[] ='change';
+				break;
 
-				if (($oAllowedValues->Count() == 1) && ($bMandatory == 'true') )
+				case 'select':
+				default:
+				$sSelectMode = 'true';
+				
+				$sHelpText = ''; //$this->oAttDef->GetHelpOnEdition();
+				
+				$sHTMLValue = "<select title=\"$sHelpText\" name=\"{$sAttrFieldPrefix}{$sFieldName}\" id=\"$this->iId\">\n";
+				if ($bSearchMode)
 				{
-					// When there is only once choice, select it by default
-					$sSelected = ' selected';
+					$sDisplayValue = isset($aArgs['sDefaultValue']) ? $aArgs['sDefaultValue'] : Dict::S('UI:SearchValue:Any');
+					$sHTMLValue .= "<option value=\"\">$sDisplayValue</option>\n";			
 				}
 				else
 				{
-					$sSelected = ($value == $key) ? ' selected' : '';
+					$sHTMLValue .= "<option value=\"\">".Dict::S('UI:SelectOne')."</option>\n";
 				}
-				$sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
-			}
-			$sHTMLValue .= "</select>\n";
-			$oPage->add_ready_script(
+				$oAllowedValues->Rewind();
+				while($oObj = $oAllowedValues->Fetch())
+				{
+					$key = $oObj->GetKey();
+					$display_value = $oObj->Get('friendlyname');
+	
+					if (($oAllowedValues->Count() == 1) && ($bMandatory == 'true') )
+					{
+						// When there is only once choice, select it by default
+						$sSelected = ' selected';
+					}
+					else
+					{
+						$sSelected = ($value == $key) ? ' selected' : '';
+					}
+					$sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
+				}
+				$sHTMLValue .= "</select>\n";
+				$oPage->add_ready_script(
 <<<EOF
 		oACWidget_{$this->iId} = new ExtKeyWidget('{$this->iId}', '{$this->sTargetClass}', '$sFilter', '$sTitle', true, $sWizHelper);
 		oACWidget_{$this->iId}.emptyHtml = "<div style=\"background: #fff; border:0; text-align:center; vertical-align:middle;\"><p>$sMessage</p></div>";
@@ -158,7 +180,8 @@ class UIExtKeyWidget
 		$('#$this->iId').bind('change', function() { $(this).trigger('extkeychange') } );
 
 EOF
-);
+				);
+			} // Switch
 		}
 		else
 		{
@@ -200,7 +223,7 @@ EOF
 EOF
 );
 		}
-		if (MetaModel::IsHierarchicalClass($this->sTargetClass) !== false)
+		if ($bExtensions && MetaModel::IsHierarchicalClass($this->sTargetClass) !== false)
 		{
 			$sHTMLValue .= "<a class=\"no-arrow\" href=\"javascript:oACWidget_{$this->iId}.HKDisplay();\"><img id=\"mini_tree_{$this->iId}\" style=\"border:0;vertical-align:middle;\" src=\"../images/mini_tree.gif\" /></a>&nbsp;";
 			$oPage->add_ready_script(
@@ -212,7 +235,7 @@ EOF
 EOF
 );
 		}
-		if ($bCreate)
+		if ($bCreate && $bExtensions)
 		{
 			$sHTMLValue .= "<a class=\"no-arrow\" href=\"javascript:oACWidget_{$this->iId}.CreateObject();\"><img id=\"mini_add_{$this->iId}\" style=\"border:0;vertical-align:middle;\" src=\"../images/mini_add.gif\" /></a>&nbsp;";
 			$oPage->add_ready_script(

+ 47 - 0
application/webpage.class.inc.php

@@ -275,6 +275,53 @@ class WebPage
 		$sHtml .= "</table>\n";
 		return $sHtml;
     }
+
+	/**
+	 * Build a set of radio buttons suitable for editing a field/attribute of an object (including its validation)
+	 * @param $aAllowedValues hash Array of value => display_value
+	 * @param $value mixed Current value for the field/attribute
+	 * @param $iId mixed Unique Id for the input control in the page
+	 * @param $sFieldName string The name of the field, attr_<$sFieldName> will hold the value for the field
+	 * @param $bMandatory bool Whether or not the field is mandatory
+	 * @param $bVertical bool Disposition of the radio buttons vertical or horizontal
+	 * @param $sValidationField string HTML fragment holding the validation field (exclamation icon...)
+	 * @return string The HTML fragment corresponding to the radio buttons
+	 */
+	public function GetRadioButtons($aAllowedValues, $value, $iId, $sFieldName, $bMandatory, $bVertical, $sValidationField)
+	{
+		$idx = 0;
+		$sHTMLValue = '';
+		foreach($aAllowedValues as $key => $display_value)
+		{
+			if ((count($aAllowedValues) == 1) && ($bMandatory == 'true') )
+			{
+				// When there is only once choice, select it by default
+				$sSelected = ' checked';
+			}
+			else
+			{
+				$sSelected = ($value == $key) ? ' checked' : '';
+			}
+			$sHTMLValue .= "<input type=\"radio\" id=\"{$iId}_{$key}\" name=\"attr_radio_$sFieldName\" onChange=\"$('#{$iId}').val(this.value).trigger('change');\" value=\"$key\"$sSelected><label class=\"radio\" for=\"{$iId}_{$key}\">&nbsp;$display_value</label>&nbsp;";
+			if ($bVertical)
+			{
+				if ($idx == 0)
+				{
+					// Validation icon at the end of the first line
+					$sHTMLValue .= "&nbsp;{$sValidationField}\n";							
+				}
+				$sHTMLValue .= "<br>\n";
+			}
+			$idx++;
+		}
+		$sHTMLValue .= "<input type=\"hidden\" id=\"$iId\" name=\"attr_$sFieldName\" value=\"$value\"/>";
+		if (!$bVertical)					
+		{
+			// Validation icon at the end of the line
+			$sHTMLValue .= "&nbsp;{$sValidationField}\n";
+		}
+		return $sHTMLValue;
+	}
 	
 	/**
 	 * Outputs (via some echo) the complete HTML page by assembling all its elements

+ 6 - 0
core/attributedef.class.inc.php

@@ -1109,6 +1109,11 @@ class AttributeString extends AttributeDBField
 		$sEscaped = str_replace($sFrom, $sTo, (string)$sValue);
 		return $sTextQualifier.$sEscaped.$sTextQualifier;
 	}
+
+	public function GetDisplayStyle()
+	{
+		return $this->GetOptional('display_style', 'select');
+	}
 }
 
 /**
@@ -2355,6 +2360,7 @@ class AttributeExternalKey extends AttributeDBFieldVoid
 	public function GetTargetClass($iType = EXTKEY_RELATIVE) {return $this->Get("targetclass");}
 	public function GetKeyAttDef($iType = EXTKEY_RELATIVE){return $this;}
 	public function GetKeyAttCode() {return $this->GetCode();} 
+	public function GetDisplayStyle() { return $this->GetOptional('display_style', 'select'); }
 	
 
 	public function GetDefaultValue() {return 0;}