field.class.inc.php 7.9 KB

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