Passed
Push — master ( f0a80d...74d5c1 )
by Andreas
18:22
created

_get_help_element()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 17
nc 5
nop 0
dl 0
loc 29
ccs 0
cts 18
cp 0
crap 42
rs 9.0777
c 2
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
 * Style helper methods
11
 *
12
 * @package midgard.admin.asgard
13
 */
14
class midgard_admin_asgard_stylehelper
15
{
16
    /**
17
     * The current request data
18
     *
19
     * @var array
20
     */
21
    private $_data;
22
23 7
    public function __construct(array &$data)
24
    {
25 7
        $this->_data =& $data;
26 7
        midcom::get()->head->enable_jquery_ui(['accordion']);
27 7
    }
28
29 4
    public function render_help()
30
    {
31 4
        if (empty($this->_data['object'])) {
32 1
            return;
33
        }
34
35 3
        if (   midcom::get()->dbfactory->is_a($this->_data['object'], 'midgard_style')
36
            && (   $this->_data['handler_id'] !== 'object_create'
37 3
                || $this->_data['current_type'] == 'midgard_element')) {
38
            // Suggest element names to create under a style
39
            $this->_data['help_style_elementnames'] = $this->_get_style_elements_and_nodes($this->_data['object']->id);
40
            midcom_show_style('midgard_admin_asgard_stylehelper_elementnames');
41
42 3
        } elseif (   midcom::get()->dbfactory->is_a($this->_data['object'], 'midgard_element')
43 3
                  && $this->_get_help_element()) {
44
            midcom_show_style('midgard_admin_asgard_stylehelper_element');
45
        }
46 3
    }
47
48
    private function _get_help_element() : bool
49
    {
50
        if (   empty($this->_data['object']->name)
51
            || empty($this->_data['object']->style)) {
52
            // We cannot help with empty elements
53
            return false;
54
        }
55
56
        if ($this->_data['object']->name == 'ROOT') {
57
            $this->_data['help_style_element'] = [
58
                'component' => 'midcom',
59
                'default'   => file_get_contents(MIDCOM_ROOT . '/midcom/style/ROOT.php'),
60
            ];
61
            return true;
62
        }
63
64
        // Find the element we're looking for
65
        $style_elements = $this->_get_style_elements_and_nodes($this->_data['object']->style);
66
        foreach ($style_elements['elements'] as $component => $elements) {
67
            if (!empty($elements[$this->_data['object']->name])) {
68
                $element_path = $elements[$this->_data['object']->name];
69
                $this->_data['help_style_element'] = [
70
                    'component' => $component,
71
                    'default'   => file_get_contents($element_path),
72
                ];
73
                return true;
74
            }
75
        }
76
        return false;
77
    }
78
79
    private function _get_style_elements_and_nodes(int $style_id) : array
80
    {
81
        $results = [
82
            'elements' => [
83
                'midcom' => [
84
                    'style-init' => '',
85
                    'style-finish' => '',
86
                 ]
87
            ],
88
            'nodes' => [],
89
        ];
90
91
        if (!$style_id) {
92
            return $results;
93
        }
94
        $style_path = midcom_db_style::path_from_id($style_id);
95
        $style_nodes = $this->_get_nodes_using_style($style_path);
96
97
        foreach ($style_nodes as $node) {
98
            if (!isset($results['nodes'][$node->component])) {
99
                $results['nodes'][$node->component] = [];
100
                // Get the list of style elements for the component
101
                $results['elements'][$node->component] = $this->_get_component_default_elements($node->component);
102
            }
103
104
            $results['nodes'][$node->component][] = $node;
105
        }
106
107
        return $results;
108
    }
109
110
    /**
111
     * Get list of topics using a particular style
112
     *
113
     * @return midcom_db_topic[] List of folders
114
     */
115
    private function _get_nodes_using_style(string $style) : array
116
    {
117
        $style_nodes = [];
118
        // Get topics directly using the style
119
        $qb = midcom_db_topic::new_query_builder();
120
        $qb->add_constraint('style', '=', $style);
121
122
        foreach ($qb->execute() as $node) {
123
            $style_nodes[] = $node;
124
125
            if ($node->styleInherit) {
126
                $child_nodes = $this->_get_nodes_inheriting_style($node);
127
                $style_nodes = array_merge($style_nodes, $child_nodes);
128
            }
129
        }
130
131
        return $style_nodes;
132
    }
133
134
    private function _get_nodes_inheriting_style(midcom_db_topic $node) : array
135
    {
136
        $nodes = [];
137
        $qb = midcom_db_topic::new_query_builder();
138
        $qb->add_constraint('up', '=', $node->id);
139
        $qb->add_constraint('style', '=', '');
140
141
        foreach ($qb->execute() as $child_node) {
142
            $nodes[] = $child_node;
143
            $subnodes = $this->_get_nodes_inheriting_style($child_node);
144
            $nodes = array_merge($nodes, $subnodes);
145
        }
146
147
        return $nodes;
148
    }
149
150
    /**
151
     * List the default template elements shipped with a component
152
     *
153
     * @param string $component Component to look elements for
154
     * @return array List of elements found indexed by the element name
155
     */
156
    private function _get_component_default_elements(string $component) : array
157
    {
158
        $elements = [];
159
160
        // Path to the file system
161
        $path = midcom::get()->componentloader->path_to_snippetpath($component) . '/style';
162
163
        if (!is_dir($path)) {
164
            debug_add("Directory {$path} not found.");
165
            return $elements;
166
        }
167
168
        foreach (glob($path . '/*.php') as $filepath) {
169
            $elements[basename($filepath, '.php')] = $filepath;
170
        }
171
172
        return $elements;
173
    }
174
}
175