Skip to content

Commit

Permalink
Update: Show multiple categories
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnitto committed May 23, 2023
1 parent 9389e75 commit 53c7489
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 154 deletions.
53 changes: 41 additions & 12 deletions Classes/Controller/BackendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Garagist\Mautic\Service\TaskService;
use Garagist\Mautic\Service\TestEmailService;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Eel\FlowQuery\FlowQuery;
use Neos\Error\Messages\Message;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\I18n\EelHelper\TranslationHelper;
Expand Down Expand Up @@ -160,24 +161,22 @@ public function indexAction(): void
$categoryList = [];
$hasCategories = false;
foreach ($nodes as $node) {
$categoryNode = $this->nodeService->getParentByType($node, 'Garagist.Mautic:Mixin.Category');
$category = $this->getCategories($node);
$identifier = $node->getIdentifier();
$categoryIdentifier = $categoryNode ? $categoryNode->getIdentifier() : null;
$title = $node->getProperty('title');
$categoryTitle = $categoryNode ? $categoryNode->getProperty('title') : null;
$count = count($this->mauticService->getEmailsNodeIdentifier(
$node->getIdentifier()
));
if ($categoryNode) {
$categoryList[$categoryIdentifier] = $categoryTitle;
if ($category) {
$categoryList[$category['identifier']] = $category['title'];
$hasCategories = true;
}
$pages[$identifier] = [
"count" => $count,
"node" => $node,
"title" => $title,
"categoryTitle" => $categoryTitle,
"categoryIdentifier" => $categoryIdentifier,
"categoryTitle" => $category ? $category['title'] : null,
"categoryIdentifier" => $category ? $category['identifier'] : null,
];
}

Expand Down Expand Up @@ -413,7 +412,7 @@ public function unlockAction(
public function nodeAction(NodeInterface $node): void
{
$ping = $this->ping();
$categoryNode = $this->nodeService->getParentByType($node, 'Garagist.Mautic:Mixin.Category');
$categories = $this->getCategories($node);
$emails = $this->mauticService->getEmailsNodeIdentifier($node->getIdentifier());
$flashMessages = $this->flashMessageService->getFlashMessageContainerForRequest($this->request)->getMessagesAndFlush();
$prefilledSegments = $this->mauticService->getPrefilledSegments($node);
Expand All @@ -422,7 +421,7 @@ public function nodeAction(NodeInterface $node): void
$this->view->assignMultiple([
'emails' => $emails,
'node' => $node,
'categoryNode' => $categoryNode,
'categoryNodes' => $categories ? $categories['nodes'] : null,
'prefilledSegments' => $prefilledSegments,
'allSegments' => $allSegments,
'flashMessages' => $flashMessages,
Expand All @@ -443,7 +442,7 @@ public function detailAction(
MauticEmail $email
): void {
$ping = $this->ping();
$categoryNode = $this->nodeService->getParentByType($node, 'Garagist.Mautic:Mixin.Category');
$categories = $this->getCategories($node);
$mauticRecord = $this->apiService->findMauticRecordByEmailIdentifier($email->getEmailIdentifier());
$history = $this->mauticService->getAuditLog($email);
$prefilledSegments = $this->mauticService->getPrefilledSegments($node);
Expand All @@ -452,12 +451,14 @@ public function detailAction(
$testEmailRecipients = $this->testEmailService->getTestEmailRecipients();

// Disable tracking pixel for the preview
$mauticRecord['customHtml'] = str_replace($this->trackingPixel, '<!-- Tracking Pixel disabled for preview ' . $this->trackingPixel . '-->', $mauticRecord['customHtml']);
if ($mauticRecord) {
$mauticRecord['customHtml'] = str_replace($this->trackingPixel, '<!-- Tracking Pixel disabled for preview ' . $this->trackingPixel . '-->', $mauticRecord['customHtml']);
}

$this->view->assignMultiple([
'email' => $email,
'node' => $node,
'categoryNode' => $categoryNode,
'categoryNodes' => $categories ? $categories['nodes'] : null,
'history' => $history,
'mauticRecord' => $mauticRecord,
'allSegments' => $allSegments,
Expand Down Expand Up @@ -700,4 +701,32 @@ private function ping(): bool

return $ping;
}

/**
* Get category id and name
*
* @param NodeInterface $node
* @return array|null
*/
private function getCategories(NodeInterface $node): ?array
{
$flowQuery = new FlowQuery(array($node));
$parents = $flowQuery->parents('[instanceof Garagist.Mautic:Mixin.Category]')->get();
if (!count($parents)) {
return null;
}
$identifier = $parents[0]->getIdentifier();
$parents = array_reverse($parents);
$title = [];
foreach ($parents as $entry) {
$title[] = $entry->getProperty('title');
}
$title = implode('', $title);

return [
'identifier' => $identifier,
'title' => $title,
'nodes' => $parents,
];
}
}
60 changes: 35 additions & 25 deletions Resources/Private/Modules/Views/Detail.fusion
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
Garagist.Mautic.BackendController.detail = Neos.Fusion:Component {
title = ${q(node).property('title')}
action = ${Configuration.Setting('Garagist.Mautic.action')}
categoryTitle = ${categoryNode ? q(categoryNode).property('title') : null}

categories = Neos.Fusion:Map {
items = ${categoryNodes || []}
itemRenderer = Neos.Fusion:DataStructure {
title = ${q(item).property('title')}
node = ${item}
}
}

emailData = Garagist.Mautic:Component.EmailData {
node = ${node}
Expand All @@ -17,15 +24,16 @@ Garagist.Mautic.BackendController.detail = Neos.Fusion:Component {
<div class="space-y-4">
<h1 class="text-2xl">
{props._i18n.id('newsletter.headline').translate()}
<Neos.Fusion:Link.Action
@if={categoryNode}
href.action="link"
href.arguments.node={categoryNode}
class="hover:!underline focus:!underline"
target="_blank"
content={props.categoryTitle}
/>
{categoryNode ? ' › ' : ''}
<Neos.Fusion:Loop items={props.categories}>
<Neos.Fusion:Link.Action
href.action="link"
href.arguments.node={item.node}
class="hover:!underline focus:!underline"
target="_blank"
content={item.title}
/>
{' › '}
</Neos.Fusion:Loop>
<Neos.Fusion:Link.Action
href.action="link"
href.arguments.node={node}
Expand Down Expand Up @@ -114,22 +122,24 @@ Garagist.Mautic.BackendController.detail = Neos.Fusion:Component {
</section>
</div>

<h2 @if={ping} class="text-xl mt-8 mb-4">{props._i18n.id('email.content').translate()}</h2>
<div @if={ping} class="grid gap-8 grid-flow-row auto-rows-auto grid-cols-1 lg:grid-cols-2">
<div>
<h3 class="text-md mb-4">{props._i18n.id('email.content.html').translate()}</h3>
<iframe
srcdoc={mauticRecord.customHtml}
class="bg-white w-full h-[calc(100vh-150px)]"
></iframe>
</div>
<div>
<h3 class="text-md mb-4">{props._i18n.id('email.content.plaintext').translate()}</h3>
<pre class="block bg-slate-200 text-neos-gray-dark px-8 py-12 overflow-auto h-[calc(100vh-150px)] text-xs md:text-sm lg:text-xs xl:text-sm">
{mauticRecord.plainText}
</pre>
<Neos.Fusion:Fragment @if={ping && mauticRecord}>
<h2 class="text-xl mt-8 mb-4">{props._i18n.id('email.content').translate()}</h2>
<div class="grid gap-8 grid-flow-row auto-rows-auto grid-cols-1 lg:grid-cols-2">
<div>
<h3 class="text-md mb-4">{props._i18n.id('email.content.html').translate()}</h3>
<iframe
srcdoc={mauticRecord.customHtml}
class="bg-white w-full h-[calc(100vh-150px)]"
></iframe>
</div>
<div>
<h3 class="text-md mb-4">{props._i18n.id('email.content.plaintext').translate()}</h3>
<pre class="block bg-slate-200 text-neos-gray-dark px-8 py-12 overflow-auto h-[calc(100vh-150px)] text-xs md:text-sm lg:text-xs xl:text-sm">
{mauticRecord.plainText}
</pre>
</div>
</div>
</div>
</Neos.Fusion:Fragment>
</main>
<div class="neos-footer">
<Neos.Fusion:Link.Action
Expand Down
Loading

0 comments on commit 53c7489

Please sign in to comment.