瀏覽代碼

CustomFields: support of RadioField or SelectObject +"radio" control

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@3987 a333f486-631f-4898-b8df-5754b55c2be0
romainq 9 年之前
父節點
當前提交
5d3ac4d5d6

+ 2 - 1
sources/form/field/field.class.inc.php

@@ -58,7 +58,8 @@ abstract class Field
 	public function __construct($sId, Closure $onFinalizeCallback = null)
 	{
 		$this->sId = $sId;
-		$this->sGlobalId = 'field_' . $sId . '_' . uniqid();
+		// No space in such an id, that could be used as a DOM node id
+		$this->sGlobalId = 'field_' . str_replace(' ', '_', $sId) . '_' . uniqid();
 		$this->sLabel = static::DEFAULT_LABEL;
 		$this->bHidden = static::DEFAULT_HIDDEN;
 		$this->bReadOnly = static::DEFAULT_READ_ONLY;

+ 16 - 0
sources/form/field/selectobjectfield.class.inc.php

@@ -33,12 +33,18 @@ class SelectObjectField extends Field
 	protected $iMaximumComboLength;
 	protected $iMinAutoCompleteChars;
 
+	protected $iControlType;
+
+	const CONTROL_SELECT = 1;
+	const CONTROL_RADIO_VERTICAL = 2;
+
 	public function __construct($sId, Closure $onFinalizeCallback = null)
 	{
 		parent::__construct($sId, $onFinalizeCallback);
 		$this->oSearch = null;
 		$this->iMaximumComboLength = null;
 		$this->iMinAutoCompleteChars = 3;
+		$this->iControlType = self::CONTROL_SELECT;
 	}
 
 	public function SetSearch(DBSearch $oSearch)
@@ -56,6 +62,11 @@ class SelectObjectField extends Field
 		$this->iMinAutoCompleteChars = $iMinAutoCompleteChars;
 	}
 
+	public function SetControlType($iControlType)
+	{
+		$this->iControlType = $iControlType;
+	}
+
 	/**
 	 * Sets if the field is mandatory or not.
 	 * Setting the value will automatically add/remove a MandatoryValidator to the Field
@@ -100,4 +111,9 @@ class SelectObjectField extends Field
 	{
 		return $this->iMinAutoCompleteChars;
 	}
+
+	public function GetControlType()
+	{
+		return $this->iControlType;
+	}
 }

+ 1 - 0
sources/renderer/console/consoleformrenderer.class.inc.php

@@ -37,6 +37,7 @@ class ConsoleFormRenderer extends FormRenderer
 		$this->AddSupportedField('StringField', 'ConsoleSimpleFieldRenderer');
 		$this->AddSupportedField('SelectField', 'ConsoleSimpleFieldRenderer');
 		$this->AddSupportedField('TextAreaField', 'ConsoleSimpleFieldRenderer');
+		$this->AddSupportedField('RadioField', 'ConsoleSimpleFieldRenderer');
 		$this->AddSupportedField('SelectObjectField', 'ConsoleSelectObjectFieldRenderer');
 		$this->AddSupportedField('SubFormField', 'ConsoleSubFormFieldRenderer');
 	}

+ 39 - 2
sources/renderer/console/fieldrenderer/consoleselectobjectfieldrenderer.class.inc.php

@@ -18,13 +18,13 @@
 
 namespace Combodo\iTop\Renderer\Console\FieldRenderer;
 
-use Combodo\iTop\Form\Field\StringField;
 use Combodo\iTop\Form\Validator\MandatoryValidator;
-use Combodo\iTop\Form\Validator\Validator;
 use \Dict;
 use \DBObjectSet;
 use Combodo\iTop\Renderer\FieldRenderer;
 use Combodo\iTop\Renderer\RenderingOutput;
+use Combodo\iTop\Form\Field\SelectObjectField;
+
 
 class ConsoleSelectObjectFieldRenderer extends FieldRenderer
 {
@@ -73,6 +73,7 @@ class ConsoleSelectObjectFieldRenderer extends FieldRenderer
 			if ($iCount > $iMaxComboLength)
 			{
 				// Auto-complete
+				//
 				$sEditType = 'autocomplete';
 				$aExtKeyParams = array();
 				$aExtKeyParams['iFieldSize'] = 10;
@@ -101,9 +102,44 @@ class ConsoleSelectObjectFieldRenderer extends FieldRenderer
 					$oOutput->AddCssFile($sFile);
 				}
 			}
+			elseif($this->oField->GetControlType() == SelectObjectField::CONTROL_RADIO_VERTICAL)
+			{
+				// Radio buttons (vertical)
+				//
+				$sEditType = 'radio';
+				$bVertical = true;
+				$idx = 0;
+				$bMandatory = $this->oField->GetMandatory();
+				$value = $this->oField->GetCurrentValue();
+				$sId = $this->oField->GetGlobalId();
+				$oOutput->AddHtml('<div>');
+				while ($oObject = $oSet->Fetch())
+				{
+					$iObject = $oObject->GetKey();
+					$sLabel = $oObject->Get('friendlyname');
+					if (($iCount == 1) && $bMandatory)
+					{
+						// When there is only once choice, select it by default
+						$sSelected = ' checked';
+					}
+					else
+					{
+						$sSelected = ($value == $iObject) ? ' checked' : '';
+					}
+					$oOutput->AddHtml("<input type=\"radio\" id=\"{$sId}_{$iObject}\" name=\"radio_$sId\" onChange=\"$('#{$sId}').val(this.value).trigger('change');\" value=\"$iObject\"$sSelected><label class=\"radio\" for=\"{$sId}_{$iObject}\">&nbsp;".htmlentities($sLabel, ENT_QUOTES, 'UTF-8')."</label>&nbsp;");
+					if ($bVertical)
+					{
+						$oOutput->AddHtml("<br>\n");
+					}
+					$idx++;
+				}
+				$oOutput->AddHtml('</div>');
+				$oOutput->AddHtml("<input type=\"hidden\" id=\"$sId\" name=\"$sId\" value=\"$value\"/>");
+			}
 			else
 			{
 				// Drop-down select
+				//
 				$sEditType = 'select';
 				$oOutput->AddHtml('<select class="form-field-data" id="'.$this->oField->GetGlobalId().'">');
 				$oOutput->AddHtml('<option value="">'.Dict::S('UI:SelectOne').'</option>');
@@ -189,6 +225,7 @@ EOF
 		switch ($sEditType)
 		{
 			case 'autocomplete':
+			case 'radio':
 				$oOutput->AddJs(
 					<<<EOF
 	                    $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").form_field('option', 'get_current_value_callback', function(me){ return $(me.element).find('#{$this->oField->GetGlobalId()}').val();});

+ 45 - 1
sources/renderer/console/fieldrenderer/consolesimplefieldrenderer.class.inc.php

@@ -21,7 +21,7 @@ namespace Combodo\iTop\Renderer\Console\FieldRenderer;
 use \Dict;
 use Combodo\iTop\Renderer\FieldRenderer;
 use Combodo\iTop\Renderer\RenderingOutput;
-use \Combodo\iTop\Form\Field\TextAreaField;
+use Combodo\iTop\Form\Field\TextAreaField;
 use \InlineImage;
 use \UserRights;
 
@@ -115,6 +115,49 @@ EOF
 					$oOutput->AddHtml('<span class="form_validation"></span>');
 					$oOutput->AddHtml('</td>');
 					break;
+
+				case 'Combodo\\iTop\\Form\\Field\\RadioField':
+					$oOutput->AddHtml('<td class="form-field-content">');
+					if ($this->oField->GetReadOnly())
+					{
+						$aChoices = $this->oField->GetChoices();
+						$sCurrentLabel = isset($aChoices[$this->oField->GetCurrentValue()]) ? $aChoices[$this->oField->GetCurrentValue()] : '' ;
+						$oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
+						$oOutput->AddHtml('<span class="form-field-data">'.htmlentities($sCurrentLabel, ENT_QUOTES, 'UTF-8').'</span>');
+					}
+					else
+					{
+						$bVertical = true;
+						$idx = 0;
+						$bMandatory = $this->oField->GetMandatory();
+						$value = $this->oField->GetCurrentValue();
+						$sId = $this->oField->GetGlobalId();
+						$oOutput->AddHtml('<div>');
+						$aChoices = $this->oField->GetChoices();
+						foreach ($aChoices as $sChoice => $sLabel)
+						{
+							if ((count($aChoices)== 1) && $bMandatory)
+							{
+								// When there is only once choice, select it by default
+								$sSelected = ' checked';
+							}
+							else
+							{
+								$sSelected = ($value == $sChoice) ? ' checked' : '';
+							}
+							$oOutput->AddHtml("<input type=\"radio\" id=\"{$sId}_{$idx}\" name=\"radio_$sId\" onChange=\"$('#{$sId}').val(this.value).trigger('change');\" value=\"".htmlentities($sChoice, ENT_QUOTES, 'UTF-8')."\"$sSelected><label class=\"radio\" for=\"{$sId}_{$idx}\">&nbsp;".htmlentities($sLabel, ENT_QUOTES, 'UTF-8')."</label>&nbsp;");
+							if ($bVertical)
+							{
+								$oOutput->AddHtml("<br>\n");
+							}
+							$idx++;
+						}
+						$oOutput->AddHtml('</div>');
+						$oOutput->AddHtml("<input type=\"hidden\" id=\"$sId\" name=\"$sId\" value=\"$value\"/>");
+					}
+					$oOutput->AddHtml('<span class="form_validation"></span>');
+					$oOutput->AddHtml('</td>');
+					break;
 			}
 			$oOutput->AddHtml('</tr>');
 			$oOutput->AddHtml('</table>');
@@ -141,6 +184,7 @@ EOF
 				break;
 
 			case 'Combodo\\iTop\\Form\\Field\\SelectField':
+			case 'Combodo\\iTop\\Form\\Field\\RadioField':
 				$oOutput->AddJs(
 <<<EOF
                     $("#{$this->oField->GetGlobalId()}").off("change").on("change", function(){