Passed
Push — master ( 8da34e...083348 )
by Thomas
02:23
created

EmailTemplatesAdmin::SendTestEmailTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 10
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
use SilverStripe\Control\Director;
13
14
/**
15
 * Manage your email templates
16
 *
17
 * @author lekoala
18
 */
19
class EmailTemplatesAdmin extends ModelAdmin
20
{
21
22
    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...
23
        EmailTemplate::class,
24
        SentEmail::class,
25
        Emailing::class,
26
    );
27
    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...
28
    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...
29
    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...
30
    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...
31
        'ImportForm',
32
        'SearchForm',
33
        'PreviewEmail',
34
        'PreviewEmailing',
35
        'SendEmailing',
36
        'ViewSentEmail',
37
        'SendTestEmailTemplate',
38
    );
39
40
    public function subsiteCMSShowInMenu()
41
    {
42
        return true;
43
    }
44
45
    public function getList()
46
    {
47
        $list = parent::getList();
48
        return $list;
49
    }
50
51
    /**
52
     * Called by EmailTemplate
53
     *
54
     * @return string
55
     */
56
    public function SendEmailing()
57
    {
58
        $id = (int) $this->getRequest()->getVar('id');
59
60
        /* @var $Emailing Emailing */
61
        $Emailing = Emailing::get()->byID($id);
62
        $emails = $Emailing->getEmailByLocales();
63
64
        $errors = 0;
65
        foreach ($emails as $locale => $email) {
66
            // Wrap with withLocale to make sure any environment variable (urls, etc) are properly set when sending
67
            $res = null;
68
            FluentHelper::withLocale($locale, function () use ($email, &$res) {
69
                try {
70
                    $res = $email->send();
71
                } catch (Exception $ex) {
72
                    return $ex->getMessage();
73
                }
74
                return $res;
75
            });
76
            if (!$res) {
77
                $errors++;
78
            }
79
        }
80
81
        $message =  _t('EmailTemplatesAdmin.EMAILING_ERROR', 'There was an error sending email');
82
83
        if ($errors == 0) {
84
            $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...
85
            $Emailing->write();
86
87
            $message = _t('EmailTemplatesAdmin.EMAILING_SENT', 'Emailing sent');
88
        }
89
90
        if (Director::is_ajax()) {
91
            return $message;
92
        }
93
94
        return $this->redirectBack();
95
    }
96
97
    /**
98
     * Called by EmailTemplate::previewTab
99
     *
100
     * @return string
101
     */
102
    public function PreviewEmail()
103
    {
104
        // Prevent CMS styles to interfere with preview
105
        Requirements::clear();
106
107
        $id = (int) $this->getRequest()->getVar('id');
108
109
        /* @var $EmailTemplate EmailTemplate */
110
        $EmailTemplate = EmailTemplate::get()->byID($id);
111
        $html = $EmailTemplate->renderTemplate(true);
112
113
        Requirements::restore();
114
115
        return $html;
116
    }
117
118
    /**
119
     * Called by Emailing::previewTab
120
     *
121
     * @return string
122
     */
123
    public function PreviewEmailing()
124
    {
125
        // Prevent CMS styles to interfere with preview
126
        Requirements::clear();
127
128
        $id = (int) $this->getRequest()->getVar('id');
129
130
        /* @var $Emailing Emailing */
131
        $Emailing = Emailing::get()->byID($id);
132
        $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

132
        /** @scrutinizer ignore-call */ 
133
        $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...
133
134
        Requirements::restore();
135
136
        return $html;
137
    }
138
139
    public function SendTestEmailTemplate()
140
    {
141
        $id = (int) $this->getRequest()->getVar('id');
142
143
        /* @var $emailTemplate EmailTemplate */
144
        $emailTemplate = EmailTemplate::get()->byID($id);
145
146
        $email = $emailTemplate->getEmail();
147
        $emailTemplate->setPreviewData($email);
148
        $result = $email->send();
149
150
        print_r($result);
151
        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...
152
    }
153
154
    /**
155
     * Called by SentEmail
156
     *
157
     * @return string
158
     */
159
    public function ViewSentEmail()
160
    {
161
        // Prevent CMS styles to interfere with preview
162
        Requirements::clear();
163
164
        $id = (int) $this->getRequest()->getVar('id');
165
166
        /* @var $SentEmail SentEmail */
167
        $SentEmail = SentEmail::get()->byID($id);
168
        $html = $SentEmail->Body;
169
170
        Requirements::restore();
171
172
        return $html;
173
    }
174
}
175