refactor(program): move item types out of Settings > Configuration
Relocate the item type lists (insights, allocations, objectives, tasks, stakeholders, audience values) from the Configuration tab to a dedicated "Item types" page under Programs, served by ItemTypesController at /program-item-types and guarded by the new program:itemtypes:view permission. The Program settings tab in Configuration now only holds the general settings (currency, stakeholder phone region, audience attribute toggles), so ConfigSubscriber no longer needs the item repositories; the in-use type protection moved to ItemTypeInUseGuard.
This commit is contained in:
1 parent
71ff11390b
commit
687d33d30c
14 files changed
+444
-219
No files matched your search
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MauticPlugin\ProgramBundle\Controller;
|
||||
|
||||
use Mautic\CoreBundle\Configurator\Configurator;
|
||||
use Mautic\CoreBundle\Controller\AbstractFormController;
|
||||
use Mautic\CoreBundle\Helper\CacheHelper;
|
||||
use MauticPlugin\ProgramBundle\Form\Type\ItemTypesType;
|
||||
use MauticPlugin\ProgramBundle\Service\ItemTypeInUseGuard;
|
||||
use MauticPlugin\ProgramBundle\Service\ItemTypeProvider;
|
||||
use Symfony\Component\Form\FormError;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* The Item types page (Programs > Item types): the type lists of the plugin's items — insights
|
||||
* (description items), allocations, objectives, tasks, stakeholders, and the values offered for
|
||||
* an audience's decision roles and tones. Formerly part of Settings > Configuration > Program
|
||||
* Settings; now on its own page under Programs, so a user with the program:itemtypes permissions
|
||||
* manages the types without being an administrator.
|
||||
*
|
||||
* Each list is a local.php parameter (ItemTypeProvider::PARAMETERS), saved through core's
|
||||
* Configurator exactly like Settings > Configuration saves its own forms: merge, write, refresh
|
||||
* the config. A type that items still use cannot be removed (ItemTypeInUseGuard).
|
||||
*
|
||||
* Permissions: program:itemtypes:view shows the page, program:itemtypes:edit saves it.
|
||||
*/
|
||||
class ItemTypesController extends AbstractFormController
|
||||
{
|
||||
public const ROUTE_INDEX = 'program_item_types_index';
|
||||
|
||||
/**
|
||||
* Shows the lists, and saves them on POST.
|
||||
*/
|
||||
public function indexAction(
|
||||
Request $request,
|
||||
FormFactoryInterface $formFactory,
|
||||
ItemTypeProvider $typeProvider,
|
||||
ItemTypeInUseGuard $inUseGuard,
|
||||
Configurator $configurator,
|
||||
CacheHelper $cacheHelper,
|
||||
): Response {
|
||||
if (!$this->security->isGranted('program:itemtypes:view')) {
|
||||
$this->throwAccessDenied();
|
||||
}
|
||||
|
||||
$action = $this->generateUrl(self::ROUTE_INDEX);
|
||||
|
||||
// The current lists, each keyed by the local.php parameter that stores it.
|
||||
$data = [];
|
||||
foreach (ItemTypeProvider::PARAMETERS as $kind => $parameter) {
|
||||
$data[$parameter] = $typeProvider->getTypes($kind);
|
||||
}
|
||||
|
||||
$form = $formFactory->create(ItemTypesType::class, $data, ['action' => $action]);
|
||||
$isWritable = $configurator->isFileWritable();
|
||||
|
||||
if ('POST' === $request->getMethod()) {
|
||||
if (!$this->security->isGranted('program:itemtypes:edit')) {
|
||||
$this->throwAccessDenied();
|
||||
}
|
||||
|
||||
// Cancel: back to a fresh form, nothing saved.
|
||||
if ($this->isFormCancelled($form)) {
|
||||
return new RedirectResponse($action);
|
||||
}
|
||||
|
||||
if ($isWritable && $this->isFormValid($form)) {
|
||||
/** @var array<string, mixed> $formData local.php parameter => its types */
|
||||
$formData = $form->getData();
|
||||
|
||||
$removedInUse = $inUseGuard->getRemovedInUseTypes($formData);
|
||||
|
||||
if ([] !== $removedInUse) {
|
||||
foreach ($removedInUse as $names) {
|
||||
$form->addError(new FormError(
|
||||
$this->translator->trans('mautic.program.config.type.in_use', ['%types%' => implode(', ', $names)], 'validators')
|
||||
));
|
||||
}
|
||||
} else {
|
||||
// Merge each list into the local configuration, then write — the same thing
|
||||
// Settings > Configuration does with its own forms.
|
||||
foreach (ItemTypeProvider::PARAMETERS as $kind => $parameter) {
|
||||
if (array_key_exists($parameter, $formData)) {
|
||||
$configurator->mergeParameters([$parameter => $formData[$parameter]]);
|
||||
}
|
||||
}
|
||||
|
||||
$configurator->write();
|
||||
|
||||
$this->addFlashMessage('mautic.program.item_type.saved');
|
||||
|
||||
// local.php changed: the cached container is stale until refreshed.
|
||||
$cacheHelper->refreshConfig();
|
||||
|
||||
return new RedirectResponse($action);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->delegateView([
|
||||
'viewParameters' => [
|
||||
'form' => $form->createView(),
|
||||
'isWritable' => $isWritable,
|
||||
],
|
||||
'contentTemplate' => '@Program/ItemTypes/form.html.twig',
|
||||
'passthroughVars' => [
|
||||
'activeLink' => '#'.self::ROUTE_INDEX,
|
||||
'mauticContent' => 'programItemTypes',
|
||||
'route' => $action,
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user