ContainerElements.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\UI\Traits;
  3. use \ModulesGarden\ProxmoxAddon\Core\DependencyInjection\DependencyInjection;
  4. use \ModulesGarden\ProxmoxAddon\Core\ServiceLocator;
  5. /**
  6. * Container Elements related functions
  7. * Base Container Trait
  8. *
  9. * @author Sławomir Miśkowicz <slawomir@modulesgarden.com>
  10. */
  11. trait ContainerElements
  12. {
  13. /**
  14. * a default container name for all elements in UI components
  15. * @var type string
  16. */
  17. protected $defaultContainer = 'elements';
  18. /*
  19. * a list of allowed containers, it this way in order to prevent
  20. * an accidental overwrite other variables
  21. */
  22. protected $elementsContainers = ['elements'];
  23. /**
  24. * an array of elements for every UI component
  25. * @var type array
  26. */
  27. protected $elements = [];
  28. public function addElement($element, $containerName = null)
  29. {
  30. $element = $this->prepareElementInstance($element);
  31. $this->checkElementItemContext();
  32. $this->addElementToContainer($element, $containerName);
  33. return $this;
  34. }
  35. protected function prepareElementInstance($element)
  36. {
  37. if (is_string($element))
  38. {
  39. $element = DependencyInjection::create($element);
  40. }
  41. $element->setIndex(ServiceLocator::call('request')->get('index'));
  42. return $element;
  43. }
  44. protected function checkElementItemContext()
  45. {
  46. if ($this->isIdEqual($this->getRequestValue('loadData')))
  47. {
  48. self::$findItemContext = true;
  49. }
  50. }
  51. /**
  52. * do not use this function directly, addElement function is the only proper way
  53. */
  54. protected function addElementToContainer($element, $containerName = null)
  55. {
  56. /**
  57. * unique element id
  58. */
  59. $id = $element->getId();
  60. $container = $this->getElementContainerName($containerName);
  61. if (!$container)
  62. {
  63. return null;
  64. }
  65. if (!isset($this->{$container}[$id]))
  66. {
  67. $this->{$container}[$id] = $element;
  68. $this->registerMainContainerAdditions($this->{$container}[$id]);
  69. }
  70. }
  71. protected function registerMainContainerAdditions(&$element)
  72. {
  73. $element->setMainContainer($this->mainContainer);
  74. if ($element instanceof \ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AjaxElementInterface)
  75. {
  76. $this->mainContainer->addAjaxElement($element);
  77. }
  78. if ($element->isVueComponent())
  79. {
  80. $this->mainContainer->addVueComponent($element);
  81. }
  82. }
  83. /**
  84. * determines property name where the element will be added
  85. * @param type $containerName
  86. * @return type null|string
  87. */
  88. public function getElementContainerName($containerName = null)
  89. {
  90. /**
  91. * if not set use default container
  92. */
  93. if ($containerName === null)
  94. {
  95. return $this->defaultContainer;
  96. }
  97. /**
  98. * if invalid value provided
  99. */
  100. if (!$containerName || !is_string($containerName) || $containerName === '')
  101. {
  102. return null;
  103. }
  104. /**
  105. * container does not exists, or is not on the allowed list
  106. */
  107. if (!property_exists($this, $containerName) || !in_array($containerName, $this->elementsContainers))
  108. {
  109. return null;
  110. }
  111. /**
  112. * container is not an array
  113. */
  114. if (!is_array($this->{$containerName}))
  115. {
  116. return null;
  117. }
  118. return $containerName;
  119. }
  120. public function addNewElementsContainer($containerName = null)
  121. {
  122. if (!is_string($containerName) || $containerName === '')
  123. {
  124. return $this;
  125. }
  126. if (!in_array($containerName, $this->elementsContainers))
  127. {
  128. $this->elementsContainers[] = $containerName;
  129. }
  130. return $this;
  131. }
  132. /**
  133. * @return array
  134. */
  135. public function getElements($containerName = null)
  136. {
  137. if (!$containerName)
  138. {
  139. return $this->elements;
  140. }
  141. if (in_array($containerName, $this->elementsContainers) && property_exists($this, $containerName))
  142. {
  143. return $this->{$containerName};
  144. }
  145. return [];
  146. }
  147. public function elementsExists($containerName = null)
  148. {
  149. if (!$containerName)
  150. {
  151. return count($this->elements) > 0;
  152. }
  153. if (in_array($containerName, $this->elementsContainers) && property_exists($this, $containerName))
  154. {
  155. return count($this->{$containerName}) > 0;
  156. }
  157. return false;
  158. }
  159. public function getElementById($id, $containerName = null)
  160. {
  161. if (!$containerName)
  162. {
  163. return $this->elements[$id];
  164. }
  165. if (in_array($containerName, $this->elementsContainers) && property_exists($this, $containerName))
  166. {
  167. return $this->{$containerName}[$id];
  168. }
  169. return null;
  170. }
  171. public function insertElementById($id, $containerName = null)
  172. {
  173. if (isset($this->elements[$id]) && !$containerName)
  174. {
  175. return $this->elements[$id]->getHtml();
  176. }
  177. if (in_array($containerName, $this->elementsContainers) && property_exists($this, $containerName))
  178. {
  179. return $this->{$containerName}[$id]->getHtml();
  180. }
  181. return '';
  182. }
  183. public function getElementsContainers()
  184. {
  185. return $this->elementsContainers;
  186. }
  187. }