field.class.inc.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. // Copyright (C) 2010-2016 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. namespace Combodo\iTop\Form\Field;
  19. use \Closure;
  20. use \Combodo\iTop\Form\Validator\Validator;
  21. use \Combodo\iTop\Form\Validator\MandatoryValidator;
  22. /**
  23. * Description of Field
  24. *
  25. * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
  26. */
  27. abstract class Field
  28. {
  29. const DEFAULT_LABEL = '';
  30. const DEFAULT_READ_ONLY = false;
  31. const DEFAULT_MANDATORY = false;
  32. const DEFAULT_VALID = true;
  33. protected $sId;
  34. protected $sLabel;
  35. protected $bReadOnly;
  36. protected $bMandatory;
  37. protected $aValidators;
  38. protected $bValid;
  39. protected $aErrorMessages;
  40. protected $currentValue;
  41. protected $onFinalizeCallback;
  42. /**
  43. *
  44. * @param Closure $callback (Used in the $oForm->AddField($sId, ..., function() use ($oManager, $oForm, '...') { ... } ); )
  45. */
  46. public function __construct($sId, Closure $onFinalizeCallback = null)
  47. {
  48. $this->sId = $sId;
  49. $this->sLabel = static::DEFAULT_LABEL;
  50. $this->bReadOnly = static::DEFAULT_READ_ONLY;
  51. $this->bMandatory = static::DEFAULT_MANDATORY;
  52. $this->aValidators = array();
  53. $this->bValid = static::DEFAULT_VALID;
  54. $this->aErrorMessages = array();
  55. $this->onFinalizeCallback = $onFinalizeCallback;
  56. }
  57. public function GetId()
  58. {
  59. return $this->sId;
  60. }
  61. public function GetLabel()
  62. {
  63. return $this->sLabel;
  64. }
  65. public function GetReadOnly()
  66. {
  67. return $this->bReadOnly;
  68. }
  69. public function GetMandatory()
  70. {
  71. return $this->bMandatory;
  72. }
  73. public function GetValidators()
  74. {
  75. return $this->aValidators;
  76. }
  77. /**
  78. * Returns the current validation state of the field (true|false).
  79. * It DOESN'T make the validation, see Validate() instead.
  80. *
  81. * @return boolean
  82. */
  83. public function GetValid()
  84. {
  85. return $this->bValid;
  86. }
  87. public function GetErrorMessages()
  88. {
  89. return $this->aErrorMessages;
  90. }
  91. public function GetCurrentValue()
  92. {
  93. return $this->currentValue;
  94. }
  95. public function SetLabel($sLabel)
  96. {
  97. $this->sLabel = $sLabel;
  98. return $this;
  99. }
  100. public function SetReadOnly($bReadOnly)
  101. {
  102. $this->bReadOnly = $bReadOnly;
  103. return $this;
  104. }
  105. public function SetMandatory($bMandatory)
  106. {
  107. // Before changing the property, we check if it was already mandatory. If not, we had the mandatory validator
  108. if ($bMandatory && !$this->bMandatory)
  109. {
  110. $this->AddValidator(new MandatoryValidator());
  111. }
  112. if (!$bMandatory)
  113. {
  114. foreach ($this->aValidators as $iKey => $oValue)
  115. {
  116. if ($oValue::Getname() === MandatoryValidator::GetName())
  117. {
  118. unset($this->aValidators[$iKey]);
  119. }
  120. }
  121. }
  122. $this->bMandatory = $bMandatory;
  123. return $this;
  124. }
  125. public function SetValidators($aValidators)
  126. {
  127. $this->aValidators = $aValidators;
  128. return $this;
  129. }
  130. /**
  131. * Note : Function is protected as bValid should not be set from outside
  132. *
  133. * @param boolean $bValid
  134. * @return \Combodo\iTop\Form\Field\Field
  135. */
  136. protected function SetValid($bValid)
  137. {
  138. $this->bValid = $bValid;
  139. return $this;
  140. }
  141. /**
  142. * Note : Function is protected as aErrorMessages should not be set from outside
  143. *
  144. * @param array $aErrorMessages
  145. * @return \Combodo\iTop\Form\Field\Field
  146. */
  147. protected function SetErrorMessages($aErrorMessages)
  148. {
  149. $this->aErrorMessages = $aErrorMessages;
  150. return $this;
  151. }
  152. public function SetCurrentValue($currentValue)
  153. {
  154. $this->currentValue = $currentValue;
  155. return $this;
  156. }
  157. public function SetOnFinalizeCallback(Closure $onFinalizeCallback)
  158. {
  159. $this->onFinalizeCallback = $onFinalizeCallback;
  160. return $this;
  161. }
  162. public function AddValidator(Validator $oValidator)
  163. {
  164. $this->aValidators[] = $oValidator;
  165. return $this;
  166. }
  167. public function RemoveValidator(Validator $oValidator)
  168. {
  169. foreach ($this->aValidators as $iKey => $oValue)
  170. {
  171. if ($oValue === $oValidator)
  172. {
  173. unset($this->aValidators[$iKey]);
  174. }
  175. }
  176. return $this;
  177. }
  178. /**
  179. * Note : Function is protected as aErrorMessages should not be add from outside
  180. *
  181. * @param string $sErrorMessage
  182. * @return \Combodo\iTop\Field\Field
  183. */
  184. protected function AddErrorMessage($sErrorMessage)
  185. {
  186. $this->aErrorMessages[] = $sErrorMessage;
  187. return $this;
  188. }
  189. /**
  190. * Note : Function is protected as aErrorMessages should not be set from outside
  191. *
  192. * @return \Combodo\iTop\Field\Field
  193. */
  194. protected function EmptyErrorMessages()
  195. {
  196. $this->aErrorMessages = array();
  197. return $this;
  198. }
  199. public function OnCancel()
  200. {
  201. // Overload when needed
  202. }
  203. public function OnFinalize()
  204. {
  205. if ($this->onFinalizeCallback !== null)
  206. {
  207. // Note : We MUST have a temp variable to call the Closure. otherwise it won't work when the Closure is a class member
  208. $callback = $this->onFinalizeCallback;
  209. $callback();
  210. }
  211. }
  212. /**
  213. * Checks the validators to see if the field's current value is valid.
  214. * Then sets $bValid and $aErrorMessages.
  215. *
  216. * @return boolean
  217. */
  218. abstract public function Validate();
  219. }