2009-11-02 05:02:57 +01:00
|
|
|
<?php
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2017-09-06 05:49:23 +02:00
|
|
|
namespace SilverStripe\ContentReview\Extensions;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
|
|
|
use SilverStripe\ContentReview\Jobs\ContentReviewNotificationJob;
|
|
|
|
use SilverStripe\ContentReview\Models\ContentReviewLog;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Forms\CompositeField;
|
|
|
|
use SilverStripe\Forms\DateField;
|
2017-09-13 01:16:47 +02:00
|
|
|
use SilverStripe\Forms\DateTimeField;
|
2017-09-06 05:49:23 +02:00
|
|
|
use SilverStripe\Forms\DropdownField;
|
2018-01-08 22:11:31 +01:00
|
|
|
use SilverStripe\Forms\FieldList;
|
2017-09-06 05:49:23 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldDataColumns;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldSortableHeader;
|
2018-01-08 22:11:31 +01:00
|
|
|
use SilverStripe\Forms\HeaderField;
|
2017-09-06 05:49:23 +02:00
|
|
|
use SilverStripe\Forms\ListboxField;
|
2018-01-08 22:11:31 +01:00
|
|
|
use SilverStripe\Forms\LiteralField;
|
2017-09-06 05:49:23 +02:00
|
|
|
use SilverStripe\Forms\OptionsetField;
|
|
|
|
use SilverStripe\Forms\ReadonlyField;
|
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\ORM\DataExtension;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\FieldType\DBDate;
|
2018-01-08 22:11:31 +01:00
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
2017-09-06 05:49:23 +02:00
|
|
|
use SilverStripe\ORM\SS_List;
|
|
|
|
use SilverStripe\Security\Group;
|
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Permission;
|
|
|
|
use SilverStripe\Security\PermissionProvider;
|
|
|
|
use SilverStripe\Security\Security;
|
|
|
|
use SilverStripe\SiteConfig\SiteConfig;
|
|
|
|
use SilverStripe\View\Requirements;
|
|
|
|
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
|
2017-09-07 06:14:37 +02:00
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJobService;
|
2017-09-06 05:49:23 +02:00
|
|
|
|
2009-11-02 05:02:57 +01:00
|
|
|
/**
|
2015-11-02 00:27:42 +01:00
|
|
|
* Set dates at which content needs to be reviewed and provide a report and emails to alert
|
|
|
|
* to content needing review.
|
|
|
|
*
|
|
|
|
* @property string $ContentReviewType
|
2018-01-08 04:47:31 +01:00
|
|
|
* @property int $ReviewPeriodDays
|
|
|
|
* @property Date $NextReviewDate
|
2015-11-02 00:27:42 +01:00
|
|
|
* @property string $LastEditedByName
|
|
|
|
* @property string $OwnerNames
|
2009-11-02 05:02:57 +01:00
|
|
|
*
|
2015-11-02 00:27:42 +01:00
|
|
|
* @method DataList ReviewLogs()
|
|
|
|
* @method DataList ContentReviewGroups()
|
|
|
|
* @method DataList ContentReviewUsers()
|
2009-11-02 05:02:57 +01:00
|
|
|
*/
|
2015-11-02 00:27:42 +01:00
|
|
|
class SiteTreeContentReview extends DataExtension implements PermissionProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-01-08 04:47:31 +01:00
|
|
|
private static $db = [
|
2015-11-02 00:27:42 +01:00
|
|
|
"ContentReviewType" => "Enum('Inherit, Disabled, Custom', 'Inherit')",
|
2018-01-08 04:47:31 +01:00
|
|
|
"ReviewPeriodDays" => "Int",
|
|
|
|
"NextReviewDate" => "Date",
|
|
|
|
"LastEditedByName" => "Varchar(255)",
|
|
|
|
"OwnerNames" => "Varchar(255)",
|
|
|
|
];
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-01-08 04:47:31 +01:00
|
|
|
private static $defaults = [
|
2015-11-02 00:27:42 +01:00
|
|
|
"ContentReviewType" => "Inherit",
|
2018-01-08 04:47:31 +01:00
|
|
|
];
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-01-08 04:47:31 +01:00
|
|
|
private static $has_many = [
|
2017-09-06 05:49:23 +02:00
|
|
|
"ReviewLogs" => ContentReviewLog::class,
|
2018-01-08 04:47:31 +01:00
|
|
|
];
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-01-08 04:47:31 +01:00
|
|
|
private static $belongs_many_many = [
|
2017-09-06 05:49:23 +02:00
|
|
|
"ContentReviewGroups" => Group::class,
|
2018-01-08 04:47:31 +01:00
|
|
|
"ContentReviewUsers" => Member::class,
|
|
|
|
];
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-01-08 04:47:31 +01:00
|
|
|
private static $schedule = [
|
|
|
|
0 => "No automatic review date",
|
|
|
|
1 => "1 day",
|
|
|
|
7 => "1 week",
|
|
|
|
30 => "1 month",
|
|
|
|
60 => "2 months",
|
|
|
|
91 => "3 months",
|
2015-11-02 00:27:42 +01:00
|
|
|
121 => "4 months",
|
|
|
|
152 => "5 months",
|
|
|
|
183 => "6 months",
|
|
|
|
365 => "12 months",
|
2018-01-08 04:47:31 +01:00
|
|
|
];
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get_schedule()
|
|
|
|
{
|
2017-09-06 05:49:23 +02:00
|
|
|
return Config::inst()->get(static::class, 'schedule');
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a list of groups and members and return a list of unique member.
|
|
|
|
*
|
|
|
|
* @param SS_List $groups
|
|
|
|
* @param SS_List $members
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
public static function merge_owners(SS_List $groups, SS_List $members)
|
|
|
|
{
|
|
|
|
$contentReviewOwners = new ArrayList();
|
|
|
|
|
|
|
|
if ($groups->count()) {
|
2018-01-08 04:47:31 +01:00
|
|
|
$groupIDs = [];
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
foreach ($groups as $group) {
|
|
|
|
$familyIDs = $group->collateFamilyIDs();
|
|
|
|
|
|
|
|
if (is_array($familyIDs)) {
|
2022-04-13 00:23:56 +02:00
|
|
|
$groupIDs = array_merge($groupIDs, array_values($familyIDs ?? []));
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 00:23:56 +02:00
|
|
|
array_unique($groupIDs ?? []);
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2022-04-13 00:23:56 +02:00
|
|
|
if (count($groupIDs ?? [])) {
|
2018-01-08 04:47:31 +01:00
|
|
|
$groupMembers = DataObject::get(Member::class)
|
|
|
|
->where("\"Group\".\"ID\" IN (" . implode(",", $groupIDs) . ")")
|
2015-11-02 00:27:42 +01:00
|
|
|
->leftJoin("Group_Members", "\"Member\".\"ID\" = \"Group_Members\".\"MemberID\"")
|
2017-09-06 05:49:23 +02:00
|
|
|
/** @skipUpgrade */
|
|
|
|
->leftJoin('Group', "\"Group_Members\".\"GroupID\" = \"Group\".\"ID\"");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
$contentReviewOwners->merge($groupMembers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$contentReviewOwners->merge($members);
|
|
|
|
$contentReviewOwners->removeDuplicates();
|
|
|
|
|
|
|
|
return $contentReviewOwners;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FieldList $actions
|
|
|
|
*/
|
|
|
|
public function updateCMSActions(FieldList $actions)
|
|
|
|
{
|
2017-09-11 02:01:41 +02:00
|
|
|
if (!$this->canBeReviewedBy(Security::getCurrentUser())) {
|
|
|
|
return;
|
|
|
|
}
|
2016-05-17 06:34:00 +02:00
|
|
|
|
2017-10-10 22:42:28 +02:00
|
|
|
Requirements::css('silverstripe/contentreview:client/dist/styles/contentreview.css');
|
|
|
|
Requirements::javascript('silverstripe/contentreview:client/dist/js/contentreview.js');
|
2016-05-17 06:34:00 +02:00
|
|
|
|
2017-09-11 02:01:41 +02:00
|
|
|
$reviewTab = LiteralField::create('ContentReviewButton', $this->owner->renderWith(__CLASS__ . '_button'));
|
|
|
|
$actions->insertAfter('MajorActions', $reviewTab);
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns false if the content review have disabled.
|
|
|
|
*
|
|
|
|
* @param SiteTree $page
|
|
|
|
*
|
2018-01-08 04:47:31 +01:00
|
|
|
* @return bool|DBDate
|
2015-11-02 00:27:42 +01:00
|
|
|
*/
|
|
|
|
public function getReviewDate(SiteTree $page = null)
|
|
|
|
{
|
|
|
|
if ($page === null) {
|
|
|
|
$page = $this->owner;
|
|
|
|
}
|
|
|
|
|
2017-09-06 05:49:23 +02:00
|
|
|
if ($page->obj('NextReviewDate')->exists()) {
|
|
|
|
return $page->obj('NextReviewDate');
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$options = $this->owner->getOptions();
|
|
|
|
|
|
|
|
if (!$options) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$options->ReviewPeriodDays) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Failover to check on ReviewPeriodDays + LastEdited
|
2017-09-06 06:53:45 +02:00
|
|
|
$nextReviewUnixSec = strtotime(' + ' . $options->ReviewPeriodDays . ' days', DBDatetime::now()->getTimestamp());
|
2017-09-06 05:49:23 +02:00
|
|
|
$date = DBDate::create('NextReviewDate');
|
|
|
|
$date->setValue($nextReviewUnixSec);
|
2015-11-02 00:27:42 +01:00
|
|
|
return $date;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the object that have the information about the content review settings. Either:
|
|
|
|
*
|
|
|
|
* - a SiteTreeContentReview decorated object
|
|
|
|
* - the default SiteTree config
|
|
|
|
* - false if this page have it's content review disabled
|
|
|
|
*
|
|
|
|
* Will go through parents and root pages will use the site config if their setting is Inherit.
|
|
|
|
*
|
|
|
|
* @return bool|DataObject
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function getOptions()
|
|
|
|
{
|
|
|
|
if ($this->owner->ContentReviewType == "Custom") {
|
|
|
|
return $this->owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->owner->ContentReviewType == "Disabled") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$page = $this->owner;
|
|
|
|
|
|
|
|
// $page is inheriting it's settings from it's parent, find
|
|
|
|
// the first valid parent with a valid setting
|
|
|
|
while ($parent = $page->Parent()) {
|
|
|
|
// Root page, use site config
|
|
|
|
if (!$parent->exists()) {
|
|
|
|
return SiteConfig::current_site_config();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($parent->ContentReviewType == "Custom") {
|
|
|
|
return $parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($parent->ContentReviewType == "Disabled") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$page = $parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Exception("This shouldn't really happen, as per usual developer logic.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getOwnerNames()
|
|
|
|
{
|
|
|
|
$options = $this->getOptions();
|
|
|
|
|
2018-01-08 04:47:31 +01:00
|
|
|
$names = [];
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
if (!$options) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($options->OwnerGroups() as $group) {
|
|
|
|
$names[] = $group->getBreadcrumbs(" > ");
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($options->OwnerUsers() as $group) {
|
|
|
|
$names[] = $group->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode(", ", $names);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function getEditorName()
|
|
|
|
{
|
2017-09-06 05:49:23 +02:00
|
|
|
$member = Security::getCurrentUser();
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
if ($member) {
|
2016-05-17 06:34:00 +02:00
|
|
|
return $member->getTitle();
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all Members that are Content Owners to this page. This includes checking group
|
|
|
|
* hierarchy and adding any direct users.
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
public function ContentReviewOwners()
|
|
|
|
{
|
|
|
|
return SiteTreeContentReview::merge_owners(
|
|
|
|
$this->OwnerGroups(),
|
|
|
|
$this->OwnerUsers()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ManyManyList
|
|
|
|
*/
|
|
|
|
public function OwnerGroups()
|
|
|
|
{
|
|
|
|
return $this->owner->getManyManyComponents("ContentReviewGroups");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ManyManyList
|
|
|
|
*/
|
|
|
|
public function OwnerUsers()
|
|
|
|
{
|
|
|
|
return $this->owner->getManyManyComponents("ContentReviewUsers");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FieldList $fields
|
|
|
|
*/
|
|
|
|
public function updateSettingsFields(FieldList $fields)
|
|
|
|
{
|
2017-11-20 21:18:31 +01:00
|
|
|
if ($this->owner->hasMethod('displayContentReview') && !$this->owner->displayContentReview()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-08 22:11:31 +01:00
|
|
|
Requirements::javascript('silverstripe/contentreview:client/dist/js/contentreview.js');
|
2015-11-02 00:27:42 +01:00
|
|
|
// Display read-only version only
|
|
|
|
if (!Permission::check("EDIT_CONTENT_REVIEW_FIELDS")) {
|
|
|
|
$schedule = self::get_schedule();
|
2018-01-08 04:47:31 +01:00
|
|
|
$contentOwners = ReadonlyField::create(
|
|
|
|
"ROContentOwners",
|
|
|
|
_t(__CLASS__ . ".CONTENTOWNERS", "Content Owners"),
|
|
|
|
$this->getOwnerNames()
|
|
|
|
);
|
|
|
|
$nextReviewAt = DateField::create(
|
|
|
|
'RONextReviewDate',
|
|
|
|
_t(__CLASS__ . ".NEXTREVIEWDATE", "Next review date"),
|
|
|
|
$this->owner->NextReviewDate
|
|
|
|
);
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
if (!isset($schedule[$this->owner->ReviewPeriodDays])) {
|
2018-01-08 04:47:31 +01:00
|
|
|
$reviewFreq = ReadonlyField::create(
|
|
|
|
"ROReviewPeriodDays",
|
|
|
|
_t(__CLASS__ . ".REVIEWFREQUENCY", "Review frequency"),
|
|
|
|
$schedule[0]
|
|
|
|
);
|
2015-11-02 00:27:42 +01:00
|
|
|
} else {
|
2018-01-08 04:47:31 +01:00
|
|
|
$reviewFreq = ReadonlyField::create(
|
|
|
|
"ROReviewPeriodDays",
|
|
|
|
_t(__CLASS__ . ".REVIEWFREQUENCY", "Review frequency"),
|
|
|
|
$schedule[$this->owner->ReviewPeriodDays]
|
|
|
|
);
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$logConfig = GridFieldConfig::create()
|
2017-09-06 05:49:23 +02:00
|
|
|
->addComponent(Injector::inst()->create(GridFieldSortableHeader::class))
|
|
|
|
->addComponent($logColumns = Injector::inst()->create(GridFieldDataColumns::class));
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
// Cast the value to the users preferred date format
|
2018-01-08 04:47:31 +01:00
|
|
|
$logColumns->setFieldCasting([
|
2017-09-13 01:16:47 +02:00
|
|
|
'Created' => DateTimeField::class . '->value',
|
2018-01-08 04:47:31 +01:00
|
|
|
]);
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
$logs = GridField::create("ROReviewNotes", "Review Notes", $this->owner->ReviewLogs(), $logConfig);
|
|
|
|
|
2018-01-08 04:47:31 +01:00
|
|
|
$optionsFrom = ReadonlyField::create(
|
|
|
|
"ROType",
|
|
|
|
_t(__CLASS__ . ".SETTINGSFROM", "Options are"),
|
|
|
|
$this->owner->ContentReviewType
|
|
|
|
);
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2018-01-08 04:47:31 +01:00
|
|
|
$fields->addFieldsToTab("Root.ContentReview", [
|
2015-11-02 00:27:42 +01:00
|
|
|
$contentOwners,
|
|
|
|
$nextReviewAt->performReadonlyTransformation(),
|
|
|
|
$reviewFreq,
|
|
|
|
$optionsFrom,
|
|
|
|
$logs,
|
2018-01-08 04:47:31 +01:00
|
|
|
]);
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-08 04:47:31 +01:00
|
|
|
$options = [];
|
2018-01-08 21:53:59 +01:00
|
|
|
$options["Disabled"] = _t(__CLASS__ . ".DISABLE", "Disable content review");
|
|
|
|
$options["Inherit"] = _t(__CLASS__ . ".INHERIT", "Inherit from parent page");
|
|
|
|
$options["Custom"] = _t(__CLASS__ . ".CUSTOM", "Custom settings");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2018-01-08 04:47:31 +01:00
|
|
|
$viewersOptionsField = OptionsetField::create(
|
|
|
|
"ContentReviewType",
|
|
|
|
_t(__CLASS__ . ".OPTIONS", "Options"),
|
|
|
|
$options
|
|
|
|
);
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2021-09-14 03:53:25 +02:00
|
|
|
$users = Permission::get_members_by_permission(['CMS_ACCESS_CMSMain', 'CMS_ACCESS_LeftAndMain', 'ADMIN']);
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
$usersMap = $users->map("ID", "Title")->toArray();
|
|
|
|
|
|
|
|
asort($usersMap);
|
|
|
|
|
2018-01-08 21:53:59 +01:00
|
|
|
$userField = ListboxField::create("OwnerUsers", _t(__CLASS__ . ".PAGEOWNERUSERS", "Users"), $usersMap)
|
2015-11-18 04:38:28 +01:00
|
|
|
->addExtraClass('custom-setting')
|
2018-01-08 21:53:59 +01:00
|
|
|
->setAttribute("data-placeholder", _t(__CLASS__ . ".ADDUSERS", "Add users"))
|
|
|
|
->setDescription(_t(__CLASS__ . '.OWNERUSERSDESCRIPTION', 'Page owners that are responsible for reviews'));
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2018-01-08 04:47:31 +01:00
|
|
|
$groupsMap = [];
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
foreach (Group::get() as $group) {
|
|
|
|
$groupsMap[$group->ID] = $group->getBreadcrumbs(" > ");
|
|
|
|
}
|
|
|
|
asort($groupsMap);
|
|
|
|
|
2018-01-08 21:53:59 +01:00
|
|
|
$groupField = ListboxField::create("OwnerGroups", _t(__CLASS__ . ".PAGEOWNERGROUPS", "Groups"), $groupsMap)
|
2015-11-18 04:38:28 +01:00
|
|
|
->addExtraClass('custom-setting')
|
2018-01-08 21:53:59 +01:00
|
|
|
->setAttribute("data-placeholder", _t(__CLASS__ . ".ADDGROUP", "Add groups"))
|
|
|
|
->setDescription(_t(__CLASS__ . ".OWNERGROUPSDESCRIPTION", "Page owners that are responsible for reviews"));
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2018-01-08 21:53:59 +01:00
|
|
|
$reviewDate = DateField::create("NextReviewDate", _t(__CLASS__ . ".NEXTREVIEWDATE", "Next review date"))
|
|
|
|
->setDescription(_t(__CLASS__ . ".NEXTREVIEWDATADESCRIPTION", "Leave blank for no review"));
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2015-11-17 03:12:20 +01:00
|
|
|
$reviewFrequency = DropdownField::create(
|
2015-11-18 04:38:28 +01:00
|
|
|
"ReviewPeriodDays",
|
2018-01-08 21:53:59 +01:00
|
|
|
_t(__CLASS__ . ".REVIEWFREQUENCY", "Review frequency"),
|
2015-11-18 04:38:28 +01:00
|
|
|
self::get_schedule()
|
|
|
|
)
|
|
|
|
->addExtraClass('custom-setting')
|
2018-01-08 04:47:31 +01:00
|
|
|
->setDescription(_t(
|
|
|
|
__CLASS__ . ".REVIEWFREQUENCYDESCRIPTION",
|
|
|
|
"The review date will be set to this far in the future whenever the page is published"
|
|
|
|
));
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2018-01-08 04:47:31 +01:00
|
|
|
$notesField = GridField::create(
|
|
|
|
"ReviewNotes",
|
|
|
|
"Review Notes",
|
|
|
|
$this->owner->ReviewLogs(),
|
|
|
|
GridFieldConfig_RecordEditor::create()
|
|
|
|
);
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2018-01-08 04:47:31 +01:00
|
|
|
$fields->addFieldsToTab("Root.ContentReview", [
|
2018-01-08 21:53:59 +01:00
|
|
|
HeaderField::create('ContentReviewHeader', _t(__CLASS__ . ".REVIEWHEADER", "Content review"), 2),
|
2015-11-02 00:27:42 +01:00
|
|
|
$viewersOptionsField,
|
|
|
|
CompositeField::create(
|
|
|
|
$userField,
|
|
|
|
$groupField,
|
|
|
|
$reviewDate,
|
|
|
|
$reviewFrequency
|
2015-11-17 03:12:20 +01:00
|
|
|
)->addExtraClass("review-settings"),
|
2018-01-08 04:47:31 +01:00
|
|
|
ReadonlyField::create(
|
|
|
|
"ROContentOwners",
|
|
|
|
_t(__CLASS__ . ".CONTENTOWNERS", "Content Owners"),
|
|
|
|
$this->getOwnerNames()
|
|
|
|
),
|
2015-11-02 00:27:42 +01:00
|
|
|
$notesField,
|
2018-01-08 04:47:31 +01:00
|
|
|
]);
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a ContentReviewLog and connects it to this Page.
|
|
|
|
*
|
|
|
|
* @param Member $reviewer
|
|
|
|
* @param string $message
|
|
|
|
*/
|
|
|
|
public function addReviewNote(Member $reviewer, $message)
|
|
|
|
{
|
|
|
|
$reviewLog = ContentReviewLog::create();
|
|
|
|
$reviewLog->Note = $message;
|
|
|
|
$reviewLog->ReviewerID = $reviewer->ID;
|
|
|
|
$this->owner->ReviewLogs()->add($reviewLog);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Advance review date to the next date based on review period or set it to null
|
|
|
|
* if there is no schedule. Returns true if date was required and false is content
|
|
|
|
* review is 'off'.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function advanceReviewDate()
|
|
|
|
{
|
2017-09-06 06:53:45 +02:00
|
|
|
$nextDateTimestamp = false;
|
2015-11-02 00:27:42 +01:00
|
|
|
$options = $this->getOptions();
|
|
|
|
|
|
|
|
if ($options && $options->ReviewPeriodDays) {
|
2017-09-06 06:53:45 +02:00
|
|
|
$nextDateTimestamp = strtotime(
|
|
|
|
' + ' . $options->ReviewPeriodDays . ' days',
|
|
|
|
DBDatetime::now()->getTimestamp()
|
|
|
|
);
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2018-06-20 09:31:33 +02:00
|
|
|
$this->owner->NextReviewDate = DBDate::create()->setValue($nextDateTimestamp)->Format(DBDate::ISO_DATE);
|
2015-11-02 00:27:42 +01:00
|
|
|
$this->owner->write();
|
|
|
|
}
|
|
|
|
|
2020-10-29 02:40:29 +01:00
|
|
|
if ($options && $options->ReviewPeriodDays == 0) {
|
|
|
|
$this->owner->NextReviewDate = null;
|
|
|
|
$this->owner->write();
|
|
|
|
}
|
|
|
|
|
2018-01-08 04:47:31 +01:00
|
|
|
return (bool)$nextDateTimestamp;
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a review is due by a member for this owner.
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canBeReviewedBy(Member $member = null)
|
|
|
|
{
|
2022-06-22 02:25:59 +02:00
|
|
|
if (!$this->owner->obj('NextReviewDate')->exists()) {
|
2015-11-02 00:27:42 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-22 02:25:59 +02:00
|
|
|
if ($this->owner->obj('NextReviewDate')->InFuture()) {
|
2015-11-02 00:27:42 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = $this->getOptions();
|
2017-09-06 05:49:23 +02:00
|
|
|
|
2017-07-11 23:23:30 +02:00
|
|
|
if (!$options) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2017-10-06 05:39:53 +02:00
|
|
|
if (!$options
|
|
|
|
// Options can be a SiteConfig with different extension applied
|
2018-01-08 04:47:31 +01:00
|
|
|
|| (!$options->hasExtension(__CLASS__)
|
|
|
|
&& !$options->hasExtension(ContentReviewDefaultSettings::class))
|
2017-10-06 05:39:53 +02:00
|
|
|
) {
|
2017-03-23 23:52:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-02 00:27:42 +01:00
|
|
|
if ($options->OwnerGroups()->count() == 0 && $options->OwnerUsers()->count() == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$member) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-22 02:25:59 +02:00
|
|
|
// Check whether this user is allowed to review the content of the page.
|
|
|
|
if ($this->owner->hasMethod("canReviewContent") && !$this->owner->canReviewContent($member)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-02 00:27:42 +01:00
|
|
|
if ($member->inGroups($options->OwnerGroups())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($options->OwnerUsers()->find("ID", $member->ID)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the review data from the review period, if set.
|
|
|
|
*/
|
|
|
|
public function onBeforeWrite()
|
|
|
|
{
|
2016-05-17 06:34:00 +02:00
|
|
|
// Only update if DB fields have been changed
|
|
|
|
$changedFields = $this->owner->getChangedFields(true, 2);
|
2017-09-06 05:49:23 +02:00
|
|
|
if ($changedFields) {
|
2016-05-17 06:34:00 +02:00
|
|
|
$this->owner->LastEditedByName = $this->owner->getEditorName();
|
|
|
|
$this->owner->OwnerNames = $this->owner->getOwnerNames();
|
|
|
|
}
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
// If the user changed the type, we need to recalculate the review date.
|
|
|
|
if ($this->owner->isChanged("ContentReviewType", 2)) {
|
|
|
|
if ($this->owner->ContentReviewType == "Disabled") {
|
|
|
|
$this->setDefaultReviewDateForDisabled();
|
|
|
|
} elseif ($this->owner->ContentReviewType == "Custom") {
|
|
|
|
$this->setDefaultReviewDateForCustom();
|
|
|
|
} else {
|
|
|
|
$this->setDefaultReviewDateForInherited();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that a inherited page always have a next review date
|
|
|
|
if ($this->owner->ContentReviewType == "Inherit" && !$this->owner->NextReviewDate) {
|
|
|
|
$this->setDefaultReviewDateForInherited();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to update all the child pages that inherit this setting. We can only
|
|
|
|
// change children after this record has been created, otherwise the stageChildren
|
|
|
|
// method will grab all pages in the DB (this messes up unit testing)
|
|
|
|
if (!$this->owner->exists()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-17 06:34:00 +02:00
|
|
|
// parent page change its review period
|
2015-11-02 00:27:42 +01:00
|
|
|
// && !$this->owner->isChanged('ContentReviewType', 2)
|
2017-09-06 05:49:23 +02:00
|
|
|
if ($this->owner->isChanged('ReviewPeriodDays', 2)) {
|
2017-09-06 06:53:45 +02:00
|
|
|
$nextReviewUnixSec = strtotime(
|
|
|
|
' + ' . $this->owner->ReviewPeriodDays . ' days',
|
|
|
|
DBDatetime::now()->getTimestamp()
|
|
|
|
);
|
2017-09-06 05:49:23 +02:00
|
|
|
$this->owner->NextReviewDate = DBDate::create()->setValue($nextReviewUnixSec)->Format('y-MM-dd');
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setDefaultReviewDateForDisabled()
|
|
|
|
{
|
|
|
|
$this->owner->NextReviewDate = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setDefaultReviewDateForCustom()
|
|
|
|
{
|
2015-11-18 04:38:28 +01:00
|
|
|
// Don't overwrite existing value
|
2015-11-02 00:27:42 +01:00
|
|
|
if ($this->owner->NextReviewDate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->owner->NextReviewDate = null;
|
|
|
|
$nextDate = $this->getReviewDate();
|
|
|
|
|
|
|
|
if (is_object($nextDate)) {
|
|
|
|
$this->owner->NextReviewDate = $nextDate->getValue();
|
|
|
|
} else {
|
|
|
|
$this->owner->NextReviewDate = $nextDate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setDefaultReviewDateForInherited()
|
|
|
|
{
|
2015-11-18 04:38:28 +01:00
|
|
|
// Don't overwrite existing value
|
|
|
|
if ($this->owner->NextReviewDate) {
|
|
|
|
return;
|
|
|
|
}
|
2015-11-17 03:12:20 +01:00
|
|
|
|
2015-11-02 00:27:42 +01:00
|
|
|
$options = $this->getOptions();
|
|
|
|
$nextDate = null;
|
|
|
|
|
2015-11-17 03:12:20 +01:00
|
|
|
if ($options instanceof SiteTree) {
|
|
|
|
$nextDate = $this->getReviewDate($options);
|
2015-11-02 00:27:42 +01:00
|
|
|
} elseif ($options instanceof SiteConfig) {
|
|
|
|
$nextDate = $this->getReviewDate();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_object($nextDate)) {
|
|
|
|
$this->owner->NextReviewDate = $nextDate->getValue();
|
|
|
|
} else {
|
|
|
|
$this->owner->NextReviewDate = $nextDate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide permissions to the CMS.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providePermissions()
|
|
|
|
{
|
2018-01-08 04:47:31 +01:00
|
|
|
return [
|
|
|
|
"EDIT_CONTENT_REVIEW_FIELDS" => [
|
2015-11-02 00:27:42 +01:00
|
|
|
"name" => "Set content owners and review dates",
|
2018-01-08 21:53:59 +01:00
|
|
|
"category" => _t("SilverStripe\\Security\\Permission.CONTENT_CATEGORY", "Content permissions"),
|
2015-11-02 00:27:42 +01:00
|
|
|
"sort" => 50,
|
2018-01-08 04:47:31 +01:00
|
|
|
],
|
|
|
|
];
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the queued jobs module is installed, queue up the first job for 9am tomorrow morning
|
|
|
|
* (by default).
|
|
|
|
*/
|
|
|
|
public function requireDefaultRecords()
|
|
|
|
{
|
2017-09-06 05:49:23 +02:00
|
|
|
if (class_exists(ContentReviewNotificationJob::class)) {
|
2015-11-02 00:27:42 +01:00
|
|
|
// Ensure there is not already a job queued
|
2017-09-06 05:49:23 +02:00
|
|
|
if (QueuedJobDescriptor::get()->filter("Implementation", ContentReviewNotificationJob::class)->first()) {
|
2015-11-02 00:27:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-06 05:49:23 +02:00
|
|
|
$nextRun = Injector::inst()->create(ContentReviewNotificationJob::class);
|
|
|
|
$runHour = Config::inst()->get(ContentReviewNotificationJob::class, "first_run_hour");
|
2018-01-08 04:47:31 +01:00
|
|
|
$firstRunTime = date(
|
|
|
|
"Y-m-d H:i:s",
|
2022-04-13 00:23:56 +02:00
|
|
|
mktime($runHour ?? 0, 0, 0, date("m"), date("d") + 1, date("y"))
|
2018-01-08 04:47:31 +01:00
|
|
|
);
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2017-09-07 06:14:37 +02:00
|
|
|
singleton(QueuedJobService::class)->queueJob(
|
2015-11-02 00:27:42 +01:00
|
|
|
$nextRun,
|
|
|
|
$firstRunTime
|
|
|
|
);
|
|
|
|
|
|
|
|
DB::alteration_message(sprintf("Added ContentReviewNotificationJob to run at %s", $firstRunTime));
|
|
|
|
}
|
|
|
|
}
|
2009-11-02 05:02:57 +01:00
|
|
|
}
|