Completed
Push — master ( 876d54...079a66 )
by Andreas
14:11
created

midcom_admin_folder_management   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 81.03%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
dl 0
loc 125
ccs 47
cts 58
cp 0.8103
rs 10
c 1
b 0
f 0
wmc 23

5 Methods

Rating   Name   Duplication   Size   Complexity  
A _on_initialize() 0 13 3
B list_components() 0 27 9
A list_styles() 0 25 3
A get_component_list() 0 27 5
A list_theme_styles() 0 11 3
1
<?php
2
/**
3
 * @package midcom.admin.folder
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
use Symfony\Component\Finder\Finder;
10
11
/**
12
 * Folder management class.
13
 *
14
 * @package midcom.admin.folder
15
 */
16
class midcom_admin_folder_management extends midcom_baseclasses_components_plugin
17
{
18
    /**
19
     * Initializes the context data and toolbar objects
20
     */
21 8
    public function _on_initialize()
22
    {
23 8
        $config = $this->_request_data['plugin_config'];
24 8
        if ($config) {
25
            foreach ($config as $key => $value) {
26
                $this->$key = $value;
27
            }
28
        }
29
30
        // Ensure we get the correct styles
31 8
        midcom::get()->style->prepend_component_styledir('midcom.admin.folder');
32
33 8
        $this->_request_data['folder'] = $this->_topic;
34 8
    }
35
36
    /**
37
     * List names of the non-purecore components
38
     */
39 2
    public static function get_component_list() : array
40
    {
41 2
        $components = [];
42
43
        // Loop through the list of components of component loader
44 2
        foreach (midcom::get()->componentloader->manifests as $manifest) {
45
            // Skip purecode components
46 2
            if ($manifest->purecode) {
47 2
                continue;
48
            }
49
50
            // Skip components beginning with midcom or midgard
51 2
            if (   preg_match('/^(midcom|midgard)\./', $manifest->name)
52 2
                && $manifest->name != 'midcom.helper.search') {
53 2
                continue;
54
            }
55
56 2
            $components[$manifest->name] = [
57 2
                'name'        => $manifest->get_name_translated(),
58 2
                'description' => midcom::get()->i18n->get_string($manifest->description, $manifest->name)
59
            ];
60
        }
61
62
        // Sort the components in alphabetical order (by key i.e. component class name)
63 2
        asort($components);
64
65 2
        return $components;
66
    }
67
68
    /**
69
     * Populate user interface for editing and creating topics
70
     */
71 2
    public static function list_components($current_selection) : array
72
    {
73 2
        $list = [];
74
75 2
        $urltopics = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_URLTOPICS);
76 2
        if ($urltopic = end($urltopics)) {
0 ignored issues
show
Bug introduced by
It seems like $urltopics can also be of type false; however, parameter $array of end() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        if ($urltopic = end(/** @scrutinizer ignore-type */ $urltopics)) {
Loading history...
77
            if (empty($urltopic->component)) {
78
                $list[''] = '';
79
            }
80
        }
81 2
        $allowed = midcom::get()->config->get('component_listing_allowed', []);
82 2
        $excluded = midcom::get()->config->get('component_listing_excluded', []);
83
84 2
        foreach (self::get_component_list() as $component => $details) {
85 2
            if ($component !== $current_selection) {
86 2
                if (!empty($allowed) && !in_array($component, $allowed)) {
87
                    continue;
88
                }
89
90 2
                if (!empty($excluded) && in_array($component, $excluded)) {
91
                    continue;
92
                }
93
            }
94 2
            $list[$component] = "{$details['name']} ({$component})";
95
        }
96
97 2
        return $list;
98
    }
99
100
    /**
101
     * List available style templates
102
     */
103 2
    public static function list_styles($up = 0, $prefix = '/', $spacer = '') : array
104
    {
105 2
        static $style_array = [];
106
107 2
        $style_array[''] = midcom::get()->i18n->get_string('default', 'midcom.admin.folder');
108
109
        // Give an option for creating a new layout template
110 2
        $style_array['__create'] = midcom::get()->i18n->get_string('new layout template', 'midcom.admin.folder');
111
112 2
        $qb = midcom_db_style::new_query_builder();
113 2
        $qb->add_constraint('up', '=', $up);
114
115 2
        foreach ($qb->execute() as $style) {
116
            $style_string = "{$prefix}{$style->name}";
117
118
            // Hide common unwanted material with heuristics
119
            if (preg_match('/(asgard|empty)/i', $style_string)) {
120
                continue;
121
            }
122
123
            $style_array[$style_string] = "{$spacer}{$style->name}";
124
            self::list_styles($style->id, $style_string . '/', $spacer . '&nbsp;&nbsp;');
125
        }
126
127 2
        return self::list_theme_styles($style_array);
128
    }
129
130 2
    public static function list_theme_styles(array $styles) : array
131
    {
132 2
        $theme_styledir = OPENPSA2_THEME_ROOT . '/' . midcom::get()->config->get('theme') . '/style';
133 2
        if (is_dir($theme_styledir)) {
134 2
        $finder = new Finder();
135 2
            foreach ($finder->directories()->in($theme_styledir) as $dir) {
136 2
                $label = preg_replace('/.+?\//', '&nbsp;&nbsp;', $dir->getRelativePathname());
137 2
                $styles['theme:/' . $dir->getRelativePathname()] = $label;
138
            }
139
        }
140 2
        return $styles;
141
    }
142
}
143