2021-06-19 21:30:03 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace A2nt\CMSNiceties\Extensions;
|
|
|
|
|
|
|
|
use SilverStripe\Forms\CheckboxField;
|
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldDetailForm;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldEditButton;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
|
|
|
|
use SilverStripe\Forms\HeaderField;
|
|
|
|
use SilverStripe\Forms\LiteralField;
|
|
|
|
use SilverStripe\ORM\DataExtension;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use A2nt\CMSNiceties\Models\Notification;
|
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton;
|
|
|
|
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
|
|
|
|
use Symbiote\GridFieldExtensions\GridFieldTitleHeader;
|
|
|
|
|
2022-05-10 13:09:06 +02:00
|
|
|
/**
|
|
|
|
* Class \A2nt\CMSNiceties\Extensions\NotificationsExtension
|
|
|
|
*
|
|
|
|
* @property \A2nt\CMSNiceties\Extensions\NotificationsExtension $owner
|
|
|
|
* @property boolean $ShowNotifications
|
|
|
|
* @method \SilverStripe\ORM\DataList|\A2nt\CMSNiceties\Models\Notification[] Notifications()
|
|
|
|
*/
|
2021-06-19 21:30:03 +02:00
|
|
|
class NotificationsExtension extends DataExtension
|
|
|
|
{
|
|
|
|
private static $db = [
|
|
|
|
'ShowNotifications' => 'Boolean(1)',
|
|
|
|
];
|
|
|
|
|
|
|
|
private static $has_many = [
|
|
|
|
'Notifications' => Notification::class,
|
|
|
|
];
|
|
|
|
|
|
|
|
public function updateCMSFields(FieldList $fields)
|
|
|
|
{
|
|
|
|
$tab = $fields->findOrMakeTab('Root.Notifications');
|
|
|
|
|
|
|
|
if(!$this->owner->exists()) {
|
|
|
|
$tab->push(LiteralField::create(
|
|
|
|
'NotificationsNotice',
|
|
|
|
'<p class="message notice">The object must be saved before notifications can be added</p>'
|
|
|
|
));
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$items = $this->owner->Notifications();
|
|
|
|
|
|
|
|
$config = GridFieldConfig::create();
|
|
|
|
$config->addComponents([
|
|
|
|
new GridFieldToolbarHeader(),
|
|
|
|
new GridFieldTitleHeader(),
|
|
|
|
new GridFieldEditableColumns(),
|
|
|
|
new GridFieldAddNewInlineButton('toolbar-header-right'),
|
|
|
|
new GridFieldDetailForm(),
|
|
|
|
new GridFieldEditButton(),
|
|
|
|
new GridFieldDeleteAction(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$tab->setChildren(FieldList::create(
|
|
|
|
HeaderField::create('NotificationsHeader','Notifications'),
|
|
|
|
LiteralField::create(
|
|
|
|
'CurrentNotifications',
|
|
|
|
'<b>Current:</b>'
|
|
|
|
.$this->owner->renderWith('App\\Objects\\NotificationsList')
|
|
|
|
),
|
|
|
|
CheckboxField::create('ShowNotifications'),
|
|
|
|
GridField::create(
|
|
|
|
'Notifications',
|
|
|
|
'',
|
|
|
|
$items,
|
|
|
|
$config
|
|
|
|
)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function NotificationsToday()
|
|
|
|
{
|
|
|
|
$items = $this->owner->Notifications();
|
|
|
|
$time = time();
|
|
|
|
|
|
|
|
return $items->filter([
|
|
|
|
'DateOn:LessThanOrEqual' => date('Y-m-d', $time),
|
|
|
|
'DateOff:GreaterThanOrEqual' => date('Y-m-d', $time),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|