1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DNADesign\Elemental\Controllers; |
4
|
|
|
|
5
|
|
|
use DNADesign\Elemental\Models\BaseElement; |
6
|
|
|
use SilverStripe\Control\Controller; |
7
|
|
|
use SilverStripe\Control\Director; |
8
|
|
|
use SilverStripe\View\Requirements; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Optional controller for every element which has its own logic, e.g. in forms. |
12
|
|
|
* |
13
|
|
|
* It always handles a single element, usually passed in as a database |
14
|
|
|
* identifier through the controller URL. Needs to be constructed as a nested |
15
|
|
|
* controller within a {@link ContentController}. |
16
|
|
|
* |
17
|
|
|
* ## Forms |
18
|
|
|
* You can add forms like in any other SilverStripe controller. If you need |
19
|
|
|
* access to the element from within a form, you can use |
20
|
|
|
* `$this->controller->getElement()` inside the form logic. |
21
|
|
|
* |
22
|
|
|
* @package Elemental |
23
|
|
|
*/ |
24
|
|
|
class ElementController extends Controller |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var BaseElement $element |
28
|
|
|
*/ |
29
|
|
|
protected $element; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* A list of default (example) styles to include |
33
|
|
|
* |
34
|
|
|
* @config |
35
|
|
|
* @var string[] |
36
|
|
|
*/ |
37
|
|
|
private static $default_styles = []; |
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Whether to include default (example) styles |
41
|
|
|
* |
42
|
|
|
* @config |
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
private static $include_default_styles = true; |
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param BaseElement $element |
49
|
|
|
*/ |
50
|
|
|
public function __construct(BaseElement $element) |
51
|
|
|
{ |
52
|
|
|
$this->element = $element; |
53
|
|
|
|
54
|
|
|
parent::__construct(); |
55
|
|
|
|
56
|
|
|
$this->setFailover($this->element); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return BaseElement |
61
|
|
|
*/ |
62
|
|
|
public function getElement() |
63
|
|
|
{ |
64
|
|
|
return $this->element; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Renders the managed {@link BaseElement} wrapped with the current |
69
|
|
|
* {@link ElementController}. |
70
|
|
|
* |
71
|
|
|
* @return string HTML |
72
|
|
|
*/ |
73
|
|
|
public function forTemplate() |
74
|
|
|
{ |
75
|
|
|
$defaultStyles = $this->config()->get('default_styles'); |
76
|
|
|
if ($this->config()->get('include_default_styles') && !empty($defaultStyles)) { |
77
|
|
|
foreach ($defaultStyles as $stylePath) { |
78
|
|
|
Requirements::css($stylePath); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$template = $this->element->config()->get('controller_template'); |
83
|
|
|
|
84
|
|
|
return $this->renderWith([ |
85
|
|
|
'type' => 'Layout', |
86
|
|
|
'DNADesign\\Elemental\\'.$template |
87
|
|
|
]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $action |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function Link($action = null) |
96
|
|
|
{ |
97
|
|
|
$page = Director::get_current_page(); |
98
|
|
|
|
99
|
|
|
if ($page && !($page instanceof ElementController)) { |
100
|
|
|
return Controller::join_links( |
101
|
|
|
$page->Link($action), |
102
|
|
|
'#'. $this->element->getAnchor() |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$curr = Controller::curr(); |
107
|
|
|
|
108
|
|
|
if ($curr && !($curr instanceof ElementController)) { |
109
|
|
|
return Controller::join_links( |
110
|
|
|
$curr->Link($action), |
111
|
|
|
'#'. $this->element->getAnchor() |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|