Passed
Push — master ( 1d6e3e...f1fbfb )
by Andreas
11:14
created

midcom_admin_folder_management   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 109
ccs 42
cts 49
cp 0.8571
rs 10
wmc 18

5 Methods

Rating   Name   Duplication   Size   Complexity  
A list_components() 0 20 6
A list_theme_styles() 0 11 3
A list_styles() 0 25 3
A _on_initialize() 0 4 1
A get_component_list() 0 27 5
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
        // Ensure we get the correct styles
24 8
        midcom::get()->style->prepend_component_styledir('midcom.admin.folder');
25
    }
26
27
    /**
28
     * List names of the non-purecore components
29
     */
30 3
    public static function get_component_list() : array
31
    {
32 3
        $components = [];
33
34
        // Loop through the list of components of component loader
35 3
        foreach (midcom::get()->componentloader->get_manifests() as $manifest) {
36
            // Skip purecode components
37 3
            if ($manifest->purecode) {
38 3
                continue;
39
            }
40
41
            // Skip components beginning with midcom or midgard
42 3
            if (   preg_match('/^(midcom|midgard)\./', $manifest->name)
43 3
                && $manifest->name != 'midcom.helper.search') {
44 3
                continue;
45
            }
46
47 3
            $components[$manifest->name] = [
48 3
                'name'        => $manifest->get_name_translated(),
49 3
                'description' => midcom::get()->i18n->get_string($manifest->description, $manifest->name)
50 3
            ];
51
        }
52
53
        // Sort the components in alphabetical order (by key i.e. component class name)
54 3
        asort($components);
55
56 3
        return $components;
57
    }
58
59
    /**
60
     * Populate user interface for editing and creating topics
61
     */
62 3
    public static function list_components(string $current_selection) : array
63
    {
64 3
        $list = [];
65 3
        $allowed = midcom::get()->config->get_array('component_listing_allowed');
66 3
        $excluded = midcom::get()->config->get_array('component_listing_excluded');
67
68 3
        foreach (self::get_component_list() as $component => $details) {
69 3
            if ($component !== $current_selection) {
70 3
                if (!empty($allowed) && !in_array($component, $allowed)) {
71
                    continue;
72
                }
73
74 3
                if (in_array($component, $excluded)) {
75
                    continue;
76
                }
77
            }
78 3
            $list[$component] = "{$details['name']} ({$component})";
79
        }
80
81 3
        return $list;
82
    }
83
84
    /**
85
     * List available style templates
86
     */
87 2
    public static function list_styles(int $up = 0, string $prefix = '/', string $spacer = '') : array
88
    {
89 2
        static $style_array = [];
90
91 2
        $style_array[''] = midcom::get()->i18n->get_string('default', 'midcom.admin.folder');
92
93
        // Give an option for creating a new layout template
94 2
        $style_array['__create'] = midcom::get()->i18n->get_string('new layout template', 'midcom.admin.folder');
95
96 2
        $qb = midcom_db_style::new_query_builder();
97 2
        $qb->add_constraint('up', '=', $up);
98
99 2
        foreach ($qb->execute() as $style) {
100
            $style_string = "{$prefix}{$style->name}";
101
102
            // Hide common unwanted material with heuristics
103
            if (preg_match('/(asgard|empty)/i', $style_string)) {
104
                continue;
105
            }
106
107
            $style_array[$style_string] = "{$spacer}{$style->name}";
108
            self::list_styles($style->id, $style_string . '/', $spacer . '&nbsp;&nbsp;');
109
        }
110
111 2
        return self::list_theme_styles($style_array);
112
    }
113
114 2
    public static function list_theme_styles(array $styles) : array
115
    {
116 2
        $theme_styledir = midcom::get()->getProjectDir() . '/var/themes/' . midcom::get()->config->get('theme') . '/style';
117 2
        if (is_dir($theme_styledir)) {
118 2
            $finder = new Finder();
119 2
            foreach ($finder->directories()->in($theme_styledir) as $dir) {
120 2
                $label = preg_replace('/.+?\//', '&nbsp;&nbsp;', $dir->getRelativePathname());
121 2
                $styles['theme:/' . $dir->getRelativePathname()] = $label;
122
            }
123
        }
124 2
        return $styles;
125
    }
126
}
127