Skip to content

Commit

Permalink
feature: add ArebrickOverviewController in Bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
mike4git committed Aug 7, 2024
1 parent 37330ad commit 1a55ae5
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"phpstan/phpstan-phpunit": "^1.1",
"phpstan/phpstan-symfony": "^1.2",
"phpunit/phpunit": "^9.3",
"teamneusta/converter-bundle": "^1.6",
"teamneusta/pimcore-testing-framework": "^0.11.1"
},
"extra": {
Expand Down
10 changes: 10 additions & 0 deletions config/packages/neusta_converter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
neusta_converter:
converter:
brick.converter:
target: Neusta\Pimcore\AreabrickConfigBundle\Bricks\Model\BrickView
populators:
- Neusta\Pimcore\AreabrickConfigBundle\Bricks\Populator\BrickPagePopulator
properties:
name: ~
id: ~
tags: ~
3 changes: 3 additions & 0 deletions config/pimcore/routing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
neusta_pimcore_areabrick_config:
resource: '@NeustaPimcoreAreabrickConfigBundle/Controller/Admin/'
type: annotation
24 changes: 24 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
imports:
- { resource: packages/* }
- { resource: pimcore/* }

services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false

Neusta\Pimcore\AreabrickConfigBundle\Controller\Admin\AreabrickOverviewController:
class: Neusta\Pimcore\AreabrickConfigBundle\Controller\Admin\AreabrickOverviewController
public: true
arguments:
$brickConverter: '@brick.converter'
tags: [ 'controller.service_arguments' ]

Neusta\Pimcore\AreabrickConfigBundle\Bricks\Populator\BrickPagePopulator: ~

15 changes: 15 additions & 0 deletions src/Bricks/Model/BrickView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

namespace Neusta\Pimcore\AreabrickConfigBundle\Bricks\Model;

class BrickView
{
public string $name;
public string $id;

/** @var array<string> */
public array $tags;

/** @var array<mixed> */
public array $pageFullPaths;
}
29 changes: 29 additions & 0 deletions src/Bricks/Populator/BrickPagePopulator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

namespace Neusta\Pimcore\AreabrickConfigBundle\Bricks\Populator;

use Neusta\ConverterBundle\Converter\Context\GenericContext;
use Neusta\ConverterBundle\Populator;
use Neusta\Pimcore\AreabrickConfigBundle\Bricks\Model\BrickView;
use Pimcore\Db;
use Pimcore\Extension\Document\Areabrick\AbstractAreabrick;
use Pimcore\Model\Document\Page;

/**
* @implements Populator<AbstractAreabrick, BrickView, GenericContext>
*/
class BrickPagePopulator implements Populator
{
public function populate(object $target, object $source, ?object $ctx = null): void
{
$editableUsages = Db::get()->fetchAllAssociative("SELECT DISTINCT documentId FROM documents_editables WHERE data LIKE '%" . $source->getId() . "%'");
$target->pageFullPaths = [];
foreach (array_values($editableUsages) as $docId) {
$page = Page::getById($docId['documentId']);
$target->pageFullPaths[] = [
$page?->isPublished(),
$page?->getFullPath(),
];
}
}
}
40 changes: 40 additions & 0 deletions src/Controller/Admin/AreabrickOverviewController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);

namespace Neusta\Pimcore\AreabrickConfigBundle\Controller\Admin;

use Neusta\ConverterBundle\Converter;
use Pimcore\Controller\UserAwareController;
use Pimcore\Extension\Document\Areabrick\AreabrickManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

// Todo: check if this can't be accessed outside the admin context in Pimcore 11
final class AreabrickOverviewController extends UserAwareController
{
public function __construct(
private AreabrickManagerInterface $areabrickManager,
private Converter $brickConverter,
) {
}

#[Route('/admin/areabricks/list', name: 'areabrick_overview', methods: ['GET'])]
public function defaultAction(Request $request): Response
{
$ctx = null;

$bricks = array_map(
function ($item) use ($ctx) {
return $this->brickConverter->convert($item, $ctx);
},
$this->areabrickManager->getBricks()
);

return $this->render(
'bricks/default.html.twig',
[
'bricks' => $bricks,
]
);
}
}
17 changes: 17 additions & 0 deletions src/DependencyInjection/NeustaPimcoreAreabrickConfigExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace Neusta\Pimcore\AreabrickConfigBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader;

class NeustaPimcoreAreabrickConfigExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(\dirname(__DIR__, 2) . '/config'));
$loader->load('services.yaml');
}
}
2 changes: 1 addition & 1 deletion src/EditableDialogBox/EditableItem/NumericItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(string $name, int $min, int $max)
public function setDefaultValue(int $value): static
{
if ($this->min > $value || $value > $this->max) {
throw new \InvalidArgumentException(sprintf('Default value "%d" is out of bounds: [%d,%d]', $value, $this->min, $this->max));
throw new \InvalidArgumentException(\sprintf('Default value "%d" is out of bounds: [%d,%d]', $value, $this->min, $this->max));
}

return $this->addConfig('defaultValue', $value);
Expand Down
7 changes: 7 additions & 0 deletions src/NeustaPimcoreAreabrickConfigBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Neusta\Pimcore\AreabrickConfigBundle;

use Neusta\ConverterBundle\NeustaConverterBundle;
use Pimcore\Extension\Bundle\AbstractPimcoreBundle;
use Pimcore\Extension\Bundle\Traits\PackageVersionTrait;
use Pimcore\HttpKernel\BundleCollection\BundleCollection;

final class NeustaPimcoreAreabrickConfigBundle extends AbstractPimcoreBundle
{
Expand All @@ -13,4 +15,9 @@ public function getPath(): string
{
return __DIR__;
}

public static function registerDependentBundles(BundleCollection $collection): void
{
$collection->addBundle(NeustaConverterBundle::class);
}
}
70 changes: 70 additions & 0 deletions templates/bricks/default.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example</title>
</head>

<body>

<style type="text/css">
table {
background-color: #ffffff;
border-collapse: collapse;
border: none;
}
thead {
background-color: #0096f3;
}
tfoot {
background-color: #F7F1D4;
font-size: 80%;
border-top: 1px solid #999;
}
td, th {
text-align: left;
padding: 0.5em 1em;
}
</style>


<div id="site">
<table id="logo">
<thead>
<th>Name</th>
<th>ID</th>
<th>Tags</th>
<th><strike>Unpublished</strike> Pages (using Brick)</th>
</thead>
{% for brick in bricks %}
<tbody>
<td>{{ brick.name }}</td>
<td>{{ brick.id }}</td>
<td>
{% for tag in brick.tags %}
{% if tag in brick.tags %}
{{ tag }},
{% else %}
<strikethrough>{{ tag }}</strikethrough>,
{% endif %}
{% endfor %}
</td>
<td>
{% for fullPath in brick.pageFullPaths %}
{% if fullPath[0] %}
<a href="{{ fullPath[1] }}">{{ fullPath[1] }}</a>,
{% else %}
<strike>{{ fullPath[1] }}</strike>
{% endif %}
{% endfor %}
</td>
</tbody>
{% endfor %}
</table>
</div>

</body>
</html>

0 comments on commit 1a55ae5

Please sign in to comment.