form.class.inc.php 7.1 KB

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