Files
mautic-marketing-program-bu…/Controller/TaskController.php
T
Alexandre Gain debce4ba4a fix(program): endless spinner on client-side buttons; program pages never ran their JS
Two defects kept the task form's dependency/RACI rows appearing stuck, and left the program pages uninitialized:

- Add btn-nospin to the 14 client-side Add/Remove buttons (task dependency
  and RACI add/remove, program form collections, item type rows, read-more
  toggle): core binds startIconSpinOnEvent to .btn:not(.btn-nospin) and only
  the ajax handlers restore the icon, so these non-ajax buttons spun forever
- Add ProgramController::getJsLoadMethodPrefix() returning 'mt3sMpProgram':
  core dispatches Mautic[getJsLoadMethodPrefix() + 'OnLoad'](), and without
  the override it fell back to the model alias 'mt3smp.program', which
  nothing listens to, so collections/tabs/sortables were never initialized
- Set the Program list/form/details mauticContent blocks to mt3sMpProgram
  so Mautic.mt3sMpProgramOnLoad() is dispatched on those pages
- TaskController: pass mauticContent=mt3sMpProgram in the post-save
  redirect back to the program view so the program page's JS re-fires
  after saving a task from program context
2026-11-15 17:48:40 +01:00

189 lines
6.5 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 mt3smp.task (mt3s.marketing_program.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 'mt3smp.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('mt3smp.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);
}
}