1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DNADesign\ElementalVirtual\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ElementalVirtual\Forms\ElementalGridFieldAddExistingAutocompleter; |
|
|
|
|
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'; |
|
|
|
|
25
|
|
|
|
26
|
|
|
private static $has_one = [ |
|
|
|
|
27
|
|
|
'LinkedElement' => BaseElement::class |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private static $description = 'Reused element'; |
|
|
|
|
34
|
|
|
|
35
|
|
|
private static $table_name = 'ElementVirtual'; |
|
|
|
|
36
|
|
|
|
37
|
|
|
private static $singular_name = 'virtual block'; |
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param BaseElement |
41
|
|
|
* @param boolean $isSingleton |
42
|
|
|
* @param DataModel $model |
|
|
|
|
43
|
|
|
*/ |
44
|
|
|
public function __construct($record = null, $isSingleton = false, $model = null) |
45
|
|
|
{ |
46
|
|
|
parent::__construct($record, $isSingleton, $model); |
|
|
|
|
47
|
|
|
|
48
|
|
|
$this->LinkedElement()->setVirtualOwner($this); |
|
|
|
|
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')) |
|
|
|
|
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()); |
|
|
|
|
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'); |
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths