/** * Add/remove leads from campaigns based on lead list changes * * @param ListChangeEvent $event */ public function onLeadListChange(ListChangeEvent $event) { $lead = $event->getLead(); $list = $event->getList(); $action = $event->wasAdded() ? 'added' : 'removed'; $repo = $this->campaignModel->getRepository(); //get campaigns for the list $listCampaigns = $repo->getPublishedCampaignsByLeadLists($list->getId()); $leadLists = $this->leadModel->getLists($lead, true); $leadListIds = array_keys($leadLists); // If the lead was removed then don't count it if ($action == 'removed') { $key = array_search($list->getId(), $leadListIds); unset($leadListIds[$key]); } if (!empty($listCampaigns)) { foreach ($listCampaigns as $c) { $campaign = $this->em->getReference('MauticCampaignBundle:Campaign', $c['id']); if (!isset($campaignLists[$c['id']])) { $campaignLists[$c['id']] = array_keys($c['lists']); } if ($action == 'added') { $this->campaignModel->addLead($campaign, $lead); } else { if (array_intersect($leadListIds, $campaignLists[$c['id']])) { continue; } $this->campaignModel->removeLead($campaign, $lead); } unset($campaign); } } }
/** * @param ReportBuilderEvent $event * @param array $columns */ private function injectAttributionReportData(ReportBuilderEvent $event, array $columns, $type) { $attributionColumns = ['log.campaign_id' => ['label' => 'mautic.lead.report.attribution.campaign_id', 'type' => 'int', 'link' => 'mautic_campaign_action'], 'log.date_triggered' => ['label' => 'mautic.lead.report.attribution.action_date', 'type' => 'datetime'], 'c.name' => ['alias' => 'campaign_name', 'label' => 'mautic.lead.report.attribution.campaign_name', 'type' => 'string'], 'l.stage_id' => ['label' => 'mautic.lead.report.attribution.stage_id', 'type' => 'int', 'link' => 'mautic_stage_action'], 's.name' => ['alias' => 'stage_name', 'label' => 'mautic.lead.report.attribution.stage_name', 'type' => 'string'], 'channel' => ['alias' => 'channel', 'formula' => 'SUBSTRING_INDEX(e.type, \'.\', 1)', 'label' => 'mautic.lead.report.attribution.channel', 'type' => 'string'], 'channel_action' => ['alias' => 'channel_action', 'formula' => 'SUBSTRING_INDEX(e.type, \'.\', -1)', 'label' => 'mautic.lead.report.attribution.channel_action', 'type' => 'string'], 'e.name' => ['alias' => 'action_name', 'label' => 'mautic.lead.report.attribution.action_name', 'type' => 'string']]; $filters = $columns = array_merge($columns, $event->getCategoryColumns('cat.'), $attributionColumns); // Setup available channels $availableChannels = $this->campaignModel->getEvents(); $channels = []; $channelActions = []; foreach ($availableChannels['decision'] as $channel => $decision) { $parts = explode('.', $channel); $channelName = $parts[0]; $channels[$channelName] = $this->translator->hasId('mautic.channel.' . $channelName) ? $this->translator->trans('mautic.channel.' . $channelName) : ucfirst($channelName); unset($parts[0]); $actionValue = implode('.', $parts); if ($this->translator->hasId('mautic.channel.action.' . $channel)) { $actionName = $this->translator->trans('mautic.channel.action.' . $channel); } elseif ($this->translator->hasId('mautic.campaign.' . $channel)) { $actionName = $this->translator->trans('mautic.campaign.' . $channel); } else { $actionName = $channelName . ': ' . $actionValue; } $channelActions[$actionValue] = $actionName; } $filters['channel'] = ['label' => 'mautic.lead.report.attribution.channel', 'type' => 'select', 'list' => $channels]; $filters['channel_action'] = ['label' => 'mautic.lead.report.attribution.channel_action', 'type' => 'select', 'list' => $channelActions]; $this->channelActions = $channelActions; $this->channels = $channels; unset($channelActions, $channels); // Setup available channels $campaigns = $this->campaignModel->getRepository()->getSimpleList(); $filters['log.campaign_id'] = ['label' => 'mautic.lead.report.attribution.filter.campaign', 'type' => 'select', 'list' => $campaigns]; unset($campaigns); // Setup stages list $userStages = $this->stageModel->getUserStages(); $stages = []; foreach ($userStages as $stage) { $stages[$stage['id']] = $stage['name']; } $filters['l.stage_id'] = ['label' => 'mautic.lead.report.attribution.filter.stage', 'type' => 'select', 'list' => $stages]; unset($stages); $context = "contact.attribution.{$type}"; $event->addGraph($context, 'pie', 'mautic.lead.graph.pie.attribution_stages')->addGraph($context, 'pie', 'mautic.lead.graph.pie.attribution_campaigns')->addGraph($context, 'pie', 'mautic.lead.graph.pie.attribution_actions')->addGraph($context, 'pie', 'mautic.lead.graph.pie.attribution_channels'); $data = ['display_name' => 'mautic.lead.report.attribution.' . $type, 'columns' => $columns, 'filters' => $filters]; $event->addTable($context, $data, 'contacts'); }