Completed
Push — master ( aa5488...8fd3ef )
by Will
13s
created

ElementVirtual::isInvalidPublishState()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace DNADesign\ElementalVirtual\Model;
4
5
use SilverStripe\ElementalVirtual\Forms\ElementalGridFieldAddExistingAutocompleter;
0 ignored issues
show
Bug introduced by
The type SilverStripe\ElementalVi...ddExistingAutocompleter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use DNADesign\Elemental\Models\BaseElement;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\LiteralField;
9
use SilverStripe\Forms\Tab;
10
use SilverStripe\Forms\TabSet;
11
use SilverStripe\ORM\FieldType\DBField;
12
use SilverStripe\ORM\FieldType\DBHTMLText;
13
14
/**
15
 * Virtual Linked Element.
16
 *
17
 * As elemental is based on a natural has_one relation to an object,
18
 * this allows the same element to be linked to multiple pages.
19
 *
20
 * {@see ElementalGridFieldAddExistingAutocompleter}
21
 */
22
class ElementVirtual extends BaseElement
23
{
24
    private static $icon = 'dnadesign/silverstripe-elemental-virtual:images/virtual.svg';
0 ignored issues
show
Unused Code introduced by
The property $icon 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...
25
26
    private static $has_one = [
0 ignored issues
show
Unused Code introduced by
The property $has_one 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...
27
        'LinkedElement' => BaseElement::class
28
    ];
29
30
    /**
31
     * @var string
32
     */
33
    private static $description = 'Reused element';
0 ignored issues
show
Unused Code introduced by
The property $description 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...
34
35
    private static $table_name = 'ElementVirtual';
0 ignored issues
show
Unused Code introduced by
The property $table_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...
36
37
    private static $singular_name = 'virtual block';
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...
38
39
    /**
40
     * @param BaseElement
41
     * @param boolean $isSingleton
42
     * @param DataModel $model
0 ignored issues
show
Bug introduced by
The type DNADesign\ElementalVirtual\Model\DataModel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
     */
44
    public function __construct($record = null, $isSingleton = false, $model = null)
45
    {
46
        parent::__construct($record, $isSingleton, $model);
0 ignored issues
show
Bug introduced by
It seems like $model can also be of type DNADesign\ElementalVirtual\Model\DataModel; however, parameter $queryParams of SilverStripe\ORM\DataObject::__construct() does only seem to accept array, 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

46
        parent::__construct($record, $isSingleton, /** @scrutinizer ignore-type */ $model);
Loading history...
47
48
        $this->LinkedElement()->setVirtualOwner($this);
0 ignored issues
show
Bug introduced by
The method LinkedElement() does not exist on DNADesign\ElementalVirtual\Model\ElementVirtual. 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

48
        $this->/** @scrutinizer ignore-call */ 
49
               LinkedElement()->setVirtualOwner($this);
Loading history...
49
    }
50
51
    /**
52
     * Block should not appear in the create list
53
     *
54
     */
55
    public function canCreateElemental()
56
    {
57
        return false;
58
    }
59
60
    public function getCMSFields()
61
    {
62
        $message = sprintf(
63
            '<p>%s</p><p><a href="%2$s">Click here to edit the original</a></p>',
64
            _t(__CLASS__ . '.VirtualDescription', 'This is a virtual copy of an element.'),
65
            $this->LinkedElement()->getEditLink()
66
        );
67
68
        $fields = FieldList::create(
69
            TabSet::create('Root', $main = Tab::create('Main'))
0 ignored issues
show
Bug introduced by
'Main' of type string 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

69
            TabSet::create('Root', $main = Tab::create(/** @scrutinizer ignore-type */ 'Main'))
Loading history...
Bug introduced by
It seems like $main = SilverStripe\Forms\Tab::create('Main') can also be of type object; however, parameter $args of SilverStripe\View\ViewableData::create() does only seem to accept array, 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

69
            TabSet::create('Root', /** @scrutinizer ignore-type */ $main = Tab::create('Main'))
Loading history...
70
        );
71
72
        if ($this->isInvalidPublishState()) {
73
            $warning = _t(
74
                __CLASS__ . '.InvalidPublishStateWarning',
75
                'Error: The original element is not published. This element will not work on the live site until you click the link below and publish it.'
76
            );
77
            $main->push(LiteralField::create('WarningHeader', '<p class="message error">' . $warning . '</p>'));
78
        }
79
        $main->push(LiteralField::create('Existing', $message));
80
81
        $this->extend('updateCMSFields', $fields);
82
83
        return $fields;
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getType()
90
    {
91
        return sprintf(
92
            "%s (%s)",
93
            $this->LinkedElement()->getType(),
94
            _t(__CLASS__ . '.BlockType', 'Virtual')
95
        );
96
    }
97
98
    /**
99
     * Detect when a user has published a linked element but has not published
100
     * the LinkedElement.
101
     *
102
     * @return boolean
103
     */
104
    public function isInvalidPublishState()
105
    {
106
        $element = $this->LinkedElement();
107
108
        return (!$element->isPublished() && $this->isPublished());
0 ignored issues
show
Bug introduced by
The method isPublished() does not exist on DNADesign\ElementalVirtual\Model\ElementVirtual. 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

108
        return (!$element->isPublished() && $this->/** @scrutinizer ignore-call */ isPublished());
Loading history...
109
    }
110
111
    public function getCMSPublishedState()
112
    {
113
        if ($this->isInvalidPublishState()) {
114
            $colour = '#C00';
115
            $text = _t(__CLASS__ . '.InvalidPublishStateError', 'Error');
116
            $html = DBHTMLText::create('PublishedState');
0 ignored issues
show
Bug introduced by
'PublishedState' of type string 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

116
            $html = DBHTMLText::create(/** @scrutinizer ignore-type */ 'PublishedState');
Loading history...
117
            $html->setValue(sprintf(
118
                '<span style="color: %s;">%s</span>',
119
                $colour,
120
                htmlentities($text)
121
            ));
122
            return $html;
123
        }
124
125
        $publishedState = null;
126
127
        foreach ($this->getExtensionInstances() as $instance) {
128
            if (method_exists($instance, 'getCMSPublishedState')) {
129
                $instance->setOwner($this);
130
                $publishedState = $instance->getCMSPublishedState();
131
                $instance->clearOwner();
132
                break;
133
            }
134
        }
135
136
        return $publishedState;
137
    }
138
139
140
    /**
141
     * Get a unique anchor name.
142
     *
143
     * @return string
144
     */
145
    public function getAnchor()
146
    {
147
        $linkedElement = $this->LinkedElement();
148
149
        if ($linkedElement && $linkedElement->exists()) {
150
            return $linkedElement->getAnchor();
151
        }
152
153
        return 'e' . $this->ID;
154
    }
155
156
    /**
157
     * @return string
158
     */
159
    public function getSummary()
160
    {
161
        if ($linked = $this->LinkedElement()) {
162
            return $linked->getSummary();
163
        }
164
    }
165
}
166