subformfield.class.inc.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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\Field;
  19. use \Closure;
  20. use \Combodo\iTop\Form\Form;
  21. /**
  22. * Description of SubFormField
  23. *
  24. * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
  25. */
  26. class SubFormField extends Field
  27. {
  28. protected $oForm;
  29. /**
  30. * Default constructor
  31. *
  32. * @param string $sId
  33. * @param string $sParentFormId
  34. * @param Closure $onFinalizeCallback
  35. */
  36. public function __construct($sId, Closure $onFinalizeCallback = null)
  37. {
  38. $this->oForm = new Form('subform_' . $sId);
  39. parent::__construct($sId, $onFinalizeCallback);
  40. }
  41. /**
  42. *
  43. * @return \Combodo\iTop\Form\Form
  44. */
  45. public function GetForm()
  46. {
  47. return $this->oForm;
  48. }
  49. /**
  50. *
  51. * @param \Combodo\iTop\Form\Field\Form $oForm
  52. * @return \Combodo\iTop\Form\Field\SubFormField
  53. */
  54. public function SetForm(Form $oForm)
  55. {
  56. $this->oForm = $oForm;
  57. return $this;
  58. }
  59. /**
  60. * Checks the validators to see if the field's current value is valid.
  61. * Then sets $bValid and $aErrorMessages.
  62. *
  63. * @return boolean
  64. */
  65. public function Validate()
  66. {
  67. return $this->oForm->Validate();
  68. }
  69. /**
  70. *
  71. * @return boolean
  72. */
  73. public function GetValid()
  74. {
  75. return $this->oForm->GetValid();
  76. }
  77. /**
  78. *
  79. * @return array
  80. */
  81. public function GetErrorMessages()
  82. {
  83. $aRet = array();
  84. foreach ($this->oForm->GetErrorMessages() as $sSubFieldId => $aSubFieldMessages)
  85. {
  86. $aRet[] = $sSubFieldId.': '.implode(', ', $aSubFieldMessages);
  87. }
  88. return $aRet;
  89. }
  90. /**
  91. *
  92. * @return array
  93. */
  94. public function GetCurrentValue()
  95. {
  96. return $this->oForm->GetCurrentValues();
  97. }
  98. /**
  99. *
  100. * @param array $value
  101. * @return \Combodo\iTop\Form\Field\SubFormField
  102. */
  103. public function SetCurrentValue($value)
  104. {
  105. $this->oForm->SetCurrentValues($value);
  106. return $this;
  107. }
  108. /**
  109. * Sets the mandatory flag on all the fields on the form
  110. *
  111. * @param boolean $bMandatory
  112. */
  113. public function SetMandatory($bMandatory)
  114. {
  115. foreach ($this->oForm->GetFields() as $oField)
  116. {
  117. $oField->SetMandatory($bMandatory);
  118. }
  119. parent::SetMandatory($bMandatory);
  120. }
  121. /**
  122. * Sets the read-only flag on all the fields on the form
  123. *
  124. * @param boolean $bReadOnly
  125. */
  126. public function SetReadOnly($bReadOnly)
  127. {
  128. foreach ($this->oForm->GetFields() as $oField)
  129. {
  130. $oField->SetReadOnly($bReadOnly);
  131. $oField->SetMandatory(false);
  132. }
  133. parent::SetReadOnly($bReadOnly);
  134. }
  135. /**
  136. * Sets the hidden flag on all the fields on the form
  137. *
  138. * @param boolean $bHidden
  139. */
  140. public function SetHidden($bHidden)
  141. {
  142. foreach ($this->oForm->GetFields() as $oField)
  143. {
  144. $oField->SetHidden($bHidden);
  145. }
  146. parent::SetHidden($bHidden);
  147. }
  148. /**
  149. * @param $sFormPath
  150. * @return Form|null
  151. */
  152. public function FindSubForm($sFormPath)
  153. {
  154. return $this->oForm->FindSubForm($sFormPath);
  155. }
  156. public function OnFinalize()
  157. {
  158. $sFormId = 'subform_' . $this->sId;
  159. if ($this->sFormPath !== null)
  160. {
  161. $sFormId = $this->sFormPath . '-' . $sFormId;
  162. }
  163. $this->oForm->SetId($sFormId);
  164. // Calling first the field callback,
  165. // Then only calling finalize on the subform's fields
  166. parent::OnFinalize();
  167. $this->oForm->Finalize();
  168. }
  169. }