form.class.inc.php 7.9 KB

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