Skip to content

Commit

Permalink
First draft of generic asset controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierstoval committed Oct 14, 2024
1 parent 174e57e commit 8995174
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
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
101 changes: 101 additions & 0 deletions src/Glpi/Controller/GenericListcontroller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?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;

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 GenericListcontroller extends AbstractController
{
#[Route("/{type}/search", name: "generic_list")]
public function __invoke(Request $request): Response
{
$type = $request->attributes->getString('type');

$class = $this->getClassFromType($type);

if (!$class) {
throw new NotFoundHttpException(\sprintf('No class found for type "%s".', $type));
}

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

return $this->render('search/generic_list.html.twig', [
'object_class' => $class,
]);
}

/**
* @return class-string<\CommonDBTM>|null
*/
private function getClassFromType(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
39 changes: 39 additions & 0 deletions templates/search/generic_list.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{#
# ---------------------------------------------------------------------
#
# 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/>.
#
# ---------------------------------------------------------------------
#}

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

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

{{ call('Glpi\\Search\\SearchEngine::show', [object_class]) }}

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

0 comments on commit 8995174

Please sign in to comment.