Files
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

178 lines
5.5 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Controller;
use Mautic\CoreBundle\Controller\AbstractStandardFormController;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\SmartLink;
use MauticPlugin\MT3SMarketingProgramBundle\Model\SmartLinkModel;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Create, edit and delete Smart Links (main menu > Smart Links), using core's standard CRUD flow.
*
* Model mt3smarketingprogram.smart_link (mautic.mt3smarketingprogram.model.smart_link), permission base
* mt3smarketingprogram:smart_links, translation base mt3s.smart_links.smart_links.
*
* The slug of a saved link is immutable: the form disables the field, the model refuses another
* slug, and this controller drops the submitted value (nothing to re-check). There is no clone:
* a new link gets a new slug (the "Generate" button offers one).
*/
class SmartLinkController extends AbstractStandardFormController
{
public const ROUTE_INDEX = 'mt3s_smart_links_index';
public const ROUTE_ACTION = 'mt3s_smart_links_action';
protected function getModelName(): string
{
return 'mt3smarketingprogram.smart_link';
}
protected function getTemplateBase(): string
{
return '@MT3SMarketingProgram/SmartLink';
}
protected function getIndexRoute(): string
{
return self::ROUTE_INDEX;
}
protected function getActionRoute(): string
{
return self::ROUTE_ACTION;
}
/**
* The OnLoad JS prefix: core dispatches Mautic[getJsLoadMethodPrefix() + "OnLoad"]()
* (Mautic.mt3sMpSmartLinkOnLoad in Assets/js/mt3s-mp-smartlinks.js). Without this
* override, core falls back to the model name, which nothing listens to.
*/
protected function getJsLoadMethodPrefix(): string
{
return 'mt3sMpSmartLink';
}
/**
* Core builds mautic.<model name> (singular smart_link); every key of the feature lives
* under mt3s.smart_links.smart_links.
*/
protected function getTranslationBase(): string
{
return 'mt3s.smart_links.smart_links';
}
/**
* @param int $page
*/
public function indexAction(Request $request, $page = 1): Response
{
return $this->indexStandard($request, $page);
}
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);
}
/**
* Never save the slug of an existing link: it is immutable once created. The field is
* disabled in the form, so nothing is submitted for it, this is a safety net.
*
* @param SmartLink $entity
* @param string $action
* @param int|null $objectId
* @param bool $isClone
*/
protected function beforeEntitySave($entity, Form $form, $action, $objectId = null, $isClone = false): bool
{
if ($entity instanceof SmartLink && null !== $entity->getId()) {
$formSlug = $form->has('slug') ? (string) $form->get('slug')->getData() : '';
if ('' !== $formSlug && $formSlug !== (string) $entity->getSlug()) {
$this->addFlashMessage('mt3s.smart_links.smart_links.error.slug_immutable', ['%slug%' => $formSlug], \Mautic\CoreBundle\Service\FlashBag::LEVEL_ERROR);
return false;
}
}
return parent::beforeEntitySave($entity, $form, $action, $objectId, $isClone);
}
/**
* The list shows the short link, built from the configured domain (a reminder of it is
* also displayed above the form of a link), and the number of hits of each link.
*
* @param array<string, mixed> $args
* @param string $action
*
* @return array<string, mixed>
*/
protected function getViewArguments(array $args, $action): array
{
$args = parent::getViewArguments($args, $action);
if ('index' === $action) {
$model = $this->getModel($this->getModelName());
\assert($model instanceof SmartLinkModel);
$args['viewParameters']['shortLinkDomain'] = $model->getShortLinkDomain();
$args['viewParameters']['hitCounts'] = $model->getHitCounts();
}
return $args;
}
/**
* Order by slug: the entity has no "name" column.
*/
protected function getDefaultOrderColumn(): string
{
return 'slug';
}
/**
* Smart Links don't have a detail page; after Save or Cancel go back to the list.
*
* @param string $action
*/
protected function getPostActionControllerAction($action): string
{
return 'index';
}
/**
* A saved Smart Link is never cloned: its slug is immutable, and a clone would need a new one.
*
* @param int|null $objectId
*/
public function cloneAction(Request $request, $objectId): Response
{
$this->throwAccessDenied();
}
}