form.class.inc.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. public function __construct($sId)
  35. {
  36. $this->sId = $sId;
  37. $this->aFields = array();
  38. $this->aDependencies = array();
  39. $this->bValid = true;
  40. $this->aErrorMessages = array();
  41. }
  42. public function GetId()
  43. {
  44. return $this->sId;
  45. }
  46. public function GetFields()
  47. {
  48. return $this->aFields;
  49. }
  50. public function GetDependencies()
  51. {
  52. return $this->aDependencies;
  53. }
  54. public function GetCurrentValues()
  55. {
  56. $aValues = array();
  57. foreach ($this->aFields as $sId => $oField)
  58. {
  59. $aValues[$sId] = $oField->GetCurrentValue();
  60. }
  61. return $aValues;
  62. }
  63. public function SetCurrentValues($aValues)
  64. {
  65. foreach ($aValues as $sId => $value)
  66. {
  67. $oField = $this->GetField($sId);
  68. $oField->SetCurrentValue($value);
  69. }
  70. }
  71. /**
  72. * Returns the current validation state of the form (true|false).
  73. * It DOESN'T make the validation, see Validate() instead.
  74. *
  75. * @return boolean
  76. */
  77. public function GetValid()
  78. {
  79. return $this->bValid;
  80. }
  81. /**
  82. * Note : Function is protected as bValid should not be set from outside
  83. *
  84. * @param boolean $bValid
  85. * @return \Combodo\iTop\Form\Form
  86. */
  87. protected function SetValid($bValid)
  88. {
  89. $this->bValid = $bValid;
  90. return $this;
  91. }
  92. public function GetErrorMessages()
  93. {
  94. return $this->aErrorMessages;
  95. }
  96. /**
  97. * Note : Function is protected as aErrorMessages should not be set from outside
  98. *
  99. * @param array $aErrorMessages
  100. * @param string $sFieldId
  101. * @return \Combodo\iTop\Form\Form
  102. */
  103. protected function SetErrorMessages($aErrorMessages, $sFieldId = null)
  104. {
  105. if ($sFieldId === null)
  106. {
  107. $this->aErrorMessages = $aErrorMessages;
  108. }
  109. else
  110. {
  111. $this->aErrorMessages[$sFieldId] = $aErrorMessages;
  112. }
  113. return $this;
  114. }
  115. /**
  116. * If $sFieldId is not set, the $sErrorMessage will be added to the general form messages
  117. *
  118. * Note : Function is protected as aErrorMessages should not be add from outside
  119. *
  120. * @param string $sErrorMessage
  121. * @param string $sFieldId
  122. * @return \Combodo\iTop\Form\Form
  123. */
  124. protected function AddErrorMessage($sErrorMessage, $sFieldId = '_main')
  125. {
  126. if (!isset($this->aErrorMessages[$sFieldId]))
  127. {
  128. $this->aErrorMessages[$sFieldId] = array();
  129. }
  130. $this->aErrorMessages[$sFieldId][] = $sErrorMessage;
  131. return $this;
  132. }
  133. /**
  134. * Note : Function is protected as aErrorMessages should not be set from outside
  135. *
  136. * @return \Combodo\iTop\Form\Form
  137. */
  138. protected function EmptyErrorMessages()
  139. {
  140. $this->aErrorMessages = array();
  141. return $this;
  142. }
  143. public function GetField($sId)
  144. {
  145. if (!array_key_exists($sId, $this->aFields))
  146. {
  147. throw new Exception('Field with ID "' . $sId . '" was not found in the Form.');
  148. }
  149. return $this->aFields[$sId];
  150. }
  151. public function HasField($sId)
  152. {
  153. return array_key_exists($sId, $this->aFields);
  154. }
  155. public function AddField(Field $oField, $aDependsOnIds = array())
  156. {
  157. $oField->SetFormPath($this->sId);
  158. $this->aFields[$oField->GetId()] = $oField;
  159. return $this;
  160. }
  161. public function RemoveField($sId)
  162. {
  163. if (array_key_exists($sId, $this->aFields))
  164. {
  165. unset($this->aFields[$sId]);
  166. }
  167. return $this;
  168. }
  169. /**
  170. * Returns a array (list) of the fields ordered by their dependencies.
  171. *
  172. * @return array
  173. */
  174. public function GetOrderedFields()
  175. {
  176. // TODO : Do this so it flatten the array
  177. return $this->aFields;
  178. }
  179. /**
  180. * Returns an array of field ids the $sFieldId depends on.
  181. *
  182. * @param string $sFieldId
  183. * @return array
  184. * @throws Exception
  185. */
  186. public function GetFieldDependencies($sFieldId)
  187. {
  188. if (!array_key_exists($sFieldId, $this->aDependencies))
  189. {
  190. throw new Exception('Field with ID "' . $sFieldId . '" had no dependancies declared in the Form.');
  191. }
  192. return $this->aDependencies[$sFieldId];
  193. }
  194. public function AddFieldDependencies($sFieldId, array $aDependsOnIds)
  195. {
  196. foreach ($aDependsOnIds as $sDependsOnId)
  197. {
  198. $this->AddFieldDependency($sFieldId, $sDependsOnId);
  199. }
  200. return $this;
  201. }
  202. public function AddFieldDependency($sFieldId, $sDependsOnId)
  203. {
  204. if (!array_key_exists($sFieldId, $this->aDependencies))
  205. {
  206. $this->aDependencies[$sFieldId] = array();
  207. }
  208. $this->aDependencies[$sFieldId][] = $sDependsOnId;
  209. return $this;
  210. }
  211. /**
  212. * 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
  213. * (It kind of reversed the dependencies array)
  214. *
  215. * eg :
  216. * - 'service' => array('subservice', 'template')
  217. * - 'subservice' => array()
  218. * - ...
  219. *
  220. * @return array
  221. */
  222. public function GetFieldsImpacts()
  223. {
  224. $aRes = array();
  225. foreach ($this->aDependencies as $sImpactedFieldId => $aDependentFieldsIds)
  226. {
  227. foreach ($aDependentFieldsIds as $sDependentFieldId)
  228. {
  229. if (!array_key_exists($sDependentFieldId, $aRes))
  230. {
  231. $aRes[$sDependentFieldId] = array();
  232. }
  233. $aRes[$sDependentFieldId][] = $sImpactedFieldId;
  234. }
  235. }
  236. return $aRes;
  237. }
  238. public function Finalize()
  239. {
  240. //TODO : Call GetOrderedFields
  241. // Must call OnFinalize on each fields, regarding the dependencies order
  242. // On a SubFormField, will call its own Finalize
  243. foreach ($this->aFields as $sId => $oField)
  244. {
  245. $oField->OnFinalize();
  246. }
  247. }
  248. public function Validate()
  249. {
  250. $this->SetValid(true);
  251. $this->EmptyErrorMessages();
  252. foreach ($this->aFields as $oField)
  253. {
  254. if (!$oField->Validate())
  255. {
  256. $this->SetValid(false);
  257. foreach ($oField->GetErrorMessages() as $sErrorMessage)
  258. {
  259. $this->AddErrorMessage(Dict::S($sErrorMessage), $oField->Getid());
  260. }
  261. }
  262. }
  263. return $this->GetValid();
  264. }
  265. }