Spaces:
No application file
No application file
File size: 5,798 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
<?php
namespace Mautic\CategoryBundle\Model;
use Doctrine\ORM\EntityManager;
use Mautic\CategoryBundle\CategoryEvents;
use Mautic\CategoryBundle\Entity\Category;
use Mautic\CategoryBundle\Entity\CategoryRepository;
use Mautic\CategoryBundle\Event\CategoryEvent;
use Mautic\CategoryBundle\Form\Type\CategoryType;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Helper\UserHelper;
use Mautic\CoreBundle\Model\FormModel;
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
use Mautic\CoreBundle\Translation\Translator;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\EventDispatcher\Event;
/**
* @extends FormModel<Category>
*/
class CategoryModel extends FormModel
{
/**
* @var array<string,mixed[]>
*/
private array $categoriesByBundleCache = [];
public function __construct(
protected RequestStack $requestStack,
EntityManager $em,
CorePermissions $security,
EventDispatcherInterface $dispatcher,
UrlGeneratorInterface $router,
Translator $translator,
UserHelper $userHelper,
LoggerInterface $mauticLogger,
CoreParametersHelper $coreParametersHelper
) {
parent::__construct($em, $security, $dispatcher, $router, $translator, $userHelper, $mauticLogger, $coreParametersHelper);
}
public function getRepository(): CategoryRepository
{
$repository = $this->em->getRepository(Category::class);
\assert($repository instanceof CategoryRepository);
return $repository;
}
public function getNameGetter(): string
{
return 'getTitle';
}
public function getPermissionBase($bundle = null): string
{
if (null === $bundle) {
$bundle = $this->requestStack->getCurrentRequest()->get('bundle');
}
if ('global' === $bundle || empty($bundle)) {
$bundle = 'category';
}
return $bundle.':categories';
}
public function saveEntity($entity, $unlock = true): void
{
$alias = $entity->getAlias();
if (empty($alias)) {
$alias = $entity->getTitle();
}
$alias = $this->cleanAlias($alias, '', 0, '-');
// make sure alias is not already taken
$repo = $this->getRepository();
$testAlias = $alias;
$bundle = $entity->getBundle();
$count = $repo->checkUniqueCategoryAlias($bundle, $testAlias, $entity);
$aliasTag = $count;
while ($count) {
$testAlias = $alias.$aliasTag;
$count = $repo->checkUniqueCategoryAlias($bundle, $testAlias, $entity);
++$aliasTag;
}
if ($testAlias != $alias) {
$alias = $testAlias;
}
$entity->setAlias($alias);
parent::saveEntity($entity, $unlock);
}
/**
* @param string|null $action
* @param array $options
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function createForm($entity, FormFactoryInterface $formFactory, $action = null, $options = []): \Symfony\Component\Form\FormInterface
{
if (!$entity instanceof Category) {
throw new MethodNotAllowedHttpException(['Category']);
}
if (!empty($action)) {
$options['action'] = $action;
}
return $formFactory->create(CategoryType::class, $entity, $options);
}
/**
* Get a specific entity or generate a new one if id is empty.
*/
public function getEntity($id = null): ?Category
{
if (null === $id) {
return new Category();
}
return parent::getEntity($id);
}
/**
* @throws MethodNotAllowedHttpException
*/
protected function dispatchEvent($action, &$entity, $isNew = false, Event $event = null): ?Event
{
if (!$entity instanceof Category) {
throw new MethodNotAllowedHttpException(['Category']);
}
switch ($action) {
case 'pre_save':
$name = CategoryEvents::CATEGORY_PRE_SAVE;
break;
case 'post_save':
$name = CategoryEvents::CATEGORY_POST_SAVE;
break;
case 'pre_delete':
$name = CategoryEvents::CATEGORY_PRE_DELETE;
break;
case 'post_delete':
$name = CategoryEvents::CATEGORY_POST_DELETE;
break;
default:
return null;
}
if ($this->dispatcher->hasListeners($name)) {
if (empty($event)) {
$event = new CategoryEvent($entity, $isNew);
$event->setEntityManager($this->em);
}
$this->dispatcher->dispatch($event, $name);
return $event;
} else {
return null;
}
}
/**
* Get list of entities for autopopulate fields.
*
* @param string $bundle
* @param string $filter
* @param int $limit
*
* @return mixed[]
*/
public function getLookupResults($bundle, $filter = '', $limit = 10): array
{
$key = $bundle.$filter.$limit;
if (!empty($this->categoriesByBundleCache[$key])) {
return $this->categoriesByBundleCache[$key];
}
return $this->categoriesByBundleCache[$key] = $this->getRepository()->getCategoryList($bundle, $filter, $limit, 0);
}
}
|