| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Web\Common; |
| 4 |
|
| 5 |
use ipl\Html\BaseHtmlElement; |
| 6 |
use ipl\Html\Html; |
| 7 |
use ipl\Stdlib\BaseFilter; |
| 8 |
use ipl\Stdlib\Filter; |
| 9 |
use ipl\Web\Filter\QueryString; |
| 10 |
use ipl\Web\Url; |
| 11 |
use ipl\Web\Widget\Link; |
| 12 |
use ipl\Web\Widget\StateBadge; |
| 13 |
|
| 14 |
/** |
| 15 |
* @deprecated Use {@see \Icinga\Module\Icingadb\Common\StateBadges} instead. |
| 16 |
*/ |
| 17 |
abstract class StateBadges extends BaseHtmlElement |
| 18 |
{ |
| 19 |
use BaseFilter; |
| 20 |
|
| 21 |
/** @var object $item */ |
| 22 |
protected $item; |
| 23 |
|
| 24 |
/** @var string */ |
| 25 |
protected $type; |
| 26 |
|
| 27 |
/** @var string Prefix */ |
| 28 |
protected $prefix; |
| 29 |
|
| 30 |
/** @var Url Badge link */ |
| 31 |
protected $url; |
| 32 |
|
| 33 |
protected $tag = 'ul'; |
| 34 |
|
| 35 |
protected $defaultAttributes = ['class' => 'state-badges']; |
| 36 |
|
| 37 |
/** |
| 38 |
* Create a new widget for state badges |
| 39 |
* |
| 40 |
* @param object $item |
| 41 |
*/ |
| 42 |
public function __construct($item) |
| 43 |
{ |
| 44 |
$this->item = $item; |
| 45 |
$this->type = $this->getType(); |
| 46 |
$this->prefix = $this->getPrefix(); |
| 47 |
$this->url = $this->getBaseUrl(); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get the badge base URL |
| 52 |
* |
| 53 |
* @return Url |
| 54 |
*/ |
| 55 |
abstract protected function getBaseUrl(): Url; |
| 56 |
|
| 57 |
/** |
| 58 |
* Get the type of the items |
| 59 |
* |
| 60 |
* @return string |
| 61 |
*/ |
| 62 |
abstract protected function getType(): string; |
| 63 |
|
| 64 |
/** |
| 65 |
* Get the prefix for accessing state information |
| 66 |
* |
| 67 |
* @return string |
| 68 |
*/ |
| 69 |
abstract protected function getPrefix(): string; |
| 70 |
|
| 71 |
/** |
| 72 |
* Get the integer of the given state text |
| 73 |
* |
| 74 |
* @param string $state |
| 75 |
* |
| 76 |
* @return int |
| 77 |
*/ |
| 78 |
abstract protected function getStateInt(string $state): int; |
| 79 |
|
| 80 |
/** |
| 81 |
* Get the badge URL |
| 82 |
* |
| 83 |
* @return Url |
| 84 |
*/ |
| 85 |
public function getUrl(): Url |
| 86 |
{ |
| 87 |
return $this->url; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Set the badge URL |
| 92 |
* |
| 93 |
* @param Url $url |
| 94 |
* |
| 95 |
* @return $this |
| 96 |
*/ |
| 97 |
public function setUrl(Url $url): self |
| 98 |
{ |
| 99 |
$this->url = $url; |
| 100 |
|
| 101 |
return $this; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Create a badge link |
| 106 |
* |
| 107 |
* @param mixed $content |
| 108 |
* @param ?array $filter |
| 109 |
* |
| 110 |
* @return Link |
| 111 |
*/ |
| 112 |
public function createLink($content, ?array $filter = null): Link |
| 113 |
{ |
| 114 |
$url = clone $this->getUrl(); |
| 115 |
|
| 116 |
$urlFilter = Filter::all(); |
| 117 |
if (! empty($filter)) { |
| 118 |
foreach ($filter as $column => $value) { |
| 119 |
$urlFilter->add(Filter::equal($column, $value)); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
if ($this->hasBaseFilter()) { |
| 124 |
$urlFilter->add($this->getBaseFilter()); |
| 125 |
} |
| 126 |
|
| 127 |
if (! $urlFilter->isEmpty()) { |
| 128 |
$url->setFilter($urlFilter); |
| 129 |
} |
| 130 |
|
| 131 |
return new Link($content, $url); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Create a state bade |
| 136 |
* |
| 137 |
* @param string $state |
| 138 |
* |
| 139 |
* @return ?BaseHtmlElement |
| 140 |
*/ |
| 141 |
protected function createBadge(string $state) |
| 142 |
{ |
| 143 |
$key = $this->prefix . "_{$state}"; |
| 144 |
|
| 145 |
if (isset($this->item->$key) && $this->item->$key) { |
| 146 |
return Html::tag('li', $this->createLink( |
| 147 |
new StateBadge($this->item->$key, $state), |
| 148 |
[$this->type . '.state.soft_state' => $this->getStateInt($state)] |
| 149 |
)); |
| 150 |
} |
| 151 |
|
| 152 |
return null; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Create a state group |
| 157 |
* |
| 158 |
* @param string $state |
| 159 |
* |
| 160 |
* @return ?BaseHtmlElement |
| 161 |
*/ |
| 162 |
protected function createGroup(string $state) |
| 163 |
{ |
| 164 |
$content = []; |
| 165 |
$handledKey = $this->prefix . "_{$state}_handled"; |
| 166 |
$unhandledKey = $this->prefix . "_{$state}_unhandled"; |
| 167 |
|
| 168 |
if (isset($this->item->$unhandledKey) && $this->item->$unhandledKey) { |
| 169 |
$content[] = Html::tag('li', $this->createLink( |
| 170 |
new StateBadge($this->item->$unhandledKey, $state), |
| 171 |
[ |
| 172 |
$this->type . '.state.soft_state' => $this->getStateInt($state), |
| 173 |
$this->type . '.state.is_handled' => 'n' |
| 174 |
] |
| 175 |
)); |
| 176 |
} |
| 177 |
|
| 178 |
if (isset($this->item->$handledKey) && $this->item->$handledKey) { |
| 179 |
$content[] = Html::tag('li', $this->createLink( |
| 180 |
new StateBadge($this->item->$handledKey, $state, true), |
| 181 |
[ |
| 182 |
$this->type . '.state.soft_state' => $this->getStateInt($state), |
| 183 |
$this->type . '.state.is_handled' => 'y' |
| 184 |
] |
| 185 |
)); |
| 186 |
} |
| 187 |
|
| 188 |
if (empty($content)) { |
| 189 |
return null; |
| 190 |
} |
| 191 |
|
| 192 |
return Html::tag('li', Html::tag('ul', $content)); |
| 193 |
} |
| 194 |
} |