1 | <?php |
||||
2 | |||||
3 | /* |
||||
4 | * @copyright 2016 Mautic Contributors. All rights reserved |
||||
5 | * @author Mautic, Inc. |
||||
6 | * |
||||
7 | * @link https://mautic.org |
||||
8 | * |
||||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||||
10 | */ |
||||
11 | |||||
12 | namespace Mautic\ChannelBundle\Model; |
||||
13 | |||||
14 | use Mautic\CampaignBundle\Model\CampaignModel; |
||||
15 | use Mautic\ChannelBundle\ChannelEvents; |
||||
16 | use Mautic\ChannelBundle\Entity\Message; |
||||
17 | use Mautic\ChannelBundle\Event\MessageEvent; |
||||
18 | use Mautic\ChannelBundle\Form\Type\MessageType; |
||||
19 | use Mautic\ChannelBundle\Helper\ChannelListHelper; |
||||
20 | use Mautic\CoreBundle\Model\AjaxLookupModelInterface; |
||||
21 | use Mautic\CoreBundle\Model\FormModel; |
||||
22 | use Symfony\Component\EventDispatcher\Event; |
||||
23 | use Symfony\Component\Form\FormFactory; |
||||
24 | use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; |
||||
25 | |||||
26 | /** |
||||
27 | * Class MessageModel. |
||||
28 | */ |
||||
29 | class MessageModel extends FormModel implements AjaxLookupModelInterface |
||||
30 | { |
||||
31 | const CHANNEL_FEATURE = 'marketing_messages'; |
||||
32 | |||||
33 | /** |
||||
34 | * @var ChannelListHelper |
||||
35 | */ |
||||
36 | protected $channelListHelper; |
||||
37 | |||||
38 | /** |
||||
39 | * @var CampaignModel |
||||
40 | */ |
||||
41 | protected $campaignModel; |
||||
42 | |||||
43 | /** |
||||
44 | * @var |
||||
45 | */ |
||||
46 | protected static $channels; |
||||
47 | |||||
48 | /** |
||||
49 | * MessageModel constructor. |
||||
50 | * |
||||
51 | * @param ChannelListHelper $channelListHelper |
||||
52 | * @param CampaignModel $campaignModel |
||||
53 | */ |
||||
54 | public function __construct(ChannelListHelper $channelListHelper, CampaignModel $campaignModel) |
||||
55 | { |
||||
56 | $this->channelListHelper = $channelListHelper; |
||||
57 | $this->campaignModel = $campaignModel; |
||||
58 | } |
||||
59 | |||||
60 | /** |
||||
61 | * @param Message $entity |
||||
62 | * @param bool $unlock |
||||
63 | */ |
||||
64 | public function saveEntity($entity, $unlock = true) |
||||
65 | { |
||||
66 | $isNew = $entity->isNew(); |
||||
67 | |||||
68 | parent::saveEntity($entity, $unlock); |
||||
69 | |||||
70 | if (!$isNew) { |
||||
71 | // Update the channels |
||||
72 | $channels = $entity->getChannels(); |
||||
73 | foreach ($channels as $channel) { |
||||
74 | $channel->setMessage($entity); |
||||
75 | } |
||||
76 | $this->getRepository()->saveEntities($channels); |
||||
77 | } |
||||
78 | } |
||||
79 | |||||
80 | /** |
||||
81 | * @return string |
||||
82 | */ |
||||
83 | public function getPermissionBase() |
||||
84 | { |
||||
85 | return 'channel:messages'; |
||||
86 | } |
||||
87 | |||||
88 | /** |
||||
89 | * @return \Doctrine\ORM\EntityRepository|\Mautic\ChannelBundle\Entity\MessageRepository |
||||
90 | */ |
||||
91 | public function getRepository() |
||||
92 | { |
||||
93 | return $this->em->getRepository('MauticChannelBundle:Message'); |
||||
94 | } |
||||
95 | |||||
96 | /** |
||||
97 | * @param null $id |
||||
98 | * |
||||
99 | * @return Form |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
100 | */ |
||||
101 | public function getEntity($id = null) |
||||
102 | { |
||||
103 | if ($id === null) { |
||||
0 ignored issues
–
show
|
|||||
104 | return new Message(); |
||||
0 ignored issues
–
show
|
|||||
105 | } |
||||
106 | |||||
107 | return parent::getEntity($id); |
||||
108 | } |
||||
109 | |||||
110 | /** |
||||
111 | * @param object $entity |
||||
112 | * @param FormFactory $formFactory |
||||
113 | * @param null $action |
||||
114 | * @param array $options |
||||
115 | * |
||||
116 | * @return \Symfony\Component\Form\FormInterface |
||||
117 | */ |
||||
118 | public function createForm($entity, $formFactory, $action = null, $options = []) |
||||
119 | { |
||||
120 | if (!empty($action)) { |
||||
121 | $options['action'] = $action; |
||||
122 | } |
||||
123 | |||||
124 | return $formFactory->create(MessageType::class, $entity, $options); |
||||
125 | } |
||||
126 | |||||
127 | /** |
||||
128 | * @return array |
||||
129 | */ |
||||
130 | public function getChannels() |
||||
131 | { |
||||
132 | if (!self::$channels) { |
||||
133 | $channels = $this->channelListHelper->getFeatureChannels(self::CHANNEL_FEATURE); |
||||
134 | |||||
135 | // Validate channel configs |
||||
136 | foreach ($channels as $channel => $config) { |
||||
137 | if (!isset($config['lookupFormType']) && !isset($config['propertiesFormType'])) { |
||||
138 | throw new \InvalidArgumentException('lookupFormType and/or propertiesFormType are required for channel '.$channel); |
||||
139 | } |
||||
140 | |||||
141 | switch (true) { |
||||
142 | case $this->translator->hasId('mautic.channel.'.$channel): |
||||
143 | $label = $this->translator->trans('mautic.channel.'.$channel); |
||||
144 | break; |
||||
145 | case $this->translator->hasId('mautic.'.$channel): |
||||
146 | $label = $this->translator->trans('mautic.'.$channel); |
||||
147 | break; |
||||
148 | case $this->translator->hasId('mautic.'.$channel.'.'.$channel): |
||||
149 | $label = $this->translator->trans('mautic.'.$channel.'.'.$channel); |
||||
150 | break; |
||||
151 | default: |
||||
152 | $label = ucfirst($channel); |
||||
153 | } |
||||
154 | $config['label'] = $label; |
||||
155 | |||||
156 | $channels[$channel] = $config; |
||||
157 | } |
||||
158 | |||||
159 | self::$channels = $channels; |
||||
160 | } |
||||
161 | |||||
162 | return self::$channels; |
||||
163 | } |
||||
164 | |||||
165 | /** |
||||
166 | * @param $type |
||||
167 | * @param string $filter |
||||
168 | * @param int $limit |
||||
169 | * @param int $start |
||||
170 | * @param array $options |
||||
171 | * |
||||
172 | * @return array |
||||
173 | */ |
||||
174 | public function getLookupResults($type, $filter = '', $limit = 10, $start = 0, $options = []) |
||||
175 | { |
||||
176 | $results = []; |
||||
177 | switch ($type) { |
||||
178 | case 'channel.message': |
||||
179 | $entities = $this->getRepository()->getMessageList( |
||||
180 | $filter, |
||||
181 | $limit, |
||||
182 | $start, |
||||
183 | $this->security->isGranted($this->getPermissionBase().':viewother') |
||||
184 | ); |
||||
185 | |||||
186 | foreach ($entities as $entity) { |
||||
187 | $results[] = [ |
||||
188 | 'label' => $entity['name'], |
||||
189 | 'value' => $entity['id'], |
||||
190 | ]; |
||||
191 | } |
||||
192 | |||||
193 | break; |
||||
194 | } |
||||
195 | |||||
196 | return $results; |
||||
197 | } |
||||
198 | |||||
199 | /** |
||||
200 | * @param $messageId |
||||
201 | * |
||||
202 | * @return array |
||||
203 | */ |
||||
204 | public function getMessageChannels($messageId) |
||||
205 | { |
||||
206 | return $this->getRepository()->getMessageChannels($messageId); |
||||
0 ignored issues
–
show
|
|||||
207 | } |
||||
208 | |||||
209 | /** |
||||
210 | * @param $channelId |
||||
211 | * |
||||
212 | * @return array |
||||
213 | */ |
||||
214 | public function getChannelMessageByChannelId($channelId) |
||||
215 | { |
||||
216 | return $this->getRepository()->getChannelMessageByChannelId($channelId); |
||||
0 ignored issues
–
show
|
|||||
217 | } |
||||
218 | |||||
219 | /** |
||||
220 | * @param $messageId |
||||
221 | * @param null $dateFrom |
||||
222 | * @param null $dateTo |
||||
223 | * @param null $channel |
||||
224 | * |
||||
225 | * @return array |
||||
226 | */ |
||||
227 | public function getLeadStatsPost($messageId, $dateFrom = null, $dateTo = null, $channel = null) |
||||
228 | { |
||||
229 | $eventLog = $this->campaignModel->getCampaignLeadEventLogRepository(); |
||||
230 | |||||
231 | return $eventLog->getChartQuery( |
||||
232 | [ |
||||
233 | 'type' => 'message.send', |
||||
234 | 'dateFrom' => $dateFrom, |
||||
235 | 'dateTo' => $dateTo, |
||||
236 | 'channel' => 'channel.message', |
||||
237 | 'channelId' => $messageId, |
||||
238 | 'logChannel' => $channel, |
||||
239 | ] |
||||
240 | ); |
||||
241 | } |
||||
242 | |||||
243 | /** |
||||
244 | * @param $messageId |
||||
245 | * @param null $dateFrom |
||||
246 | * @param null $dateTo |
||||
247 | * |
||||
248 | * @return mixed |
||||
249 | */ |
||||
250 | public function getMarketingMessagesEventLogs($messageId, $dateFrom = null, $dateTo = null) |
||||
251 | { |
||||
252 | $eventLog = $this->campaignModel->getCampaignLeadEventLogRepository(); |
||||
253 | |||||
254 | return $eventLog->getEventLogs(['type' => 'message.send', 'dateFrom' => $dateFrom, 'dateTo' => $dateTo, 'channel' => 'message', 'channelId' => $messageId]); |
||||
0 ignored issues
–
show
The method
getEventLogs() does not exist on Mautic\CampaignBundle\En...\LeadEventLogRepository . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
255 | } |
||||
256 | |||||
257 | /** |
||||
258 | * Get the channel name from the database. |
||||
259 | * |
||||
260 | * @param int $id |
||||
261 | * @param string $entityName |
||||
262 | * @param string $nameColumn |
||||
263 | * |
||||
264 | * @return string|null |
||||
265 | */ |
||||
266 | public function getChannelName($id, $entityName, $nameColumn = 'name') |
||||
267 | { |
||||
268 | if (!$id || !$entityName || !$nameColumn) { |
||||
269 | return null; |
||||
270 | } |
||||
271 | |||||
272 | $repo = $this->em->getRepository($entityName); |
||||
273 | $qb = $repo->createQueryBuilder('e') |
||||
274 | ->select('e.'.$nameColumn) |
||||
275 | ->where('e.id = :id') |
||||
276 | ->setParameter('id', (int) $id); |
||||
277 | $result = $qb->getQuery()->getOneOrNullResult(); |
||||
278 | |||||
279 | if (isset($result[$nameColumn])) { |
||||
280 | return $result[$nameColumn]; |
||||
281 | } |
||||
282 | |||||
283 | return null; |
||||
284 | } |
||||
285 | |||||
286 | /** |
||||
287 | * {@inheritdoc} |
||||
288 | * |
||||
289 | * @throws MethodNotAllowedHttpException |
||||
290 | */ |
||||
291 | protected function dispatchEvent($action, &$entity, $isNew = false, Event $event = null) |
||||
292 | { |
||||
293 | if (!$entity instanceof Message) { |
||||
294 | throw new MethodNotAllowedHttpException(['Message']); |
||||
295 | } |
||||
296 | |||||
297 | switch ($action) { |
||||
298 | case 'pre_save': |
||||
299 | $name = ChannelEvents::MESSAGE_PRE_SAVE; |
||||
300 | break; |
||||
301 | case 'post_save': |
||||
302 | $name = ChannelEvents::MESSAGE_POST_SAVE; |
||||
303 | break; |
||||
304 | case 'pre_delete': |
||||
305 | $name = ChannelEvents::MESSAGE_PRE_DELETE; |
||||
306 | break; |
||||
307 | case 'post_delete': |
||||
308 | $name = ChannelEvents::MESSAGE_POST_DELETE; |
||||
309 | break; |
||||
310 | default: |
||||
311 | return null; |
||||
312 | } |
||||
313 | |||||
314 | if ($this->dispatcher->hasListeners($name)) { |
||||
315 | if (empty($event)) { |
||||
316 | $event = new MessageEvent($entity, $isNew); |
||||
317 | } |
||||
318 | $this->dispatcher->dispatch($name, $event); |
||||
319 | |||||
320 | return $event; |
||||
321 | } |
||||
322 | |||||
323 | return null; |
||||
324 | } |
||||
325 | } |
||||
326 |