Files
mautic-marketing-program-bu…/Controller/TaskController.php
T
Alexandre Gain b138056e43 style: fix indentation, alignment, and docblock annotations
- Correct getChoices() docblock to reflect translated label => enum
  backing value order instead of value => label
- Re-indent Smart Links menu config to match actual array nesting
- Align assignment operators in RefreshStatsCommand
- Add missing newline at end of RefreshStatsCommand.php
2026-11-21 11:23:33 +01:00

201 lines
6.8 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);
}
/**
* @param string $action
* @param int|null $objectId
* @param bool $isClone
*
* @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;
}
/**
* @param Task $entity
* @param string $action
* @param mixed $pass
*
* @return mixed
*/
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
* @param string $action
*
* @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);
}
}