Files
mautic-marketing-program-bu…/Model/PlanModel.php
T
2026-11-07 19:31:49 +01:00

78 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\PlanBundle\Model;
use Mautic\CoreBundle\Model\FormModel;
use MauticPlugin\PlanBundle\Entity\Plan;
use MauticPlugin\PlanBundle\Entity\PlanRepository;
use MauticPlugin\PlanBundle\Form\Type\PlanType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
/**
* @extends FormModel<Plan>
*/
class PlanModel extends FormModel
{
public function getRepository(): PlanRepository
{
$repository = $this->em->getRepository(Plan::class);
\assert($repository instanceof PlanRepository);
return $repository;
}
public function getPermissionBase(): string
{
return 'plan:plans';
}
/**
* With no ID, returns a new plan with the current user as owner.
*/
public function getEntity($id = null): ?Plan
{
if (null === $id) {
$plan = new Plan();
$plan->setOwner($this->userHelper->getUser(true));
return $plan;
}
$plan = parent::getEntity($id);
return $plan instanceof Plan ? $plan : null;
}
/**
* Keeps objective and description item positions contiguous (0..n-1) before saving.
*/
public function saveEntity($entity, $unlock = true): void
{
if ($entity instanceof Plan) {
$entity->normalizePositions();
}
parent::saveEntity($entity, $unlock);
}
/**
* @param array<string, mixed> $options
*/
public function createForm($entity, FormFactoryInterface $formFactory, $action = null, $options = []): FormInterface
{
if (!$entity instanceof Plan) {
throw new MethodNotAllowedHttpException(['Plan']);
}
if (!empty($action)) {
$options['action'] = $action;
}
return $formFactory->create(PlanType::class, $entity, $options);
}
}