BannerBlock::getType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

71
        if ($this->/** @scrutinizer ignore-call */ File() && $this->File()->exists()) {
Loading history...
72
            return $this->getSummaryThumbnail() . $this->dbObject('Content')->Summary(20);
73
        }
74
        return '';
75
    }
76
77
    /**
78
     * Given a set of JSON data, decode it, attach the relevant Page object and return as ArrayData
79
     *
80
     * @param string $linkJson
81
     * @return ArrayData|null
82
     */
83
    protected function decodeLinkData($linkJson)
84
    {
85
        if (!$linkJson || $linkJson === 'null') {
86
            return;
87
        }
88
89
        $data = ArrayData::create(Convert::json2obj($linkJson));
90
91
        // Link page, if selected
92
        if ($data->PageID) {
93
            $data->setField('Page', self::get_by_id(SiteTree::class, $data->PageID));
94
        }
95
96
        return $data;
97
    }
98
}
99