Passed
Push — master ( 79436d...224c58 )
by Nicolaas
20:19 queued 16:05
created

CMSNicetiesTraitForCMSLinks::editLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 11
rs 9.9666
1
<?php
2
3
namespace Sunnysideup\CMSNiceties\Traits;
4
5
use SilverStripe\Admin\ModelAdmin;
6
// use SilverStripe\Forms\GridField\GridFieldArchiveAction;
7
use SilverStripe\CMS\Model\SiteTree;
8
use SilverStripe\Control\Controller;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Forms\HTMLReadonlyField;
11
12
trait CMSNicetiesTraitForCMSLinks
13
{
14
    public function CMSEditLink(): string
15
    {
16
        if ($this instanceof SiteTree) {
17
            return parent::CMSEditLink();
18
        }
19
20
        $cont = $this->myModelAdminController();
21
        if ($cont) {
22
            return $this->editLink($cont);
23
        }
24
25
        return '404-cms-edit-link-not-found';
26
    }
27
28
    public function CMSEditLinkLimited(): string
29
    {
30
        $cont = $this->myModelAdminController();
31
        if ($cont) {
32
            return $this->editLink($cont);
33
        }
34
35
        return '404-cms-edit-link-not-found';
36
    }
37
38
    protected function editLink($controller): string
39
    {
40
        return Controller::join_links(
41
            $controller->Link(),
42
            $this->sanitisedClassName(),
43
            'EditForm',
44
            'field',
45
            $this->sanitisedClassName(),
46
            'item',
47
            $this->ID,
48
            'edit'
49
        );
50
    }
51
52
    public function CMSEditLinkField(string $relName, string $name = ''): HTMLReadonlyField
53
    {
54
        $obj = $this->{$relName}();
55
        if (! $name) {
56
            $nameOptions = $this->fieldLabels();
0 ignored issues
show
Bug introduced by
It seems like fieldLabels() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

56
            /** @scrutinizer ignore-call */ 
57
            $nameOptions = $this->fieldLabels();
Loading history...
57
            $name = $nameOptions[$relName] ?? $nameOptions[$relName . 'ID'] ?? 'error';
58
        }
59
60
        if ($obj && $obj->exists()) {
61
            $value = '<a href="' . $obj->CMSEditLink() . '">' . $obj->getTitle() . '</a>';
62
        } else {
63
            $value = '<em>(none)</em>';
64
        }
65
66
        return HTMLReadonlyField::create(
67
            $relName . 'Link',
68
            $name,
69
            $value
70
        );
71
    }
72
73
    public function CMSAddLink(): string
74
    {
75
        $controller = $this->myModelAdminController();
76
        if ($controller) {
77
            return $controller->Link() .
78
                $this->sanitisedClassName() . '/EditForm/field/' .
79
                $this->sanitisedClassName() . '/item/new';
80
        }
81
82
        return '404-cms-add-link-not-found';
83
    }
84
85
    public function CMSListLink(): string
86
    {
87
        $controller = $this->myModelAdminController();
88
        if ($controller) {
89
            return $controller->Link() . $this->sanitisedClassName();
90
        }
91
92
        return '404-cms-list-link-not-found';
93
    }
94
95
    /**
96
     * @return null|ModelAdmin
97
     */
98
    protected function myModelAdminController()
99
    {
100
        $modelAdminClassName = $this->Config()->get('primary_model_admin_class');
0 ignored issues
show
Bug introduced by
It seems like Config() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

100
        $modelAdminClassName = $this->/** @scrutinizer ignore-call */ Config()->get('primary_model_admin_class');
Loading history...
101
        $obj = null;
102
        if ($modelAdminClassName) {
103
            /** @var null|ModelAdmin $obj */
104
            $obj = Injector::inst()->get($modelAdminClassName);
105
        }
106
107
        return $obj;
108
    }
109
110
    /**
111
     * Sanitise a model class' name for inclusion in a link.
112
     */
113
    protected function sanitisedClassName(): string
114
    {
115
        $className = $this->hasMethod('classNameForModelAdmin') ? $this->classNameForModelAdmin() : $this->ClassName;
0 ignored issues
show
Bug introduced by
It seems like hasMethod() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

115
        $className = $this->/** @scrutinizer ignore-call */ hasMethod('classNameForModelAdmin') ? $this->classNameForModelAdmin() : $this->ClassName;
Loading history...
Bug introduced by
It seems like classNameForModelAdmin() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

115
        $className = $this->hasMethod('classNameForModelAdmin') ? $this->/** @scrutinizer ignore-call */ classNameForModelAdmin() : $this->ClassName;
Loading history...
116
117
        return str_replace('\\', '-', $className);
118
    }
119
}
120