-
Notifications
You must be signed in to change notification settings - Fork 24
/
PluginSubscriber.php
79 lines (66 loc) · 2.22 KB
/
PluginSubscriber.php
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
<?php
/*
* @copyright 2017 Trinoco. All rights reserved
* @author Trinoco
*
* @link http://trinoco.nl
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
namespace MauticPlugin\MauticFBAdsCustomAudiencesBundle\EventListener;
use Mautic\CoreBundle\EventListener\CommonSubscriber;
use Mautic\PluginBundle\Event\PluginIntegrationEvent;
use Mautic\PluginBundle\PluginEvents;
use MauticPlugin\MauticFBAdsCustomAudiencesBundle\Helper\FbAdsApiHelper;
/**
* Class PluginSubscriber.
*/
class PluginSubscriber extends CommonSubscriber
{
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
PluginEvents::PLUGIN_ON_INTEGRATION_CONFIG_SAVE => ['onIntegrationConfigSave', 0],
];
}
public function onIntegrationConfigSave(PluginIntegrationEvent $event) {
if ($event->getIntegrationName() == 'FBAdsCustomAudiences') {
//$integration = $event->getIntegration();
$changes = $event->getEntity()->getChanges();
if (isset($changes['isPublished'])) {
$integration = $event->getIntegration();
$api = FbAdsApiHelper::init($integration);
if ($api) {
$lists = $this->em->getRepository('MauticLeadBundle:LeadList')->getLists();
if ($changes['isPublished'][1] == 0) {
foreach ($lists as $list) {
FbAdsApiHelper::deleteList($list['name']);
}
}
else {
$listsLeads = $this->em->getRepository('MauticLeadBundle:LeadList')->getLeadsByList($lists);
foreach ($lists as $list) {
$listEntity = $this->em->getRepository('MauticLeadBundle:LeadList')->getEntity($list['id']);
$audience = FbAdsApiHelper::addList($listEntity);
$leads = $listsLeads[$listEntity->getId()];
$users = array();
foreach ($leads as $lead) {
$users[] = array(
$lead['firstname'],
$lead['lastname'],
$lead['email'],
$lead['mobile'],
$lead['country'],
);
}
FbAdsApiHelper::addUsers($audience, $users);
}
}
}
}
}
}
}