bssubformfieldrenderer.class.inc.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. // Copyright (C) 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\Renderer\Bootstrap\FieldRenderer;
  19. use \Dict;
  20. use Combodo\iTop\Renderer\Bootstrap\BsFormRenderer;
  21. use Combodo\iTop\Renderer\FieldRenderer;
  22. use Combodo\iTop\Renderer\RenderingOutput;
  23. class BsSubFormFieldRenderer extends FieldRenderer
  24. {
  25. public function Render()
  26. {
  27. $oOutput = new RenderingOutput();
  28. // Checking if subform has visible fields
  29. $bHasVisibleFields = false;
  30. foreach ($this->oField->GetForm()->GetFields() as $oSubFormField)
  31. {
  32. $sSubFormFieldClass = get_class($oSubFormField);
  33. // Note : This is a dirty hack for templates. As they show a label when there is no template, we have to detect it...
  34. if (($sSubFormFieldClass !== 'Combodo\iTop\Form\Field\HiddenField') && ($oSubFormField->GetId() !== '_no_template_'))
  35. {
  36. $bHasVisibleFields = true;
  37. }
  38. }
  39. // Showing subform if there are visible fields
  40. if (!$bHasVisibleFields)
  41. {
  42. $oOutput->AddHtml('<div class="hidden">');
  43. }
  44. if (($this->oField->GetLabel() !== null) && ($this->oField->GetLabel() !== ''))
  45. {
  46. $oOutput->AddHtml('<fieldset><legend>' . $this->oField->GetLabel() . '</legend>');
  47. }
  48. $oOutput->AddHtml('<div id="fieldset_' . $this->oField->GetGlobalId() . '">');
  49. $oOutput->AddHtml('</div>');
  50. if (($this->oField->GetLabel() !== null) && ($this->oField->GetLabel() !== ''))
  51. {
  52. $oOutput->AddHtml('</fieldset>');
  53. }
  54. if (!$bHasVisibleFields)
  55. {
  56. $oOutput->AddHtml('</div>');
  57. }
  58. $oRenderer = new BsFormRenderer($this->oField->GetForm());
  59. $aRenderRes = $oRenderer->Render();
  60. $aFieldSetOptions = array(
  61. 'fields_list' => $aRenderRes,
  62. 'fields_impacts' => $this->oField->GetForm()->GetFieldsImpacts(),
  63. 'form_path' => $this->oField->GetForm()->GetId()
  64. );
  65. $sFieldSetOptions = json_encode($aFieldSetOptions);
  66. $oOutput->AddJs(
  67. <<<EOF
  68. $("#fieldset_{$this->oField->GetGlobalId()}").field_set($sFieldSetOptions);
  69. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").subform_field({field_set: $("#fieldset_{$this->oField->GetGlobalId()}")});
  70. EOF
  71. );
  72. return $oOutput;
  73. }
  74. }