Spaces:
No application file
No application file
File size: 8,478 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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
<?php
namespace Mautic\ChannelBundle\Model;
use Doctrine\ORM\EntityManager;
use Mautic\CampaignBundle\Model\CampaignModel;
use Mautic\ChannelBundle\ChannelEvents;
use Mautic\ChannelBundle\Entity\Message;
use Mautic\ChannelBundle\Entity\MessageRepository;
use Mautic\ChannelBundle\Event\MessageEvent;
use Mautic\ChannelBundle\Form\Type\MessageType;
use Mautic\ChannelBundle\Helper\ChannelListHelper;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Helper\UserHelper;
use Mautic\CoreBundle\Model\AjaxLookupModelInterface;
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\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\EventDispatcher\Event;
/**
* @extends FormModel<Message>
*
* @implements AjaxLookupModelInterface<Message>
*/
class MessageModel extends FormModel implements AjaxLookupModelInterface
{
public const CHANNEL_FEATURE = 'marketing_messages';
protected static $channels;
public function __construct(
protected ChannelListHelper $channelListHelper,
protected CampaignModel $campaignModel,
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);
}
/**
* @param Message $entity
* @param bool $unlock
*/
public function saveEntity($entity, $unlock = true): void
{
$isNew = $entity->isNew();
parent::saveEntity($entity, $unlock);
if (!$isNew) {
// Update the channels
$channels = $entity->getChannels();
foreach ($channels as $channel) {
$channel->setMessage($entity);
}
$this->getRepository()->saveEntities($channels);
}
}
public function getPermissionBase(): string
{
return 'channel:messages';
}
public function getRepository(): ?MessageRepository
{
return $this->em->getRepository(Message::class);
}
public function getEntity($id = null): ?Message
{
if (null === $id) {
return new Message();
}
return parent::getEntity($id);
}
/**
* @param object $entity
* @param array $options
*
* @return \Symfony\Component\Form\FormInterface<mixed>
*/
public function createForm($entity, FormFactoryInterface $formFactory, $action = null, $options = []): \Symfony\Component\Form\FormInterface
{
if (!empty($action)) {
$options['action'] = $action;
}
return $formFactory->create(MessageType::class, $entity, $options);
}
/**
* @return array
*/
public function getChannels()
{
if (!self::$channels) {
$channels = $this->channelListHelper->getFeatureChannels(self::CHANNEL_FEATURE);
// Validate channel configs
foreach ($channels as $channel => $config) {
if (!isset($config['lookupFormType']) && !isset($config['propertiesFormType'])) {
throw new \InvalidArgumentException('lookupFormType and/or propertiesFormType are required for channel '.$channel);
}
$label = match (true) {
$this->translator->hasId('mautic.channel.'.$channel) => $this->translator->trans('mautic.channel.'.$channel),
$this->translator->hasId('mautic.'.$channel) => $this->translator->trans('mautic.'.$channel),
$this->translator->hasId('mautic.'.$channel.'.'.$channel) => $this->translator->trans('mautic.'.$channel.'.'.$channel),
default => ucfirst($channel),
};
$config['label'] = $label;
$channels[$channel] = $config;
}
self::$channels = $channels;
}
return self::$channels;
}
/**
* @param string $filter
* @param int $limit
* @param int $start
* @param array $options
*/
public function getLookupResults($type, $filter = '', $limit = 10, $start = 0, $options = []): array
{
$results = [];
switch ($type) {
case 'channel.message':
$entities = $this->getRepository()->getMessageList(
$filter,
$limit,
$start
);
foreach ($entities as $entity) {
$results[] = [
'label' => $entity['name'],
'value' => $entity['id'],
];
}
break;
}
return $results;
}
public function getMessageChannels($messageId): array
{
return $this->getRepository()->getMessageChannels($messageId);
}
/**
* @return array
*/
public function getChannelMessageByChannelId($channelId)
{
return $this->getRepository()->getChannelMessageByChannelId($channelId);
}
public function getLeadStatsPost($messageId, $dateFrom = null, $dateTo = null, $channel = null): array
{
$eventLog = $this->campaignModel->getCampaignLeadEventLogRepository();
return $eventLog->getChartQuery(
[
'type' => 'message.send',
'dateFrom' => $dateFrom,
'dateTo' => $dateTo,
'channel' => 'channel.message',
'channelId' => $messageId,
'logChannel' => $channel,
]
);
}
/**
* @return mixed
*/
public function getMarketingMessagesEventLogs($messageId, $dateFrom = null, $dateTo = null)
{
$eventLog = $this->campaignModel->getCampaignLeadEventLogRepository();
return $eventLog->getEventLogs(['type' => 'message.send', 'dateFrom' => $dateFrom, 'dateTo' => $dateTo, 'channel' => 'message', 'channelId' => $messageId]);
}
/**
* Get the channel name from the database.
*
* @template T of object
*
* @param int $id
* @param class-string<T> $entityName
* @param string $nameColumn
*
* @return string|null
*/
public function getChannelName($id, $entityName, $nameColumn = 'name')
{
if (!$id || !$entityName || !$nameColumn) {
return null;
}
$repo = $this->em->getRepository($entityName);
$qb = $repo->createQueryBuilder('e')
->select('e.'.$nameColumn)
->where('e.id = :id')
->setParameter('id', (int) $id);
$result = $qb->getQuery()->getOneOrNullResult();
return $result[$nameColumn] ?? null;
}
/**
* @throws MethodNotAllowedHttpException
*/
protected function dispatchEvent($action, &$entity, $isNew = false, Event $event = null): ?Event
{
if (!$entity instanceof Message) {
throw new MethodNotAllowedHttpException(['Message']);
}
switch ($action) {
case 'pre_save':
$name = ChannelEvents::MESSAGE_PRE_SAVE;
break;
case 'post_save':
$name = ChannelEvents::MESSAGE_POST_SAVE;
break;
case 'pre_delete':
$name = ChannelEvents::MESSAGE_PRE_DELETE;
break;
case 'post_delete':
$name = ChannelEvents::MESSAGE_POST_DELETE;
break;
default:
return null;
}
if ($this->dispatcher->hasListeners($name)) {
if (empty($event)) {
$event = new MessageEvent($entity, $isNew);
}
$this->dispatcher->dispatch($event, $name);
return $event;
}
return null;
}
}
|