form.class.inc.php 8.7 KB

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