Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzmany committed Nov 20, 2019
0 parents commit 3eb35cf
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 0 deletions.
Binary file added Assets/img/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 87 additions & 0 deletions Command/SourceCreatedMigrationCommand.php
@@ -0,0 +1,87 @@
<?php

/*
* @copyright 2019 Mautic Contributors. All rights reserved
* @author Mautic
*
* @link http://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace MauticPlugin\MauticCustomReportBundle\Command;

use Doctrine\ORM\EntityManager;
use Mautic\CoreBundle\Command\ModeratedCommand;
use phpDocumentor\Reflection\Types\Parent_;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class SourceCreatedMigrationCommand extends ModeratedCommand
{
/**
* @var EntityManager
*/
private $entityManager;

/**
* SourceCreatedMigrationCommand constructor.
*
* @param EntityManager $entityManager
*/
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('mautic:source:created:cotnacts:migration')
->setDescription('Created contacts source migration')
->setHelp('Created contacts source migration');

parent::configure();
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$key = __CLASS__;
if (!$this->checkRunStatus($input, $output, $key)) {
return 0;
}

$maxIdQuery = 'SELECT MAX(ccl.log_id) FROM '.MAUTIC_TABLE_PREFIX.'custom_contact_log ccl';
$maxLogId = (int) $this->entityManager->getConnection()->query($maxIdQuery)->fetchColumn();

$countQuery = 'SELECT COUNT(lel.id) FROM '.$this->getLeadEventLogQueryPart().' AND lel.id > '.$maxLogId;
$numberOfImportedRows = $this->entityManager->getConnection()->query($countQuery)->fetchColumn();

$query = 'INSERT INTO '.MAUTIC_TABLE_PREFIX.'custom_contact_log (lead_id, log_id, url, date_added)
SELECT lel.lead_id, lel.id,ph.url,lel.date_added FROM '.$this->getLeadEventLogQueryPart();
if ($maxLogId) {
$query.= ' AND lel.id > '.$maxLogId;
}
$this->entityManager->getConnection()->query($query);

$output->writeln(sprintf("Migrated %s lines", $numberOfImportedRows));
}

/**
* @return string
*/
private function getLeadEventLogQueryPart()
{
return MAUTIC_TABLE_PREFIX.'lead_event_log lel
LEFT JOIN '.MAUTIC_TABLE_PREFIX.'page_hits ph ON ph.id = lel.object_id
WHERE lel.bundle = \'page\' and lel.object = \'hit\' AND lel.action = \'created_contact\'';
}
}
50 changes: 50 additions & 0 deletions Config/config.php
@@ -0,0 +1,50 @@
<?php

return [
'name' => 'MauticCustomReportBundle',
'description' => 'Custom reports for Mautic',
'version' => '1.0',
'author' => 'MTCExtendee',

'routes' => [
],

'services' => [
'events'=>[
'mautic.customreport.created_contact' => [
'class' => \MauticPlugin\MauticCustomReportBundle\EventListener\SourceCreatedContactReportSubscriber::class,
'arguments' => [
'mautic.lead.reportbundle.fields_builder',
],
],
],
'forms' => [
],
'models' => [

],
'integrations' => [
'mautic.integration.customreport' => [
'class' => \MauticPlugin\MauticCustomReportBundle\Integration\CustomReportIntegration::class,
'arguments' => [
],
],
],
'others' => [

],
'controllers' => [
],
'commands'=>[
'mautic.customreport.command' => [
'class' => \MauticPlugin\MauticCustomReportBundle\Command\SourceCreatedMigrationCommand::class,
'arguments' => [
'doctrine.orm.entity_manager'
],
'tag' => 'console.command',
],
]
],
'parameters' => [
],
];
121 changes: 121 additions & 0 deletions EventListener/SourceCreatedContactReportSubscriber.php
@@ -0,0 +1,121 @@
<?php

/*
* @copyright 2019 Mautic Contributors. All rights reserved
* @author Mautic
*
* @link http://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace MauticPlugin\MauticCustomReportBundle\EventListener;

use Mautic\CoreBundle\EventListener\CommonSubscriber;
use Mautic\LeadBundle\EventListener\ReportSubscriber;
use Mautic\LeadBundle\Report\FieldsBuilder;
use Mautic\ReportBundle\Event\ReportBuilderEvent;
use Mautic\ReportBundle\Event\ReportGeneratorEvent;
use Mautic\ReportBundle\ReportEvents;

class SourceCreatedContactReportSubscriber extends CommonSubscriber
{
const SOURCE_CREATED_CONTACT = 'source.created_contact';

/**
* @var FieldsBuilder
*/
private $fieldsBuilder;

/**
* @param FieldsBuilder $fieldsBuilder
*/
public function __construct(FieldsBuilder $fieldsBuilder)
{
$this->fieldsBuilder = $fieldsBuilder;
}

/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
ReportEvents::REPORT_ON_BUILD => ['onReportBuilder', 0],
ReportEvents::REPORT_ON_GENERATE => ['onReportGenerate', 0],
];
}

/**
* Add available tables and columns to the report builder lookup.
*
* @param ReportBuilderEvent $event
*/
public function onReportBuilder(ReportBuilderEvent $event)
{
if (!$event->checkContext([self::SOURCE_CREATED_CONTACT])) {
return;
}

$columns = $this->fieldsBuilder->getLeadFieldsColumns('l.');


$addColumns = [
'ccl.url' => [
'label' => 'mautic.customreport.report.source.created_contact',
'type' => 'string',
],
'ccl.date_added' => [
'label' => 'mautic.customreport.report.date_added',
'type' => 'datetime',
],
'hits' => [
'formula'=>'(SELECT COUNT(ccl2.url) FROM '.MAUTIC_TABLE_PREFIX.'custom_contact_log ccl2
INNER JOIN '.MAUTIC_TABLE_PREFIX.'leads l ON l.id = ccl2.lead_id AND l.email IS NOT NULL
WHERE ccl2.date_added BETWEEN :dateFrom AND :dateTo AND ccl2.url = ccl.url)',
'label' => 'mautic.customreport.report.hits',
'type' => 'int',
],
];

$data = [
'display_name' => 'mautic.customreport.report.source.created_contact',
'columns' => array_merge($columns, $addColumns),
'filters' => $columns,
];
$event->addTable(self::SOURCE_CREATED_CONTACT, $data, ReportSubscriber::GROUP_CONTACTS);

unset($columns, $filters, $columns, $data);
}

/**
* Initialize the QueryBuilder object to generate reports from.
*
* @param ReportGeneratorEvent $event
*/
public function onReportGenerate(ReportGeneratorEvent $event)
{
if (!$event->checkContext([self::SOURCE_CREATED_CONTACT])) {
return;
}

$qb = $event->getQueryBuilder();
$qb->from(MAUTIC_TABLE_PREFIX.'custom_contact_log', 'ccl');
$qb->innerJoin('ccl',MAUTIC_TABLE_PREFIX.'leads', 'l', 'l.id = ccl.lead_id AND l.email IS NOT NULL');


if (empty($event->getReport()->getGroupBy())) {
$qb->addGroupBy('ccl.url');
$qb->addGroupBy('l.id');
}

if (empty($event->getReport()->getOrderColumns())) {
$qb->addOrderBy('hits1','DESC');
$qb->addOrderBy('l.points','DESC');
}

$event->applyDateFilters($qb, 'date_added', 'ccl');

$event->setQueryBuilder($qb);
}
}
43 changes: 43 additions & 0 deletions Integration/CustomReportIntegration.php
@@ -0,0 +1,43 @@
<?php

/*
* @copyright 2019 MTCExtendee. All rights reserved
* @author MTCExtendee
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace MauticPlugin\MauticCustomReportBundle\Integration;

use Mautic\PluginBundle\Integration\AbstractIntegration;


class CustomReportIntegration extends AbstractIntegration
{
const INTEGRATION_NAME = 'CustomReport';

public function getName()
{
return self::INTEGRATION_NAME;
}

public function getDisplayName()
{
return 'Custom Report';
}

public function getAuthenticationType()
{
return 'none';
}

public function getRequiredKeyFields()
{
return [
];
}

public function getIcon()
{
return 'plugins/MauticCustomReportBundle/Assets/img/icon.png';
}
}
18 changes: 18 additions & 0 deletions MauticCustomReportBundle.php
@@ -0,0 +1,18 @@
<?php

/*
* @copyright 2019 MTCExtendee. All rights reserved
* @author MTCExtendee
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace MauticPlugin\MauticCustomReportBundle;

use Mautic\PluginBundle\Bundle\PluginBundleBase;

/**
* Class MauticCustomReportBundle.
*/
class MauticCustomReportBundle extends PluginBundleBase
{
}
19 changes: 19 additions & 0 deletions README.md
@@ -0,0 +1,19 @@
# Mautic Custom Report for Mautic

## Installation

### Manual

1. Use last version
2. Unzip files to plugins/MauticCustomReportBundle
3. Clear cache (app/cache/prod/)
4. Go to /s/plugins/reload

## More Mautic stuff

- Plugins from Mautic Extendee Family https://mtcextendee.com/plugins
- Mautic themes https://mtcextendee.com/themes

### Credits

<div>Icons made by <a href="https://www.flaticon.com/authors/pause08" title="Pause08">Pause08</a>

0 comments on commit 3eb35cf

Please sign in to comment.