1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2014 Mautic Contributors. All rights reserved |
||
5 | * @author Mautic |
||
6 | * |
||
7 | * @link http://mautic.org |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace Mautic\FormBundle\EventListener; |
||
13 | |||
14 | use Mautic\CampaignBundle\CampaignEvents; |
||
15 | use Mautic\CampaignBundle\Event\CampaignBuilderEvent; |
||
16 | use Mautic\CampaignBundle\Event\CampaignExecutionEvent; |
||
17 | use Mautic\CampaignBundle\Executioner\RealTimeExecutioner; |
||
18 | use Mautic\FormBundle\Event\SubmissionEvent; |
||
19 | use Mautic\FormBundle\Form\Type\CampaignEventFormFieldValueType; |
||
20 | use Mautic\FormBundle\Form\Type\CampaignEventFormSubmitType; |
||
21 | use Mautic\FormBundle\FormEvents; |
||
22 | use Mautic\FormBundle\Model\FormModel; |
||
23 | use Mautic\FormBundle\Model\SubmissionModel; |
||
24 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
||
25 | |||
26 | class CampaignSubscriber implements EventSubscriberInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var FormModel |
||
30 | */ |
||
31 | private $formModel; |
||
32 | |||
33 | /** |
||
34 | * @var SubmissionModel |
||
35 | */ |
||
36 | private $formSubmissionModel; |
||
37 | |||
38 | /** |
||
39 | * @var RealTimeExecutioner |
||
40 | */ |
||
41 | private $realTimeExecutioner; |
||
42 | |||
43 | public function __construct(FormModel $formModel, SubmissionModel $formSubmissionModel, RealTimeExecutioner $realTimeExecutioner) |
||
44 | { |
||
45 | $this->formModel = $formModel; |
||
46 | $this->formSubmissionModel = $formSubmissionModel; |
||
47 | $this->realTimeExecutioner = $realTimeExecutioner; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @return array |
||
52 | */ |
||
53 | public static function getSubscribedEvents() |
||
54 | { |
||
55 | return [ |
||
56 | CampaignEvents::CAMPAIGN_ON_BUILD => ['onCampaignBuild', 0], |
||
57 | FormEvents::FORM_ON_SUBMIT => ['onFormSubmit', 0], |
||
58 | FormEvents::ON_CAMPAIGN_TRIGGER_DECISION => ['onCampaignTriggerDecision', 0], |
||
59 | FormEvents::ON_CAMPAIGN_TRIGGER_CONDITION => ['onCampaignTriggerCondition', 0], |
||
60 | ]; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Add the option to the list. |
||
65 | */ |
||
66 | public function onCampaignBuild(CampaignBuilderEvent $event) |
||
67 | { |
||
68 | $trigger = [ |
||
69 | 'label' => 'mautic.form.campaign.event.submit', |
||
70 | 'description' => 'mautic.form.campaign.event.submit_descr', |
||
71 | 'formType' => CampaignEventFormSubmitType::class, |
||
72 | 'eventName' => FormEvents::ON_CAMPAIGN_TRIGGER_DECISION, |
||
73 | ]; |
||
74 | $event->addDecision('form.submit', $trigger); |
||
75 | |||
76 | $trigger = [ |
||
77 | 'label' => 'mautic.form.campaign.event.field_value', |
||
78 | 'description' => 'mautic.form.campaign.event.field_value_descr', |
||
79 | 'formType' => CampaignEventFormFieldValueType::class, |
||
80 | 'formTheme' => 'MauticFormBundle:FormTheme\FieldValueCondition', |
||
81 | 'eventName' => FormEvents::ON_CAMPAIGN_TRIGGER_CONDITION, |
||
82 | ]; |
||
83 | $event->addCondition('form.field_value', $trigger); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Trigger campaign event for when a form is submitted. |
||
88 | */ |
||
89 | public function onFormSubmit(SubmissionEvent $event) |
||
90 | { |
||
91 | $form = $event->getSubmission()->getForm(); |
||
92 | $this->realTimeExecutioner->execute('form.submit', $form, 'form', $form->getId()); |
||
93 | } |
||
94 | |||
95 | public function onCampaignTriggerDecision(CampaignExecutionEvent $event) |
||
96 | { |
||
97 | $eventDetails = $event->getEventDetails(); |
||
98 | |||
99 | if (null === $eventDetails) { |
||
100 | return $event->setResult(true); |
||
101 | } |
||
102 | |||
103 | $limitToForms = $event->getConfig()['forms']; |
||
104 | |||
105 | //check against selected forms |
||
106 | if (!empty($limitToForms) && !in_array($eventDetails->getId(), $limitToForms)) { |
||
107 | return $event->setResult(false); |
||
108 | } |
||
109 | |||
110 | return $event->setResult(true); |
||
111 | } |
||
112 | |||
113 | public function onCampaignTriggerCondition(CampaignExecutionEvent $event) |
||
114 | { |
||
115 | $lead = $event->getLead(); |
||
116 | |||
117 | if (!$lead || !$lead->getId()) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
118 | return $event->setResult(false); |
||
119 | } |
||
120 | |||
121 | $operators = $this->formModel->getFilterExpressionFunctions(); |
||
122 | $form = $this->formModel->getRepository()->findOneById($event->getConfig()['form']); |
||
123 | |||
124 | if (!$form || !$form->getId()) { |
||
125 | return $event->setResult(false); |
||
126 | } |
||
127 | |||
128 | $field = $this->formModel->findFormFieldByAlias($form, $event->getConfig()['field']); |
||
129 | |||
130 | $result = $this->formSubmissionModel->getRepository()->compareValue( |
||
131 | $lead->getId(), |
||
132 | $form->getId(), |
||
133 | $form->getAlias(), |
||
134 | $event->getConfig()['field'], |
||
135 | $event->getConfig()['value'], |
||
136 | $operators[$event->getConfig()['operator']]['expr'], |
||
137 | $field ? $field->getType() : null |
||
138 | ); |
||
139 | |||
140 | $event->setChannel('form', $form->getId()); |
||
141 | |||
142 | return $event->setResult($result); |
||
143 | } |
||
144 | } |
||
145 |