cms-niceties/src/Models/Notification.php

93 lines
2.1 KiB
PHP
Raw Normal View History

2021-06-19 21:30:03 +02:00
<?php
/**
* Created by PhpStorm.
* User: tony
* Date: 9/12/18
* Time: 2:55 AM
*/
namespace A2nt\CMSNiceties\Models;
2022-07-02 18:29:25 +02:00
use gorriecoe\Link\Models\Link;
use gorriecoe\LinkField\LinkField;
2021-06-19 21:30:03 +02:00
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\SiteConfig\SiteConfig;
2022-05-10 13:09:06 +02:00
/**
* Class \A2nt\CMSNiceties\Models\Notification
*
* @property string $Title
* @property string $Content
* @property string $DateOn
* @property string $DateOff
* @property string $Area
* @property int $ParentID
* @property int $TargetLinkID
* @method \SilverStripe\SiteConfig\SiteConfig Parent()
* @method \Sheadawson\Linkable\Models\Link TargetLink()
*/
2021-06-19 21:30:03 +02:00
class Notification extends DataObject
{
private static $table_name = 'Notification';
private static $db = [
'Title' => 'Varchar(255)',
'Content' => 'Text',
'DateOn' => 'Date',
'DateOff' => 'Date',
'Area' => 'Enum("Site","Site")',
];
private static $has_one = [
'Parent' => SiteConfig::class,
'TargetLink' => Link::class,
];
private static $defaults = [
'Area' => 'Site',
];
private static $summary_fields = [
'Title' => 'Title',
'Content' => 'Text',
'DateOn' => 'Turn on date',
'DateOff' => 'Turn off date',
];
private static $default_sort = 'DateOn DESC, DateOff DESC, Title ASC';
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldsToTab('Root.Main', [
2022-07-02 18:29:25 +02:00
LinkField::create('TargetLink', 'Link', $this),
2021-06-19 21:30:03 +02:00
]);
return $fields;
}
public function validate()
{
$result = parent::validate();
if (!$this->getField('DateOn') || !$this->getField('DateOff')) {
return $result->addError(
'Turn on and turn off dates are required.',
ValidationResult::TYPE_ERROR
);
}
if (!$this->getField('Content')) {
return $result->addError(
'Text field required.',
ValidationResult::TYPE_ERROR
);
}
return $result;
}
}