170 lines
4.8 KiB
PHP
170 lines
4.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\Homepage\Block;
|
|
|
|
use Magento\Framework\DataObject\IdentityInterface;
|
|
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
|
|
use Magento\Framework\View\Element\Template;
|
|
use Magento\Framework\View\Element\Template\Context;
|
|
use Magento\Store\Model\ScopeInterface;
|
|
use Mageplaza\Blog\Helper\Data as BlogHelper;
|
|
use Mageplaza\Blog\Helper\Image as BlogImageHelper;
|
|
use Mageplaza\Blog\Model\Post;
|
|
use Mageplaza\Blog\Model\ResourceModel\Post\CollectionFactory as PostCollectionFactory;
|
|
|
|
class NewsRoom extends Template implements IdentityInterface
|
|
{
|
|
private const XML_PATH_IDS = 'esun_homepage/news_room/ids';
|
|
|
|
/**
|
|
* @var array<int, array<string, mixed>>|null
|
|
*/
|
|
private ?array $items = null;
|
|
|
|
public function __construct(
|
|
Context $context,
|
|
private readonly PostCollectionFactory $postCollectionFactory,
|
|
private readonly BlogHelper $blogHelper,
|
|
private readonly TimezoneInterface $timezone,
|
|
array $data = []
|
|
) {
|
|
parent::__construct($context, $data);
|
|
}
|
|
|
|
/**
|
|
* @return array<int, array<string, mixed>>
|
|
*/
|
|
public function getItems(): array
|
|
{
|
|
if ($this->items === null) {
|
|
$ids = $this->getConfiguredIds();
|
|
$this->items = [];
|
|
|
|
if (!$ids) {
|
|
return $this->items;
|
|
}
|
|
|
|
$collection = $this->postCollectionFactory->create();
|
|
$collection->addFieldToFilter('post_id', ['in' => $ids])
|
|
->addFieldToFilter('enabled', 1)
|
|
->addFieldToFilter('publish_date', ['to' => $this->timezone->date()->format('Y-m-d H:i:s')])
|
|
->setOrder('publish_date', 'desc');
|
|
|
|
foreach ($collection as $post) {
|
|
$this->items[] = $this->preparePostItem($post);
|
|
}
|
|
}
|
|
|
|
return $this->items;
|
|
}
|
|
|
|
public function getCacheKeyInfo(): array
|
|
{
|
|
return [
|
|
'ESUN_HOMEPAGE_NEWS_ROOM',
|
|
(int) $this->_storeManager->getStore()->getId(),
|
|
implode('_', $this->getConfiguredIds()),
|
|
];
|
|
}
|
|
|
|
public function getIdentities(): array
|
|
{
|
|
$identities = [];
|
|
|
|
foreach ($this->getItems() as $item) {
|
|
$identities[] = Post::CACHE_TAG . '_' . (int) $item['id'];
|
|
}
|
|
|
|
return array_values(array_unique($identities));
|
|
}
|
|
|
|
/**
|
|
* @return array<int, int>
|
|
*/
|
|
private function getConfiguredIds(): array
|
|
{
|
|
$value = (string) $this->_scopeConfig->getValue(
|
|
self::XML_PATH_IDS,
|
|
ScopeInterface::SCOPE_STORE
|
|
);
|
|
|
|
if (trim($value) === '') {
|
|
return [];
|
|
}
|
|
|
|
$ids = preg_split('/[\s,;]+/', $value, -1, PREG_SPLIT_NO_EMPTY) ?: [];
|
|
$ids = array_map('intval', $ids);
|
|
$ids = array_filter($ids, static fn (int $id): bool => $id > 0);
|
|
|
|
return array_values(array_unique($ids));
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function preparePostItem(Post $post): array
|
|
{
|
|
$imageUrl = $this->getPostImageUrl((string) $post->getImage());
|
|
|
|
return [
|
|
'id' => (int) $post->getId(),
|
|
'title' => (string) $post->getName(),
|
|
'image_url' => $imageUrl,
|
|
'publish_date' => $this->formatPublishDate((string) $post->getData('publish_date')),
|
|
'publish_date_iso' => $this->formatPublishDateIso((string) $post->getData('publish_date')),
|
|
'tag_name' => $this->getFirstTagName($post) ?: $this->getFirstCategoryName($post),
|
|
'url' => (string) $post->getUrl((int) $this->_storeManager->getStore()->getId()),
|
|
];
|
|
}
|
|
|
|
private function getPostImageUrl(string $image): string
|
|
{
|
|
if ($image === '') {
|
|
return $this->getViewFileUrl('Mageplaza_Blog::media/images/mageplaza-logo-default.png');
|
|
}
|
|
|
|
return (string) $this->blogHelper->getImageHelper()->resizeImage(
|
|
$image,
|
|
'640x',
|
|
BlogImageHelper::TEMPLATE_MEDIA_TYPE_POST
|
|
);
|
|
}
|
|
|
|
private function formatPublishDate(string $date): string
|
|
{
|
|
if ($date === '') {
|
|
return '';
|
|
}
|
|
|
|
return $this->timezone->date(new \DateTime($date))->format('y.m.d');
|
|
}
|
|
|
|
private function formatPublishDateIso(string $date): string
|
|
{
|
|
if ($date === '') {
|
|
return '';
|
|
}
|
|
|
|
return $this->timezone->date(new \DateTime($date))->format('Y-m-d');
|
|
}
|
|
|
|
private function getFirstTagName(Post $post): string
|
|
{
|
|
foreach ($post->getSelectedTagsCollection() as $tag) {
|
|
return (string) $tag->getName();
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
private function getFirstCategoryName(Post $post): string
|
|
{
|
|
foreach ($post->getSelectedCategoriesCollection() as $category) {
|
|
return (string) $category->getName();
|
|
}
|
|
|
|
return '';
|
|
}
|
|
}
|