@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at upstream/main 218 lines 4.6 kB view raw
1<?php 2 3final class PHUIInfoView extends AphrontTagView { 4 5 const SEVERITY_ERROR = 'error'; 6 const SEVERITY_WARNING = 'warning'; 7 const SEVERITY_NOTICE = 'notice'; 8 const SEVERITY_NODATA = 'nodata'; 9 const SEVERITY_SUCCESS = 'success'; 10 const SEVERITY_PLAIN = 'plain'; 11 const SEVERITY_MFA = 'mfa'; 12 13 private $title; 14 private $errors = array(); 15 private $severity = null; 16 private $id; 17 private $buttons = array(); 18 private $isHidden; 19 private $flush; 20 private $icon; 21 22 /** 23 * Set a title 24 * 25 * @param string|null $title 26 * @return self 27 */ 28 public function setTitle($title) { 29 $this->title = $title; 30 return $this; 31 } 32 33 /** 34 * @param string $severity One of the SEVERITY_* constants defined in 35 * PHUIInfoView 36 * @return $this 37 */ 38 public function setSeverity($severity) { 39 $this->severity = $severity; 40 return $this; 41 } 42 43 /** 44 * @return string One of the SEVERITY_* constants defined in PHUIInfoView 45 */ 46 private function getSeverity() { 47 $severity = $this->severity ? $this->severity : self::SEVERITY_ERROR; 48 return $severity; 49 } 50 51 public function setErrors(array $errors) { 52 $this->errors = $errors; 53 return $this; 54 } 55 56 public function setID($id) { 57 $this->id = $id; 58 return $this; 59 } 60 61 public function setIsHidden($bool) { 62 $this->isHidden = $bool; 63 return $this; 64 } 65 66 public function setFlush($flush) { 67 $this->flush = $flush; 68 return $this; 69 } 70 71 public function setIcon($icon) { 72 if ($icon instanceof PHUIIconView) { 73 $this->icon = $icon; 74 } else { 75 $icon = id(new PHUIIconView()) 76 ->setIcon($icon); 77 $this->icon = $icon; 78 } 79 80 return $this; 81 } 82 83 private function getIcon() { 84 if ($this->icon) { 85 return $this->icon; 86 } 87 88 $icon = null; 89 switch ($this->getSeverity()) { 90 case self::SEVERITY_ERROR: 91 $icon = 'fa-exclamation-circle'; 92 break; 93 case self::SEVERITY_WARNING: 94 $icon = 'fa-exclamation-triangle'; 95 break; 96 case self::SEVERITY_NOTICE: 97 $icon = 'fa-info-circle'; 98 break; 99 case self::SEVERITY_PLAIN: 100 case self::SEVERITY_NODATA: 101 return null; 102 case self::SEVERITY_SUCCESS: 103 $icon = 'fa-check-circle'; 104 break; 105 case self::SEVERITY_MFA: 106 $icon = 'fa-lock'; 107 break; 108 } 109 110 $icon = id(new PHUIIconView()) 111 ->setIcon($icon) 112 ->addClass('phui-info-icon'); 113 return $icon; 114 } 115 116 public function addButton(PHUIButtonView $button) { 117 $this->buttons[] = $button; 118 return $this; 119 } 120 121 protected function getTagAttributes() { 122 $classes = array(); 123 $classes[] = 'phui-info-view'; 124 $classes[] = 'phui-info-severity-'.$this->getSeverity(); 125 $classes[] = 'grouped'; 126 if ($this->flush) { 127 $classes[] = 'phui-info-view-flush'; 128 } 129 if ($this->getIcon()) { 130 $classes[] = 'phui-info-has-icon'; 131 } 132 133 return array( 134 'id' => $this->id, 135 'class' => implode(' ', $classes), 136 'style' => $this->isHidden ? 'display: none;' : null, 137 ); 138 } 139 140 protected function getTagContent() { 141 require_celerity_resource('phui-info-view-css'); 142 143 $errors = $this->errors; 144 if (count($errors) > 1) { 145 $list = array(); 146 foreach ($errors as $error) { 147 $list[] = phutil_tag( 148 'li', 149 array(), 150 $error); 151 } 152 $list = phutil_tag( 153 'ul', 154 array( 155 'class' => 'phui-info-view-list', 156 ), 157 $list); 158 } else if (count($errors) == 1) { 159 $list = head($this->errors); 160 } else { 161 $list = null; 162 } 163 164 $title = $this->title; 165 if ($title || phutil_nonempty_string($title)) { 166 $title = phutil_tag( 167 'h1', 168 array( 169 'class' => 'phui-info-view-head', 170 ), 171 $title); 172 } else { 173 $title = null; 174 } 175 176 $children = $this->renderChildren(); 177 if ($list) { 178 $children[] = $list; 179 } 180 181 $body = null; 182 if (!empty($children)) { 183 $body = phutil_tag( 184 'div', 185 array( 186 'class' => 'phui-info-view-body', 187 ), 188 $children); 189 } 190 191 $buttons = null; 192 if (!empty($this->buttons)) { 193 $buttons = phutil_tag( 194 'div', 195 array( 196 'class' => 'phui-info-view-actions', 197 ), 198 $this->buttons); 199 } 200 201 $icon = null; 202 if ($this->getIcon()) { 203 $icon = phutil_tag( 204 'div', 205 array( 206 'class' => 'phui-info-view-icon', 207 ), 208 $this->getIcon()); 209 } 210 211 return array( 212 $icon, 213 $buttons, 214 $title, 215 $body, 216 ); 217 } 218}