field.class.inc.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 $sGlobalId;
  35. protected $sFormPath;
  36. protected $sLabel;
  37. protected $bReadOnly;
  38. protected $bMandatory;
  39. protected $aValidators;
  40. protected $bValid;
  41. protected $aErrorMessages;
  42. protected $currentValue;
  43. protected $onFinalizeCallback;
  44. /**
  45. *
  46. * @param Closure $callback (Used in the $oForm->AddField($sId, ..., function() use ($oManager, $oForm, '...') { ... } ); )
  47. */
  48. public function __construct($sId, Closure $onFinalizeCallback = null)
  49. {
  50. $this->sId = $sId;
  51. $this->sGlobalId = 'field_'.$sId.uniqid();
  52. $this->sLabel = static::DEFAULT_LABEL;
  53. $this->bReadOnly = static::DEFAULT_READ_ONLY;
  54. $this->bMandatory = static::DEFAULT_MANDATORY;
  55. $this->aValidators = array();
  56. $this->bValid = static::DEFAULT_VALID;
  57. $this->aErrorMessages = array();
  58. $this->onFinalizeCallback = $onFinalizeCallback;
  59. }
  60. /**
  61. * Get the field id within its container form
  62. * @return string
  63. */
  64. public function GetId()
  65. {
  66. return $this->sId;
  67. }
  68. /**
  69. * Get a unique field id within the top level form
  70. * @return string
  71. */
  72. public function GetGlobalId()
  73. {
  74. return $this->sGlobalId;
  75. }
  76. /**
  77. * Get the id of the container form
  78. * @return string
  79. */
  80. public function GetFormPath()
  81. {
  82. return $this->sFormPath;
  83. }
  84. public function GetLabel()
  85. {
  86. return $this->sLabel;
  87. }
  88. public function GetReadOnly()
  89. {
  90. return $this->bReadOnly;
  91. }
  92. public function GetMandatory()
  93. {
  94. return $this->bMandatory;
  95. }
  96. public function GetValidators()
  97. {
  98. return $this->aValidators;
  99. }
  100. /**
  101. * Returns the current validation state of the field (true|false).
  102. * It DOESN'T make the validation, see Validate() instead.
  103. *
  104. * @return boolean
  105. */
  106. public function GetValid()
  107. {
  108. return $this->bValid;
  109. }
  110. public function GetErrorMessages()
  111. {
  112. return $this->aErrorMessages;
  113. }
  114. public function GetCurrentValue()
  115. {
  116. return $this->currentValue;
  117. }
  118. public function SetLabel($sLabel)
  119. {
  120. $this->sLabel = $sLabel;
  121. return $this;
  122. }
  123. public function SetReadOnly($bReadOnly)
  124. {
  125. $this->bReadOnly = $bReadOnly;
  126. return $this;
  127. }
  128. public function SetMandatory($bMandatory)
  129. {
  130. // Before changing the property, we check if it was already mandatory. If not, we had the mandatory validator
  131. if ($bMandatory && !$this->bMandatory)
  132. {
  133. $this->AddValidator(new MandatoryValidator());
  134. }
  135. if (!$bMandatory)
  136. {
  137. foreach ($this->aValidators as $iKey => $oValue)
  138. {
  139. if ($oValue::Getname() === MandatoryValidator::GetName())
  140. {
  141. unset($this->aValidators[$iKey]);
  142. }
  143. }
  144. }
  145. $this->bMandatory = $bMandatory;
  146. return $this;
  147. }
  148. public function SetValidators($aValidators)
  149. {
  150. $this->aValidators = $aValidators;
  151. return $this;
  152. }
  153. /**
  154. * Note : Function is protected as bValid should not be set from outside
  155. *
  156. * @param boolean $bValid
  157. * @return \Combodo\iTop\Form\Field\Field
  158. */
  159. protected function SetValid($bValid)
  160. {
  161. $this->bValid = $bValid;
  162. return $this;
  163. }
  164. /**
  165. * Note : Function is protected as aErrorMessages should not be set from outside
  166. *
  167. * @param array $aErrorMessages
  168. * @return \Combodo\iTop\Form\Field\Field
  169. */
  170. protected function SetErrorMessages($aErrorMessages)
  171. {
  172. $this->aErrorMessages = $aErrorMessages;
  173. return $this;
  174. }
  175. public function SetCurrentValue($currentValue)
  176. {
  177. $this->currentValue = $currentValue;
  178. return $this;
  179. }
  180. public function SetOnFinalizeCallback(Closure $onFinalizeCallback)
  181. {
  182. $this->onFinalizeCallback = $onFinalizeCallback;
  183. return $this;
  184. }
  185. /**
  186. * Called by the form when adding the field
  187. */
  188. public function SetFormPath($sFormPath)
  189. {
  190. $this->sFormPath = $sFormPath;
  191. }
  192. public function AddValidator(Validator $oValidator)
  193. {
  194. $this->aValidators[] = $oValidator;
  195. return $this;
  196. }
  197. public function RemoveValidator(Validator $oValidator)
  198. {
  199. foreach ($this->aValidators as $iKey => $oValue)
  200. {
  201. if ($oValue === $oValidator)
  202. {
  203. unset($this->aValidators[$iKey]);
  204. }
  205. }
  206. return $this;
  207. }
  208. /**
  209. * Note : Function is protected as aErrorMessages should not be add from outside
  210. *
  211. * @param string $sErrorMessage
  212. * @return \Combodo\iTop\Form\Field\Field
  213. */
  214. protected function AddErrorMessage($sErrorMessage)
  215. {
  216. $this->aErrorMessages[] = $sErrorMessage;
  217. return $this;
  218. }
  219. /**
  220. * Note : Function is protected as aErrorMessages should not be set from outside
  221. *
  222. * @return \Combodo\iTop\Form\Field\Field
  223. */
  224. protected function EmptyErrorMessages()
  225. {
  226. $this->aErrorMessages = array();
  227. return $this;
  228. }
  229. public function OnCancel()
  230. {
  231. // Overload when needed
  232. }
  233. public function OnFinalize()
  234. {
  235. if ($this->onFinalizeCallback !== null)
  236. {
  237. // Note : We MUST have a temp variable to call the Closure. otherwise it won't work when the Closure is a class member
  238. $callback = $this->onFinalizeCallback;
  239. $callback($this);
  240. }
  241. }
  242. /**
  243. * Checks the validators to see if the field's current value is valid.
  244. * Then sets $bValid and $aErrorMessages.
  245. *
  246. * @return boolean
  247. */
  248. abstract public function Validate();
  249. }