first delivery
This commit is contained in:
1 parent
793fbc0c91
commit
f6684f3b2e
65 files changed
+5828
No files matched your search
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MauticPlugin\PlanBundle\Controller;
|
||||
|
||||
use Mautic\CoreBundle\Controller\AbstractStandardFormController;
|
||||
use MauticPlugin\PlanBundle\Entity\Plan;
|
||||
use MauticPlugin\PlanBundle\Model\TaskModel;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* List, view, create, edit and delete plans, using core's standard CRUD flow.
|
||||
*
|
||||
* Core derives most names from the model name "plan": model mautic.plan.model.plan, permission base
|
||||
* plan:plans (from PlanModel), translation base mautic.plan, session base mautic.plan.
|
||||
*/
|
||||
class PlanController extends AbstractStandardFormController
|
||||
{
|
||||
public const ROUTE_INDEX = 'plan_index';
|
||||
public const ROUTE_ACTION = 'plan_action';
|
||||
|
||||
protected function getModelName(): string
|
||||
{
|
||||
return 'plan';
|
||||
}
|
||||
|
||||
protected function getTemplateBase(): string
|
||||
{
|
||||
return '@Plan/Plan';
|
||||
}
|
||||
|
||||
/**
|
||||
* Core would build mautic_plan_index; the plugin's routes use the plan_ prefix.
|
||||
*/
|
||||
protected function getIndexRoute(): string
|
||||
{
|
||||
return self::ROUTE_INDEX;
|
||||
}
|
||||
|
||||
protected function getActionRoute(): string
|
||||
{
|
||||
return self::ROUTE_ACTION;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $page
|
||||
*/
|
||||
public function indexAction(Request $request, $page = 1): Response
|
||||
{
|
||||
return $this->indexStandard($request, $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* The plan detail page (tabs: Details, Description, Objectives, Allocation, Targets, Tasks).
|
||||
*
|
||||
* Core's viewStandard() isn't used: for users who aren't administrators it checks an
|
||||
* "<entity>:export:enable" permission, which plans don't have.
|
||||
*
|
||||
* @param int $objectId
|
||||
*/
|
||||
public function viewAction(Request $request, $objectId): Response
|
||||
{
|
||||
$plan = $this->getModel($this->getModelName())->getEntity((int) $objectId);
|
||||
|
||||
if (!$plan instanceof Plan) {
|
||||
return $this->notFound('mautic.plan.error.notfound');
|
||||
}
|
||||
|
||||
if (!$this->checkActionPermission('view', $plan)) {
|
||||
return $this->accessDenied();
|
||||
}
|
||||
|
||||
$base = $this->getPermissionBase();
|
||||
$permissions = $this->security->isGranted(
|
||||
[
|
||||
$base.':create',
|
||||
$base.':editown',
|
||||
$base.':editother',
|
||||
$base.':deleteown',
|
||||
$base.':deleteother',
|
||||
],
|
||||
'RETURN_ARRAY',
|
||||
null,
|
||||
true
|
||||
);
|
||||
|
||||
$taskPermissions = $this->security->isGranted(
|
||||
[
|
||||
'plan:tasks:create',
|
||||
'plan:tasks:editown',
|
||||
'plan:tasks:editother',
|
||||
],
|
||||
'RETURN_ARRAY',
|
||||
null,
|
||||
true
|
||||
);
|
||||
|
||||
return $this->delegateView([
|
||||
'viewParameters' => [
|
||||
'item' => $plan,
|
||||
'permissions' => $permissions,
|
||||
'taskTree' => $this->getTaskModel()->getTaskTree($plan),
|
||||
'taskPermissions' => $taskPermissions,
|
||||
// Core's page_actions helper would otherwise build mautic_plan_* route names.
|
||||
'indexRoute' => self::ROUTE_INDEX,
|
||||
'actionRoute' => self::ROUTE_ACTION,
|
||||
'tmpl' => $request->isXmlHttpRequest() ? $request->get('tmpl', 'details') : 'details',
|
||||
],
|
||||
'contentTemplate' => $this->getTemplateName('details.html.twig'),
|
||||
'passthroughVars' => [
|
||||
'activeLink' => '#plan_index',
|
||||
'mauticContent' => $this->getJsLoadMethodPrefix(),
|
||||
'route' => $this->generateUrl(self::ROUTE_ACTION, ['objectAction' => 'view', 'objectId' => $plan->getId()]),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the plan's task tree to the form, for the read-only Tasks tab.
|
||||
*
|
||||
* @param array<string, mixed> $args
|
||||
* @param string $action
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function getViewArguments(array $args, $action): array
|
||||
{
|
||||
$plan = $args['viewParameters']['entity'] ?? null;
|
||||
|
||||
if (in_array($action, ['new', 'edit'], true) && $plan instanceof Plan && null !== $plan->getId()) {
|
||||
$args['viewParameters']['taskTree'] = $this->getTaskModel()->getTaskTree($plan);
|
||||
}
|
||||
|
||||
return parent::getViewArguments($args, $action);
|
||||
}
|
||||
|
||||
private function getTaskModel(): TaskModel
|
||||
{
|
||||
$model = $this->getModel('plan.task');
|
||||
\assert($model instanceof TaskModel);
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function newAction(Request $request): Response
|
||||
{
|
||||
return $this->newStandard($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $objectId
|
||||
* @param bool $ignorePost
|
||||
*/
|
||||
public function editAction(Request $request, $objectId, $ignorePost = false): Response
|
||||
{
|
||||
return $this->editStandard($request, $objectId, $ignorePost);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MauticPlugin\PlanBundle\Controller;
|
||||
|
||||
use Mautic\CoreBundle\Controller\AbstractStandardFormController;
|
||||
use MauticPlugin\PlanBundle\Entity\Plan;
|
||||
use MauticPlugin\PlanBundle\Entity\Task;
|
||||
use Symfony\Component\Form\Form;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* List, create, edit and delete tasks (Planning > Tasks), using core's standard CRUD flow.
|
||||
*
|
||||
* Model plan.task (mautic.plan.model.task), permission base plan:tasks, translation base mautic.plan.task.
|
||||
*
|
||||
* Where the user lands after Save or Cancel depends on where the task form was opened from:
|
||||
* - from Planning > Tasks: back to the task list (core's default);
|
||||
* - from a plan's Tasks tab (links with ?returnTo=plan): back to that plan.
|
||||
* 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 = 'plan_task_index';
|
||||
public const ROUTE_ACTION = 'plan_task_action';
|
||||
|
||||
/**
|
||||
* Value of the returnTo query parameter that sends the user back to the plan.
|
||||
*/
|
||||
public const RETURN_TO_PLAN = 'plan';
|
||||
|
||||
private const SESSION_RETURN_PLAN = 'mautic.plan.task.return_plan.';
|
||||
|
||||
/**
|
||||
* Plan to preselect on a new task (from ?plan=ID, used by the plan's "Add task" button).
|
||||
*/
|
||||
private ?int $preselectedPlanId = null;
|
||||
|
||||
/**
|
||||
* The task saved during this request, if any.
|
||||
*/
|
||||
private ?Task $savedTask = null;
|
||||
|
||||
/**
|
||||
* Plan to go back to after Save or Cancel, or null for the task list.
|
||||
*/
|
||||
private ?int $returnPlanId = null;
|
||||
|
||||
protected function getModelName(): string
|
||||
{
|
||||
return 'plan.task';
|
||||
}
|
||||
|
||||
protected function getTemplateBase(): string
|
||||
{
|
||||
return '@Plan/Task';
|
||||
}
|
||||
|
||||
protected function getIndexRoute(): string
|
||||
{
|
||||
return self::ROUTE_INDEX;
|
||||
}
|
||||
|
||||
protected function getActionRoute(): string
|
||||
{
|
||||
return self::ROUTE_ACTION;
|
||||
}
|
||||
|
||||
protected function getJsLoadMethodPrefix(): string
|
||||
{
|
||||
return 'planTask';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $page
|
||||
*/
|
||||
public function indexAction(Request $request, $page = 1): Response
|
||||
{
|
||||
return $this->indexStandard($request, $page);
|
||||
}
|
||||
|
||||
public function newAction(Request $request): Response
|
||||
{
|
||||
$planId = $request->query->getInt('plan');
|
||||
$this->preselectedPlanId = $planId > 0 ? $planId : null;
|
||||
$this->returnPlanId = $this->resolveReturnPlan($request, 'new', $this->preselectedPlanId);
|
||||
|
||||
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);
|
||||
$planId = $task instanceof Task ? $task->getPlan()?->getId() : null;
|
||||
$this->returnPlanId = $this->resolveReturnPlan($request, (string) (int) $objectId, $planId);
|
||||
|
||||
return $this->editStandard($request, $objectId, $ignorePost);
|
||||
}
|
||||
|
||||
/**
|
||||
* When the form is opened (GET), records whether it came from a plan (?returnTo=plan); when it is
|
||||
* submitted (POST), reads that record back. Opening the same form from the task list clears it.
|
||||
*/
|
||||
private function resolveReturnPlan(Request $request, string $formKey, ?int $planId): ?int
|
||||
{
|
||||
$session = $request->getSession();
|
||||
$key = self::SESSION_RETURN_PLAN.$formKey;
|
||||
|
||||
if (!$request->isMethod('POST')) {
|
||||
$fromPlan = self::RETURN_TO_PLAN === $request->query->get('returnTo') && null !== $planId;
|
||||
$session->set($key, $fromPlan ? $planId : 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->getPlan() && null !== $this->preselectedPlanId) {
|
||||
$plan = $this->getModel('plan')->getEntity($this->preselectedPlanId);
|
||||
if ($plan instanceof Plan) {
|
||||
$entity->setPlan($plan);
|
||||
}
|
||||
}
|
||||
|
||||
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 a plan, go back to that plan. If the task
|
||||
* was moved to another plan, go to its new plan. 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->returnPlanId) {
|
||||
$planId = $this->savedTask?->getPlan()?->getId() ?? $this->returnPlanId;
|
||||
|
||||
$args['returnUrl'] = $this->generateUrl(PlanController::ROUTE_ACTION, ['objectAction' => 'view', 'objectId' => $planId]);
|
||||
$args['contentTemplate'] = PlanController::class.'::viewAction';
|
||||
$args['viewParameters'] = ['objectId' => $planId];
|
||||
$args['passthroughVars'] = array_merge($args['passthroughVars'] ?? [], ['mauticContent' => 'plan']);
|
||||
}
|
||||
|
||||
return parent::getPostActionRedirectArguments($args, $action);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user