field.class.inc.php 9.3 KB

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