Passed
Push — master ( 366727...a79a0f )
by Andreas
38:12 queued 13:17
created

_get_help_element()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

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