Issues (4)

src/Extensions/InternalExternalLinkExtension.php (4 issues)

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