field.class.inc.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. public function GetDisplayValue()
  160. {
  161. return $this->currentValue;
  162. }
  163. /**
  164. * Sets the field formpath
  165. * Usually Called by the form when adding the field
  166. *
  167. * @param string $sFormPath
  168. * @return \Combodo\iTop\Form\Field\Field
  169. */
  170. public function SetFormPath($sFormPath)
  171. {
  172. $this->sFormPath = $sFormPath;
  173. return $this;
  174. }
  175. /**
  176. *
  177. * @param type $sLabel
  178. * @return \Combodo\iTop\Form\Field\Field
  179. */
  180. public function SetLabel($sLabel)
  181. {
  182. $this->sLabel = $sLabel;
  183. return $this;
  184. }
  185. /**
  186. *
  187. * @param boolean $bHidden
  188. * @return \Combodo\iTop\Form\Field\Field
  189. */
  190. public function SetHidden($bHidden)
  191. {
  192. $this->bHidden = $bHidden;
  193. return $this;
  194. }
  195. /**
  196. *
  197. * @param boolean $bReadOnly
  198. * @return \Combodo\iTop\Form\Field\Field
  199. */
  200. public function SetReadOnly($bReadOnly)
  201. {
  202. $this->bReadOnly = $bReadOnly;
  203. return $this;
  204. }
  205. /**
  206. * Sets if the field is mandatory or not.
  207. * Setting the value will automatically add/remove a MandatoryValidator to the Field
  208. *
  209. * @param boolean $bMandatory
  210. * @return \Combodo\iTop\Form\Field\Field
  211. */
  212. public function SetMandatory($bMandatory)
  213. {
  214. // Before changing the property, we check if it was already mandatory. If not, we had the mandatory validator
  215. if ($bMandatory && !$this->bMandatory)
  216. {
  217. $this->AddValidator(new MandatoryValidator());
  218. }
  219. if (!$bMandatory)
  220. {
  221. foreach ($this->aValidators as $iKey => $oValue)
  222. {
  223. if ($oValue::Getname() === MandatoryValidator::GetName())
  224. {
  225. unset($this->aValidators[$iKey]);
  226. }
  227. }
  228. }
  229. $this->bMandatory = $bMandatory;
  230. return $this;
  231. }
  232. /**
  233. *
  234. * @param array $aValidators
  235. * @return \Combodo\iTop\Form\Field\Field
  236. */
  237. public function SetValidators($aValidators)
  238. {
  239. $this->aValidators = $aValidators;
  240. return $this;
  241. }
  242. /**
  243. * Note : Function is protected as bValid should not be set from outside
  244. *
  245. * @param boolean $bValid
  246. * @return \Combodo\iTop\Form\Field\Field
  247. */
  248. protected function SetValid($bValid)
  249. {
  250. $this->bValid = $bValid;
  251. return $this;
  252. }
  253. /**
  254. * Note : Function is protected as aErrorMessages should not be set from outside
  255. *
  256. * @param array $aErrorMessages
  257. * @return \Combodo\iTop\Form\Field\Field
  258. */
  259. protected function SetErrorMessages($aErrorMessages)
  260. {
  261. $this->aErrorMessages = $aErrorMessages;
  262. return $this;
  263. }
  264. /**
  265. *
  266. * @param mixed $currentValue
  267. * @return \Combodo\iTop\Form\Field\Field
  268. */
  269. public function SetCurrentValue($currentValue)
  270. {
  271. $this->currentValue = $currentValue;
  272. return $this;
  273. }
  274. /**
  275. *
  276. * @param Closure $onFinalizeCallback
  277. * @return \Combodo\iTop\Form\Field\Field
  278. */
  279. public function SetOnFinalizeCallback(Closure $onFinalizeCallback)
  280. {
  281. $this->onFinalizeCallback = $onFinalizeCallback;
  282. return $this;
  283. }
  284. /**
  285. *
  286. * @param Validator $oValidator
  287. * @return \Combodo\iTop\Form\Field\Field
  288. */
  289. public function AddValidator(Validator $oValidator)
  290. {
  291. $this->aValidators[] = $oValidator;
  292. return $this;
  293. }
  294. /**
  295. *
  296. * @param Validator $oValidator
  297. * @return \Combodo\iTop\Form\Field\Field
  298. */
  299. public function RemoveValidator(Validator $oValidator)
  300. {
  301. foreach ($this->aValidators as $iKey => $oValue)
  302. {
  303. if ($oValue === $oValidator)
  304. {
  305. unset($this->aValidators[$iKey]);
  306. }
  307. }
  308. return $this;
  309. }
  310. /**
  311. * Note : Function is protected as aErrorMessages should not be add from outside
  312. *
  313. * @param string $sErrorMessage
  314. * @return \Combodo\iTop\Form\Field\Field
  315. */
  316. protected function AddErrorMessage($sErrorMessage)
  317. {
  318. $this->aErrorMessages[] = $sErrorMessage;
  319. return $this;
  320. }
  321. /**
  322. * Note : Function is protected as aErrorMessages should not be set from outside
  323. *
  324. * @return \Combodo\iTop\Form\Field\Field
  325. */
  326. protected function EmptyErrorMessages()
  327. {
  328. $this->aErrorMessages = array();
  329. return $this;
  330. }
  331. /**
  332. * Returns if the field is editable. Meaning that it is not editable nor hidden.
  333. *
  334. * @return boolean
  335. */
  336. public function IsEditable()
  337. {
  338. return (!$this->bReadOnly && !$this->bHidden);
  339. }
  340. public function OnCancel()
  341. {
  342. // Overload when needed
  343. }
  344. public function OnFinalize()
  345. {
  346. if ($this->onFinalizeCallback !== null)
  347. {
  348. // Note : We MUST have a temp variable to call the Closure. otherwise it won't work when the Closure is a class member
  349. $callback = $this->onFinalizeCallback;
  350. $callback($this);
  351. }
  352. }
  353. /**
  354. * Checks the validators to see if the field's current value is valid.
  355. * Then sets $bValid and $aErrorMessages.
  356. *
  357. * @return boolean
  358. */
  359. public function Validate()
  360. {
  361. $this->SetValid(true);
  362. $this->EmptyErrorMessages();
  363. $bEmpty = ( ($this->GetCurrentValue() === null) || ($this->GetCurrentValue() === '') );
  364. if (!$bEmpty || $this->GetMandatory())
  365. {
  366. foreach ($this->GetValidators() as $oValidator)
  367. {
  368. if (!preg_match($oValidator->GetRegExp(true), $this->GetCurrentValue()))
  369. {
  370. $this->SetValid(false);
  371. $this->AddErrorMessage($oValidator->GetErrorMessage());
  372. }
  373. }
  374. }
  375. return $this->GetValid();
  376. }
  377. }