|
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
|
|
|
$main->push( |
|
55
|
|
|
new LiteralField('Existing', $message) |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
$this->extend('updateCMSFields', $fields); |
|
59
|
|
|
|
|
60
|
|
|
return $fields; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
class ElementVirtualLinked_Controller extends BaseElement_Controller { |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
protected $controllerClass; |
|
68
|
|
|
|
|
69
|
|
|
public function __construct($widget) { |
|
70
|
|
|
parent::__construct($widget); |
|
71
|
|
|
|
|
72
|
|
|
$controllerClass = get_class($this->LinkedElement()) . '_Controller'; |
|
|
|
|
|
|
73
|
|
|
if(class_exists($controllerClass)) { |
|
74
|
|
|
$this->controllerClass = $controllerClass; |
|
75
|
|
|
} else { |
|
76
|
|
|
$this->controllerClass = 'BaseElement_Controller'; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns the current widget in scope rendered into its' holder |
|
82
|
|
|
* |
|
83
|
|
|
* @return HTML |
|
|
|
|
|
|
84
|
|
|
*/ |
|
85
|
|
|
public function WidgetHolder() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->renderWith("ElementHolder_VirtualLinked"); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function __call($method, $arguments) { |
|
91
|
|
|
try { |
|
92
|
|
|
$retVal = parent::__call($method, $arguments); |
|
93
|
|
|
|
|
94
|
|
|
} catch (Exception $e) { |
|
95
|
|
|
$controller = new $this->controllerClass($this->LinkedElement()); |
|
|
|
|
|
|
96
|
|
|
$retVal = call_user_func_array(array($controller, $method), $arguments); |
|
97
|
|
|
} |
|
98
|
|
|
return $retVal; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
View Code Duplication |
public function hasMethod($action) { |
|
|
|
|
|
|
102
|
|
|
if(parent::hasMethod($action)) { |
|
103
|
|
|
return true; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$controller = new $this->controllerClass($this->LinkedElement()); |
|
|
|
|
|
|
107
|
|
|
return $controller->hasMethod($action); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
View Code Duplication |
public function hasAction($action) { |
|
|
|
|
|
|
111
|
|
|
if(parent::hasAction($action)) { |
|
112
|
|
|
return true; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$controller = new $this->controllerClass($this->LinkedElement()); |
|
|
|
|
|
|
116
|
|
|
return $controller->hasAction($action); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
View Code Duplication |
public function checkAccessAction($action) { |
|
|
|
|
|
|
120
|
|
|
if(parent::checkAccessAction($action)) { |
|
121
|
|
|
return true; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$controller = new $this->controllerClass($this->LinkedElement()); |
|
|
|
|
|
|
125
|
|
|
return $controller->checkAccessAction($action); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
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.