Passed
Push — master ( 9f4322...b2af24 )
by Will
01:32
created

ElementVirtual::getTitle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace DNADesign\ElementalVirtual\Model;
4
5
use TractorCow\AutoComplete\AutoCompleteField;
6
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...
7
use DNADesign\Elemental\Models\BaseElement;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\LiteralField;
10
use SilverStripe\Forms\Tab;
11
use SilverStripe\Forms\TabSet;
12
use SilverStripe\ORM\FieldType\DBField;
13
use SilverStripe\ORM\FieldType\DBHTMLText;
14
15
/**
16
 * Virtual Linked Element.
17
 *
18
 * As elemental is based on a natural has_one relation to an object,
19
 * this allows the same element to be linked to multiple pages.
20
 *
21
 * {@see ElementalGridFieldAddExistingAutocompleter}
22
 */
23
class ElementVirtual extends BaseElement
24
{
25
    private static $icon = 'element-icon-virtual';
0 ignored issues
show
introduced by
The private property $icon is not used, and could be removed.
Loading history...
26
27
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
28
        'LinkedElement' => BaseElement::class
29
    ];
30
31
    /**
32
     * @var string
33
     */
34
    private static $description = 'Reused element';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
35
36
    private static $table_name = 'ElementVirtual';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
37
38
    private static $singular_name = 'virtual block';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
39
40
    /**
41
     * @param BaseElement
42
     * @param boolean $isSingleton
43
     * @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...
44
     */
45
    public function __construct($record = null, $isSingleton = false, $model = null)
46
    {
47
        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

47
        parent::__construct($record, $isSingleton, /** @scrutinizer ignore-type */ $model);
Loading history...
48
49
        $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

49
        $this->/** @scrutinizer ignore-call */ 
50
               LinkedElement()->setVirtualOwner($this);
Loading history...
50
    }
51
52
    public function getCMSFields()
53
    {
54
        $invalid = $this->isInvalidPublishState();
55
56
        $this->beforeUpdateCMSFields(function (FieldList $fields) use ($invalid) {
57
            $fields->removeByName('Title');
58
59
            $message = sprintf(
60
                '<p>%s</p><p><a href="%2$s" target="_blank">Click here to edit the original</a></p>',
61
                _t(__CLASS__ . '.VirtualDescription', 'This is a virtual copy of an element.'),
62
                $this->LinkedElement()->getEditLink()
63
            );
64
65
            if ($invalid) {
66
                $warning = _t(
67
                    __CLASS__ . '.InvalidPublishStateWarning',
68
                    '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.'
69
                );
70
71
                $fields->addFieldToTab('Root.Main', LiteralField::create('WarningHeader', '<p class="message error">' . $warning . '</p>'));
72
            }
73
74
            $autocomplete = AutoCompleteField::create(
75
                'LinkedElementID',
76
                _t(__CLASS__ . '.LinkedElement', 'Linked Element'),
77
                '',
78
                BaseElement::class,
79
                'Title'
80
            );
81
82
            $autocomplete->setLabelField('VirtualLinkedSummary');
83
84
            $fields->replaceField(
85
                'LinkedElementID',
86
                $autocomplete
87
            );
88
            $fields->addFieldToTab('Root.Main', LiteralField::create('Existing', $message));
89
        });
90
91
        return parent::getCMSFields();
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getType()
98
    {
99
        return sprintf(
100
            _t(__CLASS__ . '.BlockType', 'Virtual Block')
101
        );
102
    }
103
104
    /**
105
     * Detect when a user has published a linked element but has not published
106
     * the LinkedElement.
107
     *
108
     * @return boolean
109
     */
110
    public function isInvalidPublishState()
111
    {
112
        $element = $this->LinkedElement();
113
114
        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

114
        return (!$element->isPublished() && $this->/** @scrutinizer ignore-call */ isPublished());
Loading history...
115
    }
116
117
    /**
118
     * Get a unique anchor name.
119
     *
120
     * @return string
121
     */
122
    public function getAnchor()
123
    {
124
        $linkedElement = $this->LinkedElement();
125
126
        if ($linkedElement && $linkedElement->exists()) {
127
            return $linkedElement->getAnchor();
128
        }
129
130
        return 'e' . $this->ID;
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    public function getSummary()
137
    {
138
        if ($linked = $this->LinkedElement()) {
139
            return $linked->getSummary();
140
        }
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    public function getTitle()
147
    {
148
        if ($linked = $this->LinkedElement()) {
149
            return $linked->Title;
150
        }
151
    }
152
153
    /**
154
     * Override to render template based on LinkedElement
155
     *
156
     * @return string|null HTML
157
     */
158
    public function forTemplate($holder = true)
159
    {
160
        if ($linked = $this->LinkedElement()) {
161
            return $linked->forTemplate($holder);
162
        }
163
        return null;
164
    }
165
166
    /**
167
     *
168
     */
169
    public function inlineEditable()
170
    {
171
        return false;
172
    }
173
174
    protected function provideBlockSchema()
175
    {
176
        $blockSchema = parent::provideBlockSchema();
177
        $blockSchema['content'] = $this->getSummary();
178
        return $blockSchema;
179
    }
180
}
181