Completed
Push — master ( 01c28b...0d4f0a )
by Andreas
17:26
created

midgard_admin_asgard_handler_components::_show_component()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midgard.admin.asgard
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
/**
10
 * Component display
11
 *
12
 * @package midgard.admin.asgard
13
 */
14
class midgard_admin_asgard_handler_components extends midcom_baseclasses_components_handler
15
{
16
    use midgard_admin_asgard_handler;
17
18 2
    public function _on_initialize()
19
    {
20 2
        $this->add_stylesheet(MIDCOM_STATIC_URL . '/midgard.admin.asgard/components.css');
21 2
    }
22
23 2
    private function _load_component_data($name, midcom_core_manifest $manifest) : array
24
    {
25
        $component_array = [
26 2
            'name' => $name,
27 2
            'title' => $this->_i18n->get_string($name, $name),
28 2
            'purecode' => $manifest->purecode,
29 2
            'icon' => midcom::get()->componentloader->get_component_icon($name),
30 2
            'description' => $manifest->description,
31 2
            'toolbar' => new midcom_helper_toolbar()
32
        ];
33 2
        $component_array['toolbar']->add_item([
34 2
            MIDCOM_TOOLBAR_URL => $this->router->generate('components_configuration', ['component' => $name]),
35 2
            MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('component configuration'),
36 2
            MIDCOM_TOOLBAR_GLYPHICON => 'wrench',
37
        ]);
38
39 2
        $component_array['toolbar']->add_help_item($name);
40
41 2
        return $component_array;
42
    }
43
44 1
    private function _list_components()
45
    {
46 1
        $this->_request_data['components'] = [];
47 1
        $this->_request_data['libraries'] = [];
48
49 1
        foreach (midcom::get()->componentloader->manifests as $name => $manifest) {
50 1
            $type = ($manifest->purecode) ? 'libraries' : 'components';
51
52 1
            $component_array = $this->_load_component_data($name, $manifest);
53
54 1
            $this->_request_data[$type][$name] = $component_array;
55
        }
56 1
    }
57
58
    /**
59
     * Component list view
60
     *
61
     * @param array $data The local request data.
62
     */
63 1
    public function _handler_list(array &$data)
64
    {
65 1
        $data['view_title'] = $this->_l10n->get('components');
66
67 1
        $this->_list_components();
68
69
        // Set the breadcrumb data
70 1
        $this->add_breadcrumb($this->router->generate('welcome'), $this->_l10n->get($this->_component));
71 1
        $this->add_breadcrumb($this->router->generate('components'), $this->_l10n->get('components'));
72 1
        return $this->get_response();
73
    }
74
75
    /**
76
     * Shows the loaded components
77
     *
78
     * @param mixed $handler_id The ID of the handler.
79
     * @param array $data The local request data.
80
     */
81 1
    public function _show_list($handler_id, array &$data)
82
    {
83 1
        $this->_show_lists('components');
84 1
        $this->_show_lists('libraries');
85 1
    }
86
87 1
    private function _show_lists($type)
88
    {
89 1
        $this->_request_data['list_type'] = $type;
90 1
        midcom_show_style('midgard_admin_asgard_components_header');
91 1
        foreach ($this->_request_data[$type] as $component_data) {
92 1
            $this->_request_data['component_data'] = $component_data;
93 1
            midcom_show_style('midgard_admin_asgard_components_item');
94
        }
95 1
        midcom_show_style('midgard_admin_asgard_components_footer');
96 1
    }
97
98
    /**
99
     * Component display
100
     *
101
     * @param string $component The component name
102
     * @param array $data The local request data.
103
     */
104 1
    public function _handler_component($component, array &$data)
105
    {
106 1
        $data['component'] = $component;
107 1
        if (!midcom::get()->componentloader->is_installed($component)) {
108
            throw new midcom_error_notfound("Component {$component} is not installed.");
109
        }
110
111 1
        $data['component_data'] = $this->_load_component_data($component, midcom::get()->componentloader->manifests[$component]);
112
113 1
        $data['view_title'] = $data['component_data']['title'];
114
115 1
        $data['asgard_toolbar']->add_item([
116 1
            MIDCOM_TOOLBAR_URL => $this->router->generate('components_configuration', ['component' => $component]),
117 1
            MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('component configuration'),
118 1
            MIDCOM_TOOLBAR_GLYPHICON => 'wrench',
119
        ]);
120
121
        // Set the breadcrumb data
122 1
        $this->add_breadcrumb($this->router->generate('welcome'), $this->_l10n->get($this->_component));
123 1
        $this->add_breadcrumb($this->router->generate('components'), $this->_l10n->get('components'));
124 1
        $this->add_breadcrumb('', $data['component_data']['title']);
125 1
        return $this->get_response('midgard_admin_asgard_components_component');
126
    }
127
}
128