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

Extract manuallink form to a controller #18486

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
108 changes: 0 additions & 108 deletions front/manuallink.form.php

This file was deleted.

89 changes: 89 additions & 0 deletions src/Glpi/Controller/ItemType/Form/ManualLinkFormController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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\ItemType\Form;

use Glpi\Controller\GenericFormController;
use Glpi\Routing\Attribute\ItemtypeFormLegacyRoute;
use Glpi\Routing\Attribute\ItemtypeFormRoute;
use ManualLink;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Glpi\Exception\Http\NotFoundHttpException;

class ManualLinkFormController extends GenericFormController
{
#[ItemtypeFormRoute(ManualLink::class)]
#[ItemtypeFormLegacyRoute(ManualLink::class)]
public function __invoke(Request $request): Response
{
$request->attributes->set('class', ManualLink::class);

if ($request->query->has('id') && !(new ManualLink())->getFromDB($_REQUEST['id'])) {
throw new NotFoundHttpException('No item found for given id');
}

if (
$request->query->has('id')
&& $request->query->get('itemtype')
&& $request->query->get('items_id')
) {
return $this->handleItemTypes($request);
}

return parent::__invoke($request);
}

public function handleItemTypes(Request $request): Response
{
$link = new ManualLink();
$id = $link->isNewItem() ? null : $link->fields['id'];
$itemtype = $link->isNewItem() ? $request->query->get('itemtype') : $link->fields['itemtype'];
$items_id = $link->isNewItem() ? $request->query->get('items_id') : $link->fields['items_id'];

$form_options = $link->getFormOptionsFromUrl($request->query->all());
$form_options['formoptions'] = 'data-track-changes=true';
if ($link->maybeTemplate()) {
$form_options['withtemplate'] = $request->query->get('withtemplate', '');
}
$form_options['itemtype'] = $itemtype;
$form_options['items_id'] = $items_id;

return $this->render('pages/generic_form.html.twig', [
'id' => $id,
'object_class' => $link::class,
'form_options' => $form_options,
]);
}
}
5 changes: 5 additions & 0 deletions src/ManualLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public static function getTypeName($nb = 0)
return _n('Manual link', 'Manual links', $nb);
}

public static function getLogDefaultServiceName(): string
{
return 'tools';
}

public function getLogTypeID()
{
return [$this->fields['itemtype'], $this->fields['items_id']];
Expand Down
Loading