field.class.inc.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. * Default constructor
  46. *
  47. * @param string $sId
  48. * @param Closure $onFinalizeCallback (Used in the $oForm->AddField($sId, ..., function() use ($oManager, $oForm, '...') { ... } ); )
  49. */
  50. public function __construct($sId, Closure $onFinalizeCallback = null)
  51. {
  52. $this->sId = $sId;
  53. $this->sGlobalId = 'field_' . $sId . '_' . uniqid();
  54. $this->sLabel = static::DEFAULT_LABEL;
  55. $this->bReadOnly = static::DEFAULT_READ_ONLY;
  56. $this->bMandatory = static::DEFAULT_MANDATORY;
  57. $this->aValidators = array();
  58. $this->bValid = static::DEFAULT_VALID;
  59. $this->aErrorMessages = array();
  60. $this->onFinalizeCallback = $onFinalizeCallback;
  61. }
  62. /**
  63. * Returns the field id within its container form
  64. *
  65. * @return string
  66. */
  67. public function GetId()
  68. {
  69. return $this->sId;
  70. }
  71. /**
  72. * Returns a unique field id within the top level form
  73. *
  74. * @return string
  75. */
  76. public function GetGlobalId()
  77. {
  78. return $this->sGlobalId;
  79. }
  80. /**
  81. * Returns the id of the container form
  82. *
  83. * @return string
  84. */
  85. public function GetFormPath()
  86. {
  87. return $this->sFormPath;
  88. }
  89. /**
  90. *
  91. * @return string
  92. */
  93. public function GetLabel()
  94. {
  95. return $this->sLabel;
  96. }
  97. /**
  98. *
  99. * @return boolean
  100. */
  101. public function GetReadOnly()
  102. {
  103. return $this->bReadOnly;
  104. }
  105. /**
  106. *
  107. * @return boolean
  108. */
  109. public function GetMandatory()
  110. {
  111. return $this->bMandatory;
  112. }
  113. /**
  114. *
  115. * @return array
  116. */
  117. public function GetValidators()
  118. {
  119. return $this->aValidators;
  120. }
  121. /**
  122. * Returns the current validation state of the field (true|false).
  123. * It DOESN'T make the validation, see Validate() instead.
  124. *
  125. * @return boolean
  126. */
  127. public function GetValid()
  128. {
  129. return $this->bValid;
  130. }
  131. /**
  132. *
  133. * @return array
  134. */
  135. public function GetErrorMessages()
  136. {
  137. return $this->aErrorMessages;
  138. }
  139. /**
  140. *
  141. * @return array
  142. */
  143. public function GetCurrentValue()
  144. {
  145. return $this->currentValue;
  146. }
  147. /**
  148. * Sets the field formpath
  149. * Usually Called by the form when adding the field
  150. *
  151. * @param string $sFormPath
  152. * @return \Combodo\iTop\Form\Field\Field
  153. */
  154. public function SetFormPath($sFormPath)
  155. {
  156. $this->sFormPath = $sFormPath;
  157. return $this;
  158. }
  159. /**
  160. *
  161. * @param type $sLabel
  162. * @return \Combodo\iTop\Form\Field\Field
  163. */
  164. public function SetLabel($sLabel)
  165. {
  166. $this->sLabel = $sLabel;
  167. return $this;
  168. }
  169. /**
  170. *
  171. * @param boolean $bReadOnly
  172. * @return \Combodo\iTop\Form\Field\Field
  173. */
  174. public function SetReadOnly($bReadOnly)
  175. {
  176. $this->bReadOnly = $bReadOnly;
  177. return $this;
  178. }
  179. /**
  180. * Sets if the field is mandatory or not.
  181. * Setting the value will automatically add/remove a MandatoryValidator to the Field
  182. *
  183. * @param boolean $bMandatory
  184. * @return \Combodo\iTop\Form\Field\Field
  185. */
  186. public function SetMandatory($bMandatory)
  187. {
  188. // Before changing the property, we check if it was already mandatory. If not, we had the mandatory validator
  189. if ($bMandatory && !$this->bMandatory)
  190. {
  191. $this->AddValidator(new MandatoryValidator());
  192. }
  193. if (!$bMandatory)
  194. {
  195. foreach ($this->aValidators as $iKey => $oValue)
  196. {
  197. if ($oValue::Getname() === MandatoryValidator::GetName())
  198. {
  199. unset($this->aValidators[$iKey]);
  200. }
  201. }
  202. }
  203. $this->bMandatory = $bMandatory;
  204. return $this;
  205. }
  206. /**
  207. *
  208. * @param array $aValidators
  209. * @return \Combodo\iTop\Form\Field\Field
  210. */
  211. public function SetValidators($aValidators)
  212. {
  213. $this->aValidators = $aValidators;
  214. return $this;
  215. }
  216. /**
  217. * Note : Function is protected as bValid should not be set from outside
  218. *
  219. * @param boolean $bValid
  220. * @return \Combodo\iTop\Form\Field\Field
  221. */
  222. protected function SetValid($bValid)
  223. {
  224. $this->bValid = $bValid;
  225. return $this;
  226. }
  227. /**
  228. * Note : Function is protected as aErrorMessages should not be set from outside
  229. *
  230. * @param array $aErrorMessages
  231. * @return \Combodo\iTop\Form\Field\Field
  232. */
  233. protected function SetErrorMessages($aErrorMessages)
  234. {
  235. $this->aErrorMessages = $aErrorMessages;
  236. return $this;
  237. }
  238. /**
  239. *
  240. * @param mixed $currentValue
  241. * @return \Combodo\iTop\Form\Field\Field
  242. */
  243. public function SetCurrentValue($currentValue)
  244. {
  245. $this->currentValue = $currentValue;
  246. return $this;
  247. }
  248. /**
  249. *
  250. * @param Closure $onFinalizeCallback
  251. * @return \Combodo\iTop\Form\Field\Field
  252. */
  253. public function SetOnFinalizeCallback(Closure $onFinalizeCallback)
  254. {
  255. $this->onFinalizeCallback = $onFinalizeCallback;
  256. return $this;
  257. }
  258. /**
  259. *
  260. * @param Validator $oValidator
  261. * @return \Combodo\iTop\Form\Field\Field
  262. */
  263. public function AddValidator(Validator $oValidator)
  264. {
  265. $this->aValidators[] = $oValidator;
  266. return $this;
  267. }
  268. /**
  269. *
  270. * @param Validator $oValidator
  271. * @return \Combodo\iTop\Form\Field\Field
  272. */
  273. public function RemoveValidator(Validator $oValidator)
  274. {
  275. foreach ($this->aValidators as $iKey => $oValue)
  276. {
  277. if ($oValue === $oValidator)
  278. {
  279. unset($this->aValidators[$iKey]);
  280. }
  281. }
  282. return $this;
  283. }
  284. /**
  285. * Note : Function is protected as aErrorMessages should not be add from outside
  286. *
  287. * @param string $sErrorMessage
  288. * @return \Combodo\iTop\Form\Field\Field
  289. */
  290. protected function AddErrorMessage($sErrorMessage)
  291. {
  292. $this->aErrorMessages[] = $sErrorMessage;
  293. return $this;
  294. }
  295. /**
  296. * Note : Function is protected as aErrorMessages should not be set from outside
  297. *
  298. * @return \Combodo\iTop\Form\Field\Field
  299. */
  300. protected function EmptyErrorMessages()
  301. {
  302. $this->aErrorMessages = array();
  303. return $this;
  304. }
  305. public function OnCancel()
  306. {
  307. // Overload when needed
  308. }
  309. public function OnFinalize()
  310. {
  311. if ($this->onFinalizeCallback !== null)
  312. {
  313. // Note : We MUST have a temp variable to call the Closure. otherwise it won't work when the Closure is a class member
  314. $callback = $this->onFinalizeCallback;
  315. $callback($this);
  316. }
  317. }
  318. /**
  319. * Checks the validators to see if the field's current value is valid.
  320. * Then sets $bValid and $aErrorMessages.
  321. *
  322. * @return boolean
  323. */
  324. abstract public function Validate();
  325. }