ja.son-williams.ca/www/plugins/jasonwilliams/feed/components/ShortFeed.php

80 lines
2.4 KiB
PHP

<?php namespace JasonWilliams\Feed\Components;
use Cms\Classes\ComponentBase;
use JasonWilliams\Feed\Models\FeedItem;
use JasonWilliams\Feed\Models\Tags;
class ShortFeed extends ComponentBase
{
private $channelfilter;
public function componentDetails()
{
return [
'name' => 'Short Feed',
'description' => 'Displays a mini-feed of the most recent feed items, loaded with other page content'
];
}
public function defineProperties()
{
return [
'channelFilter' => [
'title' => 'Channels',
'description' => 'A comma-separated list of channels to include in feed.',
'type' => 'string',
'default' => ''
],
'tagFilter' => [
'title' => 'Tag',
'description' => 'The tag to display in the feed.',
'type' => 'string',
'default' => ''
],
'maxItems' => [
'title' => 'Max items',
'description' => 'How many feed items should be displayed?',
'default' => 10,
'type' => 'string',
'validationPattern' => '^[0-9]+$',
'validationMessage' => 'The Max items property must be numeric'
]
];
}
public function onRun()
{
date_default_timezone_set('America/Edmonton');
// Set up the results query
$results = FeedItem::orderBy('timestamp', 'desc')->take($this->property('maxItems'));
// Do we need to filter based on a tag?
if ($this->property('tagFilter') != null)
{
$results->join('jasonwilliams_feed_tags', 'jasonwilliams_feed_.id', '=', 'jasonwilliams_feed_tags.feed_item_id');
$results->where('tag', $this->property('tagFilter'));
}
// Do we need to filter based on the channel?
if ($this->property('channelFilter') != null && $this->property('channelFilter') != 'all')
{
$this->channelfilter = explode(',', $this->property('channelFilter'));
$results->whereHas('channel', function($q) {
$firstchannel = true;
foreach ($this->channelfilter as $channel)
{
if ($firstchannel) $q->where('slug', $channel);
else $q->orWhere('slug', $channel);
$firstchannel = false;
}
});
}
$this->page['posts'] = $results->get();
}
}