Passed
Push — master ( 499c98...fcda52 )
by Nicolaas
01:55 queued 22s
created

InternalExternalLinkExtension::onBeforeWrite()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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