Passed
Push — master ( e2682f...a96ff7 )
by Nicolaas
02:17
created

DashboardWelcomeQuicklinks::Title()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Sunnysideup\DashboardWelcomeQuicklinks\Admin;
4
5
use SilverStripe\Admin\LeftAndMain;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Admin\LeftAndMain 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\Core\ClassInfo;
7
use SilverStripe\Core\Injector\Injector;
8
use SilverStripe\Forms\LiteralField;
9
use SilverStripe\ORM\ArrayList;
10
use SilverStripe\ORM\DataObject;
11
use SilverStripe\SiteConfig\SiteConfig;
0 ignored issues
show
Bug introduced by
The type SilverStripe\SiteConfig\SiteConfig 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...
12
use SilverStripe\View\ArrayData;
13
use Sunnysideup\DashboardWelcomeQuicklinks\Api\DefaultDashboardProvider;
14
use Sunnysideup\DashboardWelcomeQuicklinks\Interfaces\DashboardWelcomeQuickLinksProvider;
15
16
/**
17
 * Class \Sunnysideup\DashboardWelcomeQuicklinks\Admin\DashboardWelcomeQuicklinks
18
 *
19
 */
20
class DashboardWelcomeQuicklinks extends LeftAndMain
21
{
22
    private static $url_segment = 'go';
0 ignored issues
show
introduced by
The private property $url_segment is not used, and could be removed.
Loading history...
23
24
    private static $use_default_dashboard = true;
0 ignored issues
show
introduced by
The private property $use_default_dashboard is not used, and could be removed.
Loading history...
25
26
    private static $menu_title = 'Quick-links';
0 ignored issues
show
introduced by
The private property $menu_title is not used, and could be removed.
Loading history...
27
28
    private static $menu_icon_class = 'font-icon-dashboard';
0 ignored issues
show
introduced by
The private property $menu_icon_class is not used, and could be removed.
Loading history...
29
30
    private static $menu_priority = 99999;
0 ignored issues
show
introduced by
The private property $menu_priority is not used, and could be removed.
Loading history...
31
32
    public function getEditForm($id = null, $fields = null)
33
    {
34
        $form = parent::getEditForm($id, $fields);
35
36
        // if ($form instanceof HTTPResponse) {
37
        //     return $form;
38
        // }
39
        // $form->Fields()->removeByName('LastVisited');
40
41
        $this->updateFormWithQuicklinks($form);
42
43
        return $form;
44
    }
45
46
    public function updateFormWithQuicklinks($form)
47
    {
48
        $shortcuts = $this->getLinksFromImplementor();
49
        $html = '';
50
        if (count($shortcuts)) {
51
            $html = '<div class="grid-wrapper">';
52
53
            usort(
54
                $shortcuts,
55
                function ($a, $b) {
56
                    ($a['SortOrder'] ?? 0) <=> ($b['SortOrder'] ?? 0);
57
                }
58
            );
59
60
            foreach ($shortcuts as $groupCode => $groupDetails) {
61
                $colour = '';
62
                if (!empty($groupDetails['Colour'])) {
63
                    $colour = 'style="background-color: ' . $groupDetails['Colour'] . '"';
64
                }
65
                $icon = '';
66
                if (!empty($groupDetails['IconClass'])) {
67
                    $icon = '<i class="' . $groupDetails['IconClass'] . '"></i> ';
68
                }
69
                $html .= '
70
                <div class="grid-cell" ' . $colour . '>
71
                    <h1>' . $icon . '' . ($groupDetails['Title'] ?? $groupCode) . '</h1>';
72
                $items = $groupDetails['Items'] ?? [];
73
                if (!empty($entry['Link']) && class_exists($entry['Link'])) {
74
                    $obj = Injector::inst()->get($entry['Link']);
75
                    if ($obj instanceof DataObject) {
76
                        $entry['Link'] = DataObject::get_one($entry['Link'])->CMSEditLink();
77
                    } else {
78
                        $entry['Link'] = $obj->Link();
79
                    }
80
                }
81
                foreach ($items as $entry) {
82
                    $html .= $this->makeShortCut(
83
                        $entry['Title'],
84
                        $entry['Link'],
85
                        $entry['OnClick'] ?? '',
86
                        $entry['Script'] ?? '',
87
                        $entry['Style'] ?? '',
88
                        $entry['IconClass'] ?? '',
89
                        $entry['Target'] ?? '',
90
                    )->Field();
91
                }
92
                $html .= '</div>';
93
            }
94
        }
95
        $kc = ['#F2F3F4', '#222222', '#F3C300', '#875692', '#F38400', '#A1CAF1', '#BE0032', '#C2B280', '#848482', '#008856', '#E68FAC', '#0067A5', '#F99379', '#604E97', '#F6A600', '#B3446C', '#DCD300', '#882D17', '#8DB600', '#654522', '#E25822', '#2B3D26'];
96
        $kcCount = count($kc);
97
        $colours = '';
98
        foreach ($kc as $key => $colour) {
99
            $colours .= ' .grid-cell:nth-child(' . $kcCount . 'n+' . ($key + 1) . ') {background-color: ' . $colour . '55;}';
100
        }
101
        $html .= '</div>';
102
        $html .= '<style>
103
104
        .grid-wrapper {
105
          display: grid;
106
          grid-template-columns: repeat( auto-fit, minmax(300px, 1fr) );;
107
          grid-gap: 20px;
108
        }
109
110
        .grid-cell {
111
          max-width: 500px;
112
          padding: 20px;
113
          font-size: 150%;
114
          border-radius: 1rem;
115
          border: 1px dashed #004e7f55;
116
        }
117
        ' . $colours . '
118
        .grid-cell * {
119
            color: #222!important;
120
        }
121
        .grid-cell h1 {
122
            border-bottom: 1px solid #777
123
        }
124
        </style>';
125
        $form->Fields()->push(LiteralField::create('ShortCuts', $html));
126
    }
127
128
    protected function getLinksFromImplementor()
129
    {
130
        $array = [];
131
        $useDefaultDashboard = $this->config()->get('use_default_dashboard');
132
        $classNames = ClassInfo::implementorsOf(DashboardWelcomeQuickLinksProvider::class);
133
        foreach ($classNames as $className) {
134
            if($useDefaultDashboard === false && $className === DefaultDashboardProvider::class) {
135
                continue;
136
            }
137
            $array += Injector::inst()->get($className)->provideDashboardWelcomeQuickLinks();
138
        }
139
        return $array;
140
    }
141
142
    protected function makeShortCut(string $title, string $link, ?string $onclick = '', ?string $script = '', ?string $style = '', ?string $iconClass = '', ?string $target = '')
143
    {
144
        $name = preg_replace('#[\W_]+#u', '', (string) $title);
145
        $html = '';
146
        if ($onclick) {
147
            $onclick = ' onclick="' . $onclick . '"';
148
        }
149
        if ($script) {
150
            $script = '<script>' . $script . '</script>';
151
        }
152
        $icon = '';
153
        if (!empty($iconClass)) {
154
            $icon = '<i class="' . $iconClass . '"></i> ';
155
        }
156
        if($target) {
157
            $target = ' target="'.$target.'"';
158
        }
159
        if ($link) {
160
            $html = '
161
            ' . $script . '
162
            <h2 style="' . $style . '">
163
                ' . $icon . '<a href="' . $link . '" id="' . $name . '" ' . $target . ' ' . $onclick . '>' . $title . '</a>
164
            </h2>';
165
        } else {
166
            $html = '
167
            ' . $script . '
168
            <p>
169
                &raquo; ' . $title . '
170
            </p>
171
            ';
172
        }
173
        if ($style) {
174
            $html .= '<style>' . $style . '</style>';
175
        }
176
177
        return LiteralField::create(
178
            $name,
179
            $html
180
        );
181
    }
182
    /**
183
     * @return string
184
     */
185
    public function Title()
186
    {
187
        $app = $this->getApplicationName();
188
        $siteConfigTitle = SiteConfig::current_site_config()->Title;
189
        if($siteConfigTitle) {
190
            $app = $siteConfigTitle . ' ('.$app.')';
191
        }
192
        return ($section = $this->SectionTitle()) ? sprintf('%s for %s', $section, $app) : $app;
193
    }
194
    /**
195
     * @param bool $unlinked
196
     * @return ArrayList<ArrayData>
197
     */
198
    public function Breadcrumbs($unlinked = false)
199
    {
200
        $items = new ArrayList([
201
            new ArrayData([
202
                'Title' => $this->Title(),
203
                'Link' => ($unlinked) ? false : $this->Link()
204
            ])
205
        ]);
206
207
        return $items;
208
    }
209
}
210