Passed
Pull Request — master (#49)
by
unknown
03:10
created

ElementVirtual::LinkedElementRelation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
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 = '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 = true;
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
            $availableBlocks = BaseElement::get()->filter('AvailableGlobally', 1)->exclude('ClassName', get_class($this));
71
            $fields->replaceField(
72
                'LinkedElementID',
73
                TagField::create("LinkedElementRelation", $this->fieldLabel('LinkedElement'), $availableBlocks)
0 ignored issues
show
Bug introduced by
The type DNADesign\ElementalVirtual\Model\TagField 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...
74
                    // Bug: TagField (react) setIsMultiple results in empty (https://github.com/silverstripe/silverstripe-tagfield/issues/195)
75
    //                ->setIsMultiple(false)
76
                    ->setCanCreate(false)
77
            );
78
            
79
            if($this->LinkedElementID){
80
                $message = sprintf(
81
                    '<p>%s</p><p><a href="%2$s" target="_blank">Click here to edit the original</a></p>',
82
                    _t(__CLASS__ . '.VirtualDescription', 'This is a virtual copy of an element.'),
83
                    $this->LinkedElement()->getEditLink()
84
                );
85
                $fields->addFieldToTab('Root.Main', LiteralField::create('Existing', $message));
86
            }
87
        });
88
89
        return parent::getCMSFields();
90
    }
91
92
    /**
93
     * Create an intermediary UnsavedRelationList to have TagField save the LinkedElement into
94
     * @return UnsavedRelationList
95
     */
96
    public function LinkedElementRelation()
97
    {
98
        $this->LinkedElementRelation = UnsavedRelationList::create(
0 ignored issues
show
Bug Best Practice introduced by
The property LinkedElementRelation does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
The type DNADesign\ElementalVirtu...del\UnsavedRelationList 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...
99
            get_class($this),
100
            'LinkedElementRelation',
101
            BaseElement::class
102
        );
103
        $this->LinkedElementRelation->add($this->LinkedElementID);
104
        return $this->LinkedElementRelation;
105
    }
106
107
    /**
108
     * Transfer LinkedElement from UnsavedRelationList to has_one LinkedElementID
109
     */
110
    public function onBeforeWrite()
111
    {
112
        if($this->LinkedElementRelation && $this->LinkedElementRelation->first()){
113
            $this->LinkedElementID = $this->LinkedElementRelation->first()->ID;
0 ignored issues
show
Bug Best Practice introduced by
The property LinkedElementID does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
114
        }
115
        
116
        return parent::onBeforeWrite();
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::onBeforeWrite() targeting DNADesign\Elemental\Mode...lement::onBeforeWrite() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
117
    }
118
119
    /**
120
     * @return string
121
     */
122
    public function getType()
123
    {
124
        return sprintf(
125
            _t(__CLASS__ . '.BlockType', 'Virtual Block')
126
        );
127
    }
128
129
    /**
130
     * Detect when a user has published a linked element but has not published
131
     * the LinkedElement.
132
     *
133
     * @return boolean
134
     */
135
    public function isInvalidPublishState()
136
    {
137
        $element = $this->LinkedElement();
138
139
        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

139
        return (!$element->isPublished() && $this->/** @scrutinizer ignore-call */ isPublished());
Loading history...
140
    }
141
142
    /**
143
     * Get a unique anchor name.
144
     *
145
     * @return string
146
     */
147
    public function getAnchor()
148
    {
149
        $linkedElement = $this->LinkedElement();
150
151
        if ($linkedElement && $linkedElement->exists()) {
152
            return $linkedElement->getAnchor();
153
        }
154
155
        return 'e' . $this->ID;
156
    }
157
158
    /**
159
     * @return string
160
     */
161
    public function getSummary()
162
    {
163
        if ($linked = $this->LinkedElement()) {
164
            return $linked->getSummary();
165
        }
166
    }
167
168
    /**
169
     * @return string
170
     */
171
    public function getTitle()
172
    {
173
        if ($linked = $this->LinkedElement()) {
174
            return $linked->Title;
175
        }
176
    }
177
178
    /**
179
     * Override to render template based on LinkedElement
180
     *
181
     * @return string|null HTML
182
     */
183
    public function forTemplate($holder = true)
184
    {
185
        if ($linked = $this->LinkedElement()) {
186
            return $linked->forTemplate($holder);
187
        }
188
        return null;
189
    }
190
191
    protected function provideBlockSchema()
192
    {
193
        $blockSchema = parent::provideBlockSchema();
194
        $blockSchema['content'] = $this->getSummary();
195
        return $blockSchema;
196
    }
197
}
198