Completed
Pull Request — develop (#716)
by Agel_Nash
11:46 queued 05:21
created

mgrResources   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 160
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B setTypes() 0 34 1
A queryItemsFromDB() 0 8 4
A hasAnyPermissions() 0 8 3
C queryResources() 0 39 7
B prepareCategoryArrays() 0 21 5
1
<?php namespace EvolutionCMS\Legacy;
2
3
class mgrResources {
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
4
    /**
5
     * @var array
6
     */
7
    public $types = array();
8
    /**
9
     * @var array
10
     */
11
    public $items = array();
12
    /**
13
     * @var array
14
     */
15
    public $categories = array();
16
    /**
17
     * @var array
18
     */
19
    public $itemsPerCategory = array();
20
21
    /**
22
     * mgrResources constructor.
23
     */
24
    public function __construct() {
25
        $this->setTypes();
26
        $this->queryItemsFromDB();
27
        $this->prepareCategoryArrays();
28
    }
29
30
    /**
31
     * @return void
32
     */
33
    public function setTypes() {
34
        global $_lang;
35
        $this->types['site_templates']    = array(
36
            'title'=>$_lang["manage_templates"],
37
            'actions'=>array( 'edit'=>array(16,'edit_template'), 'duplicate'=>array(96,'new_template'), 'remove'=>array(21,'delete_template') ),
38
            'permissions'=>array('new_template','edit_template'),
39
            'name'=>'templatename'
40
        );
41
        $this->types['site_tmplvars']     = array(
42
            'title'=>$_lang["tmplvars"],
43
            'actions'=>array('edit'=>array(301,'edit_template'), 'duplicate'=>array(304,'edit_template'), 'remove'=>array(303,'edit_template')),
44
            'permissions'=>array('new_template','edit_template'),
45
        );
46
        $this->types['site_htmlsnippets'] = array(
47
            'title'=>$_lang["manage_htmlsnippets"],
48
            'actions'=>array('edit'=>array(78,'edit_chunk'), 'duplicate'=>array(97,'new_chunk'), 'remove'=>array(80,'delete_chunk')),
49
            'permissions'=>array('new_chunk','edit_chunk'),
50
        );
51
        $this->types['site_snippets']     = array(
52
            'title'=>$_lang["manage_snippets"],
53
            'actions'=>array('edit'=>array(22,'edit_snippet'), 'duplicate'=>array(98,'new_snippet'), 'remove'=>array(25,'delete_snippet')),
54
            'permissions'=>array('new_snippet','edit_snippet'),
55
        );
56
        $this->types['site_plugins']      = array(
57
            'title'=>$_lang["manage_plugins"],
58
            'actions'=>array('edit'=>array(102,'edit_plugin'), 'duplicate'=>array(105,'new_plugin'), 'remove'=>array(104,'delete_plugin')),
59
            'permissions'=>array('new_plugin','edit_plugin'),
60
        );
61
        $this->types['site_modules']      = array(
62
            'title'=>$_lang["manage_modules"],
63
            'actions'=>array('edit'=>array(108,'edit_module'), 'duplicate'=>array(111,'new_module'), 'remove'=>array(110,'delete_module')),
64
            'permissions'=>array('new_module','edit_module'),
65
        );
66
    }
67
68
    /**
69
     * @return void
70
     */
71
    public function queryItemsFromDB() {
72
        foreach($this->types as $resourceTable=>$type) {
73
            if($this->hasAnyPermissions($type['permissions'])) {
74
                $nameField = isset($type['name']) ? $type['name'] : 'name';
75
                $this->items[$resourceTable] = $this->queryResources($resourceTable, $nameField);
76
            }
77
        }
78
    }
79
80
    /**
81
     * @param array $permissions
82
     * @return bool
83
     */
84
    public function hasAnyPermissions($permissions) {
85
        $modx = evolutionCMS();
86
87
        foreach($permissions as $p)
88
            if($modx->hasPermission($p)) return true;
89
90
        return false;
91
    }
92
93
    /**
94
     * @param string $resourceTable
95
     * @param string $nameField
96
     * @return array|bool
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use false|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
97
     */
98
    public function queryResources($resourceTable, $nameField = 'name') {
99
        $modx = evolutionCMS(); global $_lang;
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
100
101
        $allowed = array(
102
            'site_htmlsnippets',
103
            'site_snippets',
104
            'site_plugins',
105
            'site_modules'
106
        );
107
        $pluginsql = !empty($resourceTable) && in_array($resourceTable, $allowed) ? $resourceTable . '.disabled, ' : '';
108
109
        $tvsql  = '';
110
        $tvjoin = '';
111
        if ($resourceTable === 'site_tmplvars') {
112
            $tvsql    = 'site_tmplvars.caption, ';
113
            $tvjoin   = sprintf('LEFT JOIN %s AS stt ON site_tmplvars.id=stt.tmplvarid GROUP BY site_tmplvars.id,reltpl', $modx->getFullTableName('site_tmplvar_templates'));
114
            $sttfield = 'IF(stt.templateid,1,0) AS reltpl,';
115
        }
116
        else $sttfield = '';
117
118
        $selectableTemplates = $resourceTable === 'site_templates' ? "{$resourceTable}.selectable, " : "";
119
120
        $rs = $modx->db->select(
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $rs. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
121
            "{$sttfield} {$pluginsql} {$tvsql} {$resourceTable}.{$nameField} as name, {$resourceTable}.id, {$resourceTable}.description, {$resourceTable}.locked, {$selectableTemplates}IF(isnull(categories.category),'{$_lang['no_category']}',categories.category) as category, categories.id as catid",
122
            $modx->getFullTableName($resourceTable) . " AS {$resourceTable}
123
	            LEFT JOIN " . $modx->getFullTableName('categories') . " AS categories ON {$resourceTable}.category = categories.id {$tvjoin}",
124
            "",
125
            "category,name"
126
        );
127
        $limit = $modx->db->getRecordCount($rs);
128
129
        if($limit < 1) return false;
130
131
        $result = array();
132
        while ($row = $modx->db->getRow($rs)) {
133
            $result[] = $row;
134
        }
135
        return $result;
136
    }
137
138
    /**
139
     * @return void
140
     */
141
    public function prepareCategoryArrays() {
142
        foreach($this->items as $type=>$items) {
143
            foreach((array)$items as $item) {
144
                $catid = $item['catid'] ? $item['catid'] : 0;
145
                $this->categories[$catid] = $item['category'];
146
147
                $item['type'] = $type;
148
                $this->itemsPerCategory[$catid][] = $item;
149
            }
150
        }
151
152
        // Sort categories by name
153
        natcasesort($this->categories);
154
155
        // Now sort by name
156
        foreach($this->itemsPerCategory as $catid=>$items) {
157
            usort($this->itemsPerCategory[$catid], function ($a, $b) {
158
                return strcasecmp($a['name'], $b['name']);
159
            });
160
        }
161
    }
162
}
163