Rename the Plan bundle to Program across config, routes, menus, and controllers (Plan -> Initiative), bump version to 0.16.0, and introduce new audience and UTM taxonomy routes and permissions to support the expanded Programs feature set.
187 lines
6.4 KiB
PHP
187 lines
6.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MauticPlugin\ProgramBundle\Controller;
|
|
|
|
use Mautic\CoreBundle\Controller\AbstractStandardFormController;
|
|
use MauticPlugin\ProgramBundle\Entity\Initiative;
|
|
use MauticPlugin\ProgramBundle\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 program.task (mautic.program.model.task), permission base program:tasks, translation base mautic.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 initiative's Tasks tab (links with ?returnTo=initiative): back to that initiative.
|
|
* 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 = 'initiative_task_index';
|
|
public const ROUTE_ACTION = 'initiative_task_action';
|
|
|
|
/**
|
|
* Value of the returnTo query parameter that sends the user back to the initiative.
|
|
*/
|
|
public const RETURN_TO_PLAN = 'initiative';
|
|
|
|
private const SESSION_RETURN_PLAN = 'mautic.program.task.return_initiative.';
|
|
|
|
/**
|
|
* Initiative to preselect on a new task (from ?initiative=ID, used by the initiative's "Add task" button).
|
|
*/
|
|
private ?int $preselectedInitiativeId = null;
|
|
|
|
/**
|
|
* The task saved during this request, if any.
|
|
*/
|
|
private ?Task $savedTask = null;
|
|
|
|
/**
|
|
* Initiative to go back to after Save or Cancel, or null for the task list.
|
|
*/
|
|
private ?int $returnInitiativeId = null;
|
|
|
|
protected function getModelName(): string
|
|
{
|
|
return 'program.task';
|
|
}
|
|
|
|
protected function getTemplateBase(): string
|
|
{
|
|
return '@Program/Task';
|
|
}
|
|
|
|
protected function getIndexRoute(): string
|
|
{
|
|
return self::ROUTE_INDEX;
|
|
}
|
|
|
|
protected function getActionRoute(): string
|
|
{
|
|
return self::ROUTE_ACTION;
|
|
}
|
|
|
|
protected function getJsLoadMethodPrefix(): string
|
|
{
|
|
return 'programTask';
|
|
}
|
|
|
|
/**
|
|
* @param int $page
|
|
*/
|
|
public function indexAction(Request $request, $page = 1): Response
|
|
{
|
|
return $this->indexStandard($request, $page);
|
|
}
|
|
|
|
public function newAction(Request $request): Response
|
|
{
|
|
$initiativeId = $request->query->getInt('initiative');
|
|
$this->preselectedInitiativeId = $initiativeId > 0 ? $initiativeId : null;
|
|
$this->returnInitiativeId = $this->resolveReturnInitiative($request, 'new', $this->preselectedInitiativeId);
|
|
|
|
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);
|
|
$initiativeId = $task instanceof Task ? $task->getInitiative()?->getId() : null;
|
|
$this->returnInitiativeId = $this->resolveReturnInitiative($request, (string) (int) $objectId, $initiativeId);
|
|
|
|
return $this->editStandard($request, $objectId, $ignorePost);
|
|
}
|
|
|
|
/**
|
|
* When the form is opened (GET), records whether it came from an initiative (?returnTo=initiative); when it is
|
|
* submitted (POST), reads that record back. Opening the same form from the task list clears it.
|
|
*/
|
|
private function resolveReturnInitiative(Request $request, string $formKey, ?int $initiativeId): ?int
|
|
{
|
|
$session = $request->getSession();
|
|
$key = self::SESSION_RETURN_PLAN.$formKey;
|
|
|
|
if (!$request->isMethod('POST')) {
|
|
$fromInitiative = self::RETURN_TO_PLAN === $request->query->get('returnTo') && null !== $initiativeId;
|
|
$session->set($key, $fromInitiative ? $initiativeId : 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->getInitiative() && null !== $this->preselectedInitiativeId) {
|
|
$initiative = $this->getModel('program.initiative')->getEntity($this->preselectedInitiativeId);
|
|
if ($initiative instanceof Initiative) {
|
|
$entity->setInitiative($initiative);
|
|
}
|
|
}
|
|
|
|
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 initiative, go back to that initiative. If the task
|
|
* was moved to another initiative, go to its new initiative. 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->returnInitiativeId) {
|
|
$initiativeId = $this->savedTask?->getInitiative()?->getId() ?? $this->returnInitiativeId;
|
|
|
|
$args['returnUrl'] = $this->generateUrl(InitiativeController::ROUTE_ACTION, ['objectAction' => 'view', 'objectId' => $initiativeId]);
|
|
$args['contentTemplate'] = InitiativeController::class.'::viewAction';
|
|
$args['viewParameters'] = ['objectId' => $initiativeId];
|
|
$args['passthroughVars'] = array_merge($args['passthroughVars'] ?? [], ['mauticContent' => 'initiative']);
|
|
}
|
|
|
|
return parent::getPostActionRedirectArguments($args, $action);
|
|
}
|
|
}
|