Passed
Push — master ( 38457e...3f9740 )
by Andreas
37:47
created

midcom_admin_folder_management::list_components()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.1666

Importance

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