icingaweb2-module-businessp.../library/Businessprocess/Renderer/TileRenderer.php
Johannes Meyer 52c150c56b Use the new Sort trait where applicable
Moves the entire order processing to the renderers as that's
where it's mostly relevant. The only cases where nodes are
still ordered outside the rendering is where changes are
applied based on user input, which happened based on what's
been previously rendered.
2023-08-03 15:19:28 +02:00

85 lines
2.7 KiB
PHP

<?php
namespace Icinga\Module\Businessprocess\Renderer;
use Icinga\Module\Businessprocess\ImportedNode;
use Icinga\Module\Businessprocess\Renderer\TileRenderer\NodeTile;
use Icinga\Module\Businessprocess\Web\Form\CsrfToken;
use ipl\Html\Html;
class TileRenderer extends Renderer
{
public function assemble()
{
$bp = $this->config;
$nodesDiv = Html::tag(
'div',
[
'class' => ['sortable', 'tiles', $this->howMany()],
'data-base-target' => '_self',
'data-sortable-disabled' => $this->isLocked() || $this->appliesCustomSorting()
? 'true'
: 'false',
'data-sortable-data-id-attr' => 'id',
'data-sortable-direction' => 'horizontal', // Otherwise movement is buggy on small lists
'data-csrf-token' => CsrfToken::generate()
]
);
if ($this->wantsRootNodes()) {
$nodesDiv->getAttributes()->add(
'data-action-url',
$this->getUrl()->with(['config' => $bp->getName()])->getAbsoluteUrl()
);
} else {
$nodeName = $this->parent instanceof ImportedNode
? $this->parent->getNodeName()
: $this->parent->getName();
$nodesDiv->getAttributes()
->add('data-node-name', $nodeName)
->add('data-action-url', $this->getUrl()
->with([
'config' => $this->parent->getBpConfig()->getName(),
'node' => $nodeName
])
->getAbsoluteUrl());
}
$path = $this->getCurrentPath();
foreach ($this->sort($this->getChildNodes()) as $name => $node) {
$this->add(new NodeTile($this, $node, $path));
}
if ($this->wantsRootNodes()) {
$unbound = $this->createUnboundParent($bp);
if ($unbound->hasChildren()) {
$this->add(new NodeTile($this, $unbound));
}
}
$nodesDiv->addHtml(...$this->getContent());
$this->setHtmlContent($nodesDiv);
}
/**
* A CSS class giving a rough indication of how many nodes we have
*
* This is used to show larger tiles when there are few and smaller
* ones if there are many.
*
* @return string
*/
protected function howMany()
{
$count = $this->countChildNodes();
$howMany = 'normal';
if ($count <= 6) {
$howMany = 'few';
} elseif ($count > 12) {
$howMany = 'many';
}
return $howMany;
}
}