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
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private static $title = 'Virtual linked Block'; |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private static $singular_name = 'Virtual linked Block'; |
|
|
|
|
24
|
|
|
|
25
|
|
|
private static $has_one = array( |
|
|
|
|
26
|
|
|
'LinkedElement' => 'BaseElement' |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
public function getTitle() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
return $this->LinkedElement()->getTitle(); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function i18n_singular_name() |
35
|
|
|
{ |
36
|
|
|
return _t(__CLASS__, $this->LinkedElement()->config()->title); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function __construct($record = null, $isSingleton = false, $model = null) { |
40
|
|
|
parent::__construct($record, $isSingleton, $model); |
41
|
|
|
$this->LinkedElement()->setVirtualOwner($this); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getCMSFields() { |
|
|
|
|
45
|
|
|
$message = sprintf('<p>%s</p><p><a href="%2$s">%2$s</a></p>', |
46
|
|
|
_t('ElementVirtualLinked.DESCRIBE', 'This is a virtual copy of a block. To edit, visit'), |
47
|
|
|
$this->LinkedElement()->getEditLink() |
|
|
|
|
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
$fields = new FieldList( |
51
|
|
|
new TabSet('Root', $main = new Tab('Main')) |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
if ($this->isInvalidPublishState()) { |
|
|
|
|
55
|
|
|
$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.'; |
56
|
|
|
$main->push(new LiteralField('WarningHeader', '<p class="message error">' .$warning. '</p>')); |
57
|
|
|
} |
58
|
|
|
$main->push(new LiteralField('Existing', $message)); |
59
|
|
|
|
60
|
|
|
$this->extend('updateCMSFields', $fields); |
61
|
|
|
|
62
|
|
|
return $fields; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
class ElementVirtualLinked_Controller extends BaseElement_Controller { |
|
|
|
|
68
|
|
|
|
69
|
|
|
protected $controllerClass; |
70
|
|
|
|
71
|
|
|
public function __construct($widget) { |
72
|
|
|
parent::__construct($widget); |
73
|
|
|
|
74
|
|
|
$controllerClass = get_class($this->LinkedElement()) . '_Controller'; |
|
|
|
|
75
|
|
|
if(class_exists($controllerClass)) { |
76
|
|
|
$this->controllerClass = $controllerClass; |
77
|
|
|
} else { |
78
|
|
|
$this->controllerClass = 'BaseElement_Controller'; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns the current widget in scope rendered into its' holder |
84
|
|
|
* |
85
|
|
|
* @return HTML |
|
|
|
|
86
|
|
|
*/ |
87
|
|
|
public function WidgetHolder() |
88
|
|
|
{ |
89
|
|
|
return $this->renderWith("ElementHolder_VirtualLinked"); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function __call($method, $arguments) { |
93
|
|
|
try { |
94
|
|
|
$retVal = parent::__call($method, $arguments); |
95
|
|
|
|
96
|
|
|
} catch (Exception $e) { |
97
|
|
|
$controller = new $this->controllerClass($this->LinkedElement()); |
|
|
|
|
98
|
|
|
$retVal = call_user_func_array(array($controller, $method), $arguments); |
99
|
|
|
} |
100
|
|
|
return $retVal; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
View Code Duplication |
public function hasMethod($action) { |
|
|
|
|
104
|
|
|
if(parent::hasMethod($action)) { |
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$controller = new $this->controllerClass($this->LinkedElement()); |
|
|
|
|
109
|
|
|
return $controller->hasMethod($action); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
View Code Duplication |
public function hasAction($action) { |
|
|
|
|
113
|
|
|
if(parent::hasAction($action)) { |
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$controller = new $this->controllerClass($this->LinkedElement()); |
|
|
|
|
118
|
|
|
return $controller->hasAction($action); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
View Code Duplication |
public function checkAccessAction($action) { |
|
|
|
|
122
|
|
|
if(parent::checkAccessAction($action)) { |
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$controller = new $this->controllerClass($this->LinkedElement()); |
|
|
|
|
127
|
|
|
return $controller->checkAccessAction($action); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Detect when a user has published a ElementVirtualLinked block |
132
|
|
|
* but has not published the LinkedElement block. |
133
|
|
|
*/ |
134
|
|
|
public function isInvalidPublishState() { |
135
|
|
|
$block = $this->LinkedElement(); |
|
|
|
|
136
|
|
|
return (!$block->isPublished() && $this->isPublished()); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function getCMSPublishedState() { |
|
|
|
|
140
|
|
|
if ($this->isInvalidPublishState()) { |
141
|
|
|
$colour = '#C00'; |
142
|
|
|
$text = 'Error'; |
143
|
|
|
$html = new HTMLText('PublishedState'); |
144
|
|
|
$html->setValue(sprintf( |
145
|
|
|
'<span style="color: %s;">%s</span>', |
146
|
|
|
$colour, |
147
|
|
|
htmlentities($text) |
148
|
|
|
)); |
149
|
|
|
return $html; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$publishedState = null; |
153
|
|
|
foreach($this->extension_instances as $instance) { |
154
|
|
|
if (method_exists($instance, 'getCMSPublishedState')) { |
155
|
|
|
$instance->setOwner($this); |
156
|
|
|
$publishedState = $instance->getCMSPublishedState(); |
157
|
|
|
$instance->clearOwner(); |
158
|
|
|
break; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
return $publishedState; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
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.