Passed
Push — master ( 2ea0b2...d8ed7e )
by Thomas
02:39 queued 11s
created

EmailTemplatesAdmin::SendEmailing()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 19
c 0
b 0
f 0
nc 6
nop 0
dl 0
loc 32
rs 9.3222
1
<?php
2
3
namespace LeKoala\EmailTemplates\Admin;
4
5
use Exception;
6
use LeKoala\EmailTemplates\Helpers\FluentHelper;
7
use SilverStripe\Admin\ModelAdmin;
8
use SilverStripe\View\Requirements;
9
use LeKoala\EmailTemplates\Models\Emailing;
10
use LeKoala\EmailTemplates\Models\SentEmail;
11
use LeKoala\EmailTemplates\Models\EmailTemplate;
12
13
/**
14
 * Manage your email templates
15
 *
16
 * @author lekoala
17
 */
18
class EmailTemplatesAdmin extends ModelAdmin
19
{
20
21
    private static $managed_models = array(
0 ignored issues
show
introduced by
The private property $managed_models is not used, and could be removed.
Loading history...
22
        EmailTemplate::class,
23
        SentEmail::class,
24
        Emailing::class,
25
    );
26
    private static $url_segment = 'email-templates';
0 ignored issues
show
introduced by
The private property $url_segment is not used, and could be removed.
Loading history...
27
    private static $menu_title = 'Emails';
0 ignored issues
show
introduced by
The private property $menu_title is not used, and could be removed.
Loading history...
28
    private static $menu_icon = 'lekoala/silverstripe-email-templates:images/mail.png';
0 ignored issues
show
introduced by
The private property $menu_icon is not used, and could be removed.
Loading history...
29
    private static $allowed_actions = array(
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
30
        'ImportForm',
31
        'SearchForm',
32
        'PreviewEmail',
33
        'PreviewEmailing',
34
        'SendEmailing',
35
        'ViewSentEmail',
36
        'SentTestEmail',
37
    );
38
39
    public function subsiteCMSShowInMenu()
40
    {
41
        return true;
42
    }
43
44
    public function getList()
45
    {
46
        $list = parent::getList();
47
        return $list;
48
    }
49
50
    /**
51
     * Called by EmailTemplate
52
     *
53
     * @return string
54
     */
55
    public function SendEmailing()
56
    {
57
        $id = (int) $this->getRequest()->getVar('id');
58
59
        /* @var $Emailing Emailing */
60
        $Emailing = Emailing::get()->byID($id);
61
        $emails = $Emailing->getEmailByLocales();
62
63
        $errors = 0;
64
        foreach ($emails as $locale => $email) {
65
            // Wrap with withLocale to make sure any environment variable (urls, etc) are properly set when sending
66
            $res = null;
67
            FluentHelper::withLocale($locale, function () use ($email, &$res) {
68
                try {
69
                    $res = $email->send();
70
                } catch (Exception $ex) {
71
                    return $ex->getMessage();
72
                }
73
                return $res;
74
            });
75
            if (!$res) {
76
                $errors++;
77
            }
78
        }
79
80
        if ($errors == 0) {
81
            $Emailing->LastSent = date('Y-m-d H:i:s');
0 ignored issues
show
Bug Best Practice introduced by
The property LastSent does not exist on LeKoala\EmailTemplates\Models\Emailing. Since you implemented __set, consider adding a @property annotation.
Loading history...
82
            $Emailing->write();
83
84
            return _t('EmailTemplatesAdmin.EMAILING_SENT', 'Emailing sent');
85
        }
86
        return _t('EmailTemplatesAdmin.EMAILING_ERROR', 'There was an error sending email');
87
    }
88
89
    /**
90
     * Called by EmailTemplate::previewTab
91
     *
92
     * @return string
93
     */
94
    public function PreviewEmail()
95
    {
96
        // Prevent CMS styles to interfere with preview
97
        Requirements::clear();
98
99
        $id = (int) $this->getRequest()->getVar('id');
100
101
        /* @var $EmailTemplate EmailTemplate */
102
        $EmailTemplate = EmailTemplate::get()->byID($id);
103
        $html = $EmailTemplate->renderTemplate(true);
104
105
        Requirements::restore();
106
107
        return $html;
108
    }
109
110
    /**
111
     * Called by Emailing::previewTab
112
     *
113
     * @return string
114
     */
115
    public function PreviewEmailing()
116
    {
117
        // Prevent CMS styles to interfere with preview
118
        Requirements::clear();
119
120
        $id = (int) $this->getRequest()->getVar('id');
121
122
        /* @var $Emailing Emailing */
123
        $Emailing = Emailing::get()->byID($id);
124
        $html = $Emailing->renderTemplate(true);
0 ignored issues
show
Unused Code introduced by
The call to LeKoala\EmailTemplates\M...iling::renderTemplate() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

124
        /** @scrutinizer ignore-call */ 
125
        $html = $Emailing->renderTemplate(true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
125
126
        Requirements::restore();
127
128
        return $html;
129
    }
130
131
    public function SentTestEmail()
132
    {
133
        $id = (int) $this->getRequest()->getVar('id');
134
135
        /* @var $emailTemplate EmailTemplate */
136
        $emailTemplate = EmailTemplate::get()->byID($id);
137
138
        $email = $emailTemplate->getEmail();
139
        $emailTemplate->setPreviewData($email);
140
        $result = $email->send();
141
142
        print_r($result);
143
        die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
144
    }
145
146
    /**
147
     * Called by SentEmail
148
     *
149
     * @return string
150
     */
151
    public function ViewSentEmail()
152
    {
153
        // Prevent CMS styles to interfere with preview
154
        Requirements::clear();
155
156
        $id = (int) $this->getRequest()->getVar('id');
157
158
        /* @var $SentEmail SentEmail */
159
        $SentEmail = SentEmail::get()->byID($id);
160
        $html = $SentEmail->Body;
161
162
        Requirements::restore();
163
164
        return $html;
165
    }
166
}
167