| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Web\Compat; |
| 4 |
|
| 5 |
use GuzzleHttp\Psr7\ServerRequest; |
| 6 |
use Icinga\Application\Version; |
| 7 |
use InvalidArgumentException; |
| 8 |
use Icinga\Web\Controller; |
| 9 |
use ipl\Html\BaseHtmlElement; |
| 10 |
use ipl\Html\HtmlDocument; |
| 11 |
use ipl\Html\HtmlString; |
| 12 |
use ipl\Html\ValidHtml; |
| 13 |
use ipl\Orm\Query; |
| 14 |
use ipl\Stdlib\Contract\Paginatable; |
| 15 |
use ipl\Web\Control\LimitControl; |
| 16 |
use ipl\Web\Control\PaginationControl; |
| 17 |
use ipl\Web\Control\SearchBar; |
| 18 |
use ipl\Web\Control\SortControl; |
| 19 |
use ipl\Web\Layout\Content; |
| 20 |
use ipl\Web\Layout\Controls; |
| 21 |
use ipl\Web\Layout\Footer; |
| 22 |
use ipl\Web\Url; |
| 23 |
use ipl\Web\Widget\Tabs; |
| 24 |
use LogicException; |
| 25 |
use Psr\Http\Message\ServerRequestInterface; |
| 26 |
|
| 27 |
class CompatController extends Controller |
| 28 |
{ |
| 29 |
/** @var Content */ |
| 30 |
protected $content; |
| 31 |
|
| 32 |
/** @var Controls */ |
| 33 |
protected $controls; |
| 34 |
|
| 35 |
/** @var HtmlDocument */ |
| 36 |
protected $document; |
| 37 |
|
| 38 |
/** @var Footer */ |
| 39 |
protected $footer; |
| 40 |
|
| 41 |
/** @var Tabs */ |
| 42 |
protected $tabs; |
| 43 |
|
| 44 |
/** @var array */ |
| 45 |
protected $parts; |
| 46 |
|
| 47 |
protected function prepareInit() |
| 48 |
{ |
| 49 |
parent::prepareInit(); |
| 50 |
|
| 51 |
$this->params->shift('isIframe'); |
| 52 |
$this->params->shift('showFullscreen'); |
| 53 |
$this->params->shift('showCompact'); |
| 54 |
$this->params->shift('renderLayout'); |
| 55 |
$this->params->shift('_disableLayout'); |
| 56 |
$this->params->shift('_dev'); |
| 57 |
if ($this->params->get('view') === 'compact') { |
| 58 |
$this->params->remove('view'); |
| 59 |
} |
| 60 |
|
| 61 |
$this->document = new HtmlDocument(); |
| 62 |
$this->document->setSeparator("\n"); |
| 63 |
$this->controls = new Controls(); |
| 64 |
$this->controls->setAttribute('id', $this->getRequest()->protectId('controls')); |
| 65 |
$this->content = new Content(); |
| 66 |
$this->content->setAttribute('id', $this->getRequest()->protectId('content')); |
| 67 |
$this->footer = new Footer(); |
| 68 |
$this->footer->setAttribute('id', $this->getRequest()->protectId('footer')); |
| 69 |
$this->tabs = new Tabs(); |
| 70 |
$this->tabs->setAttribute('id', $this->getRequest()->protectId('tabs')); |
| 71 |
$this->parts = []; |
| 72 |
|
| 73 |
$this->view->tabs = $this->tabs; |
| 74 |
$this->controls->setTabs($this->tabs); |
| 75 |
|
| 76 |
ViewRenderer::inject(); |
| 77 |
|
| 78 |
$this->view->document = $this->document; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Get the current server request |
| 83 |
* |
| 84 |
* @return ServerRequestInterface |
| 85 |
*/ |
| 86 |
public function getServerRequest() |
| 87 |
{ |
| 88 |
return ServerRequest::fromGlobals(); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get the document |
| 93 |
* |
| 94 |
* @return HtmlDocument |
| 95 |
*/ |
| 96 |
public function getDocument() |
| 97 |
{ |
| 98 |
return $this->document; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Get the tabs |
| 103 |
* |
| 104 |
* @return Tabs |
| 105 |
*/ |
| 106 |
public function getTabs() |
| 107 |
{ |
| 108 |
return $this->tabs; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Add content |
| 113 |
* |
| 114 |
* @param ValidHtml $content |
| 115 |
* |
| 116 |
* @return $this |
| 117 |
*/ |
| 118 |
protected function addContent(ValidHtml $content) |
| 119 |
{ |
| 120 |
$this->content->add($content); |
| 121 |
|
| 122 |
return $this; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Add a control |
| 127 |
* |
| 128 |
* @param ValidHtml $control |
| 129 |
* |
| 130 |
* @return $this |
| 131 |
*/ |
| 132 |
protected function addControl(ValidHtml $control) |
| 133 |
{ |
| 134 |
$this->controls->add($control); |
| 135 |
|
| 136 |
if ( |
| 137 |
$control instanceof PaginationControl |
| 138 |
|| $control instanceof LimitControl |
| 139 |
|| $control instanceof SortControl |
| 140 |
|| $control instanceof SearchBar |
| 141 |
) { |
| 142 |
$this->controls->getAttributes() |
| 143 |
->get('class') |
| 144 |
->removeValue('default-layout') |
| 145 |
->addValue('default-layout'); |
| 146 |
} |
| 147 |
|
| 148 |
return $this; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Add footer |
| 153 |
* |
| 154 |
* @param ValidHtml $footer |
| 155 |
* |
| 156 |
* @return $this |
| 157 |
*/ |
| 158 |
protected function addFooter(ValidHtml $footer) |
| 159 |
{ |
| 160 |
$this->footer->add($footer); |
| 161 |
|
| 162 |
return $this; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Add a part to be served as multipart-content |
| 167 |
* |
| 168 |
* If an id is passed the element is used as-is as the part's content. |
| 169 |
* Otherwise (no id given) the element's content is used instead. |
| 170 |
* |
| 171 |
* @param ValidHtml $element |
| 172 |
* @param string $id If not given, this is taken from $element |
| 173 |
* |
| 174 |
* @throws InvalidArgumentException If no id is given and the element also does not have one |
| 175 |
* |
| 176 |
* @return $this |
| 177 |
*/ |
| 178 |
protected function addPart(ValidHtml $element, $id = null) |
| 179 |
{ |
| 180 |
$part = new Multipart(); |
| 181 |
|
| 182 |
if ($id === null) { |
| 183 |
if (! $element instanceof BaseHtmlElement) { |
| 184 |
throw new InvalidArgumentException('If no id is given, $element must be a BaseHtmlElement'); |
| 185 |
} |
| 186 |
|
| 187 |
$id = $element->getAttributes()->get('id')->getValue(); |
| 188 |
if (! $id) { |
| 189 |
throw new InvalidArgumentException('Element has no id'); |
| 190 |
} |
| 191 |
|
| 192 |
$part->addFrom($element); |
| 193 |
} else { |
| 194 |
$part->add($element); |
| 195 |
} |
| 196 |
|
| 197 |
$this->parts[] = $part->setFor($id); |
| 198 |
|
| 199 |
return $this; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Set the given title as the window's title |
| 204 |
* |
| 205 |
* @param string $title |
| 206 |
* @param mixed ...$args |
| 207 |
* |
| 208 |
* @return $this |
| 209 |
*/ |
| 210 |
protected function setTitle($title, ...$args) |
| 211 |
{ |
| 212 |
if (! empty($args)) { |
| 213 |
$title = vsprintf($title, $args); |
| 214 |
} |
| 215 |
|
| 216 |
$this->view->title = $title; |
| 217 |
|
| 218 |
return $this; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Add an active tab with the given title and set it as the window's title too |
| 223 |
* |
| 224 |
* @param string $title |
| 225 |
* @param mixed ...$args |
| 226 |
* |
| 227 |
* @return $this |
| 228 |
*/ |
| 229 |
protected function addTitleTab($title, ...$args) |
| 230 |
{ |
| 231 |
$this->setTitle($title, ...$args); |
| 232 |
|
| 233 |
$tabName = uniqid(); |
| 234 |
$this->getTabs()->add($tabName, [ |
| 235 |
'label' => $this->view->title, |
| 236 |
'url' => $this->getRequest()->getUrl() |
| 237 |
])->activate($tabName); |
| 238 |
|
| 239 |
return $this; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Create and return the LimitControl |
| 244 |
* |
| 245 |
* This automatically shifts the limit URL parameter from {@link $params}. |
| 246 |
* |
| 247 |
* @return LimitControl |
| 248 |
*/ |
| 249 |
public function createLimitControl(): LimitControl |
| 250 |
{ |
| 251 |
$limitControl = new LimitControl(Url::fromRequest()); |
| 252 |
$limitControl->setDefaultLimit($this->getPageSize(null)); |
| 253 |
|
| 254 |
$this->params->shift($limitControl->getLimitParam()); |
| 255 |
|
| 256 |
return $limitControl; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Create and return the PaginationControl |
| 261 |
* |
| 262 |
* This automatically shifts the pagination URL parameters from {@link $params}. |
| 263 |
* |
| 264 |
* @param Paginatable $paginatable |
| 265 |
* |
| 266 |
* @return PaginationControl |
| 267 |
*/ |
| 268 |
public function createPaginationControl(Paginatable $paginatable): PaginationControl |
| 269 |
{ |
| 270 |
$paginationControl = new PaginationControl($paginatable, Url::fromRequest()); |
| 271 |
$paginationControl->setDefaultPageSize($this->getPageSize(null)); |
| 272 |
$paginationControl->setAttribute('id', $this->getRequest()->protectId('pagination-control')); |
| 273 |
|
| 274 |
$this->params->shift($paginationControl->getPageParam()); |
| 275 |
$this->params->shift($paginationControl->getPageSizeParam()); |
| 276 |
|
| 277 |
return $paginationControl->apply(); |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Create and return the SortControl |
| 282 |
* |
| 283 |
* This automatically shifts the sort URL parameter from {@link $params}. |
| 284 |
* |
| 285 |
* @param Query $query |
| 286 |
* @param array $columns Possible sort columns as sort string-label pairs |
| 287 |
* @param ?array|string $defaultSort Optional default sort column |
| 288 |
* |
| 289 |
* @return SortControl |
| 290 |
*/ |
| 291 |
public function createSortControl(Query $query, array $columns): SortControl |
| 292 |
{ |
| 293 |
$sortControl = SortControl::create($columns); |
| 294 |
|
| 295 |
$this->params->shift($sortControl->getSortParam()); |
| 296 |
|
| 297 |
$sortControl->handleRequest($this->getServerRequest()); |
| 298 |
|
| 299 |
$defaultSort = null; |
| 300 |
|
| 301 |
if (func_num_args() === 3) { |
| 302 |
$defaultSort = func_get_args()[2]; |
| 303 |
} |
| 304 |
|
| 305 |
return $sortControl->apply($query, $defaultSort); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Send a multipart update instead of a standard response |
| 310 |
* |
| 311 |
* As part of a multipart update, the tabs, content and footer as well as selected controls are |
| 312 |
* transmitted in a way the client can render them exclusively instead of a full column reload. |
| 313 |
* |
| 314 |
* By default the only control included in the response is the pagination control, if added. |
| 315 |
* |
| 316 |
* @param BaseHtmlElement ...$additionalControls Additional controls to include |
| 317 |
* |
| 318 |
* @throws LogicException In case an additional control has not been added |
| 319 |
*/ |
| 320 |
public function sendMultipartUpdate(BaseHtmlElement ...$additionalControls) |
| 321 |
{ |
| 322 |
$searchBar = null; |
| 323 |
$pagination = null; |
| 324 |
$redirectUrl = null; |
| 325 |
foreach ($this->controls->getContent() as $control) { |
| 326 |
if ($control instanceof PaginationControl) { |
| 327 |
$pagination = $control; |
| 328 |
} elseif ($control instanceof SearchBar) { |
| 329 |
$searchBar = $control; |
| 330 |
$redirectUrl = $control->getRedirectUrl(); /** @var Url $redirectUrl */ |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
if ($searchBar !== null && ($changes = $searchBar->getChanges()) !== null) { |
| 335 |
$this->addPart(HtmlString::create(json_encode($changes)), 'Behavior:InputEnrichment'); |
| 336 |
} |
| 337 |
|
| 338 |
foreach ($additionalControls as $control) { |
| 339 |
$this->addPart($control); |
| 340 |
} |
| 341 |
|
| 342 |
if ($searchBar !== null && $this->content->isEmpty() && ! $searchBar->isValid()) { |
| 343 |
// No content and an invalid search bar? That's it then, further updates are not required |
| 344 |
return; |
| 345 |
} |
| 346 |
|
| 347 |
if ($this->tabs->count() > 0) { |
| 348 |
if ($redirectUrl !== null) { |
| 349 |
$this->tabs->setRefreshUrl($redirectUrl); |
| 350 |
$this->tabs->getActiveTab()->setUrl($redirectUrl); |
| 351 |
|
| 352 |
// As long as we still depend on the legacy tab implementation |
| 353 |
// there is no other way to influence what the tab extensions |
| 354 |
// use as url. (https://github.com/Icinga/icingadb-web/issues/373) |
| 355 |
$oldPathInfo = $this->getRequest()->getPathInfo(); |
| 356 |
$oldQuery = $_SERVER['QUERY_STRING']; |
| 357 |
$this->getRequest()->setPathInfo('/' . $redirectUrl->getPath()); |
| 358 |
$_SERVER['QUERY_STRING'] = $redirectUrl->getQueryString(); |
| 359 |
$this->tabs->ensureAssembled(); |
| 360 |
$this->getRequest()->setPathInfo($oldPathInfo); |
| 361 |
$_SERVER['QUERY_STRING'] = $oldQuery; |
| 362 |
} |
| 363 |
|
| 364 |
$this->addPart($this->tabs); |
| 365 |
} |
| 366 |
|
| 367 |
if ($pagination !== null) { |
| 368 |
if ($redirectUrl !== null) { |
| 369 |
$pagination->setUrl(clone $redirectUrl); |
| 370 |
} |
| 371 |
|
| 372 |
$this->addPart($pagination); |
| 373 |
} |
| 374 |
|
| 375 |
if (! $this->content->isEmpty()) { |
| 376 |
$this->addPart($this->content); |
| 377 |
} |
| 378 |
|
| 379 |
if (! $this->footer->isEmpty()) { |
| 380 |
$this->addPart($this->footer); |
| 381 |
} |
| 382 |
|
| 383 |
if ($redirectUrl !== null) { |
| 384 |
$this->getResponse()->setHeader('X-Icinga-Location-Query', $redirectUrl->getQueryString()); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Instruct the client to side-load additional updates |
| 390 |
* |
| 391 |
* If an item in the given array is indexed by an integer, its value will be used by the client to refresh |
| 392 |
* the parent of the element identified by it. The value is expected to be a valid CSS selector such |
| 393 |
* as `.foo`, `#foo`. If indexed by a string, the client will use this index to identify a container (by id) and |
| 394 |
* will use the value (a URL) to load content into it. Since Icinga Web >= 2.12, the indices can be specified with |
| 395 |
* or without the `#` indicator. If you require compatibility with older Icinga Web versions, you have to specify |
| 396 |
* the indices (container ids) without the `#` char. |
| 397 |
* |
| 398 |
* @param array $updates |
| 399 |
* |
| 400 |
* @return void |
| 401 |
*/ |
| 402 |
public function sendExtraUpdates(array $updates) |
| 403 |
{ |
| 404 |
if (empty($updates)) { |
| 405 |
return; |
| 406 |
} |
| 407 |
|
| 408 |
$extraUpdates = []; |
| 409 |
foreach ($updates as $key => $value) { |
| 410 |
if (is_int($key)) { |
| 411 |
$extraUpdates[] = $value; |
| 412 |
} else { |
| 413 |
$extraUpdates[] = sprintf( |
| 414 |
'%s;%s', |
| 415 |
$key, |
| 416 |
$value instanceof Url ? $value->getAbsoluteUrl() : $value |
| 417 |
); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
$this->getResponse()->setHeader('X-Icinga-Extra-Updates', join(',', $extraUpdates)); |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* Close the modal content and refresh the related view |
| 426 |
* |
| 427 |
* NOTE: If you use this with older Icinga Web versions (< 2.12), you will need to specify a valid redirect url, |
| 428 |
* that will produce the same result as using the `__REFRESH__` redirect with the latest Icinga Web version. |
| 429 |
* |
| 430 |
* This is supposed to be used in combination with a modal view and closes only the modal, |
| 431 |
* and refreshes the modal opener (regardless of whether it is col1 or col2). |
| 432 |
* |
| 433 |
* @param Url|string $url |
| 434 |
* @param bool $refreshCol1 Whether to refresh col1 after the redirect. Is just for compatibility reasons and |
| 435 |
* won't be used with latest Icinga Web versions. |
| 436 |
* |
| 437 |
* @return never |
| 438 |
*/ |
| 439 |
public function closeModalAndRefreshRelatedView($url, bool $refreshCol1 = false) |
| 440 |
{ |
| 441 |
if (version_compare(Version::VERSION, '2.12.0', '<')) { |
| 442 |
if (! $url) { |
| 443 |
throw new InvalidArgumentException('No redirect url provided'); |
| 444 |
} |
| 445 |
|
| 446 |
if ($refreshCol1) { |
| 447 |
$this->sendExtraUpdates(['#col1']); |
| 448 |
} |
| 449 |
|
| 450 |
$this->redirectNow($url); |
| 451 |
} else { |
| 452 |
$this->redirectNow('__REFRESH__'); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
/** |
| 457 |
* Close the modal content and refresh all the remaining views |
| 458 |
* |
| 459 |
* NOTE: If you use this with older Icinga Web versions (< 2.12), you will need to specify a valid redirect url, |
| 460 |
* that will produce the same result as using the `__REFRESH__` redirect with the latest Icinga Web version. |
| 461 |
* |
| 462 |
* This is supposed to be used in combination with a modal view and closes only the modal content. It refreshes |
| 463 |
* the modal opener (expects to be always col2) and forces a refresh of col1. |
| 464 |
* |
| 465 |
* @param Url|string $url |
| 466 |
* |
| 467 |
* @return never |
| 468 |
*/ |
| 469 |
public function closeModalAndRefreshRemainingViews($url) |
| 470 |
{ |
| 471 |
$this->sendExtraUpdates(['#col1']); |
| 472 |
|
| 473 |
$this->closeModalAndRefreshRelatedView($url); |
| 474 |
} |
| 475 |
|
| 476 |
/** |
| 477 |
* Redirect using `__CLOSE__` |
| 478 |
* |
| 479 |
* Change to a single column layout and refresh col1 |
| 480 |
* |
| 481 |
* @return never |
| 482 |
*/ |
| 483 |
public function switchToSingleColumnLayout() |
| 484 |
{ |
| 485 |
$this->redirectNow('__CLOSE__'); |
| 486 |
} |
| 487 |
|
| 488 |
public function postDispatch() |
| 489 |
{ |
| 490 |
if (empty($this->parts)) { |
| 491 |
if (! $this->content->isEmpty()) { |
| 492 |
$this->document->prepend($this->content); |
| 493 |
|
| 494 |
if (! $this->view->compact && ! $this->controls->isEmpty()) { |
| 495 |
$this->document->prepend($this->controls); |
| 496 |
} |
| 497 |
|
| 498 |
if (! $this->footer->isEmpty()) { |
| 499 |
$this->document->add($this->footer); |
| 500 |
} |
| 501 |
} |
| 502 |
} else { |
| 503 |
$partSeparator = base64_encode(random_bytes(16)); |
| 504 |
$this->getResponse() |
| 505 |
->setHeader('X-Icinga-Multipart-Content', $partSeparator) |
| 506 |
->setHeader('Content-Type', 'application/vnd.icinga+multipart', true); |
| 507 |
|
| 508 |
$this->document->setSeparator("\n$partSeparator\n"); |
| 509 |
$this->document->add($this->parts); |
| 510 |
} |
| 511 |
|
| 512 |
parent::postDispatch(); |
| 513 |
} |
| 514 |
} |