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\StageBundle\EventListener; |
||
13 | |||
14 | use Mautic\CampaignBundle\CampaignEvents; |
||
15 | use Mautic\CampaignBundle\Event\CampaignBuilderEvent; |
||
16 | use Mautic\CampaignBundle\Event\CampaignExecutionEvent; |
||
17 | use Mautic\LeadBundle\Entity\Lead; |
||
18 | use Mautic\LeadBundle\Model\LeadModel; |
||
19 | use Mautic\StageBundle\Form\Type\StageActionChangeType; |
||
20 | use Mautic\StageBundle\Model\StageModel; |
||
21 | use Mautic\StageBundle\StageEvents; |
||
22 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
||
23 | |||
24 | class CampaignSubscriber implements EventSubscriberInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var LeadModel |
||
28 | */ |
||
29 | private $leadModel; |
||
30 | |||
31 | /** |
||
32 | * @var StageModel |
||
33 | */ |
||
34 | private $stageModel; |
||
35 | |||
36 | public function __construct(LeadModel $leadModel, StageModel $stageModel) |
||
37 | { |
||
38 | $this->leadModel = $leadModel; |
||
39 | $this->stageModel = $stageModel; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @return array |
||
44 | */ |
||
45 | public static function getSubscribedEvents() |
||
46 | { |
||
47 | return [ |
||
48 | CampaignEvents::CAMPAIGN_ON_BUILD => ['onCampaignBuild', 0], |
||
49 | StageEvents::ON_CAMPAIGN_TRIGGER_ACTION => ['onCampaignTriggerActionChangeStage', 0], |
||
50 | ]; |
||
51 | } |
||
52 | |||
53 | public function onCampaignBuild(CampaignBuilderEvent $event) |
||
54 | { |
||
55 | $action = [ |
||
56 | 'label' => 'mautic.stage.campaign.event.change', |
||
57 | 'description' => 'mautic.stage.campaign.event.change_descr', |
||
58 | 'eventName' => StageEvents::ON_CAMPAIGN_TRIGGER_ACTION, |
||
59 | 'formType' => StageActionChangeType::class, |
||
60 | 'formTheme' => 'MauticStageBundle:FormTheme\StageActionChange', |
||
61 | ]; |
||
62 | $event->addAction('stage.change', $action); |
||
63 | } |
||
64 | |||
65 | public function onCampaignTriggerActionChangeStage(CampaignExecutionEvent $event, $eventName) |
||
66 | { |
||
67 | $stageChange = false; |
||
68 | $lead = $event->getLead(); |
||
69 | $leadStage = null; |
||
70 | |||
71 | if ($lead instanceof Lead) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
72 | $leadStage = $lead->getStage(); |
||
73 | } |
||
74 | |||
75 | $stageId = (int) $event->getConfig()['stage']; |
||
76 | $stageToChangeTo = $this->stageModel->getEntity($stageId); |
||
77 | |||
78 | if (null != $stageToChangeTo && $stageToChangeTo->isPublished()) { |
||
79 | if ($leadStage && $leadStage->getWeight() <= $stageToChangeTo->getWeight()) { |
||
80 | $stageChange = true; |
||
81 | } elseif (!$leadStage) { |
||
82 | $stageChange = true; |
||
83 | } |
||
84 | } |
||
85 | |||
86 | if ($stageChange) { |
||
87 | $lead->stageChangeLogEntry( |
||
88 | $stageToChangeTo, |
||
89 | $stageToChangeTo->getId().': '.$stageToChangeTo->getName(), |
||
90 | $eventName |
||
91 | ); |
||
92 | $lead->setStage($stageToChangeTo); |
||
93 | |||
94 | $this->leadModel->saveEntity($lead); |
||
95 | } |
||
96 | |||
97 | return $event->setResult($stageChange); |
||
98 | } |
||
99 | } |
||
100 |