HtmlElements.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\UI\Traits;
  3. use ModulesGarden\ProxmoxAddon\Core\Helper;
  4. trait HtmlElements
  5. {
  6. protected $class = [];
  7. protected $name = null;
  8. protected $id = null;
  9. protected $scriptHtml = null;
  10. protected $index = null;
  11. public function setName($name = null)
  12. {
  13. if (is_string($name))
  14. {
  15. $this->name = $name;
  16. }
  17. return $this;
  18. }
  19. public function addClass($class = null)
  20. {
  21. if (is_string($class))
  22. {
  23. $this->class[] = $class;
  24. }
  25. return $this;
  26. }
  27. public function removeClass($class = null)
  28. {
  29. if (is_string($class) && in_array($this->class, $class))
  30. {
  31. $this->class = array_map(function(&$cl) use ($class)
  32. {
  33. if ($cl === $class)
  34. {
  35. unset($cl);
  36. }
  37. }, $this->class);
  38. }
  39. return $this;
  40. }
  41. public function replaceClasses($classes)
  42. {
  43. if (is_array($classes))
  44. {
  45. $this->class = $classes;
  46. }
  47. return $this;
  48. }
  49. public function getClasses()
  50. {
  51. return implode(' ', $this->class);
  52. }
  53. public function hasClass($class)
  54. {
  55. if (is_string($class) && in_array($this->class, $class))
  56. {
  57. return true;
  58. }
  59. return false;
  60. }
  61. public function setId($id = null)
  62. {
  63. if (is_string($id) || is_int($id))
  64. {
  65. $this->id = $id;
  66. }
  67. return $this;
  68. }
  69. public function setScriptHtml($scriptHtml = null)
  70. {
  71. if (is_string($scriptHtml))
  72. {
  73. $this->scriptHtml = $scriptHtml;
  74. }
  75. return $this;
  76. }
  77. protected function generateRandomId()
  78. {
  79. $stringGen = new Helper\RandomStringGenerator();
  80. $this->id = $stringGen->genRandomString('mgContElem');
  81. return $this;
  82. }
  83. protected function generateRandomName()
  84. {
  85. if ($this->id)
  86. {
  87. $this->name = $this->id;
  88. return $this;
  89. }
  90. $stringGen = new Helper\RandomStringGenerator();
  91. $this->name = $stringGen->genRandomString('mgContElem');
  92. return $this;
  93. }
  94. public function getName()
  95. {
  96. if (!$this->name)
  97. {
  98. $this->generateRandomName();
  99. }
  100. return $this->name;
  101. }
  102. public function getId()
  103. {
  104. if (!$this->id)
  105. {
  106. $this->generateRandomId();
  107. }
  108. return $this->id;
  109. }
  110. public function getRawClasses()
  111. {
  112. return $this->class;
  113. }
  114. public function getScriptHtml()
  115. {
  116. return $this->scriptHtml;
  117. }
  118. protected function prepareDefaultHtmlElements()
  119. {
  120. if (!$this->id)
  121. {
  122. $this->generateRandomId();
  123. }
  124. if (!$this->name)
  125. {
  126. $this->generateRandomName();
  127. }
  128. return $this;
  129. }
  130. public function initIds($id = null)
  131. {
  132. if (is_string($id) || is_int($id))
  133. {
  134. $this->id = $id;
  135. $this->name = $id;
  136. $this->title = $id;
  137. }
  138. return $this;
  139. }
  140. public function isIdEqual($id)
  141. {
  142. return $this->id === $id;
  143. }
  144. public function getIndex()
  145. {
  146. return $this->index ?: $this->id;
  147. }
  148. public function setIndex($index)
  149. {
  150. $this->index = $index ?: $this->id;
  151. return $this;
  152. }
  153. }