| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- namespace ModulesGarden\Servers\ProxmoxVps\Core\UI\Traits;
- use ModulesGarden\Servers\ProxmoxVps\Core\Helper;
- trait HtmlElements {
- protected $class = [];
- protected $name = null;
- protected $id = null;
- protected $scriptHtml = null;
- protected $index = null;
- public function setName($name = null) {
- if (is_string($name)) {
- $this->name = $name;
- }
- return $this;
- }
- public function addClass($class = null) {
- if (is_string($class)) {
- $this->class[] = $class;
- }
- return $this;
- }
- public function removeClass($class = null) {
- if (is_string($class) && in_array($this->class, $class)) {
- $this->class = array_map(function(&$cl) use ($class)
- {
- if ($cl === $class)
- {
- unset($cl);
- }
- }, $this->class);
- }
- return $this;
- }
- public function replaceClass($class = null, $replacement = null) {
- if (is_string($class) && trim($class) !== '' && in_array($class, $this->class)
- && is_string($replacement) && trim($replacement) !== '') {
- $keys = array_keys($this->class, $class, true);
- foreach ($keys as $key) {
- unset($this->class[$key]);
- }
- $this->class[] = $replacement;
- }
- return $this;
- }
- public function replaceClasses($classes) {
- if (is_array($classes)) {
- $this->class = $classes;
- }
- return $this;
- }
- public function getClasses() {
- return implode(' ', $this->class);
- }
- public function hasClass($class) {
- if (is_string($class) && in_array($this->class, $class)) {
- return true;
- }
- return false;
- }
- public function setId($id = null) {
- if (is_string($id) || is_int($id)) {
- $this->id = $id;
- }
- return $this;
- }
- public function setScriptHtml($scriptHtml = null) {
- if (is_string($scriptHtml)) {
- $this->scriptHtml = $scriptHtml;
- }
- return $this;
- }
- protected function generateRandomId() {
- $stringGen = new Helper\RandomStringGenerator();
- $this->id = $stringGen->genRandomString('mgContElem');
-
- return $this;
- }
- protected function generateRandomName() {
- if ($this->id) {
- $this->name = $this->id;
- return $this;
- }
- $stringGen = new Helper\RandomStringGenerator();
- $this->name = $stringGen->genRandomString('mgContElem');
-
- return $this;
- }
- public function getName()
- {
- if (!$this->name)
- {
- $this->generateRandomName();
- }
- return $this->name;
- }
- public function getId()
- {
- if (!$this->id)
- {
- $this->generateRandomId();
- }
- return $this->id;
- }
- public function getRawClasses()
- {
- return $this->class;
- }
- public function getScriptHtml()
- {
- return $this->scriptHtml;
- }
- protected function prepareDefaultHtmlElements()
- {
- if (!$this->id)
- {
- $this->generateRandomId();
- }
- if (!$this->name)
- {
- $this->generateRandomName();
- }
-
- return $this;
- }
- public function initIds($id = null)
- {
- if (is_string($id) || is_int($id))
- {
- $this->id = $id;
- $this->name = $id;
- $this->title = $id;
- }
- return $this;
- }
-
- public function isIdEqual($id) {
- return $this->id === $id;
- }
-
- public function getIndex(){
- return $this->index ? : $this->id;
- }
-
- public function setIndex($index) {
- $this->index = $index ? : $this->id;
-
- return $this;
- }
- }
|