| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Web\Widget; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Icinga\Web\Widget\Tabextension\DashboardAction; |
| 7 |
use Icinga\Web\Widget\Tabextension\MenuAction; |
| 8 |
use Icinga\Web\Widget\Tabextension\OutputFormat; |
| 9 |
use Icinga\Web\Widget\Tabextension\Tabextension; |
| 10 |
use InvalidArgumentException; |
| 11 |
use ipl\Html\BaseHtmlElement; |
| 12 |
use ipl\Html\HtmlString; |
| 13 |
use ipl\Web\Url; |
| 14 |
|
| 15 |
/** |
| 16 |
* @TODO(el): Don't depend on Icinga Web's Tabs |
| 17 |
*/ |
| 18 |
class Tabs extends BaseHtmlElement |
| 19 |
{ |
| 20 |
protected $tag = 'ul'; |
| 21 |
|
| 22 |
protected $defaultAttributes = ['class' => 'tabs primary-nav nav']; |
| 23 |
|
| 24 |
/** @var \Icinga\Web\Widget\Tabs */ |
| 25 |
protected $tabs; |
| 26 |
|
| 27 |
/** @var bool Whether data exports are enabled */ |
| 28 |
protected $dataExportsEnabled = false; |
| 29 |
|
| 30 |
/** @var bool Whether the legacy extensions should be shown by default */ |
| 31 |
protected $legacyExtensionsEnabled = true; |
| 32 |
|
| 33 |
/** @var Url */ |
| 34 |
protected $refreshUrl; |
| 35 |
|
| 36 |
public function __construct() |
| 37 |
{ |
| 38 |
$this->tabs = new \Icinga\Web\Widget\Tabs(); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Don't show legacy extensions by default |
| 43 |
*/ |
| 44 |
public function disableLegacyExtensions() |
| 45 |
{ |
| 46 |
$this->legacyExtensionsEnabled = false; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Show export actions for JSON and CSV |
| 51 |
*/ |
| 52 |
public function enableDataExports() |
| 53 |
{ |
| 54 |
$this->dataExportsEnabled = true; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Set the url for the refresh button |
| 59 |
* |
| 60 |
* @param Url $url |
| 61 |
* |
| 62 |
* @return $this |
| 63 |
*/ |
| 64 |
public function setRefreshUrl(Url $url) |
| 65 |
{ |
| 66 |
$this->refreshUrl = $url; |
| 67 |
|
| 68 |
return $this; |
| 69 |
} |
| 70 |
|
| 71 |
protected function assemble() |
| 72 |
{ |
| 73 |
if ($this->legacyExtensionsEnabled) { |
| 74 |
$this->tabs->extend(new OutputFormat( |
| 75 |
$this->dataExportsEnabled |
| 76 |
? [] |
| 77 |
: [OutputFormat::TYPE_CSV, OutputFormat::TYPE_JSON] |
| 78 |
)) |
| 79 |
->extend(new DashboardAction()) |
| 80 |
->extend(new MenuAction()); |
| 81 |
} |
| 82 |
|
| 83 |
$tabHtml = substr($this->tabs->render(), 34, -5); |
| 84 |
if ($this->refreshUrl !== null) { |
| 85 |
$tabHtml = preg_replace( |
| 86 |
'/(?<=class="refresh-container-control spinner" href=")([^"]*)/', |
| 87 |
$this->refreshUrl->getAbsoluteUrl(), |
| 88 |
$tabHtml |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
parent::add(HtmlString::create($tabHtml)); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Activate the tab with the given name |
| 97 |
* |
| 98 |
* @param string $name |
| 99 |
* |
| 100 |
* @return $this |
| 101 |
* |
| 102 |
* @throws InvalidArgumentException |
| 103 |
*/ |
| 104 |
public function activate($name) |
| 105 |
{ |
| 106 |
try { |
| 107 |
$this->tabs->activate($name); |
| 108 |
} catch (Exception $e) { |
| 109 |
throw new InvalidArgumentException($e->getMessage()); |
| 110 |
} |
| 111 |
|
| 112 |
return $this; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get active tab |
| 117 |
* |
| 118 |
* @return \Icinga\Web\Widget\Tab |
| 119 |
*/ |
| 120 |
public function getActiveTab() |
| 121 |
{ |
| 122 |
return $this->tabs->get($this->tabs->getActiveName()); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Add the given tab |
| 127 |
* |
| 128 |
* @param string $name |
| 129 |
* @param mixed $tab |
| 130 |
* |
| 131 |
* @return $this |
| 132 |
* |
| 133 |
* @throws InvalidArgumentException |
| 134 |
*/ |
| 135 |
public function add($name, $tab = null) |
| 136 |
{ |
| 137 |
if ($tab === null) { |
| 138 |
throw new InvalidArgumentException('Argument $tab is required'); |
| 139 |
} |
| 140 |
|
| 141 |
try { |
| 142 |
$this->tabs->add($name, $tab); |
| 143 |
} catch (Exception $e) { |
| 144 |
throw new InvalidArgumentException($e->getMessage()); |
| 145 |
} |
| 146 |
|
| 147 |
if (is_array($tab) && isset($tab['active']) && $tab['active']) { |
| 148 |
// Otherwise Tabs::getActiveName() returns null |
| 149 |
$this->tabs->activate($name); |
| 150 |
} |
| 151 |
|
| 152 |
return $this; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Get a tab |
| 157 |
* |
| 158 |
* @param string $name |
| 159 |
* |
| 160 |
* @return \Icinga\Web\Widget\Tab|null |
| 161 |
*/ |
| 162 |
public function get($name) |
| 163 |
{ |
| 164 |
return $this->tabs->get($name); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Count tabs |
| 169 |
* |
| 170 |
* @return int |
| 171 |
*/ |
| 172 |
public function count(): int |
| 173 |
{ |
| 174 |
return $this->tabs->count(); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Apply a Tabextension on $this->tabs object not on this class |
| 179 |
* |
| 180 |
* @param Tabextension $extension |
| 181 |
* |
| 182 |
* @return $this |
| 183 |
*/ |
| 184 |
public function extend(Tabextension $extension) |
| 185 |
{ |
| 186 |
$this->tabs->extend($extension); |
| 187 |
|
| 188 |
return $this; |
| 189 |
} |
| 190 |
} |