ContainerElements.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\UI\Traits;
  3. use \ThurData\Servers\KerioEmail\Core\DependencyInjection\DependencyInjection;
  4. use \ThurData\Servers\KerioEmail\Core\ServiceLocator;
  5. /**
  6. * Container Elements related functions
  7. * Base Container Trait
  8. *
  9. * @autor ThurData <info@thurdata.ch>
  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. /**
  29. * Adds new element to the container
  30. *
  31. * @param object|null $element
  32. * @param string|null $containerName
  33. * @return $this
  34. */
  35. public function addElement($element = null, $containerName = null)
  36. {
  37. if ($element === null)
  38. {
  39. return $this;
  40. }
  41. $element = $this->prepareElementInstance($element);
  42. $this->checkElementItemContext();
  43. $this->addElementToContainer($element, $containerName);
  44. if (!$element->wasInitialized() && $this->getRequestValue(ajax) == 1)
  45. {
  46. $element->runInitContentProcess();
  47. }
  48. return $this;
  49. }
  50. /**
  51. * if $element is a string it will be converted to object
  52. *
  53. * @param object|string $element
  54. * @return object mixed
  55. */
  56. protected function prepareElementInstance($element)
  57. {
  58. if (is_string($element))
  59. {
  60. $element = DependencyInjection::create($element);
  61. }
  62. $element->setIndex(ServiceLocator::call('request')->get('index'));
  63. return $element;
  64. }
  65. /**
  66. * check if should load vue assets for ajax calls
  67. */
  68. protected function checkElementItemContext()
  69. {
  70. if ($this->isIdEqual($this->getRequestValue('loadData')))
  71. {
  72. self::$findItemContext = true;
  73. }
  74. }
  75. /**
  76. * do not use this function directly, addElement function is the only proper way
  77. */
  78. protected function addElementToContainer($element, $containerName = null)
  79. {
  80. /**
  81. * unique element id
  82. */
  83. $id = $element->getId();
  84. $container = $this->getElementContainerName($containerName);
  85. if (!$container)
  86. {
  87. return null;
  88. }
  89. if (!isset($this->{$container}[$id]))
  90. {
  91. $this->{$container}[$id] = $element;
  92. $this->registerMainContainerAdditions($this->{$container}[$id]);
  93. }
  94. }
  95. /**
  96. * Generates ajax and vue indication for $element object in the main container
  97. *
  98. * @param $element
  99. */
  100. protected function registerMainContainerAdditions(&$element)
  101. {
  102. if ($this->mainContainer === null)
  103. {
  104. return $this;
  105. }
  106. $element->setMainContainer($this->mainContainer);
  107. if ($element instanceof \ThurData\Servers\KerioEmail\Core\UI\Interfaces\AjaxElementInterface)
  108. {
  109. $this->mainContainer->addAjaxElement($element);
  110. }
  111. if ($element->isVueComponent() && $element->isVueRegistrationAllowed())
  112. {
  113. $this->mainContainer->addVueComponent($element);
  114. }
  115. }
  116. /**
  117. * determines property name where the element will be added
  118. * @param type $containerName
  119. * @return type null|string
  120. */
  121. public function getElementContainerName($containerName = null)
  122. {
  123. /**
  124. * if not set use default container
  125. */
  126. if ($containerName === null)
  127. {
  128. return $this->defaultContainer;
  129. }
  130. /**
  131. * if invalid value provided
  132. */
  133. if (!$containerName || !is_string($containerName) || $containerName === '')
  134. {
  135. return null;
  136. }
  137. /**
  138. * container does not exists, or is not on the allowed list
  139. */
  140. if (!property_exists($this, $containerName) || !in_array($containerName, $this->elementsContainers))
  141. {
  142. return null;
  143. }
  144. /**
  145. * container is not an array
  146. */
  147. if (!is_array($this->{$containerName}))
  148. {
  149. return null;
  150. }
  151. return $containerName;
  152. }
  153. /**
  154. * States that the property should be treated as container from now on
  155. *
  156. * @param string|null $containerName
  157. * @return $this
  158. */
  159. public function addNewElementsContainer($containerName = null)
  160. {
  161. if (!is_string($containerName) || $containerName === '')
  162. {
  163. return $this;
  164. }
  165. if (!in_array($containerName, $this->elementsContainers) && property_exists($this, $containerName))
  166. {
  167. $this->elementsContainers[] = $containerName;
  168. }
  169. return $this;
  170. }
  171. /**
  172. * Returns all elements from the container $containerName
  173. *
  174. * @param string|null $containerName
  175. * @return array|type
  176. */
  177. public function getElements($containerName = null)
  178. {
  179. if (!$containerName)
  180. {
  181. return $this->elements;
  182. }
  183. if (in_array($containerName, $this->elementsContainers) && property_exists($this, $containerName))
  184. {
  185. return $this->{$containerName};
  186. }
  187. return [];
  188. }
  189. /**
  190. * Determines if container $containerName has any elements
  191. *
  192. * @param string|null $containerName
  193. * @return bool
  194. */
  195. public function elementsExists($containerName = null)
  196. {
  197. if (!$containerName)
  198. {
  199. return count($this->elements) > 0;
  200. }
  201. if (in_array($containerName, $this->elementsContainers) && property_exists($this, $containerName))
  202. {
  203. return count($this->{$containerName}) > 0;
  204. }
  205. return false;
  206. }
  207. /**
  208. * Returns an element object if exists in specified container or null otherwise
  209. *
  210. * @param string $id
  211. * @param string|null $containerName
  212. * @return object|null
  213. */
  214. public function getElementById($id, $containerName = null)
  215. {
  216. if (!$containerName)
  217. {
  218. return $this->elements[$id];
  219. }
  220. if (in_array($containerName, $this->elementsContainers) && property_exists($this, $containerName))
  221. {
  222. return $this->{$containerName}[$id];
  223. }
  224. return null;
  225. }
  226. /**
  227. * Returns an HTML of element object if exists in specified container or empty string otherwise
  228. * This is used for inserting elements HTML code directly to Smarty templates
  229. *
  230. * @param string $id
  231. * @param string|null $containerName
  232. * @return string
  233. */
  234. public function insertElementById($id, $containerName = null)
  235. {
  236. if (isset($this->elements[$id]) && !$containerName)
  237. {
  238. return $this->elements[$id]->getHtml();
  239. }
  240. if (in_array($containerName, $this->elementsContainers) && property_exists($this, $containerName))
  241. {
  242. return $this->{$containerName}[$id]->getHtml();
  243. }
  244. return '';
  245. }
  246. /**
  247. * Returns a list of available container names for $this object
  248. *
  249. * @return array
  250. */
  251. public function getElementsContainers()
  252. {
  253. return $this->elementsContainers;
  254. }
  255. /**
  256. * determines if container exists
  257. *
  258. * @param string|null $containerName
  259. * @return bool
  260. */
  261. public function elementContainerExists($containerName = null)
  262. {
  263. if (is_string($containerName) && trim($containerName) !== '' && isset($this->elementsContainers[$containerName]))
  264. {
  265. return true;
  266. }
  267. return false;
  268. }
  269. }