<?php
namespace Cms\Widgets\AlertBanner;
use Cms\ContentBundle\Service\FrontendRenderer;
use Cms\CoreBundle\Service\Slugger;
use Cms\CoreBundle\Service\StringUtils\LoremIpsumGenerator;
use Cms\CoreBundle\Util\DateTimeUtils;
use Cms\ModuleBundle\Model\AbstractModuleListWidgetProcessor;
use Cms\Modules\AlertBundle\Entity\Alert\AlertProxy;
use Cms\Modules\AlertBundle\Entity\ModuleSettings;
use Cms\Modules\AlertBundle\Model\Alert\AlertData;
use Cms\WidgetBundle\Model\ModuleCascadingSharesWidgetProcessorTrait;
use Cms\WidgetBundle\Model\ModuleLimitedListWidgetProcessorInterface;
use Cms\WidgetBundle\Model\ModuleLimitedListWidgetProcessorTrait;
use Cms\WidgetBundle\Model\WidgetObject;
/**
* Class WidgetProcessor
* @package Cms\Widgets\AlertBanner
*/
final class WidgetProcessor extends AbstractModuleListWidgetProcessor implements ModuleLimitedListWidgetProcessorInterface
{
use ModuleLimitedListWidgetProcessorTrait;
use ModuleCascadingSharesWidgetProcessorTrait;
const LIMITS__MAX = 6;
const LIMITS__DEFAULT = self::LIMITS__MAX;
/**
* {@inheritdoc}
* @param AlertBanner $object
*/
public function process(WidgetObject $object, FrontendRenderer $renderer)
{
// get the containers and stuff for this
$containers = $this->determineContainers($object, $renderer->peekGlobals());
$container = $containers[0];
// determine if finding stuff or using sample data
if ( ! empty($object->getId())) {
// make the query
$qb = $this->qb(
AlertProxy::class,
$this->flattenContainers($container, ModuleSettings::class),
$object->getTags()
)
->andWhere('proxy.data_startDate <= :now')
->andWhere('proxy.data_expiryDate >= :now')
->setParameter('now', DateTimeUtils::now())
->addOrderBy('proxy.data_level', 'ASC')
->addOrderBy('proxy.data_startDate', 'ASC')
->addOrderBy('proxy.data_expiryDate', 'ASC');
// query based on limit
$items = $this->query(AlertProxy::class, $qb, $object);
} else {
// use sample data
$items = $this->samples(
AlertProxy::class,
function (AlertProxy $proxy, $index) use ($container) {
return $proxy
->setContainer($container)
->getData()
->setAbstract($this->lipsum->words(15))
->setContent($this->lipsum->paragraphs(1, LoremIpsumGenerator::MODIFIERS__P_WRAPPING))
->setTitle($this->lipsum->words(6, LoremIpsumGenerator::MODIFIERS__CAMEL_CASE))
->setStartDate(DateTimeUtils::current())
->setExpiryDate(DateTimeUtils::afterCurrent(sprintf('PT%sH', $index)))
->setSlug(Slugger::slug($proxy->getData()->getTitle()))
->entity();
},
$this->getLimit($object)
);
}
// add extra html class if there are items, will help with certain themes
if (count($items) > 0) {
$renderer->peekGlobals()->getAssetsOrganizer()->getHtmlTag()->append(
'class',
'cs-alert-banner-active'
);
}
// get a list of popups
// TODO: can use like a callback and array filtering function for this...
$popups = [];
foreach ($items as $item) {
if ($item->getData()->getBehavior() === AlertData::BEHAVIORS__POPUP) {
$popups[] = $item;
}
}
// done
return array(
'containers' => $containers,
'container' => $container,
'items' => $items,
'popups' => $popups,
);
}
/**
* {@inheritdoc}
* @param AlertBanner $object
*/
public function plugin(WidgetObject $object, FrontendRenderer $renderer)
{
return array(
'autoplay' => ($object->getAutoplay() === true),
'speed' => ( ! empty($object->getSpeed())) ? intval($object->getSpeed()) : 5000,
'loop' => ($object->getLoop() === true),
);
}
}