BaseSection.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\Sections;
  3. use \ModulesGarden\ProxmoxAddon\Core\UI\Builder\BaseContainer;
  4. /**
  5. * Base Form Section controler
  6. *
  7. * @author Sławomir Miśkowicz <slawomir@modulesgarden.com>
  8. */
  9. class BaseSection extends BaseContainer
  10. {
  11. use \ModulesGarden\ProxmoxAddon\Core\UI\Traits\Fields;
  12. use \ModulesGarden\ProxmoxAddon\Core\UI\Traits\Sections;
  13. use \ModulesGarden\ProxmoxAddon\Core\UI\Traits\Buttons;
  14. use \ModulesGarden\ProxmoxAddon\Core\UI\Traits\Section;
  15. protected $id = 'baseSection';
  16. protected $name = 'baseSection';
  17. public function loadDataToForm(&$dataProvider)
  18. {
  19. foreach ($this->fields as &$field)
  20. {
  21. $field->setValue($dataProvider->getValueById($field->getId()));
  22. $avValues = $dataProvider->getAvailableValuesById($field->getId());
  23. if ($avValues && method_exists($field, 'setAvailableValues'))
  24. {
  25. $field->setAvailableValues($avValues);
  26. }
  27. }
  28. foreach ($this->sections as &$section)
  29. {
  30. $section->loadDataToForm($dataProvider);
  31. }
  32. }
  33. public function loadDataToFormByName(&$dataProvider)
  34. {
  35. foreach ($this->fields as &$field)
  36. {
  37. $field->setValue($dataProvider->getValueByName($field->getName()));
  38. if ($dataProvider->isDisabledById($field->getId()))
  39. {
  40. $field->disableField();
  41. }
  42. }
  43. foreach ($this->sections as &$section)
  44. {
  45. $section->loadDataToFormByName($dataProvider);
  46. }
  47. }
  48. /**
  49. * Adds field object to field list
  50. * @return $this
  51. */
  52. public function addField($field)
  53. {
  54. if ($this->groupFieldsBySectionName === true)
  55. {
  56. $field->setName($this->name . '[' . $field->getName() . ']');
  57. $field->setId($this->name . '[' . $field->getId() . ']');
  58. }
  59. $this->fields[$field->getId()] = $field;
  60. if ($field->isAjaxComponent())
  61. {
  62. $this->mainContainer->addAjaxElement($field);
  63. }
  64. if ($field->isVueComponent())
  65. {
  66. $this->mainContainer->addVueComponent($field);
  67. }
  68. return $this;
  69. }
  70. }