ElementController::Link()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 17
rs 9.6111
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 = [];
0 ignored issues
show
introduced by
The private property $default_styles is not used, and could be removed.
Loading history...
38
39
    /**
40
     * Whether to include default (example) styles
41
     *
42
     * @config
43
     * @var bool
44
     */
45
    private static $include_default_styles = true;
0 ignored issues
show
introduced by
The private property $include_default_styles is not used, and could be removed.
Loading history...
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
        $this->extend('updateForTemplateDefaultStyles', $defaultStyles);
77
78
        if ($this->config()->get('include_default_styles') && !empty($defaultStyles)) {
79
            foreach ($defaultStyles as $stylePath) {
80
                Requirements::css($stylePath);
81
            }
82
        }
83
84
        $template = 'DNADesign\\Elemental\\' . $this->element->config()->get('controller_template');
85
        $this->extend('updateForTemplateTemplate', $template);
86
87
        return $this->renderWith([
88
            'type' => 'Layout',
89
            $template
90
        ]);
91
    }
92
93
    /**
94
     * @param string $action
95
     *
96
     * @return string
97
     */
98
    public function Link($action = null)
99
    {
100
        $page = Director::get_current_page();
101
102
        if ($page && !($page instanceof ElementController)) {
103
            return Controller::join_links(
104
                $page->Link($action),
105
                '#'. $this->element->getAnchor()
106
            );
107
        }
108
109
        $curr = Controller::curr();
110
111
        if ($curr && !($curr instanceof ElementController)) {
112
            return Controller::join_links(
113
                $curr->Link($action),
114
                '#'. $this->element->getAnchor()
115
            );
116
        }
117
    }
118
}
119