EmailTemplateSiteConfigExtension   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 60
c 0
b 0
f 0
dl 0
loc 130
rs 10
wmc 30

8 Methods

Rating   Name   Duplication   Size   Complexity  
A EmailRssLink() 0 6 2
A EmailBaseColor() 0 7 3
A EmailLogoTemplate() 0 11 4
A EmailDefaultRecipient() 0 21 5
A updateCMSFields() 0 20 3
A EmailTwitterLink() 0 10 4
A EmailDefaultSender() 0 21 5
A EmailFacebookLink() 0 10 4
1
<?php
2
3
namespace LeKoala\EmailTemplates\Extensions;
4
5
use Exception;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\TextField;
8
use SilverStripe\Control\Director;
9
use SilverStripe\ORM\DataExtension;
10
use SilverStripe\Control\Email\Email;
11
use SilverStripe\SiteConfig\SiteConfig;
12
use LeKoala\EmailTemplates\Models\EmailTemplate;
0 ignored issues
show
Bug introduced by
The type LeKoala\EmailTemplates\Models\EmailTemplate 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 SilverStripe\Forms\HTMLEditor\HTMLEditorField;
14
15
/**
16
 * EmailTemplateSiteConfigExtension
17
 *
18
 * @property string $EmailFooter
19
 * @property string $DefaultFromEmail
20
 * @property string $ContactEmail
21
 *
22
 * @property-read SiteConfig&EmailTemplateSiteConfigExtension $owner
23
 *
24
 * @author Kalyptus SPRL <[email protected]>
25
 */
26
class EmailTemplateSiteConfigExtension extends DataExtension
0 ignored issues
show
Deprecated Code introduced by
The class SilverStripe\ORM\DataExtension has been deprecated: 5.3.0 Subclass SilverStripe\Core\Extension\Extension instead ( Ignorable by Annotation )

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

26
class EmailTemplateSiteConfigExtension extends /** @scrutinizer ignore-deprecated */ DataExtension
Loading history...
27
{
28
29
    private static $db = [
30
        'EmailFooter' => 'HTMLText',
31
        'DefaultFromEmail' => 'Varchar(255)',
32
        'ContactEmail' => 'Varchar(255)',
33
    ];
34
35
    public function updateCMSFields(FieldList $fields)
36
    {
37
        // Handle field update ourselves
38
        if (!SiteConfig::config()->get('email_templates_update_fields')) {
39
            return $fields;
40
        }
41
42
        // Already defined by another module
43
        if ($fields->dataFieldByName('EmailFooter')) {
44
            return $fields;
45
        }
46
47
        $EmailFooter = new HTMLEditorField('EmailFooter', _t('EmailTemplateSiteConfigExtension.EmailFooter', 'Email Footer'));
48
        $EmailFooter->setRows(3);
49
        $fields->addFieldToTab('Root.Email', $EmailFooter);
50
51
        $fields->addFieldToTab('Root.Email', new TextField('DefaultFromEmail', _t('EmailTemplateSiteConfigExtension.DefaultFromEmail', 'Default Sender')));
52
        $fields->addFieldToTab('Root.Email', new TextField('ContactEmail', _t('EmailTemplateSiteConfigExtension.ContactEmail', 'Default Recipient')));
53
54
        return $fields;
55
    }
56
57
    public function EmailDefaultRecipient()
58
    {
59
        if ($this->owner->ContactEmail) {
60
            return $this->owner->ContactEmail;
61
        }
62
63
        $config = Email::config();
64
65
        if ($config->get('default_recipient_email')) {
66
            return $config->get('default_recipient_email');
67
        }
68
69
        if ($config->get('admin_email')) {
70
            return $config->get('admin_email');
71
        }
72
73
        if ($config->get('send_all_emails_to')) {
74
            return $config->get('send_all_emails_to');
75
        }
76
77
        throw new Exception('Could not find the default email recipient');
78
    }
79
80
    public function EmailDefaultSender()
81
    {
82
        if ($this->owner->DefaultFromEmail) {
83
            return $this->owner->DefaultFromEmail;
84
        }
85
86
        $config = Email::config();
87
88
        if ($config->get('default_sender_email')) {
89
            return $config->get('default_sender_email');
90
        }
91
92
        if ($config->get('admin_email')) {
93
            return $config->get('admin_email');
94
        }
95
96
        if ($config->get('send_all_emails_from')) {
97
            return $config->get('send_all_emails_from');
98
        }
99
100
        throw new Exception('Could not find the default email sender');
101
    }
102
103
    public function EmailBaseColor()
104
    {
105
        $field = EmailTemplate::config()->get('base_color_field');
106
        if ($field && $this->owner->hasField($field)) {
107
            return $this->owner->$field;
108
        }
109
        return EmailTemplate::config()->get('base_color');
110
    }
111
112
    public function EmailLogoTemplate()
113
    {
114
        // Use EmailLogo if defined
115
        if ($this->owner->EmailLogoID) {
116
            return $this->owner->EmailLogo();
117
        }
118
        // Otherwise, use configurable field
119
        $field = EmailTemplate::config()->get('logo_field');
120
        if ($field && $this->owner->hasField($field)) {
121
            $method = str_replace('ID', '', $field);
122
            return $this->owner->$method();
123
        }
124
    }
125
126
    public function EmailTwitterLink()
127
    {
128
        if (!EmailTemplate::config()->get('show_twitter')) {
129
            return;
130
        }
131
        $field = EmailTemplate::config()->get('twitter_field');
132
        if ($field && !$this->owner->hasField($field)) {
133
            return;
134
        }
135
        return 'https://twitter.com/' . $this->owner->$field;
136
    }
137
138
    public function EmailFacebookLink()
139
    {
140
        if (!EmailTemplate::config()->get('show_facebook')) {
141
            return;
142
        }
143
        $field = EmailTemplate::config()->get('facebook_field');
144
        if ($field && !$this->owner->hasField($field)) {
145
            return;
146
        }
147
        return 'https://www.facebook.com/' . $this->owner->$field;
148
    }
149
150
    public function EmailRssLink()
151
    {
152
        if (!EmailTemplate::config()->get('show_rss')) {
153
            return;
154
        }
155
        return Director::absoluteURL('rss');
156
    }
157
}
158