Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First draft of generic asset controller #18052

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/CommonGLPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,20 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
return '';
}

public static function getSectorizedDetails(): array
{
return [];
}

public static function getHeaderParameters(): array
{
return [
static::getTypeName(\Session::getPluralNumber()),
$_SERVER['PHP_SELF'],
...static::getSectorizedDetails(),
];
}

/**
* show Tab content
*
Expand Down
102 changes: 102 additions & 0 deletions src/Glpi/Controller/Asset/CommonAssetController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Controller\Asset;

use Glpi\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Attribute\Route;

final class CommonAssetController extends AbstractController
{
#[Route("/asset/{type}/search", name: "asset_search")]
public function __invoke(Request $request): Response
{
$type = $request->attributes->getString('type');

$asset_class = $this->getAssetClass($type);

if (!$asset_class) {
throw new NotFoundHttpException('No asset type found');
}

if (!$asset_class::canView()) {
throw new AccessDeniedHttpException();
}

return $this->render('search/asset/common_asset_list.html.twig', [
'asset_class' => $asset_class,
]);
}

/**
* @return class-string<\CommonDBTM>|null
*/
private function getAssetClass(string $type): ?string
{
$class = (new \DbUtils())->fixItemtypeCase($type);

if (
$class
&& \class_exists($class)
&& \is_subclass_of($class, \CommonDBTM::class)
) {
return $this->normalizeClass($class);
}

$namespacedClass = \preg_replace_callback('~\\\([a-z])~Uu', static fn($i) => '\\' . \ucfirst($i[1]), 'Glpi\\' . \str_replace('/', '\\', $class));

if (
$namespacedClass
&& \class_exists($namespacedClass)
&& \is_subclass_of($namespacedClass, \CommonDBTM::class)
) {
return $this->normalizeClass($namespacedClass);
}

return null;
}

private function normalizeClass(string $class): string
{
if (!\class_exists($class)) {
throw new \RuntimeException('Class "$class" does not exist.');
}

return (new \ReflectionClass($class))->getName();
}
}
5 changes: 5 additions & 0 deletions src/Software.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ public function getSpecificMassiveActions($checkitem = null)
return $actions;
}

public static function getSectorizedDetails(): array
{
return ['assets', 'software'];
}

public static function processMassiveActionsForOneItemtype(
MassiveAction $ma,
CommonDBTM $item,
Expand Down
10 changes: 10 additions & 0 deletions templates/search/asset/common_asset_list.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

{% set header_params = call(asset_class ~ '::getHeaderParameters') %}

<pre>{{ asset_class ~ '::getHeaderParameters' }}</pre>

{{ call('Html::header', header_params) }}

{{ call('Search::show', [asset_class]) }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can call Glpi\Search\SearchEngine::show() directly. The old Search class doesn't serve much purpose anymore except backwards compatibility.


{{ call('Html::footer') }}
Loading