Completed
Push — master ( 0f13ab...9d4893 )
by Ben
02:07
created

SimpleStyleguideController   C

Complexity

Total Complexity 12

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 25

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 25
dl 0
loc 156
rs 5
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B index() 0 22 5
A getStyleguideData() 0 18 1
A getTestForm() 0 50 1
A getContent() 0 15 2
A getColorSwatches() 0 13 3
1
<?php
2
/**
3
 * @package simple-styleguide
4
 */
5
class SimpleStyleguideController extends Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
    /**
8
     * @config
9
     * @var array
10
     */
11
    private static $color_swatches = [];
0 ignored issues
show
Unused Code introduced by
The property $color_swatches is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
13
    /**
14
     * @var array
15
     */
16
    private static $allowed_actions = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
        'index',
18
    ];
19
20
    /**
21
     * Runs the permissiion checks, and setup of the controller view.
22
     */
23
    public function index()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
24
    {
25
        if (!Director::isDev() && !Permission::check('ADMIN')) {
26
            return Security::permissionFailure();
27
        }
28
29
        // If the subsite module is installed then set the theme based on the current subsite
30
        if (class_exists('Subsite') && Subsite::currentSubsite()) {
31
            Config::inst()->update('SSViewer', 'theme', Subsite::currentSubsite()->Theme);
32
        }
33
34
        $page = Page::get()->first();
35
        $controller = ModelAsController::controller_for($page);
36
        $controller->init();
37
38
        // requirements
39
        Requirements::css('simple-styleguide/css/styleguide.css');
40
41
        return $controller
42
            ->customise($this->getStyleGuideData())
43
            ->renderWith(['SimpleStyleguideController', 'Page']);
44
    }
45
46
    /**
47
     * Provides access to any custom function on the controller for use on the template output.
48
     * @return ArrayData
49
     */
50
    public function getStyleguideData()
51
    {
52
        $data = new ArrayData([
53
            'Title' => 'Styleguide',
54
            'Message' => DBField::create_field(
55
                'HTMLText',
56
                '<p>This controller is only accessible to developers and admin users.</p>'
57
            ),
58
            'TestForm' => $this->getTestForm(),
59
            'Content' => $this->getContent(),
60
            'ColorSwatches' => $this->getColorSwatches(),
61
        ]);
62
63
        // extensions for adding/overriding template data.
64
        $this->extend('updateStyleguideData', $data);
65
66
        return $data;
67
    }
68
69
    /**
70
     * Return a form with fields to match rendering through controller/template output.
71
     * @return Form
72
     */
73
    public function getTestForm()
74
    {
75
        $fields = FieldList::create(
76
            TextField::create('SimpleText', 'Simple Text Field'),
77
            TextField::create('SimpleTextGood', 'Simple Text Field (good)')
78
                ->setError('This is a good message', 'good'),
79
            TextField::create('SimpleTextWarning', 'Simple Text Field (warning)')
80
                ->setError('This is a warning message', 'warning'),
81
            TextField::create('SimpleTextBad', 'Simple Text Field (bad)')
82
                ->setError('This is an error message', 'bad'),
83
            NumericField::create('Number', 'Number Field'),
84
            EmailField::create('Email', "Email Field"),
85
            DropdownField::create('Dropdown', 'Normal dropdown', [
86
                '1' => 'One option',
87
                '2' => 'Two option',
88
            ]),
89
            CheckboxField::create('Checkbox', 'Checkbox'),
90
            CheckboxSetField::create('CheckboxSet', 'Checkbox set', [
91
                '1' => 'One option',
92
                '2' => 'Two option',
93
                '3' => 'Three option',
94
            ]),
95
            OptionsetField::create('Option', 'Option', [
96
                '1' => 'One option',
97
            ]),
98
            OptionsetField::create('OptionSet', 'Option set', [
99
                '1' => 'One option',
100
                '2' => 'Two option',
101
                '3' => 'Three option',
102
            ]),
103
            TextField::create('Text', 'Text')
104
                ->setDescription('This is a description')
105
        );
106
107
        $actions = FieldList::create(
108
            FormAction::create('doForm', 'Submit')
109
        );
110
111
        $required = new RequiredFields(
112
            'SimpleText',
113
            'Email',
114
            'Checkbox',
115
            'Dropdown'
116
        );
117
118
        $form = new Form($this, 'TestForm', $fields, $actions, $required);
119
        $form->setMessage('This is a form wide message. See the alerts component for site wide messages.', 'warning');
120
121
        return $form;
122
    }
123
124
    /**
125
     * Emulate an HTMLEditorField output useful for testing shortcodes and output extensions etc.
126
     * @return HTMLText
127
     */
128
    public function getContent()
129
    {
130
        $content = '';
131
132
        // add file link to html content
133
        $file = File::get()->filter('ClassName', 'File')->first();
134
        if ($file) {
135
            $content .= '<p>This is an internal <a href="[file_link,id=' . $file->ID . ']">link to a file</a> inside content</p>';
136
        }
137
138
        // add external link to html content
139
        $content .= '<p>This is an external <a href="http://google.com">link to google</a> inside content.</p>';
140
141
        return DBField::create_field('HTMLText', $content);
142
    }
143
144
    /**
145
     * @return ArrayList
146
     */
147
    public function getColorSwatches()
148
    {
149
        $list = ArrayList::create();
150
        $colors = $this->config()->color_swatches;
151
152
        if ($colors) {
153
            foreach ($colors as $color) {
154
                $list->push(ArrayData::create($color));
155
            }
156
        }
157
158
        return $list;
159
    }
160
}
161