123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- <?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\Field;
- use \Closure;
- use \Combodo\iTop\Form\Validator\Validator;
- use \Combodo\iTop\Form\Validator\MandatoryValidator;
- /**
- * Description of Field
- *
- * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
- */
- abstract class Field
- {
- const DEFAULT_LABEL = '';
- const DEFAULT_READ_ONLY = false;
- const DEFAULT_MANDATORY = false;
- const DEFAULT_VALID = true;
- protected $sId;
- protected $sGlobalId;
- protected $sFormPath;
- protected $sLabel;
- protected $bReadOnly;
- protected $bMandatory;
- protected $aValidators;
- protected $bValid;
- protected $aErrorMessages;
- protected $currentValue;
- protected $onFinalizeCallback;
- /**
- * Default constructor
- *
- * @param string $sId
- * @param Closure $onFinalizeCallback (Used in the $oForm->AddField($sId, ..., function() use ($oManager, $oForm, '...') { ... } ); )
- */
- public function __construct($sId, Closure $onFinalizeCallback = null)
- {
- $this->sId = $sId;
- $this->sGlobalId = 'field_' . $sId . '_' . uniqid();
- $this->sLabel = static::DEFAULT_LABEL;
- $this->bReadOnly = static::DEFAULT_READ_ONLY;
- $this->bMandatory = static::DEFAULT_MANDATORY;
- $this->aValidators = array();
- $this->bValid = static::DEFAULT_VALID;
- $this->aErrorMessages = array();
- $this->onFinalizeCallback = $onFinalizeCallback;
- }
- /**
- * Returns the field id within its container form
- *
- * @return string
- */
- public function GetId()
- {
- return $this->sId;
- }
- /**
- * Returns a unique field id within the top level form
- *
- * @return string
- */
- public function GetGlobalId()
- {
- return $this->sGlobalId;
- }
- /**
- * Returns the id of the container form
- *
- * @return string
- */
- public function GetFormPath()
- {
- return $this->sFormPath;
- }
- /**
- *
- * @return string
- */
- public function GetLabel()
- {
- return $this->sLabel;
- }
- /**
- *
- * @return boolean
- */
- public function GetReadOnly()
- {
- return $this->bReadOnly;
- }
- /**
- *
- * @return boolean
- */
- public function GetMandatory()
- {
- return $this->bMandatory;
- }
- /**
- *
- * @return array
- */
- public function GetValidators()
- {
- return $this->aValidators;
- }
- /**
- * Returns the current validation state of the field (true|false).
- * It DOESN'T make the validation, see Validate() instead.
- *
- * @return boolean
- */
- public function GetValid()
- {
- return $this->bValid;
- }
- /**
- *
- * @return array
- */
- public function GetErrorMessages()
- {
- return $this->aErrorMessages;
- }
- /**
- *
- * @return array
- */
- public function GetCurrentValue()
- {
- return $this->currentValue;
- }
- /**
- * Sets the field formpath
- * Usually Called by the form when adding the field
- *
- * @param string $sFormPath
- * @return \Combodo\iTop\Form\Field\Field
- */
- public function SetFormPath($sFormPath)
- {
- $this->sFormPath = $sFormPath;
- return $this;
- }
- /**
- *
- * @param type $sLabel
- * @return \Combodo\iTop\Form\Field\Field
- */
- public function SetLabel($sLabel)
- {
- $this->sLabel = $sLabel;
- return $this;
- }
- /**
- *
- * @param boolean $bReadOnly
- * @return \Combodo\iTop\Form\Field\Field
- */
- public function SetReadOnly($bReadOnly)
- {
- $this->bReadOnly = $bReadOnly;
- return $this;
- }
- /**
- * Sets if the field is mandatory or not.
- * Setting the value will automatically add/remove a MandatoryValidator to the Field
- *
- * @param boolean $bMandatory
- * @return \Combodo\iTop\Form\Field\Field
- */
- public function SetMandatory($bMandatory)
- {
- // Before changing the property, we check if it was already mandatory. If not, we had the mandatory validator
- if ($bMandatory && !$this->bMandatory)
- {
- $this->AddValidator(new MandatoryValidator());
- }
- if (!$bMandatory)
- {
- foreach ($this->aValidators as $iKey => $oValue)
- {
- if ($oValue::Getname() === MandatoryValidator::GetName())
- {
- unset($this->aValidators[$iKey]);
- }
- }
- }
- $this->bMandatory = $bMandatory;
- return $this;
- }
- /**
- *
- * @param array $aValidators
- * @return \Combodo\iTop\Form\Field\Field
- */
- public function SetValidators($aValidators)
- {
- $this->aValidators = $aValidators;
- return $this;
- }
- /**
- * Note : Function is protected as bValid should not be set from outside
- *
- * @param boolean $bValid
- * @return \Combodo\iTop\Form\Field\Field
- */
- protected function SetValid($bValid)
- {
- $this->bValid = $bValid;
- return $this;
- }
- /**
- * Note : Function is protected as aErrorMessages should not be set from outside
- *
- * @param array $aErrorMessages
- * @return \Combodo\iTop\Form\Field\Field
- */
- protected function SetErrorMessages($aErrorMessages)
- {
- $this->aErrorMessages = $aErrorMessages;
- return $this;
- }
- /**
- *
- * @param mixed $currentValue
- * @return \Combodo\iTop\Form\Field\Field
- */
- public function SetCurrentValue($currentValue)
- {
- $this->currentValue = $currentValue;
- return $this;
- }
- /**
- *
- * @param Closure $onFinalizeCallback
- * @return \Combodo\iTop\Form\Field\Field
- */
- public function SetOnFinalizeCallback(Closure $onFinalizeCallback)
- {
- $this->onFinalizeCallback = $onFinalizeCallback;
- return $this;
- }
- /**
- *
- * @param Validator $oValidator
- * @return \Combodo\iTop\Form\Field\Field
- */
- public function AddValidator(Validator $oValidator)
- {
- $this->aValidators[] = $oValidator;
- return $this;
- }
- /**
- *
- * @param Validator $oValidator
- * @return \Combodo\iTop\Form\Field\Field
- */
- public function RemoveValidator(Validator $oValidator)
- {
- foreach ($this->aValidators as $iKey => $oValue)
- {
- if ($oValue === $oValidator)
- {
- unset($this->aValidators[$iKey]);
- }
- }
- return $this;
- }
- /**
- * Note : Function is protected as aErrorMessages should not be add from outside
- *
- * @param string $sErrorMessage
- * @return \Combodo\iTop\Form\Field\Field
- */
- protected function AddErrorMessage($sErrorMessage)
- {
- $this->aErrorMessages[] = $sErrorMessage;
- return $this;
- }
- /**
- * Note : Function is protected as aErrorMessages should not be set from outside
- *
- * @return \Combodo\iTop\Form\Field\Field
- */
- protected function EmptyErrorMessages()
- {
- $this->aErrorMessages = array();
- return $this;
- }
- public function OnCancel()
- {
- // Overload when needed
- }
- public function OnFinalize()
- {
- if ($this->onFinalizeCallback !== null)
- {
- // Note : We MUST have a temp variable to call the Closure. otherwise it won't work when the Closure is a class member
- $callback = $this->onFinalizeCallback;
- $callback($this);
- }
- }
- /**
- * Checks the validators to see if the field's current value is valid.
- * Then sets $bValid and $aErrorMessages.
- *
- * @return boolean
- */
- abstract public function Validate();
- }
|