Passed
Push — master ( fcda52...d934e8 )
by Nicolaas
06:51
created

InternalExternalLinkExtension::getMyLink()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 8
eloc 15
c 3
b 0
f 0
nc 5
nop 1
dl 0
loc 23
rs 8.4444
1
<?php
2
3
namespace Sunnysideup\InternalExternalLink\Extensions;
4
5
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...
6
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...
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\HeaderField;
9
use SilverStripe\Forms\OptionsetField;
10
use SilverStripe\Forms\Tab;
11
use SilverStripe\Forms\TextField;
12
13
use SilverStripe\Forms\TreeDropdownField;
14
15
use SilverStripe\ORM\DataExtension;
16
use SilverStripe\ORM\FieldType\DBField;
17
use SilverStripe\Assets\File;
18
19
class InternalExternalLinkExtension extends DataExtension
20
{
21
    private static $db = [
22
        'LinkType' => "Enum('Internal,External,DownloadFile', 'Internal')",
23
        'ExternalLink' => 'Varchar(255)',
24
    ];
25
26
    private static $has_one = [
27
        'InternalLink' => Page::class,
28
        'DownloadFile' => File::class,
29
    ];
30
31
    /**
32
     * use the $fieldNameAppendix if you have multiple fields
33
     * @param  string     $fieldNameAppendix - optional
34
     * @return string|null
35
     */
36
    public function MyLink($fieldNameAppendix = ''): ?string
37
    {
38
        return $this->owner->getMyLink($fieldNameAppendix);
39
    }
40
41
    /**
42
     * use the $fieldNameAppendix if you have multiple fields
43
     * @param  string     $fieldNameAppendix - optional
44
     * @return string|null
45
     */
46
    public function getMyLink(?string $fieldNameAppendix = ''): ?string
47
    {
48
        $linkTypeFieldName = 'LinkType' . $fieldNameAppendix;
49
50
        $InternalLinkMethodName = 'InternalLink' . $fieldNameAppendix;
51
        $internalLinkFieldName = $InternalLinkMethodName . 'ID';
52
53
        $downloadLinkMethodName = 'DownloadFile' . $fieldNameAppendix;
54
        $downloadLinkFieldName = $downloadLinkMethodName . 'ID';
55
56
        $externalLinkFieldName = 'ExternalLink' . $fieldNameAppendix;
57
        if ($this->owner->{$linkTypeFieldName} === 'Internal' && $this->owner->{$internalLinkFieldName}) {
58
            $obj = $this->owner->{$InternalLinkMethodName}();
59
            if ($obj) {
60
                return $obj->Link();
61
            }
62
        } elseif ($this->owner->{$linkTypeFieldName} === 'External' && $this->owner->{$externalLinkFieldName}) {
63
            return DBField::create_field('Varchar', $this->owner->{$externalLinkFieldName})->url();
64
        }elseif ($this->owner->{$linkTypeFieldName} === 'DownloadFile' && $this->owner->{$downloadLinkFieldName}) {
65
            return DBField::create_field('Varchar', $this->owner->{$downloadLinkMethodName})->url();
66
        }
67
68
        return null;
69
    }
70
71
    public function updateCMSFields(FieldList $fields)
72
    {
73
        $js = <<<js
74
            var el = this;
75
            const val = jQuery(el).find('.form-check-input:checked').val();
76
            if (val === 'Internal') {
77
                jQuery('#Form_ItemEditForm_InternalLinkID_Holder').show();
78
                jQuery('#Form_ItemEditForm_ExternalLink_Holder').hide();
79
                jQuery('#Form_ItemEditForm_DownloadFile_Holder').hide();
80
            } else if(val === 'External') {
81
                jQuery('#Form_ItemEditForm_InternalLinkID_Holder').hide();
82
                jQuery('#Form_ItemEditForm_ExternalLink_Holder').show();
83
                jQuery('#Form_ItemEditForm_DownloadFile_Holder').hide();
84
            } else if(val === 'DownloadFile') {
85
                jQuery('#Form_ItemEditForm_InternalLinkID_Holder').hide();
86
                jQuery('#Form_ItemEditForm_ExternalLink_Holder').hide();
87
                jQuery('#Form_ItemEditForm_DownloadFile_Holder').show();
88
            } else {
89
                jQuery('#Form_ItemEditForm_InternalLinkID_Holder').show();
90
                jQuery('#Form_ItemEditForm_ExternalLink_Holder').show();
91
                jQuery('#Form_ItemEditForm_DownloadFile_Holder').show();
92
            }
93
94
js;
95
        // $fields->insertBefore(new Tab('Links', 'Links'), 'Settings');
96
        $fields->addFieldsToTab(
97
            'Root.Links',
98
            [
99
                HeaderField::create('Link-Details-Heading', 'Link'),
100
                OptionsetField::create('LinkType', 'Link Type', $this->owner->dbObject('LinkType')->enumValues())
101
                    ->setAttribute('onclick', $js)
102
                    ->setAttribute('onchange', $js),
103
                TreeDropdownField::create('InternalLinkID', 'Internal Link', Page::class),
104
                UploadField::create(
105
                    'DownloadFile',
106
                    'Download File'
107
                ),
108
            ]
109
        );
110
    }
111
112
    public function onBeforeWrite()
113
    {
114
        parent::onBeforeWrite();
115
        if($this->owner->LinkType === 'Internal' && ! $this->owner->InternalLinkID && $this->owner->ExternalLink) {
116
            $this->owner->LinkType = 'External';
117
        }
118
        if($this->owner->LinkType === 'External' && $this->owner->InternalLinkID && ! $this->owner->ExternalLink) {
119
            $this->owner->LinkType = 'External';
120
        }
121
    }
122
}
123