Skip to content

Commit

Permalink
Merge pull request Logicify#3 from mtcextendee/rss-support
Browse files Browse the repository at this point in the history
RSS support
  • Loading branch information
corvis authored Aug 10, 2019
2 parents 0c4e070 + 1d69831 commit b380878
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 4 deletions.
18 changes: 16 additions & 2 deletions Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
'class' => \MauticPlugin\MauticAdvancedTemplatesBundle\Helper\TemplateProcessor::class,
'arguments' => [
'monolog.logger.mautic',
'mautic.plugin.advanced_templates.helper.twig_loader_dynamiccontent'
'mautic.plugin.advanced_templates.helper.twig_loader_dynamiccontent',
'mautic.plugin.advanced_templates.helper.feed_factory'
]
],
'mautic.plugin.advanced_templates.helper.twig_loader_dynamiccontent' => [
Expand All @@ -30,7 +31,20 @@
'monolog.logger.mautic',
'mautic.model.factory'
]
]
],
'mautic.plugin.advanced_templates.helper.feed_factory' => [
'class' => \MauticPlugin\MauticAdvancedTemplatesBundle\Feed\FeedFactory::class,
'arguments' => [
'mautic.plugin.advanced_templates.helper.feed_processor'
]
],
'mautic.plugin.advanced_templates.helper.feed_processor' => [
'class' => \MauticPlugin\MauticAdvancedTemplatesBundle\Feed\FeedProcessor::class,
'arguments' => [
'mautic.lead.model.lead',
]
],

]
]
];
29 changes: 29 additions & 0 deletions Feed/Feed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace MauticPlugin\MauticAdvancedTemplatesBundle\Feed;

use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Model\LeadModel;

class Feed
{
/** @var string */
private $feed;

/** @var \SimpleXMLElement */
private $rss;

public function __construct($feed)
{
$this->feed = $feed;
$this->rss = simplexml_load_file($feed);
}

/**
* @return \SimpleXMLElement
*/
public function getItems(): \SimpleXMLElement
{
return $this->rss->channel->item;
}
}
54 changes: 54 additions & 0 deletions Feed/FeedFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace MauticPlugin\MauticAdvancedTemplatesBundle\Feed;

use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Model\LeadModel;

class FeedFactory
{

/** @var string */
private $feed;

/** @var string|null */
private $type;

/**
* @var FeedProcessor
*/
private $feedProcessor;

/**
* FeedFactory constructor.
*
* @param FeedProcessor $feedProcessor
*/
public function __construct(FeedProcessor $feedProcessor)
{
$this->feedProcessor = $feedProcessor;
}

/**
* @param int $leadId
* @param array $args
*
* @return \SimpleXMLElement|void
*/
public function getItems($leadId, array $args)
{
$this->feed = new Feed($args[0]);
$this->type = isset($args[1]) ? $args[1] : null;

switch ($this->type) {
case 'segments':
return $this->feedProcessor->getSegmentsRelatedFeedItems($leadId, $this->feed);
break;
default:
return $this->feed->getItems();
break;
}


}
}
55 changes: 55 additions & 0 deletions Feed/FeedProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace MauticPlugin\MauticAdvancedTemplatesBundle\Feed;

use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Model\LeadModel;

class FeedProcessor
{
/**
* @var LeadModel
*/
private $leadModel;

/**
* @var Lead
*/
private $lead;

/**
* RSSProcessor constructor.
*
* @param LeadModel $leadModel
* @param Feed $feed
*/
public function __construct(LeadModel $leadModel)
{
$this->leadModel = $leadModel;

}

/**
* @param int $leadId
* @param Feed $feed
*
* @return \SimpleXMLElement
*/
public function getSegmentsRelatedFeedItems($leadId, Feed $feed)
{
if ($this->lead = $this->leadModel->getEntity($leadId)) {
$contactPrefsSegmentsAliases = array_column($this->leadModel->getLists($this->lead, true, true, true), 'alias');
$items = [];
foreach ($feed->getItems() as $item) {
foreach ($item->category as $category) {
if (in_array($category, $contactPrefsSegmentsAliases)) {
$items[] = $item;
continue 2;
}
}
}
return $items;
}
}

}
22 changes: 20 additions & 2 deletions Helper/TemplateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MauticPlugin\MauticAdvancedTemplatesBundle\Helper;

use MauticPlugin\MauticAdvancedTemplatesBundle\Feed\FeedFactory;
use MauticPlugin\MauticCrmBundle\Integration\Salesforce\Object\Lead;
use Psr\Log\LoggerInterface;

class TemplateProcessor
Expand All @@ -20,12 +22,22 @@ class TemplateProcessor

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

/** @var array */
private $lead;

/**
* @var FeedFactory
*/
private $feedFactory;

/**
* TemplateProcessor constructor.
* @param LoggerInterface $logger
*
* @param LoggerInterface $logger
* @param Twig_Loader_DynamicContent $twigDynamicContentLoader
* @param FeedFactory $feedFactory
*/
public function __construct(LoggerInterface $logger, Twig_Loader_DynamicContent $twigDynamicContentLoader)
public function __construct(LoggerInterface $logger, Twig_Loader_DynamicContent $twigDynamicContentLoader, FeedFactory $feedFactory)
{
$this->logger = $logger;
$this->twigDynamicContentLoader = $twigDynamicContentLoader;
Expand All @@ -34,6 +46,7 @@ public function __construct(LoggerInterface $logger, Twig_Loader_DynamicContent
$twigDynamicContentLoader, new \Twig_Loader_Array([])
]));
$this->configureTwig($this->twigEnv);
$this->feedFactory = $feedFactory;
}


Expand Down Expand Up @@ -62,10 +75,15 @@ protected function configureTwig(\Twig_Environment $twig)
$twig->addFilter(new \Twig_SimpleFilter('json_decode', function ($string) {
return json_decode($string, true);
}));

$twig->addFilter(new \Twig_SimpleFilter('rss', function () {
return $this->feedFactory->getItems($this->lead['id'], func_get_args());
}));
}

private function processTwigBlock($lead)
{
$this->lead = $lead;
return function ($matches) use ($lead) {
$templateSource = $matches[1];
$this->logger->debug('BLOCK SOURCE: ' . var_export($templateSource, true));
Expand Down
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ There is a high probability it is compatible with other environments, but we nev
```
* Reusable TWIG snippets could be loaded form Dynamic Content entities.
* TWIG extended with some useful functions and filters (see below).
* RSS support
* RSS items related to contact's segment preferences center and RSS category
## Installation
Expand Down Expand Up @@ -128,6 +130,44 @@ Let's continue with the previous example but turn template for rendering a singl
{% END_TWIG_BLOCK %}
```
Notice prefix `dc:` which instructs template resolver to look for dynamic content instance.
### Example 4: RSS support
```twig
{% TWIG_BLOCK %}
{% set items = 'http://domain.tld/feed/' | rss %}
<ul>
{% for item in items %}
<li>
<a href=''{{ item.link }}'>{{ item.title }}</a> ({{ item.pubDate|date('m/d/Y') }})
<br />{{ item.description|raw }}
</li>
{% endfor %}
</ul>
{% END_TWIG_BLOCK %}
```

### Example 5: RSS related items to contact's segments

- Add one or more categories to item
https://www.w3schools.com/xml/rss_tag_category_item.asp
- Each contact receive personalized items based on segment assignemnt.
- Matching between item categories and segment aliases

```twig
{% TWIG_BLOCK %}
{% set items = 'http://domain.tld/feed/' | rss('segments') %}
<ul>
{% for item in items %}
<li>
<a href=''{{ item.link }}'>{{ item.title }}</a> ({{ item.pubDate|date('m/d/Y') }})
<br />{{ item.description|raw }}
</li>
{% endfor %}
</ul>
{% END_TWIG_BLOCK %}
```

## Credits

Expand Down

0 comments on commit b380878

Please sign in to comment.