CarouselPage::updateSettingsFields()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 45
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 32
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 45
rs 9.408
1
<?php
2
3
namespace ilateral\SilverStripe\Carousel\Extensions;
4
5
use SilverStripe\Dev\Debug;
6
use SilverStripe\Assets\Image;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\FieldGroup;
9
use SilverStripe\ORM\DataExtension;
10
use SilverStripe\Core\Config\Config;
11
use SilverStripe\Forms\LiteralField;
12
use SilverStripe\Forms\NumericField;
13
use SilverStripe\Forms\CheckboxField;
14
use SilverStripe\Forms\DropdownField;
15
use SilverStripe\Core\Injector\Injector;
16
use SilverStripe\Forms\GridField\GridField;
17
use Heyday\ResponsiveImages\ResponsiveImageExtension;
18
use ilateral\SilverStripe\Carousel\Model\CarouselSlide;
19
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
20
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
21
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
22
23
/**
24
 * Extension to all page objects that add carousel slide relationships
25
 * 
26
 * @author i-lateral (http://www.i-lateral.com)
27
 * @package carousel
28
 */
29
class CarouselPage extends DataExtension
30
{
31
    
32
    /**
33
     * DB Columns
34
     * 
35
     * @var array
36
     * @config
37
     */
38
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
39
        'ShowCarousel'  => 'Boolean',
40
        "CarouselShowIndicators" => "Boolean",
41
        "CarouselShowControls" => "Boolean",
42
        "CarouselProfile" => "Varchar",
43
        "CarouselInterval" => "Int"
44
    ];
45
46
    /**
47
     * Has Many relations
48
     * 
49
     * @var array
50
     * @config
51
     */
52
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
53
        'Slides' => CarouselSlide::class
54
    ];
55
56
    /**
57
     * Default variables
58
     * 
59
     * @var array
60
     * @config
61
     */
62
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
63
        'CarouselProfile' => 'ShortCarousel',
64
        'CarouselInterval' => 3000,
65
    ];
66
67
    public function updateCMSFields(FieldList $fields)
68
    {
69
        if($this->owner->ShowCarousel) {
70
            $carousel_table = GridField::create(
71
                'Slides',
72
                false,
73
                $this->owner->Slides(),
74
                GridFieldConfig_RecordEditor::create()
75
                    ->addComponent(new GridFieldOrderableRows('Sort'))
76
            );
77
78
            $fields->addFieldToTab('Root.Carousel', $carousel_table);
79
        } else {
80
            $fields->removeByName('Slides');
81
        }
82
83
        $fields->removeByName('ShowCarousel');
84
        $fields->removeByName('CarouselProfile');
85
86
        parent::updateCMSFields($fields);
87
    }
88
89
    public function updateSettingsFields(FieldList $fields)
90
    {
91
        $message = '<p>Configure this page to use a carousel</p>';
92
        
93
        $fields->addFieldToTab(
94
            'Root.Settings',
95
            LiteralField::create("CarouselMessage", $message)
96
        );
97
98
        $carousel = FieldGroup::create(
99
            CheckboxField::create(
100
                'ShowCarousel',
101
                'Show a carousel on this page?'
102
            ),
103
            CheckboxField::create(
104
                'CarouselShowIndicators',
105
                $this->owner->fieldLabel('CarouselShowIndicators')
106
            ),
107
            CheckboxField::create(
108
                'CarouselShowControls',
109
                $this->owner->fieldLabel('CarouselShowControls')
110
            )
111
        )->setTitle('Carousel');
112
113
        $fields->addFieldToTab(
114
            'Root.Settings',
115
            $carousel
116
        );
117
118
        if($this->owner->ShowCarousel) {
119
            $array = [];
120
            foreach (array_keys(Config::inst()->get(ResponsiveImageExtension::class, 'sets')) as $key => $value) {
121
                $array[$value] = $value;
122
            }
123
            $fields->addFieldsToTab(
124
                'Root.Settings',
125
                [
126
                    DropdownField::create(
127
                        'CarouselProfile',
128
                        $this->owner->fieldLabel('CarouselProfile'),
129
                        $array
130
                    )->setEmptyString('Choose one'),
131
                    NumericField::create(
132
                        'CarouselInterval',
133
                        $this->owner->fieldLabel('CarouselInterval')
134
                    )
135
                ]
136
            );
137
        }
138
    }
139
140
    public function CarouselSlides() {
141
        return $this
142
            ->owner
143
            ->renderWith(
144
                'ilateral\SilverStripe\Carousel\Includes\CarouselSlides',
145
                [
146
                    'Slides' => $this->owner->Slides(),
147
                    'Interval' => $this->owner->CarouselInterval ? $this->owner->CarouselInterval : 3000,
148
                    'ShowIndicators' => $this->owner->CarouselShowIndicators,
149
                    'ShowControls' => $this->owner->CarouselShowControls
150
                ]
151
            );
152
    }
153
}
154