HtmlElements.php 3.9 KB

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