ContentReviewEmails::getTemplateVariables()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 3
1
<?php
2
3
namespace SilverStripe\ContentReview\Tasks;
4
5
use Page;
6
use SilverStripe\ContentReview\Compatibility\ContentReviewCompatability;
7
use SilverStripe\Control\Email\Email;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\Dev\BuildTask;
10
use SilverStripe\ORM\ArrayList;
11
use SilverStripe\ORM\FieldType\DBDatetime;
12
use SilverStripe\ORM\FieldType\DBField;
13
use SilverStripe\ORM\SS_List;
14
use SilverStripe\Security\Member;
15
use SilverStripe\SiteConfig\SiteConfig;
16
use SilverStripe\View\ArrayData;
17
use SilverStripe\View\SSViewer;
18
19
/**
20
 * Daily task to send emails to the owners of content items when the review date rolls around.
21
 */
22
class ContentReviewEmails extends BuildTask
23
{
24
    /**
25
     * @param HTTPRequest $request
26
     */
27
    public function run($request)
28
    {
29
        $compatibility = ContentReviewCompatability::start();
30
31
        // First grab all the pages with a custom setting
32
        $pages = Page::get()
33
            ->filter('NextReviewDate:LessThanOrEqual', DBDatetime::now()->URLDate());
34
35
        $overduePages = $this->getOverduePagesForOwners($pages);
36
37
        // Lets send one email to one owner with all the pages in there instead of no of pages
38
        // of emails.
39
        foreach ($overduePages as $memberID => $pages) {
40
            $this->notifyOwner($memberID, $pages);
41
        }
42
43
        ContentReviewCompatability::done($compatibility);
44
    }
45
46
    /**
47
     * @param SS_List $pages
48
     *
49
     * @return array
50
     */
51
    protected function getOverduePagesForOwners(SS_List $pages)
52
    {
53
        $overduePages = array();
54
55
        foreach ($pages as $page) {
56
            if (!$page->canBeReviewedBy()) {
57
                continue;
58
            }
59
60
            $option = $page->getOptions();
61
62
            foreach ($option->ContentReviewOwners() as $owner) {
63
                if (!isset($overduePages[$owner->ID])) {
64
                    $overduePages[$owner->ID] = ArrayList::create();
65
                }
66
67
                $overduePages[$owner->ID]->push($page);
68
            }
69
        }
70
71
        return $overduePages;
72
    }
73
74
    /**
75
     * @param int           $ownerID
76
     * @param array|SS_List $pages
77
     */
78
    protected function notifyOwner($ownerID, SS_List $pages)
79
    {
80
        // Prepare variables
81
        $siteConfig = SiteConfig::current_site_config();
82
        $owner = Member::get()->byID($ownerID);
83
        $templateVariables = $this->getTemplateVariables($owner, $siteConfig, $pages);
0 ignored issues
show
Documentation introduced by
$owner is of type object<SilverStripe\ORM\DataObject>|null, but the function expects a object<SilverStripe\Security\Member>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84
85
        // Build email
86
        $email = Email::create();
87
        $email->setTo($owner->Email);
88
        $email->setFrom($siteConfig->ReviewFrom);
89
        $email->setSubject($siteConfig->ReviewSubject);
90
91
        // Get user-editable body
92
        $body = $this->getEmailBody($siteConfig, $templateVariables);
93
94
        // Populate mail body with fixed template
95
        $email->setHTMLTemplate($siteConfig->config()->get('content_review_template'));
96
        $email->setData(
97
            array_merge(
98
                $templateVariables,
99
                [
100
                    'EmailBody' => $body,
101
                    'Recipient' => $owner,
102
                    'Pages' => $pages,
103
                ]
104
            )
105
        );
106
        $email->send();
107
    }
108
109
    /**
110
     * Get string value of HTML body with all variable evaluated.
111
     *
112
     * @param SiteConfig $config
113
     * @param array List of safe template variables to expose to this template
114
     *
115
     * @return HTMLText
116
     */
117
    protected function getEmailBody($config, $variables)
118
    {
119
        $template = SSViewer::fromString($config->ReviewBody);
120
        $value = $template->process(ArrayData::create($variables));
121
122
        // Cast to HTML
123
        return DBField::create_field('HTMLText', (string) $value);
124
    }
125
126
    /**
127
     * Gets list of safe template variables and their values which can be used
128
     * in both the static and editable templates.
129
     *
130
     * {@see ContentReviewAdminHelp.ss}
131
     *
132
     * @param Member     $recipient
133
     * @param SiteConfig $config
134
     * @param SS_List    $pages
135
     *
136
     * @return array
137
     */
138
    protected function getTemplateVariables($recipient, $config, $pages)
139
    {
140
        return [
141
            'Subject' => $config->ReviewSubject,
142
            'PagesCount' => $pages->count(),
143
            'FromEmail' => $config->ReviewFrom,
144
            'ToFirstName' => $recipient->FirstName,
145
            'ToSurname' => $recipient->Surname,
146
            'ToEmail' => $recipient->Email,
147
        ];
148
    }
149
}
150