Rename the plugin's vendor/namespace from experiential/Program to mt3s/MT3SMarketingProgram (composer: mt3s/mautic-marketing-program-bundle): - ProgramBundle -> MT3SMarketingProgramBundle; extension, integration, config support, permissions, events and twig extension renamed likewise - Initiative* -> Program*: controller (InitiativeController -> ProgramController), model, entities, repositories, form types and views (new _tabs/_task_table/_tab_table_filter partials, details page) - Settings config form theme -> _config_mt3smarketingprogramconfig_widget; routes and translations re-prefixed (mt3s_marketing_program_*) - Assets: icon.svg -> mt3s-mp-icon.svg; program.js -> mt3s-mp-program.js (OnLoad hooks renamed mt3sMpProgram*) - Add Command/RefreshStatsCommand; Migrations/Version_0_17_0.php renames the tables to mt3s_mp_program_*
114 lines
3.5 KiB
PHP
114 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MauticPlugin\MT3SMarketingProgramBundle\Model;
|
|
|
|
use Mautic\CoreBundle\Model\FormModel;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Entity\Program;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Entity\ProgramRepository;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Event\MT3SMarketingProgramEvents;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Event\ProgramEvent;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Form\Type\ProgramType;
|
|
use Symfony\Component\Form\FormFactoryInterface;
|
|
use Symfony\Component\Form\FormInterface;
|
|
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
|
|
|
|
/**
|
|
* @extends FormModel<Program>
|
|
*/
|
|
class ProgramModel extends FormModel
|
|
{
|
|
public function getRepository(): ProgramRepository
|
|
{
|
|
$repository = $this->em->getRepository(Program::class);
|
|
\assert($repository instanceof ProgramRepository);
|
|
|
|
return $repository;
|
|
}
|
|
|
|
public function getPermissionBase(): string
|
|
{
|
|
return 'mt3smarketingprogram:programs';
|
|
}
|
|
|
|
/**
|
|
* With no ID, returns a new program with the current user as owner.
|
|
*/
|
|
public function getEntity($id = null): ?Program
|
|
{
|
|
if (null === $id) {
|
|
$program = new Program();
|
|
$program->setOwner($this->userHelper->getUser(true));
|
|
|
|
return $program;
|
|
}
|
|
|
|
$program = parent::getEntity($id);
|
|
|
|
return $program instanceof Program ? $program : null;
|
|
}
|
|
|
|
/**
|
|
* Keeps objective and description item positions contiguous (0..n-1) before saving, and
|
|
* dispatches the plugin's pre/post save events (created vs updated by the entity's ID).
|
|
*/
|
|
public function saveEntity($entity, $unlock = true): void
|
|
{
|
|
if ($entity instanceof Program) {
|
|
$isNew = null === $entity->getId();
|
|
|
|
$this->dispatcher->dispatch(new ProgramEvent($entity, $isNew), MT3SMarketingProgramEvents::PROGRAM_PRE_SAVE);
|
|
|
|
$entity->normalizePositions();
|
|
|
|
parent::saveEntity($entity, $unlock);
|
|
|
|
$this->dispatcher->dispatch(new ProgramEvent($entity, $isNew), MT3SMarketingProgramEvents::PROGRAM_POST_SAVE);
|
|
$this->dispatcher->dispatch(
|
|
new ProgramEvent($entity, $isNew),
|
|
$isNew ? MT3SMarketingProgramEvents::PROGRAM_CREATED : MT3SMarketingProgramEvents::PROGRAM_UPDATED
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
parent::saveEntity($entity, $unlock);
|
|
}
|
|
|
|
/**
|
|
* Dispatches the plugin's delete events around the removal.
|
|
*/
|
|
public function deleteEntity($entity): void
|
|
{
|
|
if ($entity instanceof Program) {
|
|
$this->dispatcher->dispatch(new ProgramEvent($entity, false), MT3SMarketingProgramEvents::PROGRAM_PRE_DELETE);
|
|
|
|
parent::deleteEntity($entity);
|
|
|
|
$this->dispatcher->dispatch(new ProgramEvent($entity, false), MT3SMarketingProgramEvents::PROGRAM_POST_DELETE);
|
|
$this->dispatcher->dispatch(new ProgramEvent($entity, false), MT3SMarketingProgramEvents::PROGRAM_DELETED);
|
|
|
|
return;
|
|
}
|
|
|
|
parent::deleteEntity($entity);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $options
|
|
*/
|
|
public function createForm($entity, FormFactoryInterface $formFactory, $action = null, $options = []): FormInterface
|
|
{
|
|
if (!$entity instanceof Program) {
|
|
throw new MethodNotAllowedHttpException(['Program']);
|
|
}
|
|
|
|
if (!empty($action)) {
|
|
$options['action'] = $action;
|
|
}
|
|
|
|
return $formFactory->create(ProgramType::class, $entity, $options);
|
|
}
|
|
}
|