|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Virtual Linked Element. |
|
5
|
|
|
* |
|
6
|
|
|
* As elemental is based on widgets which have a natural has_one relation to an |
|
7
|
|
|
* object, this is a workaround for allowing the same element to be linked to |
|
8
|
|
|
* multiple pages. |
|
9
|
|
|
* |
|
10
|
|
|
* {@see ElementalGridFieldAddExistingAutocompleter} |
|
11
|
|
|
* |
|
12
|
|
|
* @package elemental |
|
13
|
|
|
*/ |
|
14
|
|
|
class ElementVirtualLinked extends BaseElement |
|
|
|
|
|
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private static $title = 'Virtual linked Block'; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private static $singular_name = 'Virtual linked Block'; |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
private static $has_one = array( |
|
|
|
|
|
|
27
|
|
|
'LinkedElement' => 'BaseElement' |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
public function getTitle() |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
return $this->LinkedElement()->getTitle(); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function i18n_singular_name() |
|
36
|
|
|
{ |
|
37
|
|
|
return _t(__CLASS__, $this->LinkedElement()->config()->title); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function __construct($record = null, $isSingleton = false, $model = null) |
|
41
|
|
|
{ |
|
42
|
|
|
parent::__construct($record, $isSingleton, $model); |
|
43
|
|
|
$this->LinkedElement()->setVirtualOwner($this); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getCMSFields() |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
$message = sprintf( |
|
49
|
|
|
'<p>%s</p><p><a href="%2$s">%2$s</a></p>', |
|
50
|
|
|
_t('ElementVirtualLinked.DESCRIBE', 'This is a virtual copy of a block. To edit, visit'), |
|
51
|
|
|
$this->LinkedElement()->getEditLink() |
|
|
|
|
|
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
$fields = new FieldList( |
|
55
|
|
|
new TabSet('Root', $main = new Tab('Main')) |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
if ($this->isInvalidPublishState()) { |
|
59
|
|
|
$warning = 'Error: The original block is not published. This block will not work on the live site until you click the link below and publish it.'; |
|
60
|
|
|
$main->push(new LiteralField('WarningHeader', '<p class="message error">' .$warning. '</p>')); |
|
61
|
|
|
} |
|
62
|
|
|
$main->push(new LiteralField('Existing', $message)); |
|
63
|
|
|
|
|
64
|
|
|
$this->extend('updateCMSFields', $fields); |
|
65
|
|
|
|
|
66
|
|
|
return $fields; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Detect when a user has published a ElementVirtualLinked block |
|
72
|
|
|
* but has not published the LinkedElement block. |
|
73
|
|
|
*/ |
|
74
|
|
|
public function isInvalidPublishState() |
|
75
|
|
|
{ |
|
76
|
|
|
$block = $this->LinkedElement(); |
|
|
|
|
|
|
77
|
|
|
return (!$block->isPublished() && $this->isPublished()); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getCMSPublishedState() |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
if ($this->isInvalidPublishState()) { |
|
83
|
|
|
$colour = '#C00'; |
|
84
|
|
|
$text = 'Error'; |
|
85
|
|
|
$html = new HTMLText('PublishedState'); |
|
86
|
|
|
$html->setValue(sprintf( |
|
87
|
|
|
'<span style="color: %s;">%s</span>', |
|
88
|
|
|
$colour, |
|
89
|
|
|
htmlentities($text) |
|
90
|
|
|
)); |
|
91
|
|
|
return $html; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$publishedState = null; |
|
95
|
|
|
foreach ($this->extension_instances as $instance) { |
|
96
|
|
|
if (method_exists($instance, 'getCMSPublishedState')) { |
|
97
|
|
|
$instance->setOwner($this); |
|
98
|
|
|
$publishedState = $instance->getCMSPublishedState(); |
|
99
|
|
|
$instance->clearOwner(); |
|
100
|
|
|
break; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
return $publishedState; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
class ElementVirtualLinked_Controller extends BaseElement_Controller |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
|
|
|
|
110
|
|
|
protected $controllerClass; |
|
111
|
|
|
|
|
112
|
|
|
public function __construct($widget) |
|
113
|
|
|
{ |
|
114
|
|
|
parent::__construct($widget); |
|
115
|
|
|
|
|
116
|
|
|
$controllerClass = get_class($this->LinkedElement()) . '_Controller'; |
|
|
|
|
|
|
117
|
|
|
if (class_exists($controllerClass)) { |
|
118
|
|
|
$this->controllerClass = $controllerClass; |
|
119
|
|
|
} else { |
|
120
|
|
|
$this->controllerClass = 'BaseElement_Controller'; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Returns the current widget in scope rendered into its' holder |
|
126
|
|
|
* |
|
127
|
|
|
* @return HTML |
|
|
|
|
|
|
128
|
|
|
*/ |
|
129
|
|
|
public function WidgetHolder() |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->renderWith("ElementHolder_VirtualLinked"); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function __call($method, $arguments) |
|
135
|
|
|
{ |
|
136
|
|
|
try { |
|
137
|
|
|
$retVal = parent::__call($method, $arguments); |
|
138
|
|
|
} catch (Exception $e) { |
|
139
|
|
|
$controller = new $this->controllerClass($this->LinkedElement()); |
|
|
|
|
|
|
140
|
|
|
$retVal = call_user_func_array(array($controller, $method), $arguments); |
|
141
|
|
|
} |
|
142
|
|
|
return $retVal; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
View Code Duplication |
public function hasMethod($action) |
|
|
|
|
|
|
146
|
|
|
{ |
|
147
|
|
|
if (parent::hasMethod($action)) { |
|
148
|
|
|
return true; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$controller = new $this->controllerClass($this->LinkedElement()); |
|
|
|
|
|
|
152
|
|
|
return $controller->hasMethod($action); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
View Code Duplication |
public function hasAction($action) |
|
|
|
|
|
|
156
|
|
|
{ |
|
157
|
|
|
if (parent::hasAction($action)) { |
|
158
|
|
|
return true; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$controller = new $this->controllerClass($this->LinkedElement()); |
|
|
|
|
|
|
162
|
|
|
return $controller->hasAction($action); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
View Code Duplication |
public function checkAccessAction($action) |
|
|
|
|
|
|
166
|
|
|
{ |
|
167
|
|
|
if (parent::checkAccessAction($action)) { |
|
168
|
|
|
return true; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$controller = new $this->controllerClass($this->LinkedElement()); |
|
|
|
|
|
|
172
|
|
|
return $controller->checkAccessAction($action); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.