form.class.inc.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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;
  19. use \Exception;
  20. use \Dict;
  21. use \Combodo\iTop\Form\Field\Field;
  22. /**
  23. * Description of Form
  24. *
  25. * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
  26. */
  27. class Form
  28. {
  29. protected $sId;
  30. protected $aFields;
  31. protected $aDependencies;
  32. protected $bValid;
  33. protected $aErrorMessages;
  34. /**
  35. * Default constructor
  36. *
  37. * @param string $sId
  38. */
  39. public function __construct($sId)
  40. {
  41. $this->sId = $sId;
  42. $this->aFields = array();
  43. $this->aDependencies = array();
  44. $this->bValid = true;
  45. $this->aErrorMessages = array();
  46. }
  47. /**
  48. *
  49. * @return string
  50. */
  51. public function GetId()
  52. {
  53. return $this->sId;
  54. }
  55. /**
  56. *
  57. * @return array
  58. */
  59. public function GetFields()
  60. {
  61. return $this->aFields;
  62. }
  63. /**
  64. *
  65. * @return array
  66. */
  67. public function GetDependencies()
  68. {
  69. return $this->aDependencies;
  70. }
  71. /**
  72. * Returns a hash array of "Field id" => "Field value"
  73. *
  74. * @return array
  75. */
  76. public function GetCurrentValues()
  77. {
  78. $aValues = array();
  79. foreach ($this->aFields as $sId => $oField)
  80. {
  81. $aValues[$sId] = $oField->GetCurrentValue();
  82. }
  83. return $aValues;
  84. }
  85. /**
  86. *
  87. * @param array $aValues Must be a hash array of "Field id" => "Field value"
  88. * @return \Combodo\iTop\Form\Form
  89. */
  90. public function SetCurrentValues($aValues)
  91. {
  92. foreach ($aValues as $sId => $value)
  93. {
  94. $oField = $this->GetField($sId);
  95. $oField->SetCurrentValue($value);
  96. }
  97. return $this;
  98. }
  99. /**
  100. * Returns the current validation state of the form (true|false).
  101. * It DOESN'T make the validation, see Validate() instead.
  102. *
  103. * @return boolean
  104. */
  105. public function GetValid()
  106. {
  107. return $this->bValid;
  108. }
  109. /**
  110. * Note : Function is protected as bValid should not be set from outside
  111. *
  112. * @param boolean $bValid
  113. * @return \Combodo\iTop\Form\Form
  114. */
  115. protected function SetValid($bValid)
  116. {
  117. $this->bValid = $bValid;
  118. return $this;
  119. }
  120. /**
  121. *
  122. * @return array
  123. */
  124. public function GetErrorMessages()
  125. {
  126. return $this->aErrorMessages;
  127. }
  128. /**
  129. * Note : Function is protected as aErrorMessages should not be set from outside
  130. *
  131. * @param array $aErrorMessages
  132. * @param string $sFieldId
  133. * @return \Combodo\iTop\Form\Form
  134. */
  135. protected function SetErrorMessages($aErrorMessages, $sFieldId = null)
  136. {
  137. if ($sFieldId === null)
  138. {
  139. $this->aErrorMessages = $aErrorMessages;
  140. }
  141. else
  142. {
  143. $this->aErrorMessages[$sFieldId] = $aErrorMessages;
  144. }
  145. return $this;
  146. }
  147. /**
  148. * If $sFieldId is not set, the $sErrorMessage will be added to the general form messages
  149. *
  150. * Note : Function is protected as aErrorMessages should not be add from outside
  151. *
  152. * @param string $sErrorMessage
  153. * @param string $sFieldId
  154. * @return \Combodo\iTop\Form\Form
  155. */
  156. protected function AddErrorMessage($sErrorMessage, $sFieldId = '_main')
  157. {
  158. if (!isset($this->aErrorMessages[$sFieldId]))
  159. {
  160. $this->aErrorMessages[$sFieldId] = array();
  161. }
  162. $this->aErrorMessages[$sFieldId][] = $sErrorMessage;
  163. return $this;
  164. }
  165. /**
  166. * Note : Function is protected as aErrorMessages should not be set from outside
  167. *
  168. * @return \Combodo\iTop\Form\Form
  169. */
  170. protected function EmptyErrorMessages()
  171. {
  172. $this->aErrorMessages = array();
  173. return $this;
  174. }
  175. /**
  176. *
  177. * @param string $sId
  178. * @return \Combodo\iTop\Form\Field\Field
  179. * @throws Exception
  180. */
  181. public function GetField($sId)
  182. {
  183. if (!array_key_exists($sId, $this->aFields))
  184. {
  185. throw new Exception('Field with ID "' . $sId . '" was not found in the Form.');
  186. }
  187. return $this->aFields[$sId];
  188. }
  189. /**
  190. *
  191. * @param string $sId
  192. * @return boolean
  193. */
  194. public function HasField($sId)
  195. {
  196. return array_key_exists($sId, $this->aFields);
  197. }
  198. /**
  199. *
  200. * @param \Combodo\iTop\Form\Field\Field $oField
  201. * @param array $aDependsOnIds
  202. * @return \Combodo\iTop\Form\Form
  203. */
  204. public function AddField(Field $oField, $aDependsOnIds = array())
  205. {
  206. $oField->SetFormPath($this->sId);
  207. $this->aFields[$oField->GetId()] = $oField;
  208. return $this;
  209. }
  210. /**
  211. *
  212. * @param string $sId
  213. * @return \Combodo\iTop\Form\Form
  214. */
  215. public function RemoveField($sId)
  216. {
  217. if (array_key_exists($sId, $this->aFields))
  218. {
  219. unset($this->aFields[$sId]);
  220. }
  221. return $this;
  222. }
  223. /**
  224. * Returns a array (list) of the fields ordered by their dependencies.
  225. *
  226. * @return array
  227. */
  228. public function GetOrderedFields()
  229. {
  230. // TODO : Do this so it flatten the array
  231. return $this->aFields;
  232. }
  233. /**
  234. * Returns an array of field ids the $sFieldId depends on.
  235. *
  236. * @param string $sFieldId
  237. * @return array
  238. * @throws Exception
  239. */
  240. public function GetFieldDependencies($sFieldId)
  241. {
  242. if (!array_key_exists($sFieldId, $this->aDependencies))
  243. {
  244. throw new Exception('Field with ID "' . $sFieldId . '" had no dependancies declared in the Form.');
  245. }
  246. return $this->aDependencies[$sFieldId];
  247. }
  248. /**
  249. *
  250. * @param string $sFieldId
  251. * @param array $aDependsOnIds
  252. * @return \Combodo\iTop\Form\Form
  253. */
  254. public function AddFieldDependencies($sFieldId, array $aDependsOnIds)
  255. {
  256. foreach ($aDependsOnIds as $sDependsOnId)
  257. {
  258. $this->AddFieldDependency($sFieldId, $sDependsOnId);
  259. }
  260. return $this;
  261. }
  262. /**
  263. *
  264. * @param string $sFieldId
  265. * @param string $sDependsOnId
  266. * @return \Combodo\iTop\Form\Form
  267. */
  268. public function AddFieldDependency($sFieldId, $sDependsOnId)
  269. {
  270. if (!array_key_exists($sFieldId, $this->aDependencies))
  271. {
  272. $this->aDependencies[$sFieldId] = array();
  273. }
  274. $this->aDependencies[$sFieldId][] = $sDependsOnId;
  275. return $this;
  276. }
  277. /**
  278. * Returns a hash array of the fields impacts on other fields. Key being the field that impacts the fields stored in the value as a regular array
  279. * (It kind of reversed the dependencies array)
  280. *
  281. * eg :
  282. * - 'service' => array('subservice', 'template')
  283. * - 'subservice' => array()
  284. * - ...
  285. *
  286. * @return array
  287. */
  288. public function GetFieldsImpacts()
  289. {
  290. $aRes = array();
  291. foreach ($this->aDependencies as $sImpactedFieldId => $aDependentFieldsIds)
  292. {
  293. foreach ($aDependentFieldsIds as $sDependentFieldId)
  294. {
  295. if (!array_key_exists($sDependentFieldId, $aRes))
  296. {
  297. $aRes[$sDependentFieldId] = array();
  298. }
  299. $aRes[$sDependentFieldId][] = $sImpactedFieldId;
  300. }
  301. }
  302. return $aRes;
  303. }
  304. /**
  305. *
  306. */
  307. public function Finalize()
  308. {
  309. //TODO : Call GetOrderedFields
  310. // Must call OnFinalize on each fields, regarding the dependencies order
  311. // On a SubFormField, will call its own Finalize
  312. foreach ($this->aFields as $sId => $oField)
  313. {
  314. $oField->OnFinalize();
  315. }
  316. }
  317. /**
  318. * Validate the form and return if it's valid or not
  319. *
  320. * @return boolean
  321. */
  322. public function Validate()
  323. {
  324. $this->SetValid(true);
  325. $this->EmptyErrorMessages();
  326. foreach ($this->aFields as $oField)
  327. {
  328. if (!$oField->Validate())
  329. {
  330. $this->SetValid(false);
  331. foreach ($oField->GetErrorMessages() as $sErrorMessage)
  332. {
  333. $this->AddErrorMessage(Dict::S($sErrorMessage), $oField->Getid());
  334. }
  335. }
  336. }
  337. return $this->GetValid();
  338. }
  339. }