MainContainer.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\UI;
  3. use ThurData\Servers\KerioEmail\Core\Http\Request;
  4. use ThurData\Servers\KerioEmail\Core\DependencyInjection;
  5. use ThurData\Servers\KerioEmail\Core\UI\Builder\Context;
  6. use ThurData\Servers\KerioEmail\Core\Helper;
  7. /**
  8. * Description of Conteiner
  9. *
  10. * @autor ThurData <info@thurdata.ch>
  11. */
  12. class MainContainer extends Container
  13. {
  14. use Traits\MainContainerElements;
  15. protected $name = 'mainContainer';
  16. protected $id = 'mainContainer';
  17. protected $defaultTemplateName = 'mainContainer';
  18. protected $templateName = 'mainContainer';
  19. protected $data = [];
  20. protected $vueInstanceName = null;
  21. public function __construct($baseId = null)
  22. {
  23. parent::__construct($baseId);
  24. $this->prepareElemnentsContainers();
  25. }
  26. public function addElement($element, $containerName = null)
  27. {
  28. if (is_string($element))
  29. {
  30. $element = DependencyInjection::create($element);
  31. }
  32. $container = $this->getElementContainerName($containerName);
  33. if (!$container)
  34. {
  35. return $this;
  36. }
  37. $id = $element->getId();
  38. if (!isset($this->{$container}[$id]))
  39. {
  40. $element->setMainContainer($this);
  41. $this->{$container}[$id] = $element;
  42. if ($element instanceof \ThurData\Servers\KerioEmail\Core\UI\Interfaces\AjaxElementInterface)
  43. {
  44. $this->ajaxElements[] = &$this->{$container}[$id];
  45. }
  46. if ($element->isVueComponent())
  47. {
  48. $this->vueComponents[$element->getTemplateName()] = &$this->{$container}[$id];
  49. }
  50. }
  51. return $this;
  52. }
  53. public function getVueInstanceName()
  54. {
  55. if ($this->vueInstanceName === null)
  56. {
  57. $randomGen = new \ThurData\Servers\KerioEmail\Core\Helper\RandomStringGenerator(32);
  58. $this->vueInstanceName = $randomGen->genRandomString('mc');
  59. }
  60. return $this->vueInstanceName;
  61. }
  62. public function valicateACL($isAdmin)
  63. {
  64. foreach ($this->elements as $id => &$element)
  65. {
  66. /**
  67. * @var Context $element
  68. */
  69. if($element->setIsAdminAcl($isAdmin)->validateElement($element) === false)
  70. {
  71. unset($this->elements[$id]);
  72. Helper\sl('errorManager')->addError(__CLASS__, 'There is no implemented interface for the widget "' . get_class($element) . '".');
  73. }
  74. }
  75. foreach ($this->ajaxElements as $id => &$element)
  76. {
  77. /**
  78. * @var Context $element
  79. */
  80. if($element->setIsAdminAcl($isAdmin)->validateElement($element) === false)
  81. {
  82. unset($this->ajaxElements[$id]);
  83. Helper\sl('errorManager')->addError(__CLASS__, 'There is no implemented interface for the widget "' . get_class($element) . '".');
  84. }
  85. }
  86. return $this;
  87. }
  88. /**
  89. * @param array $data
  90. * @return $this
  91. */
  92. public function setData(array $data = [])
  93. {
  94. $this->data = $data;
  95. $this->updateData();
  96. return $this;
  97. }
  98. protected function updateData()
  99. {
  100. foreach ($this->data as $key => $value)
  101. {
  102. if (property_exists($this, $key))
  103. {
  104. $this->$key = $value;
  105. }
  106. }
  107. $this->data = [];
  108. return $this;
  109. }
  110. public function getHtml()
  111. {
  112. $this->loadDefaultNavbars();
  113. if ($this->html === '')
  114. {
  115. $this->buildHtml();
  116. }
  117. return $this->html;
  118. }
  119. /**
  120. * @return string
  121. */
  122. public function __toString()
  123. {
  124. return $this->getHtml();
  125. }
  126. public function getAjaxResponse()
  127. {
  128. $request = Request::build();
  129. foreach ($this->ajaxElements as $aElem)
  130. {
  131. if ($request->get('loadData', false) === $aElem->getId())
  132. {
  133. $response = $aElem->returnAjaxData();
  134. if ($response instanceof \ThurData\Servers\KerioEmail\Core\UI\Interfaces\ResponseInterface)
  135. {
  136. return $response->getFormatedResponse();
  137. }
  138. return $response;
  139. }
  140. }
  141. }
  142. public function getVueComponents()
  143. {
  144. $vBody = '';
  145. foreach ($this->vueComponents as $vElem)
  146. {
  147. $vBody .= $vElem->getVueComponents();
  148. }
  149. return $vBody;
  150. }
  151. public function getAjaxElems()
  152. {
  153. return $this->ajaxElements;
  154. }
  155. public function getVueComponentsJs()
  156. {
  157. $vJsBody = '';
  158. foreach ($this->vueComponents as $vElem)
  159. {
  160. $vJsBody .= $vElem->getVueComponentsJs();
  161. }
  162. return $vJsBody;
  163. }
  164. public function getDefaultVueComponentsJs()
  165. {
  166. if ($this->defaultComponentsJs === null)
  167. {
  168. $this->loadDefaultVueComponentsJs();
  169. }
  170. return $this->defaultComponentsJs;
  171. }
  172. protected function loadDefaultVueComponentsJs()
  173. {
  174. $componentsPath = str_replace(DS . 'ui' . DS, DS . 'assets' . DS . 'js' . DS . 'defaultComponents' . DS, $this->getDefaultTemplateDir());
  175. $content = scandir($componentsPath);
  176. $this->defaultComponentsJs = '';
  177. if ($content)
  178. {
  179. foreach ($content as $file)
  180. {
  181. $fileInfo = pathinfo($componentsPath . $file);
  182. if ($fileInfo['extension'] === 'js')
  183. {
  184. $jsContent = file_get_contents($componentsPath . $file);
  185. $this->defaultComponentsJs .= $jsContent ? $jsContent : '';
  186. }
  187. }
  188. }
  189. return $this;
  190. }
  191. }