field.class.inc.php 8.3 KB

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