Files
mautic-marketing-program-bu…/Controller/TaskController.php
T
Alexandre Gain 9d81d6c815 refactor(naming): normalise the Smart Links domain to the bundle's canonical names (0.18.1)
One bundle, two domains (Marketing Programs and Smart Links), one naming scheme.\nNothing in the database changed (no tables, columns, URLs or stored data);\nthe runtime was rebuilt and verified live.\n\n- Route names mautic_smartlinks_* -> mt3s_smartlinks_* (paths unchanged);\n  the menu ids follow\n- Model service aliases collapsed to one canonical alias per model:\n  mautic.mt3smarketingprogram.model.<entity> (core's mautic.<x>.model.<x>\n  pattern), so getModel('mt3smarketingprogram.smart_link'); the dead\n  mt3s.* and mautic.mt3smp.*-era aliases are gone\n- Translation keys mautic.smartlinks.* -> mt3s.smart_links.*\n  (mautic.config.tab.smartlinksconfig stays: core derives it)\n- Settings parameters smartlinks_utm_config/smartlinks_config ->\n  mt3s_smart_links_utm_config/mt3s_smart_links_config, with both legacy\n  keys (smartlinks_* of the standalone bundle, utm_manager_* of the\n  UtmManagerBundle era) still readable until the settings are saved again\n- JS loader prefixes smartlinksSmartLink/smartlinksUtmTaxonomy ->\n  mt3sMpSmartLink/mt3sMpUtmTaxonomy (the mt3sMp* family of the program\n  domain)\n- Integration/Support/ConfigSupport -> SmartlinksConfigSupport; integration\n  display names aligned ('MT3S Marketing Program' / 'Smart Links'); the\n  dead mautic.plugin.smartlinks.name catalog keys removed\n- phpunit suite renamed UtmManagerBundle -> MT3SMarketingProgramBundle;\n  version 0.18.1\n\nVerified: full php -l, 90/90 unit tests (phpunit 11.5 phar), cache clear +\nwarm + assets regenerate + plugins reload (as www-data), live: both lists\nand forms (mauticContent mt3sMpSmartLink/mt3sMpUtmTaxonomy), both config\ntabs, the menu labels, the program pages (regression), the public /l/y32b\nredirect through the legacy utm_manager_* settings (302 with the taxonomy's\nUTM values, wrong host and unknown slug 404), all ini catalogs parse, zero\nnew log errors.
2026-11-16 15:47:30 +01:00

189 lines
6.6 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Controller;
use Mautic\CoreBundle\Controller\AbstractStandardFormController;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\Program;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\Task;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* List, create, edit and delete tasks (Programs > Tasks), using core's standard CRUD flow.
*
* Model mt3smarketingprogram.task (mautic.mt3smarketingprogram.model.task), permission base mt3smarketingprogram:tasks, translation base mt3s.marketing_program.task.
*
* Where the user lands after Save or Cancel depends on where the task form was opened from:
* - from Programs > Tasks: back to the task list (core's default);
* - from an program's Tasks tab (links with ?returnTo=program): back to that program.
* The origin is kept in the session when the form is opened, because the form posts to a URL
* without the query string.
*/
class TaskController extends AbstractStandardFormController
{
public const ROUTE_INDEX = 'mt3s_marketing_program_task_index';
public const ROUTE_ACTION = 'mt3s_marketing_program_task_action';
/**
* Value of the returnTo query parameter that sends the user back to the program.
*/
public const RETURN_TO_PLAN = 'program';
private const SESSION_RETURN_PLAN = 'mt3s.marketing_program.task.return_program.';
/**
* Program to preselect on a new task (from ?program=ID, used by the program's "Add task" button).
*/
private ?int $preselectedProgramId = null;
/**
* The task saved during this request, if any.
*/
private ?Task $savedTask = null;
/**
* Program to go back to after Save or Cancel, or null for the task list.
*/
private ?int $returnProgramId = null;
protected function getModelName(): string
{
return 'mt3smarketingprogram.task';
}
protected function getTemplateBase(): string
{
return '@MT3SMarketingProgram/Task';
}
protected function getIndexRoute(): string
{
return self::ROUTE_INDEX;
}
protected function getActionRoute(): string
{
return self::ROUTE_ACTION;
}
protected function getJsLoadMethodPrefix(): string
{
return 'mt3sMpProgramTask';
}
/**
* @param int $page
*/
public function indexAction(Request $request, $page = 1): Response
{
return $this->indexStandard($request, $page);
}
public function newAction(Request $request): Response
{
$programId = $request->query->getInt('program');
$this->preselectedProgramId = $programId > 0 ? $programId : null;
$this->returnProgramId = $this->resolveReturnProgram($request, 'new', $this->preselectedProgramId);
return $this->newStandard($request);
}
/**
* @param int $objectId
* @param bool $ignorePost
*/
public function editAction(Request $request, $objectId, $ignorePost = false): Response
{
$task = $this->getModel($this->getModelName())->getEntity((int) $objectId);
$programId = $task instanceof Task ? $task->getProgram()?->getId() : null;
$this->returnProgramId = $this->resolveReturnProgram($request, (string) (int) $objectId, $programId);
return $this->editStandard($request, $objectId, $ignorePost);
}
/**
* When the form is opened (GET), records whether it came from an program (?returnTo=program); when it is
* submitted (POST), reads that record back. Opening the same form from the task list clears it.
*/
private function resolveReturnProgram(Request $request, string $formKey, ?int $programId): ?int
{
$session = $request->getSession();
$key = self::SESSION_RETURN_PLAN.$formKey;
if (!$request->isMethod('POST')) {
$fromProgram = self::RETURN_TO_PLAN === $request->query->get('returnTo') && null !== $programId;
$session->set($key, $fromProgram ? $programId : null);
}
$value = $session->get($key);
return is_int($value) ? $value : null;
}
/**
* @param int $objectId
*/
public function deleteAction(Request $request, $objectId): Response
{
return $this->deleteStandard($request, $objectId);
}
public function batchDeleteAction(Request $request): Response
{
return $this->batchDeleteStandard($request);
}
/**
* @return object|null
*/
protected function getFormEntity($action, &$objectId = null, &$isClone = false)
{
$entity = parent::getFormEntity($action, $objectId, $isClone);
if ('new' === $action && $entity instanceof Task && null === $entity->getProgram() && null !== $this->preselectedProgramId) {
$program = $this->getModel('mt3smarketingprogram.program')->getEntity($this->preselectedProgramId);
if ($program instanceof Program) {
$entity->setProgram($program);
}
}
return $entity;
}
protected function afterEntitySave($entity, Form $form, $action, $pass = null)
{
if ($entity instanceof Task) {
$this->savedTask = $entity;
}
return parent::afterEntitySave($entity, $form, $action, $pass);
}
/**
* After Save or Cancel (not Apply) on a form opened from an program, go back to that program. If the task
* was moved to another program, go to its new program. Otherwise keep core's default: the task list.
*
* @param array<string, mixed> $args
*
* @return array<string, mixed>
*/
protected function getPostActionRedirectArguments(array $args, $action): array
{
if (in_array($action, ['new', 'edit'], true) && null !== $this->returnProgramId) {
$programId = $this->savedTask?->getProgram()?->getId() ?? $this->returnProgramId;
$args['returnUrl'] = $this->generateUrl(ProgramController::ROUTE_ACTION, ['objectAction' => 'view', 'objectId' => $programId]);
$args['contentTemplate'] = ProgramController::class.'::viewAction';
$args['viewParameters'] = ['objectId' => $programId];
// Must match ProgramController::getJsLoadMethodPrefix() / the Program views'
// mauticContent block so Mautic.mt3sMpProgramOnLoad() re-fires on the program page.
$args['passthroughVars'] = array_merge($args['passthroughVars'] ?? [], ['mauticContent' => 'mt3sMpProgram']);
}
return parent::getPostActionRedirectArguments($args, $action);
}
}