Passed
Push — master ( ff9bff...99aa3f )
by
unknown
12:55 queued 05:53
created

InternalExternalLinkExtension::updateCMSFields()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 100
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 16
Bugs 0 Features 0
Metric Value
cc 2
eloc 55
c 16
b 0
f 0
nc 2
nop 1
dl 0
loc 100
rs 8.9818

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Sunnysideup\InternalExternalLink\Extensions;
4
5
use Page;
0 ignored issues
show
Bug introduced by
The type Page 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...
6
use SilverStripe\AssetAdmin\Forms\UploadField;
0 ignored issues
show
Bug introduced by
The type SilverStripe\AssetAdmin\Forms\UploadField 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...
7
use SilverStripe\Assets\File;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\HeaderField;
10
use SilverStripe\Forms\OptionsetField;
11
use SilverStripe\Forms\Tab;
12
use SilverStripe\Forms\TextField;
13
use SilverStripe\Forms\TreeDropdownField;
14
use SilverStripe\ORM\DataExtension;
15
use SilverStripe\ORM\FieldType\DBField;
16
use SilverStripe\View\Requirements;
17
18
class InternalExternalLinkExtension extends DataExtension
19
{
20
    public static $casting = [
21
        'MyLink' => 'Varchar',
22
    ];
23
    private static $db = [
24
        'LinkType' => "Enum('Internal,External,DownloadFile,Email,Phone', 'Internal')",
25
        'ExternalLink' => 'Varchar(255)',
26
    ];
27
28
    private static $has_one = [
29
        'InternalLink' => Page::class,
30
        'DownloadFile' => File::class,
31
    ];
32
33
    private static $owns = [
34
        'DownloadFile',
35
    ];
36
37
    /**
38
     * use the $fieldNameAppendix if you have multiple fields.
39
     *
40
     * @param string $fieldNameAppendix - optional
41
     */
42
    public function MyLink($fieldNameAppendix = ''): ?string
43
    {
44
        return $this->owner->getMyLink($fieldNameAppendix);
45
    }
46
47
    /**
48
     * use the $fieldNameAppendix if you have multiple fields.
49
     *
50
     * @param string $fieldNameAppendix - optional
51
     */
52
    public function getMyLink(?string $fieldNameAppendix = ''): ?string
53
    {
54
        $linkTypeFieldName = 'LinkType' . $fieldNameAppendix;
55
56
        $InternalLinkMethodName = 'InternalLink' . $fieldNameAppendix;
57
        $internalLinkFieldName = $InternalLinkMethodName . 'ID';
58
59
        $downloadLinkMethodName = 'DownloadFile' . $fieldNameAppendix;
60
        $downloadLinkFieldName = $downloadLinkMethodName . 'ID';
61
62
        $externalLinkFieldName = 'ExternalLink' . $fieldNameAppendix;
63
        if ('Internal' === $this->owner->{$linkTypeFieldName} && $this->owner->{$internalLinkFieldName}) {
64
            $obj = $this->owner->{$InternalLinkMethodName}();
65
            if ($obj) {
66
                return $obj->Link();
67
            }
68
        } elseif ('DownloadFile' === $this->owner->{$linkTypeFieldName} && $this->owner->{$downloadLinkFieldName}) {
69
            $obj = $this->owner->{$downloadLinkMethodName}();
70
            if ($obj) {
71
                return $obj->Link();
72
            }
73
        } elseif ($this->owner->{$externalLinkFieldName}) {
74
            if ('External' === $this->owner->{$linkTypeFieldName}) {
75
                return DBField::create_field('Varchar', $this->owner->{$externalLinkFieldName})->url();
76
            }
77
            if ('Email' === $this->owner->{$linkTypeFieldName}) {
78
                $val = $this->owner->{$externalLinkFieldName};
79
                if (class_exists('Sunnysideup\\EmailAddressDatabaseField\\Model\\Fieldtypes\\EmailAddress')) {
80
                    $val = DBField::create_field('EmailAddress', $val)->HiddenEmailAddress()->RAW();
81
                }
82
83
                return 'mailto:' . $val;
84
            }
85
            if ('Phone' === $this->owner->{$linkTypeFieldName}) {
86
                $val = $this->owner->{$externalLinkFieldName};
87
                if (class_exists('Sunnysideup\\PhoneField\\Model\\Fieldtypes\\PhoneField')) {
88
                    $val = DBField::create_field('PhoneField', $this->owner->{$externalLinkFieldName})->IntlFormat()->Raw();
89
                }
90
91
                return 'callto:' . $val;
92
            }
93
        }
94
95
        return null;
96
    }
97
98
    public function updateCMSFields(FieldList $fields)
99
    {
100
        $fieldNameAppendici = $this->getFieldNameAppendici();
101
        foreach ($fieldNameAppendici as $appendix) {
102
            $linkTypeClass = 'LinkType' . $appendix . '_' . rand(0, 999999);
103
            $internalClass = 'InternalLink' . $appendix . 'ID_' . rand(0, 999999);
104
            $externalClass = 'ExternalLink' . $appendix . '_' . rand(0, 999999);
105
            $downloadFileClass = 'DownloadFile' . $appendix . '_' . rand(0, 999999);
106
107
            $js = <<<js
108
                var el = this;
109
                const val = jQuery('.{$linkTypeClass}').find('.form-check-input:checked').val();
110
                const internaLinkHolder = jQuery('.{$internalClass}');
111
                const externaLinkHolder = jQuery('.{$externalClass}');
112
                const downloadLinkHolder = jQuery('.{$downloadFileClass}');
113
                if (val === 'Internal') {
114
                    internaLinkHolder.show();
115
                    externaLinkHolder.hide();
116
                    downloadLinkHolder.hide();
117
                } else if(val === 'External' || val === 'Phone' || val === 'Email') {
118
                    internaLinkHolder.hide();
119
                    externaLinkHolder.show();
120
                    downloadLinkHolder.hide();
121
                } else if(val === 'DownloadFile') {
122
                    internaLinkHolder.hide();
123
                    externaLinkHolder.hide();
124
                    downloadLinkHolder.show();
125
                } else {
126
                    internaLinkHolder.show();
127
                    externaLinkHolder.show();
128
                    downloadLinkHolder.show();
129
                }
130
131
js;
132
            Requirements::customScript(
133
                '
134
                const ' . $linkTypeClass . 'fx = function() {
135
                    ' . $js . '
136
                }
137
                jQuery(".' . $linkTypeClass . ' input").on("change click", ' . $linkTypeClass . 'fx());
138
                window.setInterval(
139
                    function() {
140
                        ' . $linkTypeClass . 'fx();
141
                    },
142
                    700
143
                )',
144
                $linkTypeClass
145
            );
146
            $fields->removeByName([
147
                'LinkType' . $appendix,
148
                'InternalLink' . $appendix . 'ID',
149
                'DownloadFile' . $appendix,
150
                'ExternalLink' . $appendix,
151
            ]);
152
            // $fields->insertBefore(new Tab('Links', 'Links'), 'Settings');
153
            $fields->addFieldsToTab(
154
                'Root.Links',
155
                [
156
                    HeaderField::create(
157
                        'Link-Details-Heading' . $appendix,
158
                        'Link'
159
                    ),
160
161
                    OptionsetField::create(
162
                        'LinkType' . $appendix,
163
                        'Link Type ' . $appendix,
164
                        $this->owner->dbObject('LinkType')->enumValues()
165
                    )
166
                        ->setAttribute('onchange', $js)
167
                        ->setAttribute('onclick', $js)
168
                        ->addExtraClass($linkTypeClass),
169
170
                    TreeDropdownField::create(
171
                        'InternalLink' . $appendix . 'ID',
172
                        'Internal Link ' . $appendix,
173
                        Page::class
174
                    )
175
                        ->addExtraClass($internalClass),
176
177
                    TextField::create(
178
                        'ExternalLink' . $appendix,
179
                        'External Link / Email / Phone'
180
                    )
181
                        ->addExtraClass($externalClass),
182
183
                    UploadField::create(
184
                        'DownloadFile' . $appendix,
185
                        'Download File ' . $appendix
186
                    )
187
                        ->addExtraClass($downloadFileClass),
188
                ]
189
            );
190
            Requirements::customScript(
191
                'window.setTimeout(
192
                    function() {
193
                        jQuery(\'input[name="LinkType' . $appendix . '"]\').change();
194
                    }
195
                    , 500
196
                )',
197
                'InternalExternalLinkKickStart' . $appendix
198
            );
199
        }
200
    }
201
202
    protected function getFieldNameAppendici(): array
203
    {
204
        if ($this->owner->hasMethod('getFieldNameAppendiciMore')) {
205
            return $this->owner->getFieldNameAppendiciMore();
206
        }
207
208
        // we need an empty string here ...
209
        return [
210
            '',
211
        ];
212
    }
213
}
214