Skip to content

Commit

Permalink
FFWEB-2743: Add export CMS feature
Browse files Browse the repository at this point in the history
Add export CMS feature with basic config blacklist pages
  • Loading branch information
Rayn93 authored Jun 30, 2023
1 parent 5598c71 commit 737020b
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Model/Config/CmsConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Factfinder\Export\Model\Config;

use Magento\Framework\App\Config\ScopeConfigInterface;

class CmsConfig
{
private const PATH_DISABLE_CMS_PAGES = 'factfinder_export/cms_export/ff_export_cms_blacklist';

public function __construct(private readonly ScopeConfigInterface $scopeConfig)
{
}

public function getCmsBlacklist(int $scopeCode = null): array
{
$pages = (string) $this->scopeConfig->getValue(self::PATH_DISABLE_CMS_PAGES, 'store', $scopeCode);

return array_filter(explode(',', $pages));
}
}
30 changes: 30 additions & 0 deletions src/Model/Export/Cms/DataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Factfinder\Export\Model\Export\Cms;

use Factfinder\Export\Api\Export\DataProviderInterface;
use Factfinder\Export\Api\Export\ExportEntityInterface;

class DataProvider implements DataProviderInterface
{
public function __construct(
private readonly Pages $pages,
private readonly PageFactory $pageFactory,
private readonly array $fields,
) {
}

/**
* @return ExportEntityInterface[]
*/
public function getEntities(): iterable
{
yield from [];

foreach ($this->pages as $page) {
yield $this->pageFactory->create(['page' => $page, 'pageFields' => $this->fields]);
}
}
}
38 changes: 38 additions & 0 deletions src/Model/Export/Cms/Field/Content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Factfinder\Export\Model\Export\Cms\Field;

use Factfinder\Export\Api\Export\FieldInterface;
use Magento\Cms\Api\Data\PageInterface;
use Magento\Email\Model\Template\Filter;
use Magento\Framework\Model\AbstractModel;

class Content implements FieldInterface
{
public function __construct(private readonly Filter $filter)
{
}

public function getName(): string
{
return 'Content';
}

/**
* @param PageInterface $page
*
* @return string
*/
public function getValue(AbstractModel $page): string
{
$filteredContent = $this->filter->filter($page->getContent());
$stylesAndScripts = '#\<(?:style|script)[^\>]*\>[^\<]*\</(?:style|script)\>#siU';
$variables = '#{{[^}]*}}#siU';
$returns = '#<br\s?\/?>#';
$whitespaces = '#(\s|&nbsp;)+#s';

return preg_replace([$stylesAndScripts, $variables, $returns, $whitespaces], ' ', $filteredContent);
}
}
37 changes: 37 additions & 0 deletions src/Model/Export/Cms/Field/Deeplink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Factfinder\Export\Model\Export\Cms\Field;

use Factfinder\Export\Api\Export\FieldInterface;
use Magento\Cms\Api\Data\PageInterface;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Model\AbstractModel;

class Deeplink implements FieldInterface
{
public function __construct(
private readonly UrlInterface $urlBuilder,
private readonly StoreManagerInterface $storeManager,
) {
}

public function getName(): string
{
return 'Deeplink';
}

/**
* @param PageInterface $page
*
* @return string
*/
public function getValue(AbstractModel $page): string
{
$this->urlBuilder->setScope($this->storeManager->getStore()->getId());

return $this->urlBuilder->getUrl(null, ['_direct' => $page->getIdentifier()]);
}
}
35 changes: 35 additions & 0 deletions src/Model/Export/Cms/Field/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Factfinder\Export\Model\Export\Cms\Field;

use Factfinder\Export\Api\Export\FieldInterface;
use Magento\Cms\Api\Data\PageInterface;
use Magento\Email\Model\Template\Filter;
use Magento\Framework\Model\AbstractModel;

class Image implements FieldInterface
{
public function __construct(private readonly Filter $filter)
{
}

public function getName(): string
{
return 'Image';
}

/**
* @param PageInterface $page
*
* @return string
*/
public function getValue(AbstractModel $page): string
{
$pattern = '#https?://[^/\s]+/\S+\.(jpe?g|png|gif)#i';
preg_match($pattern, $this->filter->filter((string) $page->getContent()), $result);

return $result[0] ?? '';
}
}
43 changes: 43 additions & 0 deletions src/Model/Export/Cms/Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Factfinder\Export\Model\Export\Cms;

use Factfinder\Export\Api\Export\ExportEntityInterface;
use Factfinder\Export\Api\Export\FieldInterface;
use Magento\Cms\Api\Data\PageInterface;

class Page implements ExportEntityInterface
{
public function __construct(
private readonly PageInterface $page,
private readonly array $pageFields = [],
) {
}

public function getId(): int
{
return (int) $this->page->getId();
}

public function toArray(): array
{
$data = [
'PageId' => (string) $this->getId(),
'Master' => (string) $this->getId(),
'Identifier' => (string) $this->page->getIdentifier(),
'Title' => (string) $this->page->getTitle(),
'ContentHeading' => (string) $this->page->getContentHeading(),
'MetaKeywords' => (string) $this->page->getMetaKeywords(),
'MetaDescription' => (string) $this->page->getMetaDescription(),
];

return array_reduce(
$this->pageFields,
fn (array $result, FieldInterface $field): array =>
[$field->getName() => $field->getValue($this->page)] + $result,
$data
);
}
}
48 changes: 48 additions & 0 deletions src/Model/Export/Cms/Pages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Factfinder\Export\Model\Export\Cms;

use Factfinder\Export\Model\Config\CmsConfig;
use Magento\Cms\Api\Data\PageInterface;
use Magento\Cms\Api\PageRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Exception\LocalizedException;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Traversable;

class Pages implements \IteratorAggregate
{
public function __construct(
private readonly PageRepositoryInterface $pageRepository,
private readonly SearchCriteriaBuilder $searchCriteriaBuilder,
private readonly CmsConfig $cmsConfig,
private readonly StoreManagerInterface $storeManager,
) {
}

/**
* @return Traversable|PageInterface[]
* @throws LocalizedException
*/
public function getIterator(): Traversable
{
$query = $this->getQuery()->create();
yield from $this->pageRepository->getList($query)->getItems();
}

protected function getQuery(): SearchCriteriaBuilder
{
$blacklist = $this->cmsConfig->getCmsBlacklist();

if ($blacklist) {
$this->searchCriteriaBuilder->addFilter('identifier', $blacklist, 'nin');
}

$inStores = [Store::DEFAULT_STORE_ID, (int) $this->storeManager->getStore()->getId()];

return $this->searchCriteriaBuilder->addFilter('store_id', $inStores, 'in');
}
}
1 change: 1 addition & 0 deletions src/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<include path="Factfinder_Export::system/general.xml" />
<include path="Factfinder_Export::system/data_transfer.xml" />
<include path="Factfinder_Export::system/export.xml" />
<include path="Factfinder_Export::system/cms.xml" />
<include path="Factfinder_Export::system/basic_auth.xml" />
<include path="Factfinder_Export::system/cron.xml" />
</section>
Expand Down
12 changes: 12 additions & 0 deletions src/etc/adminhtml/system/cms.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<include xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_include.xsd">
<group id="cms_export" translate="label" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
<label>CMS Export Settings</label>
<field id="ff_export_cms_blacklist" translate="label comment" type="multiselect" showInDefault="1" showInWebsite="1" showInStore="1" sortOrder="5">
<label>Pages Blacklist</label>
<comment>Selected pages will not be exported</comment>
<source_model>Magento\Cms\Model\Config\Source\Page</source_model>
<can_be_empty>1</can_be_empty>
</field>
</group>
</include>
2 changes: 2 additions & 0 deletions src/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<is_enabled>0</is_enabled>
<logging_enabled>0</logging_enabled>
<password backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
<version>7.3</version>
<ff_api_version>v4</ff_api_version>
</general>
<data_transfer>
<ff_export_upload_password backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
Expand Down
32 changes: 32 additions & 0 deletions src/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@
<item xsi:type="string" name="dataProvider">Factfinder\Export\Model\Export\Catalog\DataProvider</item>
<item xsi:type="string" name="fieldProvider">Factfinder\Export\Model\Export\Catalog\FieldProvider</item>
</item>
<item name="cms" xsi:type="array">
<item xsi:type="string" name="generator">Factfinder\Export\Model\Export\CmsFeed</item>
<item xsi:type="string" name="dataProvider">Factfinder\Export\Model\Export\Cms\DataProvider</item>
<item xsi:type="array" name="fieldProvider">
<item name="Content" xsi:type="object">Factfinder\Export\Model\Export\Cms\Field\Content</item>
<item name="Deeplink" xsi:type="object">Factfinder\Export\Model\Export\Cms\Field\Deeplink</item>
<item name="Image" xsi:type="object">Factfinder\Export\Model\Export\Cms\Field\Image</item>
</item>
</item>
</argument>
</arguments>
</type>
Expand All @@ -87,6 +96,19 @@
</argument>
</arguments>
</virtualType>
<virtualType name="Factfinder\Export\Model\Export\CmsFeed" type="Factfinder\Export\Model\Export\Feed">
<arguments>
<argument name="columns" xsi:type="array">
<item name="PageId" xsi:type="string">PageId</item>
<item name="Master" xsi:type="string">Master</item>
<item name="Identifier" xsi:type="string">Identifier</item>
<item name="Title" xsi:type="string">Title</item>
<item name="ContentHeading" xsi:type="string">ContentHeading</item>
<item name="MetaKeywords" xsi:type="string">MetaKeywords</item>
<item name="MetaDescription" xsi:type="string">MetaDescription</item>
</argument>
</arguments>
</virtualType>
<virtualType name="Factfinder\Export\Model\Export\Catalog\ProductField\Brand" type="Factfinder\Export\Model\Export\Catalog\ProductField\GenericField">
<arguments>
<argument name="attributeCode" xsi:type="string">manufacturer</argument>
Expand Down Expand Up @@ -119,4 +141,14 @@
<argument name="client" xsi:type="object">Magento\Framework\Filesystem\Io\Ftp</argument>
</arguments>
</type>
<type name="Factfinder\Export\Model\Export\Cms\Field\Content">
<arguments>
<argument name="filter" xsi:type="object">Magento\Email\Model\Template\Filter\Proxy</argument>
</arguments>
</type>
<type name="Factfinder\Export\Model\Export\Cms\Field\Image">
<arguments>
<argument name="filter" xsi:type="object">Magento\Email\Model\Template\Filter\Proxy</argument>
</arguments>
</type>
</config>

0 comments on commit 737020b

Please sign in to comment.