HtmlElements.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Traits;
  3. use ModulesGarden\Servers\ProxmoxCloudVps\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 replaceClass($class = null, $replacement = null)
  42. {
  43. if (is_string($class) && trim($class) !== '' && in_array($class, $this->class)
  44. && is_string($replacement) && trim($replacement) !== '')
  45. {
  46. $keys = array_keys($this->class, $class, true);
  47. foreach ($keys as $key)
  48. {
  49. unset($this->class[$key]);
  50. }
  51. $this->class[] = $replacement;
  52. }
  53. return $this;
  54. }
  55. public function replaceClasses($classes)
  56. {
  57. if (is_array($classes))
  58. {
  59. $this->class = $classes;
  60. }
  61. return $this;
  62. }
  63. public function getClasses()
  64. {
  65. return implode(' ', $this->class);
  66. }
  67. public function hasClass($class)
  68. {
  69. if (is_string($class) && in_array($this->class, $class))
  70. {
  71. return true;
  72. }
  73. return false;
  74. }
  75. public function setId($id = null)
  76. {
  77. if (is_string($id) || is_int($id))
  78. {
  79. $this->id = $id;
  80. }
  81. return $this;
  82. }
  83. public function setScriptHtml($scriptHtml = null)
  84. {
  85. if (is_string($scriptHtml))
  86. {
  87. $this->scriptHtml = $scriptHtml;
  88. }
  89. return $this;
  90. }
  91. protected function generateRandomId()
  92. {
  93. $stringGen = new Helper\RandomStringGenerator();
  94. $this->id = $stringGen->genRandomString('mgContElem');
  95. return $this;
  96. }
  97. protected function generateRandomName()
  98. {
  99. if ($this->id)
  100. {
  101. $this->name = $this->id;
  102. return $this;
  103. }
  104. $stringGen = new Helper\RandomStringGenerator();
  105. $this->name = $stringGen->genRandomString('mgContElem');
  106. return $this;
  107. }
  108. public function getName()
  109. {
  110. if (!$this->name)
  111. {
  112. $this->generateRandomName();
  113. }
  114. return $this->name;
  115. }
  116. public function getId()
  117. {
  118. if (!$this->id)
  119. {
  120. $this->generateRandomId();
  121. }
  122. return $this->id;
  123. }
  124. public function getRawClasses()
  125. {
  126. return $this->class;
  127. }
  128. public function getScriptHtml()
  129. {
  130. return $this->scriptHtml;
  131. }
  132. protected function prepareDefaultHtmlElements()
  133. {
  134. if (!$this->id)
  135. {
  136. $this->generateRandomId();
  137. }
  138. if (!$this->name)
  139. {
  140. $this->generateRandomName();
  141. }
  142. return $this;
  143. }
  144. public function initIds($id = null)
  145. {
  146. if (is_string($id) || is_int($id))
  147. {
  148. $this->id = $id;
  149. $this->name = $id;
  150. $this->title = $id;
  151. }
  152. return $this;
  153. }
  154. public function isIdEqual($id)
  155. {
  156. return $this->id === $id;
  157. }
  158. public function getIndex()
  159. {
  160. return $this->index ? : $this->id;
  161. }
  162. public function setIndex($index)
  163. {
  164. $this->index = $index ? : $this->id;
  165. return $this;
  166. }
  167. }