Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Berezovsky committed Nov 7, 2018
0 parents commit 243b59a
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
/vendor
Binary file added Assets/img/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions Config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

return [
'name' => 'Advanced Templates',
'description' => 'Plugin extends default email template capabilities with TWIG block so you can use advanced scripting techniques like conditions, loops etc',
'version' => '1.0',
'author' => 'Dmitry Berezovsky',
'services' => [
'events' => [
// Register any event listeners
'mautic.plugin.advanced_templates.email.subscriber' => [
'class' => \MauticPlugin\MauticAdvancedTemplatesBundle\EventListener\EmailSubscriber::class,
'arguments' => [
'mautic.plugin.advanced_templates.helper.template_processor'
],
],
],
'other' => [
// Template processor
'mautic.plugin.advanced_templates.helper.template_processor' => [
'class' => \MauticPlugin\MauticAdvancedTemplatesBundle\Helper\TemplateProcessor::class,
'tag' => 'advanced_templates',
'arguments' => [
'monolog.logger.mautic',
],
]
],
],
];
55 changes: 55 additions & 0 deletions EventListener/EmailSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace MauticPlugin\MauticAdvancedTemplatesBundle\EventListener;
use Mautic\CampaignBundle\Entity\Lead;
use Mautic\CoreBundle\EventListener\CommonSubscriber;
use Mautic\EmailBundle\EmailEvents;
use Mautic\EmailBundle\Event as Events;
use Mautic\CoreBundle\Exception as MauticException;
use MauticPlugin\MauticAdvancedTemplatesBundle\Helper\TemplateProcessor;
use Psr\Log\LoggerInterface;

/**
* Class EmailSubscriber.
*/
class EmailSubscriber extends CommonSubscriber
{
/**
* @var TokenHelper $tokenHelper ;
*/
protected $templateProcessor;


/**
* EmailSubscriber constructor.
*
* @param TokenHelper $tokenHelper
*/
public function __construct(TemplateProcessor $templateProcessor)
{
$this->templateProcessor = $templateProcessor;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
EmailEvents::EMAIL_ON_SEND => ['onEmailGenerate', 0],
EmailEvents::EMAIL_ON_DISPLAY => ['onEmailGenerate', 0]
];
}

/**
* Search and replace tokens with content
*
* @param Events\EmailSendEvent $event
*/
public function onEmailGenerate(Events\EmailSendEvent $event)
{
$this->logger->info('onEmailGenerate MauticAdvancedTemplatesBundle\EmailSubscriber');
$content = $event->getContent();
$content = $this->templateProcessor->processTemplate($content, $event->getLead());
$event->setContent($content);
}
}
76 changes: 76 additions & 0 deletions Helper/TemplateProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace MauticPlugin\MauticAdvancedTemplatesBundle\Helper;

use Psr\Log\LoggerInterface;

class TemplateProcessor
{

/**
* @var LoggerInterface
*/
protected $logger;

/**
* @var \Twig_Environment
*/
private $twigEnv;

private static $matchTwigBlockRegex = '/{%\s?TWIG_BLOCK\s?%}(.*?){%\s?END_TWIG_BLOCK\s?%}/ism';

/**
* TemplateProcessor constructor.
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
$this->twigEnv = new \Twig_Environment(new \Twig_Loader_Array([]));
$this->configureTwig($this->twigEnv);
}


/**
* @param string $content
* @param array $lead
* @return string
* @throws \Throwable
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Syntax
*/
public function processTemplate($content, $lead)
{
$this->logger->debug('TemplateProcessor: Processing template');
$this->logger->debug('LEAD: ' . var_export($lead, true));
$content = preg_replace_callback_array([
TemplateProcessor::$matchTwigBlockRegex => $this->processTwigBlock($lead)
], $content);
$this->logger->debug('TemplateProcessor: Template processed');
return $content;
}

protected function configureTwig(\Twig_Environment $twig)
{
// You might want to register some custom TWIG tags or functions here

// TWIG filter json_decode
$twig->addFilter(new \Twig_SimpleFilter('json_decode', function ($string) {
return json_decode($string, true);
}));
}

private function processTwigBlock($lead)
{
return function ($matches) use ($lead) {
$templateSource = $matches[1];
$this->logger->debug('BLOCK SOURCE: ' . var_export($templateSource, true));
$template = $this->twigEnv->createTemplate($templateSource);
$renderedTemplate = $template->render([
'lead' => $lead
]);
$this->logger->debug('RENDERED BLOCK: ' . var_export($renderedTemplate, true));
return $renderedTemplate;
};
}
}
9 changes: 9 additions & 0 deletions MauticAdvancedTemplatesBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace MauticPlugin\MauticAdvancedTemplatesBundle;

use Mautic\PluginBundle\Bundle\PluginBundleBase;

class MauticAdvancedTemplatesBundle extends PluginBundleBase
{
}
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "logicify/mautic-advanced-templates-bundle",
"type": "mautic-plugin",
"require": {
"mautic/composer-plugin": "^1.0"
}
}

0 comments on commit 243b59a

Please sign in to comment.