Browse Source

Prerequisites to the custom fields

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@3940 a333f486-631f-4898-b8df-5754b55c2be0
glajarige 9 năm trước cách đây
mục cha
commit
cd5cd30206

+ 1 - 1
sources/autoload.php

@@ -34,8 +34,8 @@ require_once APPROOT . 'sources/form/field/checkboxfield.class.inc.php';
 require_once APPROOT . 'sources/form/field/radiofield.class.inc.php';
 require_once APPROOT . 'sources/form/validator/validator.class.inc.php';
 require_once APPROOT . 'sources/form/validator/mandatoryvalidator.class.inc.php';
-require_once APPROOT . 'sources/form/validator/notemptyvalidator.class.inc.php';
 require_once APPROOT . 'sources/form/validator/integervalidator.class.inc.php';
+require_once APPROOT . 'sources/form/validator/notemptyextkeyvalidator.class.inc.php';
 require_once APPROOT . 'sources/renderer/formrenderer.class.inc.php';
 require_once APPROOT . 'sources/renderer/fieldrenderer.class.inc.php';
 require_once APPROOT . 'sources/renderer/renderingoutput.class.inc.php';

+ 23 - 0
sources/form/field/field.class.inc.php

@@ -31,6 +31,7 @@ use \Combodo\iTop\Form\Validator\MandatoryValidator;
 abstract class Field
 {
 	const DEFAULT_LABEL = '';
+	const DEFAULT_HIDDEN = false;
 	const DEFAULT_READ_ONLY = false;
 	const DEFAULT_MANDATORY = false;
 	const DEFAULT_VALID = true;
@@ -39,6 +40,7 @@ abstract class Field
 	protected $sGlobalId;
 	protected $sFormPath;
 	protected $sLabel;
+	protected $bHidden;
 	protected $bReadOnly;
 	protected $bMandatory;
 	protected $aValidators;
@@ -58,6 +60,7 @@ abstract class Field
 		$this->sId = $sId;
 		$this->sGlobalId = 'field_' . $sId . '_' . uniqid();
 		$this->sLabel = static::DEFAULT_LABEL;
+		$this->bHidden = static::DEFAULT_HIDDEN;
 		$this->bReadOnly = static::DEFAULT_READ_ONLY;
 		$this->bMandatory = static::DEFAULT_MANDATORY;
 		$this->aValidators = array();
@@ -109,6 +112,15 @@ abstract class Field
 	 *
 	 * @return boolean
 	 */
+	public function GetHidden()
+	{
+		return $this->bHidden;
+	}
+
+	/**
+	 *
+	 * @return boolean
+	 */
 	public function GetReadOnly()
 	{
 		return $this->bReadOnly;
@@ -187,6 +199,17 @@ abstract class Field
 
 	/**
 	 *
+	 * @param boolean $bHidden
+	 * @return \Combodo\iTop\Form\Field\Field
+	 */
+	public function SetHidden($bHidden)
+	{
+		$this->bHidden = $bHidden;
+		return $this;
+	}
+
+	/**
+	 *
 	 * @param boolean $bReadOnly
 	 * @return \Combodo\iTop\Form\Field\Field
 	 */

+ 78 - 0
sources/form/field/textareafield.class.inc.php

@@ -19,6 +19,8 @@
 
 namespace Combodo\iTop\Form\Field;
 
+use \Closure;
+use \DBObject;
 use \Combodo\iTop\Form\Field\TextField;
 
 /**
@@ -28,5 +30,81 @@ use \Combodo\iTop\Form\Field\TextField;
  */
 class TextAreaField extends TextField
 {
+	const ENUM_FORMAT_TEXT = 'text';
+	const ENUM_FORMAT_HTML = 'html';
+	const DEFAULT_FORMAT = 'html';
+
+	protected $sFormat;
+	protected $oObject;
+	protected $sTransactionId;
+
+	public function __construct($sId, Closure $onFinalizeCallback = null, DBObject $oObject = null)
+	{
+		parent::__construct($sId, $onFinalizeCallback);
+		$this->sFormat = static::DEFAULT_FORMAT;
+		$this->oObject = $oObject;
+		$this->sTransactionId = null;
+	}
+
+	/**
+	 *
+	 * @return string
+	 */
+	public function GetFormat()
+	{
+		return $this->sFormat;
+	}
+
+	/**
+	 *
+	 * @param string $sFormat
+	 * @return \Combodo\iTop\Form\Field\TextAreaField
+	 */
+	public function SetFormat($sFormat)
+	{
+		$this->sFormat = $sFormat;
+		return $this;
+	}
+
+	/**
+	 *
+	 * @return DBObject
+	 */
+	public function GetObject()
+	{
+		return $this->oObject;
+	}
+
+	/**
+	 *
+	 * @param DBObject $oObject
+	 * @return \Combodo\iTop\Form\Field\TextAreaField
+	 */
+	public function SetObject(DBObject $oObject)
+	{
+		$this->oObject = $oObject;
+		return $this;
+	}
+
+	/**
+	 * Returns the transaction id for the field. This is usally used/setted when using a html format that allows upload of files/images
+	 *
+	 * @return string
+	 */
+	public function GetTransactionId()
+	{
+		return $this->sTransactionId;
+	}
+
+	/**
+	 *
+	 * @param string $sTransactionId
+	 * @return \Combodo\iTop\Form\Field\TextAreaField
+	 */
+	public function SetTransactionId($sTransactionId)
+	{
+		$this->sTransactionId = $sTransactionId;
+		return $this;
+	}
 
 }

+ 22 - 0
sources/form/form.class.inc.php

@@ -31,6 +31,7 @@ use \Combodo\iTop\Form\Field\Field;
 class Form
 {
 	protected $sId;
+	protected $sTransactionId;
 	protected $aFields;
 	protected $aDependencies;
 	protected $bValid;
@@ -44,6 +45,7 @@ class Form
 	public function __construct($sId)
 	{
 		$this->sId = $sId;
+		$this->sTransactionId = null;
 		$this->aFields = array();
 		$this->aDependencies = array();
 		$this->bValid = true;
@@ -61,6 +63,26 @@ class Form
 
 	/**
 	 *
+	 * @return string
+	 */
+	public function GetTransactionId()
+	{
+		return $this->sTransactionId;
+	}
+
+	/**
+	 *
+	 * @param string $sTransactionId
+	 * @return \Combodo\iTop\Form\Form
+	 */
+	public function SetTransactionId($sTransactionId)
+	{
+		$this->sTransactionId = $sTransactionId;
+		return $this;
+	}
+
+	/**
+	 *
 	 * @return array
 	 */
 	public function GetFields()

+ 6 - 1
sources/form/formmanager.class.inc.php

@@ -51,7 +51,11 @@ abstract class FormManager
 		$oFormRenderer = new $sFormRendererClass();
 		$oFormRenderer->SetEndpoint($aJson['formrenderer_endpoint']);
 		$oFormManager->SetRenderer($oFormRenderer);
-		
+
+		$oFormManager->SetForm(new Form($aJson['id']));
+		$oFormManager->GetForm()->SetTransactionId($aJson['transaction_id']);
+		$oFormManager->GetRenderer()->SetForm($oFormManager->GetForm());
+
 		return $oFormManager;
 	}
 
@@ -123,6 +127,7 @@ abstract class FormManager
 		// Overload in child class when needed
 		return array(
 			'id' => $this->oForm->GetId(),
+			'transaction_id' => $this->oForm->GetTransactionId(),
 			'formmanager_class' => $this->GetClass(),
 			'formrenderer_class' => get_class($this->GetRenderer()),
 			'formrenderer_endpoint' => $this->GetRenderer()->GetEndpoint()

+ 0 - 35
sources/form/validator/notemptyvalidator.class.inc.php

@@ -1,35 +0,0 @@
-<?php
-
-// Copyright (C) 2010-2016 Combodo SARL
-//
-//   This file is part of iTop.
-//
-//   iTop is free software; you can redistribute it and/or modify	
-//   it under the terms of the GNU Affero General Public License as published by
-//   the Free Software Foundation, either version 3 of the License, or
-//   (at your option) any later version.
-//
-//   iTop is distributed in the hope that it will be useful,
-//   but WITHOUT ANY WARRANTY; without even the implied warranty of
-//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-//   GNU Affero General Public License for more details.
-//
-//   You should have received a copy of the GNU Affero General Public License
-//   along with iTop. If not, see <http://www.gnu.org/licenses/>
-
-namespace Combodo\iTop\Form\Validator;
-
-use \Combodo\iTop\Form\Validator\Validator;
-
-/**
- * Description of NotEmptyValidator
- *
- * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
- */
-class NotEmptyValidator extends Validator
-{
-	const VALIDATOR_NAME = 'notempty';
-	const DEFAULT_REGEXP = '.*\S.*';
-	const DEFAULT_ERROR_MESSAGE = 'TOTR: MUST NOT BE EMPTY MESSAGE';
-
-}

+ 49 - 15
sources/renderer/bootstrap/fieldrenderer/bssimplefieldrenderer.class.inc.php

@@ -19,7 +19,10 @@
 
 namespace Combodo\iTop\Renderer\Bootstrap\FieldRenderer;
 
+use \utils;
 use \Dict;
+use \UserRights;
+use \InlineImage;
 use \Combodo\iTop\Renderer\FieldRenderer;
 use \Combodo\iTop\Renderer\RenderingOutput;
 
@@ -41,10 +44,10 @@ class BsSimpleFieldRenderer extends FieldRenderer
 		$oOutput = new RenderingOutput();
 		$sFieldClass = get_class($this->oField);
 		$sFieldMandatoryClass = ($this->oField->GetMandatory()) ? 'form_mandatory' : '';
-
+		
 		// TODO : Shouldn't we have a field type so we don't have to maintain FQN classname ?
 		// Rendering field in edition mode
-		if (!$this->oField->GetReadOnly())
+		if (!$this->oField->GetReadOnly() && !$this->oField->GetHidden())
 		{
 			switch ($sFieldClass)
 			{
@@ -55,11 +58,13 @@ class BsSimpleFieldRenderer extends FieldRenderer
 						$oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">' . $this->oField->GetLabel() . '</label>');
 					}
 					$oOutput->AddHtml('<div class="help-block"></div>');
-					$oOutput->AddHtml('<input type="text" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="' . $this->oField->GetCurrentValue() . '" class="form-control" />');
+					$oOutput->AddHtml('<input type="text" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '" class="form-control" maxlength="255" />');
 					$oOutput->AddHtml('</div>');
 					break;
 
 				case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
+					$bRichEditor = ($this->oField->GetFormat() === 'html');
+
 					$oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
 					if ($this->oField->GetLabel() !== '')
 					{
@@ -68,6 +73,21 @@ class BsSimpleFieldRenderer extends FieldRenderer
 					$oOutput->AddHtml('<div class="help-block"></div>');
 					$oOutput->AddHtml('<textarea id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" class="form-control" rows="8">' . $this->oField->GetCurrentValue() . '</textarea>');
 					$oOutput->AddHtml('</div>');
+					// Some additional stuff if we are displaying it with a rich editor
+					if ($bRichEditor)
+					{
+						$sEditorLanguage = strtolower(trim(UserRights::GetUserLanguage()));
+						$oOutput->AddJs(
+<<<EOF
+							$('#{$this->oField->GetGlobalId()}').addClass('htmlEditor');
+							$('#{$this->oField->GetGlobalId()}').ckeditor(function(){}, {language: '$sEditorLanguage', contentsLanguage: '$sEditorLanguage'});
+EOF
+						);
+						if (($this->oField->GetObject() !== null) && ($this->oField->GetTransactionId() !== null))
+						{
+							$oOutput->AddJs(InlineImage::EnableCKEditorImageUpload($this->oField->GetObject(), utils::GetUploadTempId($this->oField->GetTransactionId())));
+						}
+					}
 					break;
 
 				case 'Combodo\\iTop\\Form\\Field\\SelectField':
@@ -116,11 +136,11 @@ class BsSimpleFieldRenderer extends FieldRenderer
 					break;
 
 				case 'Combodo\\iTop\\Form\\Field\\HiddenField':
-					$oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="' . $this->oField->GetCurrentValue() . '"/>');
+					$oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
 					break;
 			}
 		}
-		// ... and in read-only mode
+		// ... and in read-only mode (or hidden)
 		else
 		{
 			// ... specific rendering for fields with mulltiple values
@@ -136,12 +156,16 @@ class BsSimpleFieldRenderer extends FieldRenderer
 					case 'Combodo\\iTop\\Form\\Field\\StringField':
 					case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
 						$oOutput->AddHtml('<div class="form-group">');
-						if ($this->oField->GetLabel() !== '')
+						// Showing label / value only if read-only but not hidden
+						if (!$this->oField->GetHidden())
 						{
-							$oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">' . $this->oField->GetLabel() . '</label>');
+							if ($this->oField->GetLabel() !== '')
+							{
+								$oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">' . $this->oField->GetLabel() . '</label>');
+							}
+							$oOutput->AddHtml('<div class="form-control-static">' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '</div>');
 						}
-						$oOutput->AddHtml('<div class="form-control-static">' . $this->oField->GetCurrentValue() . '</div>');
-						$oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="' . $this->oField->GetCurrentValue() . '" class="form-control" />');
+						$oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '" class="form-control" />');
 						$oOutput->AddHtml('</div>');
 						break;
 
@@ -151,11 +175,15 @@ class BsSimpleFieldRenderer extends FieldRenderer
 						$sFieldValue = (isset($aFieldChoices[$this->oField->GetCurrentValue()])) ? $aFieldChoices[$this->oField->GetCurrentValue()] : Dict::S('UI:UndefinedObject');
 
 						$oOutput->AddHtml('<div class="form-group">');
-						if ($this->oField->GetLabel() !== '')
+						// Showing label / value only if read-only but not hidden
+						if (!$this->oField->GetHidden())
 						{
-							$oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">' . $this->oField->GetLabel() . '</label>');
+							if ($this->oField->GetLabel() !== '')
+							{
+								$oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">' . $this->oField->GetLabel() . '</label>');
+							}
+							$oOutput->AddHtml('<div class="form-control-static">' . $sFieldValue . '</div>');
 						}
-						$oOutput->AddHtml('<div class="form-control-static">' . $sFieldValue . '</div>');
 						$oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="' . $this->oField->GetCurrentValue() . '" class="form-control" />');
 						$oOutput->AddHtml('</div>');
 						break;
@@ -171,7 +199,7 @@ class BsSimpleFieldRenderer extends FieldRenderer
 			case 'Combodo\\iTop\\Form\\Field\\SelectField':
 			case 'Combodo\\iTop\\Form\\Field\\HiddenField':
 				$oOutput->AddJs(
-					<<<EOF
+<<<EOF
 					$("#{$this->oField->GetGlobalId()}").off("change keyup").on("change keyup", function(){
 						var me = this;
 
@@ -188,7 +216,7 @@ EOF
 			case 'Combodo\\iTop\\Form\\Field\\RadioField':
 			case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
 				$oOutput->AddJs(
-					<<<EOF
+<<<EOF
 					$("#{$this->oField->GetGlobalId()} input").off("change").on("change", function(){
 						var me = this;
 
@@ -220,7 +248,6 @@ EOF
 		switch ($sFieldClass)
 		{
 			case 'Combodo\\iTop\\Form\\Field\\StringField':
-			case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
 			case 'Combodo\\iTop\\Form\\Field\\SelectField':
 			case 'Combodo\\iTop\\Form\\Field\\HiddenField':
 			case 'Combodo\\iTop\\Form\\Field\\RadioField':
@@ -231,6 +258,13 @@ EOF
 EOF
 				);
 				break;
+			case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
+				$oOutput->AddJs(
+					<<<EOF
+					$("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").portal_form_field_html($sFormFieldOptions);
+EOF
+				);
+				break;
 		}
 
 		return $oOutput;

+ 2 - 0
sources/renderer/fieldrenderer.class.inc.php

@@ -19,6 +19,7 @@
 
 namespace Combodo\iTop\Renderer;
 
+use \DBObject;
 use \Combodo\iTop\Form\Field\Field;
 
 /**
@@ -57,6 +58,7 @@ abstract class FieldRenderer
 	public function SetEndpoint($sEndpoint)
 	{
 		$this->sEndpoint = $sEndpoint;
+		return $this;
 	}
 
 	/**

+ 7 - 2
sources/renderer/formrenderer.class.inc.php

@@ -184,16 +184,21 @@ abstract class FormRenderer
 	}
 
 	/**
+	 * Returns an array of Output for the form fields.
 	 *
+	 * @param array $aFieldIds An array of field ids. If specified, renders only those fields
 	 * @return array
 	 */
-	public function Render($aRequestedFields = null)
+	public function Render($aFieldIds = null)
 	{
 		$this->InitOutputs();
 
 		foreach ($this->oForm->GetFields() as $oField)
 		{
-			if ($aRequestedFields !== null && !in_array($oField->GetId(), $aRequestedFields)) continue;
+			if ($aFieldIds !== null && !in_array($oField->GetId(), $aFieldIds))
+			{
+				continue;
+			}
 			$this->aOutputs[$oField->GetId()] = $this->PrepareOutputForField($oField);
 		}