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

src/Models/SentEmail.php (8 issues)

1
<?php
2
3
namespace LeKoala\EmailTemplates\Models;
4
5
use SilverStripe\ORM\DB;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\LiteralField;
9
use SilverStripe\Control\Email\Email;
10
use SilverStripe\Forms\ReadonlyField;
11
use SilverStripe\Security\Permission;
12
use LeKoala\Base\Actions\CustomAction;
0 ignored issues
show
The type LeKoala\Base\Actions\CustomAction was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use LeKoala\EmailTemplates\Admin\EmailTemplatesAdmin;
14
15
/**
16
 * Defines a record that stores an email
17
 *
18
 * @link https://github.com/nyeholt/silverstripe-mailcapture/blob/master/src/Model/CapturedEmail.php
19
 * @property string $To
20
 * @property string $From
21
 * @property string $Subject
22
 * @property string $Body
23
 * @property string $CC
24
 * @property string $BCC
25
 * @property string $Results
26
 * @author lekoala
27
 */
28
class SentEmail extends DataObject
29
{
30
    private static $table_name = 'SentEmail';
0 ignored issues
show
The private property $table_name is not used, and could be removed.
Loading history...
31
32
    private static $db = array(
0 ignored issues
show
The private property $db is not used, and could be removed.
Loading history...
33
        'To' => 'Varchar(191)',
34
        'From' => 'Varchar(191)',
35
        'ReplyTo' => 'Varchar(191)',
36
        'Subject' => 'Varchar(191)',
37
        'Body' => 'HTMLText',
38
        'Headers' => 'Text',
39
        'CC' => 'Text',
40
        'BCC' => 'Text',
41
        'Results' => 'Text',
42
    );
43
    private static $summary_fields = array(
0 ignored issues
show
The private property $summary_fields is not used, and could be removed.
Loading history...
44
        'Created.Nice' => 'Date',
45
        'To' => 'To',
46
        'Subject' => 'Subject',
47
        'IsSuccess' => 'Success',
48
    );
49
    private static $default_sort = 'Created DESC';
0 ignored issues
show
The private property $default_sort is not used, and could be removed.
Loading history...
50
51
    /**
52
     * Gets a list of actions for the ModelAdmin interface
53
     * @return FieldList
54
     */
55
    public function getCMSActions()
56
    {
57
        $fields = parent::getCMSActions();
58
59
        if (class_exists(CustomAction::class)) {
60
            $fields->push(
61
                CustomAction::create('resend', 'Resend')
62
            );
63
        }
64
65
        return $fields;
66
    }
67
68
    /**
69
     * Gets a list of form fields for editing the record.
70
     * These records should never be edited, so a readonly list of fields
71
     * is forced.
72
     *
73
     * @return FieldList
74
     */
75
    public function getCMSFields()
76
    {
77
        $f = FieldList::create();
78
79
        $f->push(ReadonlyField::create('Created'));
80
        $f->push(ReadonlyField::create('From'));
81
        $f->push(ReadonlyField::create('To'));
82
        $f->push(ReadonlyField::create('Subject'));
83
84
        if ($this->BCC) {
85
            $f->push(ReadonlyField::create('BCC'));
86
        }
87
        if ($this->CC) {
88
            $f->push(ReadonlyField::create('CC'));
89
        }
90
91
        $f->push(ReadonlyField::create('Results'));
92
93
        $sanitisedModel =  str_replace('\\', '-', SentEmail::class);
94
        $adminSegment = EmailTemplatesAdmin::config()->url_segment;
95
        $iframeSrc = '/admin/' . $adminSegment . '/' . $sanitisedModel . '/ViewSentEmail/?id=' . $this->ID;
96
        $iframe = new LiteralField('iframe', '<iframe src="' . $iframeSrc . '" style="width:800px;background:#fff;border:1px solid #ccc;min-height:500px;vertical-align:top"></iframe>');
97
        $f->push($iframe);
98
99
        $this->extend('updateCMSFields', $f);
100
101
        return $f;
102
    }
103
104
    /**
105
     * @return bool
106
     */
107
    public function IsSuccess()
108
    {
109
        return $this->Results == 'true';
110
    }
111
112
    /**
113
     * Gets the BetterEmail object that was used to send this email
114
     * @return BetterEmail
0 ignored issues
show
The type LeKoala\EmailTemplates\Models\BetterEmail was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
115
     */
116
    public function getEmail()
117
    {
118
        $email = Email::create();
119
120
        $email->setTo($this->To);
121
        $email->setCc($this->CC);
122
        $email->setBCC($this->BCC);
123
        $email->setSubject($this->Subject);
124
        $email->setReplyTo($this->ReplyTo);
0 ignored issues
show
Bug Best Practice introduced by
The property ReplyTo does not exist on LeKoala\EmailTemplates\Models\SentEmail. Since you implemented __get, consider adding a @property annotation.
Loading history...
125
        $email->setBody($this->Body);
126
127
        return $email;
128
    }
129
130
    /**
131
     * A BetterButtons custom action that allows the email to be resent
132
     */
133
    public function resend()
134
    {
135
        if ($e = $this->getEmail()) {
136
            $results = $e->send();
137
138
            // Update results
139
            $this->Results = $results;
140
            $this->write();
141
142
            return 'Sent';
143
        }
144
145
        return 'Could not send email';
146
    }
147
148
149
    /**
150
     * Cleanup sent emails based on your config
151
     *
152
     * @return void
153
     */
154
    public static function cleanup()
155
    {
156
        $max = self::config()->max_records;
157
        if ($max && self::get()->count() > $max) {
158
            $method = self::config()->cleanup_method;
159
160
            // Delete all records older than cleanup_time (7 days by default)
161
            if ($method == 'time') {
162
                $time = self::config()->cleanup_time;
163
                $date = date('Y-m-d H:i:s', strtotime($time));
164
                DB::query("DELETE FROM \"SentEmail\" WHERE Created < '$date'");
165
            }
166
167
            // Delete all records that are after half the maximum number of records
168
            if ($method == 'max') {
169
                $maxID = SentEmail::get()->max('ID') - ($max / 2);
170
                DB::query("DELETE FROM \"SentEmail\" WHERE ID < '$maxID'");
171
            }
172
        }
173
    }
174
175
    /**
176
     * Defines the view permission
177
     * @param  Member $member
0 ignored issues
show
The type LeKoala\EmailTemplates\Models\Member was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
178
     * @return boolean
179
     */
180
    public function canView($member = null)
181
    {
182
        return Permission::check('CMS_ACCESS');
183
    }
184
185
    /**
186
     * Defines the edit permission
187
     * @param  Member $member
188
     * @return boolean
189
     */
190
    public function canEdit($member = null)
191
    {
192
        return false;
193
    }
194
195
    /**
196
     * Defines the create permission
197
     * @param  Member $member
198
     * @return boolean
199
     */
200
    public function canCreate($member = null, $context = [])
201
    {
202
        return false;
203
    }
204
205
    /**
206
     * Defines the delete permission
207
     * @param  Member $member
208
     * @return boolean
209
     */
210
    public function canDelete($member = null)
211
    {
212
        return Permission::check('CMS_ACCESS');
213
    }
214
}
215