ElementVirtual   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Importance

Changes 10
Bugs 2 Features 1
Metric Value
wmc 17
eloc 55
c 10
b 2
f 1
dl 0
loc 154
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getAnchor() 0 9 3
A getTitle() 0 4 2
A getCMSFields() 0 44 3
A getType() 0 4 1
A getSummary() 0 4 2
A forTemplate() 0 6 2
A isInvalidPublishState() 0 5 2
A provideBlockSchema() 0 5 1
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 = 'font-icon-block-link';
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
    private static $inline_editable = false;
0 ignored issues
show
introduced by
The private property $inline_editable is not used, and could be removed.
Loading history...
41
42
    /**
43
     * @param BaseElement
44
     * @param boolean $isSingleton
45
     * @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...
46
     */
47
    public function __construct($record = null, $isSingleton = false, $model = null)
48
    {
49
        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

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

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

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