Completed
Push — master ( 169c44...f098b1 )
by
unknown
12s
created

BannerBlock::getSummary()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace SilverStripe\ElementalBlocks\Block;
4
5
use SilverStripe\CMS\Model\SiteTree;
6
use SilverStripe\Core\Convert;
7
use SilverStripe\ElementalBlocks\Form\BlockLinkField;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\HiddenField;
10
use SilverStripe\Forms\HTMLEditor\TinyMCEConfig;
11
use SilverStripe\View\ArrayData;
12
use SilverStripe\View\Requirements;
13
14
class BannerBlock extends FileBlock
15
{
16
    private static $db = [
0 ignored issues
show
Unused Code introduced by
The property $db 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
        'Content' => 'HTMLText',
18
        'CallToActionLink' => 'Link',
19
    ];
20
21
    private static $singular_name = 'banner';
0 ignored issues
show
Unused Code introduced by
The property $singular_name 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...
22
23
    private static $plural_name = 'banners';
0 ignored issues
show
Unused Code introduced by
The property $plural_name 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...
24
25
    public function getType()
26
    {
27
        return _t(__CLASS__ . '.BlockType', 'Banner');
28
    }
29
30
    public function getCMSFields()
31
    {
32
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
33
            // Remove default scaffolded relationship fields
34
            $fields->removeByName('CallToActionLinkID');
35
36
            // Move the file upload field to be before the content
37
            $upload = $fields->fieldByName('Root.Main.File');
38
            $fields->insertBefore('Content', $upload);
39
40
            // Set the height of the content fields
41
            $fields->fieldByName('Root.Main.Content')->setRows(5);
42
        });
43
44
        // Ensure TinyMCE's javascript is loaded before the blocks overrides
45
        Requirements::javascript(TinyMCEConfig::get('cms')->getScriptURL());
46
        Requirements::javascript('silverstripe/elemental-blocks:client/dist/js/bundle.js');
47
        Requirements::css('silverstripe/elemental-blocks:client/dist/styles/bundle.css');
48
49
        return parent::getCMSFields();
50
    }
51
52
    /**
53
     * For the frontend, return a parsed set of data for use in templates
54
     *
55
     * @return ArrayData|null
56
     */
57
    public function CallToActionLink()
58
    {
59
        return $this->decodeLinkData($this->getField('CallToActionLink'));
0 ignored issues
show
Bug introduced by
It seems like $this->getField('CallToActionLink') can also be of type object; however, parameter $linkJson of SilverStripe\ElementalBl...Block::decodeLinkData() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

59
        return $this->decodeLinkData(/** @scrutinizer ignore-type */ $this->getField('CallToActionLink'));
Loading history...
60
    }
61
62
    /**
63
     * Add the banner content instead of the image title
64
     *
65
     * {@inheritDoc}
66
     */
67
    public function getSummary()
68
    {
69
        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

69
        if ($this->/** @scrutinizer ignore-call */ File() && $this->File()->exists()) {
Loading history...
70
            return $this->getSummaryThumbnail() . $this->dbObject('Content')->Summary(20);
71
        }
72
        return '';
73
    }
74
75
    /**
76
     * Given a set of JSON data, decode it, attach the relevant Page object and return as ArrayData
77
     *
78
     * @param string $linkJson
79
     * @return ArrayData|null
80
     */
81
    protected function decodeLinkData($linkJson)
82
    {
83
        if (!$linkJson) {
84
            return;
85
        }
86
87
        $data = ArrayData::create(Convert::json2obj($linkJson));
0 ignored issues
show
Bug introduced by
SilverStripe\Core\Convert::json2obj($linkJson) of type object|boolean is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

87
        $data = ArrayData::create(/** @scrutinizer ignore-type */ Convert::json2obj($linkJson));
Loading history...
88
89
        // Link page, if selected
90
        if ($data->PageID) {
91
            $data->setField('Page', self::get_by_id(SiteTree::class, $data->PageID));
92
        }
93
94
        return $data;
95
    }
96
}
97