Alert.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. require_once dirname(__FILE__) . '/' . 'Node.php';
  3. // - props[type] - string success|info|warning|danger|error
  4. // <div class="alert alert-success" role="alert">...</div>
  5. // <div class="alert alert-info" role="alert">...</div>
  6. // <div class="alert alert-warning" role="alert">...</div>
  7. // <div class="alert alert-danger" role="alert">...</div>
  8. class UI_Alert extends UI_Node implements UITagInterface {
  9. public $tagName, $props, $childrens;
  10. function __construct($tagName = null, $props = null, $childrens = null) {
  11. parent::__construct($tagName, $props, $childrens);
  12. // TODO: convert to base html tagName with childrens
  13. // or modify __toString() (NOTE: require implement tagName in frontend with name: 'P5UI__Alert' (prefix: 'p5:' -> 'P5UI__'))
  14. $DO_CONVERT_TO_NATIVE_HTML_TAGS = true;
  15. if ($DO_CONVERT_TO_NATIVE_HTML_TAGS) {
  16. $type = self::convertType($props['type']);
  17. unset($this->props['type']);
  18. $cls = "alert alert-{$type}";
  19. if (!empty($this->props['class'])) {
  20. if (is_array($this->props['class'])) $this->props['class'][] = "{$cls}";
  21. $this->props['class'] .= " {$cls}";
  22. } else {
  23. $this->props['class'] = $cls;
  24. }
  25. $this->tagName = 'div';
  26. } else { // modify __toString() (NOTE: require implement tagName in frontend with name: 'P5UI__Alert' (prefix: 'p5:' -> 'P5UI__'))
  27. }
  28. }
  29. /**
  30. * @param string $tagName = 'p5:Alert'
  31. * @param array $props
  32. * @param array $childrens
  33. *
  34. * @return string html code
  35. */
  36. static function h($tagName, $props = [], $childrens = []) {
  37. $type = self::convertType($props['type']);
  38. return UI::h('div', [ 'class' => "alert alert-{$type}" ], $childrens);
  39. }
  40. static function convertType($type) {
  41. switch (strtolower($type)) {
  42. case 'success': return 'success';
  43. case 'info': return 'info';
  44. case 'warning': return 'warning';
  45. case 'danger': return 'danger';
  46. case 'error': return 'danger';
  47. default: return 'info';
  48. }
  49. }
  50. }