Completed
Pull Request — master (#143)
by Robbie
10:48
created

WidgetController::WidgetHolder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\Widgets\Controllers;
4
5
use SilverStripe\Admin\LeftAndMain;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Core\ClassInfo;
9
use SilverStripe\i18n\i18n;
10
use SilverStripe\Security\Member;
11
use SilverStripe\Widgets\Model\Widget;
12
13
/**
14
 * Optional controller for every widget which has its own logic, e.g. in forms.
15
 *
16
 * It always handles a single widget, usually passed in as a database
17
 * identifier through the controller URL. Needs to be constructed as a nested
18
 * controller within a {@link ContentController}.
19
 *
20
 * ## Forms
21
 * You can add forms like in any other SilverStripe controller. If you need
22
 * access to the widget from within a form, you can use
23
 * `$this->controller->getWidget()` inside the form logic.
24
 *
25
 * Note: Widget controllers currently only work on {@link Page} objects,
26
 * because the logic is implemented in {@link ContentController->handleWidget()}.
27
 * Copy this logic and the URL rules to enable it for other controllers.
28
 *
29
 * @package widgets
30
 */
31
class WidgetController extends Controller
32
{
33
    /**
34
     * @var Widget
35
     */
36
    protected $widget;
37
38
    /**
39
     * @var array
40
     */
41
    private static $allowed_actions = array(
0 ignored issues
show
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
42
        'editablesegment'
43
    );
44
45
    /**
46
     * @param Widget $widget
47
     */
48
    public function __construct($widget = null)
49
    {
50
        if ($widget) {
51
            $this->widget = $widget;
52
            $this->failover = $widget;
53
        }
54
55
        parent::__construct();
56
    }
57
58
    /**
59
     * @param string $action
60
     * @return string
61
     */
62
    public function Link($action = null)
63
    {
64
        $id = ($this->widget) ? $this->widget->ID : null;
65
        $segment = Controller::join_links('widget', $id, $action);
66
67
        if ($page = Director::get_current_page()) {
68
            return $page->Link($segment);
69
        }
70
71
        return Controller::curr()->Link($segment);
72
    }
73
74
    /**
75
     * @return Widget
76
     */
77
    public function getWidget()
78
    {
79
        return $this->widget;
80
    }
81
82
    /**
83
     * Overloaded from {@link Widget->Content()} to allow for controller / form
84
     * linking.
85
     *
86
     * @return string HTML
87
     */
88
    public function Content()
89
    {
90
        return $this->renderWith(array_reverse(ClassInfo::ancestry($this->widget->class)));
91
    }
92
93
    /**
94
     * Overloaded from {@link Widget->WidgetHolder()} to allow for controller/
95
     * form linking.
96
     *
97
     * @return string HTML
98
     */
99
    public function WidgetHolder()
100
    {
101
        return $this->renderWith("WidgetHolder");
102
    }
103
104
    /**
105
     * Uses the `WidgetEditor.ss` template and {@link Widget->editablesegment()}
106
     * to render a administrator-view of the widget. It is assumed that this
107
     * view contains form elements which are submitted and saved through
108
     * {@link WidgetAreaEditor} within the CMS interface.
109
     *
110
     * @return string HTML
111
     */
112
    public function editablesegment()
113
    {
114
        // use left and main to set the html config
115
        $leftandmain = LeftAndMain::create();
116
        $leftandmain->doInit();
117
118
        // Decode if fully qualified - @see Widget::ClassName
119
        $className = str_replace('_', '\\', $this->urlParams['ID']);
120
        if (class_exists('Translatable') && Member::currentUserID()) {
121
            // set current locale based on logged in user's locale
122
            $locale = Member::currentUser()->Locale;
123
            i18n::set_locale($locale);
124
        }
125
        if (class_exists($className) && is_subclass_of($className, Widget::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \SilverStripe\Widgets\Model\Widget::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
126
            $obj = new $className();
127
            return $obj->EditableSegment();
128
        } else {
129
            user_error("Bad widget class: $className", E_USER_WARNING);
130
            return "Bad widget class name given";
131
        }
132
    }
133
}
134