Completed
Push — master ( 4a6357...521c8c )
by Franco
10s
created

code/extensions/ContentReviewDefaultSettings.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * This extensions add a default schema for new pages and pages without a content
5
 * review setting.
6
 *
7
 * @property int $ReviewPeriodDays
8
 */
9
class ContentReviewDefaultSettings extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
    /**
12
     * @config
13
     *
14
     * @var array
15
     */
16
    private static $db = array(
0 ignored issues
show
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
        'ReviewPeriodDays' => 'Int',
18
        'ReviewFrom' => 'Varchar(255)',
19
        'ReviewSubject' => 'Varchar(255)',
20
        'ReviewBody' => 'HTMLText',
21
    );
22
23
    /**
24
     * @config
25
     *
26
     * @var array
27
     */
28
    private static $defaults = array(
0 ignored issues
show
The property $defaults is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
29
        'ReviewSubject' => 'Page(s) are due for content review',
30
        'ReviewBody' => '<h2>Page(s) due for review</h2><p>There are $PagesCount pages that are due for review today by you.</p>',
31
    );
32
33
    /**
34
     * @config
35
     *
36
     * @var array
37
     */
38
    private static $many_many = array(
0 ignored issues
show
The property $many_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
39
        'ContentReviewGroups' => 'Group',
40
        'ContentReviewUsers' => 'Member',
41
    );
42
43
    /**
44
     * Template to use for content review emails.
45
     *
46
     * This should contain an $EmailBody variable as a placeholder for the user-defined copy
47
     *
48
     * @config
49
     *
50
     * @var string
51
     */
52
    private static $content_review_template = 'ContentReviewEmail';
0 ignored issues
show
The property $content_review_template is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
53
54
    /**
55
     * @return string
56
     */
57
    public function getOwnerNames()
58
    {
59
        $names = array();
60
61
        foreach ($this->OwnerGroups() as $group) {
62
            $names[] = $group->getBreadcrumbs(' > ');
63
        }
64
65
        foreach ($this->OwnerUsers() as $group) {
66
            $names[] = $group->getName();
67
        }
68
69
        return implode(', ', $names);
70
    }
71
72
    /**
73
     * @return ManyManyList
74
     */
75
    public function OwnerGroups()
76
    {
77
        return $this->owner->getManyManyComponents('ContentReviewGroups');
78
    }
79
80
    /**
81
     * @return ManyManyList
82
     */
83
    public function OwnerUsers()
84
    {
85
        return $this->owner->getManyManyComponents('ContentReviewUsers');
86
    }
87
88
    /**
89
     * @param FieldList $fields
90
     */
91
    public function updateCMSFields(FieldList $fields)
92
    {
93
        $helpText = LiteralField::create(
94
            'ContentReviewHelp',
95
            _t(
96
                'ContentReview.DEFAULTSETTINGSHELP',
97
                'These settings will apply to all pages that do not have a specific Content Review schedule.'
98
            )
99
        );
100
101
        $fields->addFieldToTab('Root.ContentReview', $helpText);
102
103
        $reviewFrequency = DropdownField::create(
104
            'ReviewPeriodDays',
105
            _t('ContentReview.REVIEWFREQUENCY', 'Review frequency'),
106
            SiteTreeContentReview::get_schedule()
107
        )
108
            ->setDescription(_t(
109
                'ContentReview.REVIEWFREQUENCYDESCRIPTION',
110
                'The review date will be set to this far in the future, whenever the page is published.'
111
            ));
112
113
        $fields->addFieldToTab('Root.ContentReview', $reviewFrequency);
114
115
        $users = Permission::get_members_by_permission(array(
116
            'CMS_ACCESS_CMSMain',
117
            'ADMIN',
118
        ));
119
120
        $usersMap = $users->map('ID', 'Title')->toArray();
0 ignored issues
show
The method toArray cannot be called on $users->map('ID', 'Title') (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
121
        asort($usersMap);
122
123
        $userField = ListboxField::create('OwnerUsers', _t('ContentReview.PAGEOWNERUSERS', 'Users'), $usersMap)
124
            ->setMultiple(true)
125
            ->setAttribute('data-placeholder', _t('ContentReview.ADDUSERS', 'Add users'))
126
            ->setDescription(_t('ContentReview.OWNERUSERSDESCRIPTION', 'Page owners that are responsible for reviews'));
127
128
        $fields->addFieldToTab('Root.ContentReview', $userField);
129
130
        $groupsMap = array();
131
132
        foreach (Group::get() as $group) {
133
            // Listboxfield values are escaped, use ASCII char instead of &raquo;
134
            $groupsMap[$group->ID] = $group->getBreadcrumbs(' > ');
135
        }
136
137
        asort($groupsMap);
138
139
        $groupField = ListboxField::create('OwnerGroups', _t('ContentReview.PAGEOWNERGROUPS', 'Groups'), $groupsMap)
140
            ->setMultiple(true)
141
            ->setAttribute('data-placeholder', _t('ContentReview.ADDGROUP', 'Add groups'))
142
            ->setDescription(_t('ContentReview.OWNERGROUPSDESCRIPTION', 'Page owners that are responsible for reviews'));
143
144
        $fields->addFieldToTab('Root.ContentReview', $groupField);
145
146
        // Email content
147
        $fields->addFieldsToTab(
148
            'Root.ContentReview',
149
            array(
150
                TextField::create('ReviewFrom', _t('ContentReview.EMAILFROM', 'From email address'))
151
                    ->setRightTitle(_t('Review.EMAILFROM_RIGHTTITLE', 'e.g: [email protected]')),
152
                TextField::create('ReviewSubject', _t('ContentReview.EMAILSUBJECT', 'Subject line')),
153
                TextAreaField::create('ReviewBody', _t('ContentReview.EMAILTEMPLATE', 'Email template')),
154
                LiteralField::create('TemplateHelp', $this->owner->renderWith('ContentReviewAdminHelp')),
155
            )
156
        );
157
    }
158
159
    /**
160
     * Get all Members that are default Content Owners. This includes checking group hierarchy
161
     * and adding any direct users.
162
     *
163
     * @return ArrayList
164
     */
165
    public function ContentReviewOwners()
166
    {
167
        return SiteTreeContentReview::merge_owners($this->OwnerGroups(), $this->OwnerUsers());
168
    }
169
170
    /**
171
     * Get the review body, falling back to the default if left blank.
172
     *
173
     * @return string HTML text
174
     */
175
    public function getReviewBody()
176
    {
177
        return $this->getWithDefault('ReviewBody');
178
    }
179
180
    /**
181
     * Get the review subject line, falling back to the default if left blank.
182
     *
183
     * @return string plain text value
184
     */
185
    public function getReviewSubject()
186
    {
187
        return $this->getWithDefault('ReviewSubject');
188
    }
189
190
    /**
191
     * Get the "from" field for review emails.
192
     *
193
     * @return string
194
     */
195
    public function getReviewFrom()
196
    {
197
        $from = $this->owner->getField('ReviewFrom');
198
        if ($from) {
199
            return $from;
200
        }
201
202
        // Fall back to admin email
203
        return Config::inst()->get('Email', 'admin_email');
204
    }
205
206
    /**
207
     * Get the value of a user-configured field, falling back to the default if left blank.
208
     *
209
     * @param string $field
210
     *
211
     * @return string
212
     */
213
    protected function getWithDefault($field)
214
    {
215
        $value = $this->owner->getField($field);
216
        if ($value) {
217
            return $value;
218
        }
219
        // fallback to default value
220
        $defaults = $this->owner->config()->defaults;
221
        if (isset($defaults[$field])) {
222
            return $defaults[$field];
223
        }
224
    }
225
}
226