aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/sidebar/ResourceTreeWidget.php
blob: 60d4b2925636a2d2211b00bc83d4e8728c6fe77c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php

/**
 * This class provides a resource tree view for the sidebar.
 *
 * @author      Moritz Strohm <strohm@data-quest.de>
 * @copyright   2017-2019
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       4.5
 */
class ResourceTreeWidget extends SidebarWidget
{
    /**
     * $root_resource is the resource whose resource tree
     * shall be displayed by this widget.
     * The resource itself and all its children are displayed.
     */
    protected $root_resources = [];
    protected $parameter_name = '';
    protected $foldable = false;
    protected $current_resource_id = null;

    /**
     * This widget must be initialised by providing at least one
     * Resource object in an array.
     *
     * @param array $root_resources The root resource objects which will be
     *     displayed by this tree view.
     * @param string $title The title of this widget.
     * @param string|null $parameter_name The name of the URL parameter which
     *     will be set when one of the resources in the tree is selected.
     *     If parameter_name is set to null the items in the resource tree
     *     widget will link to the resource's details page.
     */
    public function __construct(
        array $root_resources = [],
        $title = '',
        $parameter_name = 'tree_selected_resource'
    )
    {
        parent::__construct();

        if (!$root_resources) {
            throw new InvalidArgumentException(
                'ResourceTreeWidget instances must be initalised with at least one resource object!'
            );
        }

        //Extra check to make sure the root_resources attribute of this instance
        //is an array containing only Resource objects or objects derived
        //from the Resource class:
        foreach ($root_resources as $root_resource) {
            if ($root_resource instanceof Resource) {
                $this->root_resources[] = $root_resource;
            }
        }

        if (!$this->root_resources) {
            throw new InvalidArgumentException(
                'No Resource object has been provided to the constructor of the ResourceTreeWidget class!'
            );
        }

        $this->root_resources = SimpleORMapCollection::createFromArray(
            $this->root_resources
        );
        $this->root_resources->orderBy('sort_position DESC, name ASC, mkdate ASC');

        $this->template = 'sidebar/resource-tree-widget';

        if ($title) {
            $this->title = $title;
        } else {
            $this->title = _('Ressourcenbaum');
        }

        $this->parameter_name = $parameter_name;
        $this->forced_rendering = true;
    }

    /**
     * The render method will attach the root resource
     * of this object to the set of variables which is
     * passed to the template.
     */
    public function render($variables = [])
    {
        if (!is_array($variables)) {
            $variables = [];
        }

        $variables['resources'] = $this->root_resources;
        $variables['title'] = $this->title;
        $variables['parameter_name'] = $this->parameter_name;
        if ($this->parameter_name) {
            $variables['selected_resource'] = Request::get($this->parameter_name);
        } else {
            $variables['selected_resource'] = $this->current_resource_id;
        }

        $resource_path = [];
        //If a resource is selected we get the IDs of all parent resources
        //so that we know in the template which tree items shall be visible.
        if ($variables['selected_resource']) {
            $resource = Resource::find($variables['selected_resource']);
            if ($resource) {
                $resource_path[] = $resource->id;
                $current_parent = $resource->parent;
                while ($current_parent) {
                    $resource_path[] = $current_parent->id;
                    $current_parent = $current_parent->parent;
                }
            }
        }
        $variables['resource_path'] = $resource_path;
        $variables['max_open_depth'] = 0;
        $variables['layout_css_classes'] = $this->layout_css_classes;
        $variables['hide'] = false;

        return parent::render($variables);
    }

    public function setCurrentResource(Resource $resource)
    {
        $this->current_resource_id = $resource->id;
    }

    public function setCurrentResourceId($resource_id = null)
    {
        if ($resource_id) {
            $this->current_resource_id = $resource_id;
        }
    }

    public function setFoldable($foldable = false)
    {
        $this->foldable = (bool)$foldable;
    }

    public function isFoldable()
    {
        return $this->foldable;
    }
}