| 1: | <?php |
| 2: | |
| 3: | namespace Zippy\Html; |
| 4: | |
| 5: | use Zippy\Interfaces\Binding; |
| 6: | use Zippy\Interfaces\EventReceiver; |
| 7: | use Zippy\Exception as ZE; |
| 8: | use Zippy\WebApplication; |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | abstract class HtmlComponent |
| 18: | { |
| 19: | public $id; |
| 20: | protected $disabled = false; |
| 21: | protected $visible = true; |
| 22: | private $attributes = array(); |
| 23: | |
| 24: | |
| 25: | protected $owner = null; |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | public $tag = null; |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | public function __construct($id) { |
| 40: | if (!is_string($id) || strlen($id) == 0) { |
| 41: | throw new ZE(sprintf(ERROR_INVALID_CREATE_ID, get_class($this))); |
| 42: | } |
| 43: | if (in_array($id, array('add'))) { |
| 44: | throw new ZE(sprintf(ERROR_INVALID_ID, $id)); |
| 45: | } |
| 46: | $this->id = $id; |
| 47: | $this->attributes["id"] = $id; |
| 48: | |
| 49: | |
| 50: | |
| 51: | } |
| 52: | |
| 53: | public function __toString() { |
| 54: | $str = get_class($this) . ' ' . $this->id; |
| 55: | if ($this->owner != null) { |
| 56: | $str = ' ' . $this->owner . '->' . get_class($this) . ' ' . $this->id; |
| 57: | } |
| 58: | return $str; |
| 59: | } |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | public function setAttribute($name, $value=null) { |
| 67: | |
| 68: | $this->attributes[$name] = $value; |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | } |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | public function getAttribute($name) { |
| 90: | if (isset($this->attributes[$name])) { |
| 91: | return $this->attributes[$name] instanceof Binding ? $this->attributes[$name]->getValue() : $this->attributes[$name]; |
| 92: | } else { |
| 93: | return null; |
| 94: | } |
| 95: | } |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | public function getAttributeNames() { |
| 102: | return array_keys($this->attributes); |
| 103: | } |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | public function setOwner(HtmlContainer $owner) { |
| 110: | $this->owner = $owner; |
| 111: | } |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | protected function onAdded() { |
| 117: | |
| 118: | } |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | public function getOwner() { |
| 126: | return $this->owner; |
| 127: | } |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | public function getPageOwner() { |
| 134: | if ($this->owner == null) { |
| 135: | return null; |
| 136: | } |
| 137: | if ($this->owner instanceof \Zippy\Html\WebPage) { |
| 138: | return $this->owner; |
| 139: | } else { |
| 140: | return $this->owner->getPageOwner(); |
| 141: | } |
| 142: | } |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | public function getFormOwner() { |
| 149: | if ($this->owner == null) { |
| 150: | return null; |
| 151: | } |
| 152: | if ($this->owner instanceof \Zippy\Html\Form\Form) { |
| 153: | return $this->owner; |
| 154: | } else { |
| 155: | return $this->owner->getFormOwner(); |
| 156: | } |
| 157: | } |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | public function setVisible($visible) { |
| 165: | $this->visible = $visible; |
| 166: | |
| 167: | |
| 168: | } |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: | public function isVisible() { |
| 175: | return $this->visible; |
| 176: | } |
| 177: | |
| 178: | |
| 179: | |
| 180: | |
| 181: | protected function RenderImpl() { |
| 182: | |
| 183: | } |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | public function Render() { |
| 190: | |
| 191: | $HtmlTag = $this->getTag(); |
| 192: | |
| 193: | |
| 194: | if (isset($this->attributes["class"])) { |
| 195: | if (strlen($this->attributes["class"]) > 0) { |
| 196: | $HtmlTag->addClass($this->attributes["class"]) ; |
| 197: | } else { |
| 198: | $HtmlTag->removeClass($this->attributes["class"]) ; |
| 199: | } |
| 200: | } |
| 201: | |
| 202: | |
| 203: | foreach($HtmlTag->attributes as $a){ |
| 204: | $attributes[$a->nodeName] =$a->nodeValue; |
| 205: | }; |
| 206: | |
| 207: | |
| 208: | if (isset($this->attributes["style"])) { |
| 209: | if (strlen($this->attributes["style"]) > 0) { |
| 210: | $attributes['style'] = ($attributes['style'] ??'') . '; ' . ($this->attributes["style"] ??''); |
| 211: | } else { |
| 212: | $attributes['style'] = str_replace(($this->attributes["style"] ??''), "", ($attributes['style'] ??'')); |
| 213: | } |
| 214: | } |
| 215: | |
| 216: | unset($attributes["zippy"]); |
| 217: | |
| 218: | $attributes['id'] = $this->id; |
| 219: | |
| 220: | |
| 221: | foreach ($attributes as $key => $value) { |
| 222: | |
| 223: | if (!array_key_exists($key, $this->attributes)) { |
| 224: | $this->attributes[$key] = $value; |
| 225: | } |
| 226: | } |
| 227: | |
| 228: | |
| 229: | $this->beforeRender(); |
| 230: | |
| 231: | $this->RenderImpl(); |
| 232: | |
| 233: | foreach ($this->attributes as $name => $value) { |
| 234: | $attr = $this->getAttribute($name); |
| 235: | |
| 236: | if (strlen($attr) == 0) { |
| 237: | $HtmlTag->removeAttr($name); |
| 238: | } else{ |
| 239: | $HtmlTag->attr($name, $attr); |
| 240: | } |
| 241: | } |
| 242: | $this->afterRender(); |
| 243: | } |
| 244: | |
| 245: | |
| 246: | |
| 247: | |
| 248: | protected function getTag() : \DOMWrap\Element { |
| 249: | $p = $this->getPageOwner() ; |
| 250: | |
| 251: | $HtmlTag = WebApplication::$dom->find('[zippy="' . $this->id . '"]'); |
| 252: | |
| 253: | if ($HtmlTag->count() > 1) { |
| 254: | throw new ZE(sprintf(ERROR_MARKUP_NOTUNIQUEID, $this->id)); |
| 255: | } |
| 256: | if ($HtmlTag->count() == 0) { |
| 257: | $tag = 'zippy="' . $this->id . '"'; |
| 258: | |
| 259: | throw new ZE(sprintf(ERROR_MARKUP_NOTFOUND, $tag)); |
| 260: | } |
| 261: | return $HtmlTag->first(); |
| 262: | } |
| 263: | |
| 264: | |
| 265: | |
| 266: | |
| 267: | |
| 268: | protected function beforeRender() { |
| 269: | |
| 270: | } |
| 271: | |
| 272: | |
| 273: | |
| 274: | |
| 275: | protected function afterRender() { |
| 276: | |
| 277: | } |
| 278: | |
| 279: | |
| 280: | |
| 281: | |
| 282: | |
| 283: | |
| 284: | protected function getLabelTag() { |
| 285: | return WebApplication::$dom->find('[data-label="' . $this->id . '"]'); |
| 286: | } |
| 287: | protected function getLabelTagFor() { |
| 288: | return WebApplication::$dom->find('[for="' . $this->id . '"]'); |
| 289: | } |
| 290: | |
| 291: | |
| 292: | |
| 293: | public function getHTML(){ |
| 294: | try{ |
| 295: | $HtmlTag = $this->getTag(); |
| 296: | $tn= $HtmlTag->tagName; |
| 297: | $attr=""; |
| 298: | foreach($HtmlTag->attributes as $a){ |
| 299: | $attr = $attr. " {$a->nodeName}=\"{$a->nodeValue}\" "; |
| 300: | }; |
| 301: | |
| 302: | $html = "<{$tn} {$attr} >". $HtmlTag->html() . "</{$tn}>"; |
| 303: | |
| 304: | return $html; |
| 305: | |
| 306: | |
| 307: | } catch(\Exception $e) { |
| 308: | return null; |
| 309: | } |
| 310: | } |
| 311: | |
| 312: | |
| 313: | |
| 314: | } |
| 315: | |