Completed
Push — master ( e3d8d6...bf696b )
by Robbie
9s
created

BannerBlock   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 3 1
A CallToActionLink() 0 3 1
A getSummary() 0 6 3
A getCMSFields() 0 20 1
A decodeLinkData() 0 14 4
A provideBlockSchema() 0 5 1
1
<?php
2
3
namespace SilverStripe\ElementalBannerBlock\Block;
4
5
use SilverStripe\CMS\Model\SiteTree;
6
use SilverStripe\Core\Convert;
7
use SilverStripe\ElementalFileBlock\Block\FileBlock;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\HTMLEditor\TinyMCEConfig;
10
use SilverStripe\View\ArrayData;
11
use SilverStripe\View\Requirements;
12
13
class BannerBlock extends FileBlock
14
{
15
    private static $icon = 'font-icon-block-banner';
0 ignored issues
show
introduced by
The private property $icon is not used, and could be removed.
Loading history...
16
17
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
18
        'Content' => 'HTMLText',
19
        'CallToActionLink' => 'Link',
20
    ];
21
22
    private static $singular_name = 'banner';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
23
24
    private static $plural_name = 'banners';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
25
26
    private static $table_name = 'S_EB_BannerBlock';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
27
28
    public function getType()
29
    {
30
        return _t(__CLASS__ . '.BlockType', 'Banner');
31
    }
32
33
    public function getCMSFields()
34
    {
35
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
36
            // Remove default scaffolded relationship fields
37
            $fields->removeByName('CallToActionLinkID');
38
39
            // Move the file upload field to be before the content
40
            $upload = $fields->fieldByName('Root.Main.File');
41
            $fields->insertBefore('Content', $upload);
42
43
            // Set the height of the content fields
44
            $fields->fieldByName('Root.Main.Content')->setRows(5);
45
        });
46
47
        // Ensure TinyMCE's javascript is loaded before the blocks overrides
48
        Requirements::javascript(TinyMCEConfig::get()->getScriptURL());
49
        Requirements::javascript('silverstripe/elemental-bannerblock:client/dist/js/bundle.js');
50
        Requirements::css('silverstripe/elemental-bannerblock:client/dist/styles/bundle.css');
51
52
        return parent::getCMSFields();
53
    }
54
55
    /**
56
     * For the frontend, return a parsed set of data for use in templates
57
     *
58
     * @return ArrayData|null
59
     */
60
    public function CallToActionLink()
61
    {
62
        return $this->decodeLinkData($this->getField('CallToActionLink'));
63
    }
64
65
    /**
66
     * Add the banner content instead of the image title
67
     *
68
     * {@inheritDoc}
69
     */
70
    public function getSummary()
71
    {
72
        if ($this->File() && $this->File()->exists()) {
0 ignored issues
show
Bug introduced by
The method File() does not exist on SilverStripe\ElementalBa...Block\Block\BannerBlock. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

72
        if ($this->/** @scrutinizer ignore-call */ File() && $this->File()->exists()) {
Loading history...
73
            return $this->getSummaryThumbnail() . $this->dbObject('Content')->Summary(20);
74
        }
75
        return '';
76
    }
77
78
    /**
79
     * Return content summary for summary section of ElementEditor
80
     *
81
     * @return array
82
     */
83
    protected function provideBlockSchema()
84
    {
85
        $blockSchema = parent::provideBlockSchema();
0 ignored issues
show
introduced by
The method provideBlockSchema() does not exist on SilverStripe\ElementalFileBlock\Block\FileBlock. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

85
        /** @scrutinizer ignore-call */ 
86
        $blockSchema = parent::provideBlockSchema();
Loading history...
86
        $blockSchema['content'] = $this->dbObject('Content')->Summary(20);
87
        return $blockSchema;
88
    }
89
90
    /**
91
     * Given a set of JSON data, decode it, attach the relevant Page object and return as ArrayData
92
     *
93
     * @param string $linkJson
94
     * @return ArrayData|null
95
     */
96
    protected function decodeLinkData($linkJson)
97
    {
98
        if (!$linkJson || $linkJson === 'null') {
99
            return;
100
        }
101
102
        $data = ArrayData::create(Convert::json2obj($linkJson));
103
104
        // Link page, if selected
105
        if ($data->PageID) {
106
            $data->setField('Page', self::get_by_id(SiteTree::class, $data->PageID));
107
        }
108
109
        return $data;
110
    }
111
}
112