2014-02-20 06:05:14 +01:00
< ? php
/**
2015-11-02 00:27:42 +01:00
* This extensions add a default schema for new pages and pages without a content
* review setting .
*
* @ property int $ReviewPeriodDays
2014-02-20 06:05:14 +01:00
*/
2015-11-02 00:27:42 +01:00
class ContentReviewDefaultSettings extends DataExtension
{
/**
2015-11-17 02:17:54 +01:00
* @ config
*
2015-11-02 00:27:42 +01:00
* @ var array
*/
private static $db = array (
2015-11-17 02:17:54 +01:00
'ReviewPeriodDays' => 'Int' ,
'ReviewFrom' => 'Varchar(255)' ,
'ReviewSubject' => 'Varchar(255)' ,
2016-08-24 00:59:23 +02:00
'ReviewSubjectFirstReminder' => 'Varchar(255)' ,
'ReviewSubjectSecondReminder' => 'Varchar(255)' ,
2015-11-17 02:17:54 +01:00
'ReviewBody' => 'HTMLText' ,
2016-08-24 00:59:23 +02:00
'ReviewBodyFirstReminder' => 'HTMLText' ,
'ReviewBodySecondReminder' => 'HTMLText' ,
2016-08-23 05:36:53 +02:00
'FirstReviewDaysBefore' => 'Int' ,
'SecondReviewDaysBefore' => 'Int'
2015-11-17 02:17:54 +01:00
);
/**
* @ config
*
* @ var array
*/
private static $defaults = array (
2016-08-24 00:59:23 +02:00
'ReviewSubjectFirstReminder' => 'Page(s) are 1 month from content review' ,
'ReviewSubjectSecondReminder' => 'Page(s) are 1 week from content review' ,
2015-11-17 02:17:54 +01:00
'ReviewSubject' => 'Page(s) are due for content review' ,
2016-08-24 00:59:23 +02:00
'ReviewBodyFirstReminder' => '<h2>Page(s) 1 month from review</h2><p>There are $PagesCount pages that are due for review by you 1 month from today.</p>' ,
'ReviewBodySecondReminder' => '<h2>Page(s) 1 week from from review</h2><p>There are $PagesCount pages that are due for review by you 1 week from today.</p>' ,
2015-11-17 02:17:54 +01:00
'ReviewBody' => '<h2>Page(s) due for review</h2><p>There are $PagesCount pages that are due for review today by you.</p>' ,
2016-08-24 00:59:23 +02:00
'FirstReviewDaysBefore' => '30' ,
'SecondReviewDaysBefore' => '7'
2015-11-02 00:27:42 +01:00
);
/**
2015-11-17 02:17:54 +01:00
* @ config
2015-11-02 00:27:42 +01:00
*
* @ var array
*/
private static $many_many = array (
2015-11-17 02:17:54 +01:00
'ContentReviewGroups' => 'Group' ,
'ContentReviewUsers' => 'Member' ,
2015-11-02 00:27:42 +01:00
);
/**
2015-11-17 02:17:54 +01:00
* Template to use for content review emails .
2015-11-02 00:27:42 +01:00
*
2015-11-17 02:17:54 +01:00
* This should contain an $EmailBody variable as a placeholder for the user - defined copy
*
* @ config
*
* @ var string
*/
private static $content_review_template = 'ContentReviewEmail' ;
/**
2015-11-02 00:27:42 +01:00
* @ return string
*/
public function getOwnerNames ()
{
$names = array ();
foreach ( $this -> OwnerGroups () as $group ) {
2015-11-17 02:17:54 +01:00
$names [] = $group -> getBreadcrumbs ( ' > ' );
2015-11-02 00:27:42 +01:00
}
foreach ( $this -> OwnerUsers () as $group ) {
$names [] = $group -> getName ();
}
2015-11-17 02:17:54 +01:00
return implode ( ', ' , $names );
2015-11-02 00:27:42 +01:00
}
/**
* @ return ManyManyList
*/
public function OwnerGroups ()
{
2015-11-17 02:17:54 +01:00
return $this -> owner -> getManyManyComponents ( 'ContentReviewGroups' );
2015-11-02 00:27:42 +01:00
}
/**
* @ return ManyManyList
*/
public function OwnerUsers ()
{
2015-11-17 02:17:54 +01:00
return $this -> owner -> getManyManyComponents ( 'ContentReviewUsers' );
2015-11-02 00:27:42 +01:00
}
/**
* @ param FieldList $fields
*/
public function updateCMSFields ( FieldList $fields )
{
2015-11-17 02:17:54 +01:00
$helpText = LiteralField :: create (
'ContentReviewHelp' ,
_t (
'ContentReview.DEFAULTSETTINGSHELP' ,
'These content review settings will apply to all pages that does not have specific Content Review schedule.'
)
);
$fields -> addFieldToTab ( 'Root.ContentReview' , $helpText );
$reviewFrequency = DropdownField :: create (
'ReviewPeriodDays' ,
_t ( 'ContentReview.REVIEWFREQUENCY' , 'Review frequency' ),
SiteTreeContentReview :: get_schedule ()
)
-> setDescription ( _t (
'ContentReview.REVIEWFREQUENCYDESCRIPTION' ,
'The review date will be set to this far in the future whenever the page is published'
));
$fields -> addFieldToTab ( 'Root.ContentReview' , $reviewFrequency );
2015-11-02 00:27:42 +01:00
2016-08-23 05:36:53 +02:00
$FirstReviewDaysBefore = NumericField :: create (
'FirstReviewDaysBefore' ,
_t ( 'ContentReview.FIRSTREVIEWDAYSBEFORE' , 'First review reminder # days before final review' )
);
$fields -> addFieldToTab ( 'Root.ContentReview' , $FirstReviewDaysBefore );
$SecondReviewDaysBefore = NumericField :: create (
'SecondReviewDaysBefore' ,
_t ( 'ContentReview.SECONDREVIEWDAYSBEFORE' , 'Second review reminder # days before final review' )
);
$fields -> addFieldToTab ( 'Root.ContentReview' , $SecondReviewDaysBefore );
2015-11-02 00:27:42 +01:00
$users = Permission :: get_members_by_permission ( array (
2015-11-17 02:17:54 +01:00
'CMS_ACCESS_CMSMain' ,
'ADMIN' ,
2015-11-02 00:27:42 +01:00
));
2015-11-17 02:17:54 +01:00
$usersMap = $users -> map ( 'ID' , 'Title' ) -> toArray ();
2015-11-02 00:27:42 +01:00
asort ( $usersMap );
2015-11-17 02:17:54 +01:00
$userField = ListboxField :: create ( 'OwnerUsers' , _t ( 'ContentReview.PAGEOWNERUSERS' , 'Users' ), $usersMap )
2015-11-02 00:27:42 +01:00
-> setMultiple ( true )
2015-11-17 02:17:54 +01:00
-> setAttribute ( 'data-placeholder' , _t ( 'ContentReview.ADDUSERS' , 'Add users' ))
-> setDescription ( _t ( 'ContentReview.OWNERUSERSDESCRIPTION' , 'Page owners that are responsible for reviews' ));
2015-11-02 00:27:42 +01:00
2015-11-17 02:17:54 +01:00
$fields -> addFieldToTab ( 'Root.ContentReview' , $userField );
2015-11-02 00:27:42 +01:00
$groupsMap = array ();
foreach ( Group :: get () as $group ) {
// Listboxfield values are escaped, use ASCII char instead of »
2015-11-17 02:17:54 +01:00
$groupsMap [ $group -> ID ] = $group -> getBreadcrumbs ( ' > ' );
2015-11-02 00:27:42 +01:00
}
asort ( $groupsMap );
2015-11-17 02:17:54 +01:00
$groupField = ListboxField :: create ( 'OwnerGroups' , _t ( 'ContentReview.PAGEOWNERGROUPS' , 'Groups' ), $groupsMap )
2015-11-02 00:27:42 +01:00
-> setMultiple ( true )
2015-11-17 02:17:54 +01:00
-> setAttribute ( 'data-placeholder' , _t ( 'ContentReview.ADDGROUP' , 'Add groups' ))
-> setDescription ( _t ( 'ContentReview.OWNERGROUPSDESCRIPTION' , 'Page owners that are responsible for reviews' ));
$fields -> addFieldToTab ( 'Root.ContentReview' , $groupField );
// Email content
$fields -> addFieldsToTab (
'Root.ContentReview' ,
array (
TextField :: create ( 'ReviewFrom' , _t ( 'ContentReview.EMAILFROM' , 'From email address' ))
-> setRightTitle ( _t ( 'Review.EMAILFROM_RIGHTTITLE' , 'e.g: do-not-reply@site.com' )),
2016-08-24 00:59:23 +02:00
TextField :: create ( 'ReviewSubjectFirstReminder' , _t ( 'ContentReview.EMAILSUBJECTFIRSTREMINDER' , 'Subject line - First reminder' )),
TextField :: create ( 'ReviewSubjectSecondReminder' , _t ( 'ContentReview.EMAILSUBJECTSECONDREMINDER' , 'Subject line - Second reminder' )),
TextField :: create ( 'ReviewSubject' , _t ( 'ContentReview.EMAILSUBJECT' , 'Subject line - Review due' )),
TextAreaField :: create ( 'ReviewBodyFirstReminder' , _t ( 'ContentReview.EMAILTEMPLATEFIRSTREMINDER' , 'Email template - First reminder' )),
TextAreaField :: create ( 'ReviewBodySecondReminder' , _t ( 'ContentReview.EMAILTEMPLATESECONDREMINDER' , 'Email template - Second reminder' )),
TextAreaField :: create ( 'ReviewBody' , _t ( 'ContentReview.EMAILTEMPLATE' , 'Email template - Review due' )),
2015-11-17 02:17:54 +01:00
LiteralField :: create ( 'TemplateHelp' , $this -> owner -> renderWith ( 'ContentReviewAdminHelp' )),
)
);
2015-11-02 00:27:42 +01:00
}
/**
* Get all Members that are default Content Owners . This includes checking group hierarchy
* and adding any direct users .
*
* @ return ArrayList
*/
2015-11-17 02:17:54 +01:00
public function ContentReviewOwners ()
{
2015-11-02 00:27:42 +01:00
return SiteTreeContentReview :: merge_owners ( $this -> OwnerGroups (), $this -> OwnerUsers ());
}
2015-11-17 02:17:54 +01:00
/**
* Get the review body , falling back to the default if left blank .
*
* @ return string HTML text
*/
public function getReviewBody ()
{
return $this -> getWithDefault ( 'ReviewBody' );
}
/**
* Get the review subject line , falling back to the default if left blank .
*
* @ return string plain text value
*/
public function getReviewSubject ()
{
return $this -> getWithDefault ( 'ReviewSubject' );
}
/**
* Get the " from " field for review emails .
*
* @ return string
*/
public function getReviewFrom ()
{
$from = $this -> owner -> getField ( 'ReviewFrom' );
if ( $from ) {
return $from ;
}
// Fall back to admin email
return Config :: inst () -> get ( 'Email' , 'admin_email' );
}
/**
* Get the value of a user - configured field , falling back to the default if left blank .
*
* @ param string $field
*
* @ return string
*/
protected function getWithDefault ( $field )
{
$value = $this -> owner -> getField ( $field );
if ( $value ) {
return $value ;
}
// fallback to default value
$defaults = $this -> owner -> config () -> defaults ;
if ( isset ( $defaults [ $field ])) {
return $defaults [ $field ];
}
}
2014-02-20 06:05:14 +01:00
}