Completed
Push — develop ( 053968...47dc8d )
by Maxim
12s
created

mgrResources::queryResources()   C

Complexity

Conditions 7
Paths 48

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 28
nc 48
nop 2
dl 0
loc 39
rs 6.7272
c 0
b 0
f 0
1
<?php
2
if( ! defined('IN_MANAGER_MODE') || IN_MANAGER_MODE !== true) {
3
    die("<b>INCLUDE_ORDERING_ERROR</b><br /><br />Please use the EVO Content Manager instead of accessing this file directly.");
4
}
5
6
class mgrResources {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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...
7
    /**
8
     * @var array
9
     */
10
	public $types = array();
11
    /**
12
     * @var array
13
     */
14
    public $items = array();
15
    /**
16
     * @var array
17
     */
18
    public $categories = array();
19
    /**
20
     * @var array
21
     */
22
    public $itemsPerCategory = array();
23
24
    /**
25
     * mgrResources constructor.
26
     */
27
    public function __construct() {
28
		$this->setTypes();
29
		$this->queryItemsFromDB();
30
		$this->prepareCategoryArrays();
31
	}
32
33
    /**
34
     * @return void
35
     */
36
    public function setTypes() {
37
		global $_lang;
38
		$this->types['site_templates']    = array(
39
			'title'=>$_lang["manage_templates"],
40
			'actions'=>array( 'edit'=>array(16,'edit_template'), 'duplicate'=>array(96,'new_template'), 'remove'=>array(21,'delete_template') ),
41
			'permissions'=>array('new_template','edit_template'),
42
			'name'=>'templatename'
43
		);
44
		$this->types['site_tmplvars']     = array(
45
			'title'=>$_lang["tmplvars"],
46
			'actions'=>array('edit'=>array(301,'edit_template'), 'duplicate'=>array(304,'edit_template'), 'remove'=>array(303,'edit_template')),
47
			'permissions'=>array('new_template','edit_template'),
48
		);
49
		$this->types['site_htmlsnippets'] = array(
50
			'title'=>$_lang["manage_htmlsnippets"],
51
			'actions'=>array('edit'=>array(78,'edit_chunk'), 'duplicate'=>array(97,'new_chunk'), 'remove'=>array(80,'delete_chunk')),
52
			'permissions'=>array('new_chunk','edit_chunk'),
53
		);
54
		$this->types['site_snippets']     = array(
55
			'title'=>$_lang["manage_snippets"],
56
			'actions'=>array('edit'=>array(22,'edit_snippet'), 'duplicate'=>array(98,'new_snippet'), 'remove'=>array(25,'delete_snippet')),
57
			'permissions'=>array('new_snippet','edit_snippet'),
58
		);
59
		$this->types['site_plugins']      = array(
60
			'title'=>$_lang["manage_plugins"],
61
			'actions'=>array('edit'=>array(102,'edit_plugin'), 'duplicate'=>array(105,'new_plugin'), 'remove'=>array(104,'delete_plugin')),
62
			'permissions'=>array('new_plugin','edit_plugin'),
63
		);
64
		$this->types['site_modules']      = array(
65
			'title'=>$_lang["manage_modules"],
66
			'actions'=>array('edit'=>array(108,'edit_module'), 'duplicate'=>array(111,'new_module'), 'remove'=>array(110,'delete_module')),
67
			'permissions'=>array('new_module','edit_module'),
68
		);
69
	}
70
71
    /**
72
     * @return void
73
     */
74
    public function queryItemsFromDB() {
75
		foreach($this->types as $resourceTable=>$type) {
76
			if($this->hasAnyPermissions($type['permissions'])) {
77
				$nameField = isset($type['name']) ? $type['name'] : 'name';
78
				$this->items[$resourceTable] = $this->queryResources($resourceTable, $nameField);
79
		   }
80
		 }
81
	}
82
83
    /**
84
     * @param array $permissions
85
     * @return bool
86
     */
87
    public function hasAnyPermissions($permissions) {
88
		global $modx;
89
90
		foreach($permissions as $p)
91
			if($modx->hasPermission($p)) return true;
92
93
		return false;
94
	}
95
96
    /**
97
     * @param string $resourceTable
98
     * @param string $nameField
99
     * @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...
100
     */
101
    public function queryResources($resourceTable, $nameField = 'name') {
102
		global $modx, $_lang;
103
104
        $allowed = array(
105
            'site_htmlsnippets',
106
            'site_snippets',
107
            'site_plugins',
108
            'site_modules'
109
        );
110
		$pluginsql = !empty($resourceTable) && in_array($resourceTable, $allowed) ? $resourceTable . '.disabled, ' : '';
111
112
		$tvsql  = '';
113
		$tvjoin = '';
114
		if ($resourceTable === 'site_tmplvars') {
115
			$tvsql    = 'site_tmplvars.caption, ';
116
			$tvjoin   = sprintf('LEFT JOIN %s AS stt ON site_tmplvars.id=stt.tmplvarid GROUP BY site_tmplvars.id,reltpl', $modx->getFullTableName('site_tmplvar_templates'));
117
			$sttfield = 'IF(stt.templateid,1,0) AS reltpl,';
118
		}
119
		else $sttfield = '';
120
121
		$selectableTemplates = $resourceTable === 'site_templates' ? "{$resourceTable}.selectable, " : "";
122
123
		$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...
124
			"{$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",
125
			$modx->getFullTableName($resourceTable) . " AS {$resourceTable}
126
	            LEFT JOIN " . $modx->getFullTableName('categories') . " AS categories ON {$resourceTable}.category = categories.id {$tvjoin}",
127
			"",
128
			"category,name"
129
		);
130
		$limit = $modx->db->getRecordCount($rs);
131
132
		if($limit < 1) return false;
133
134
		$result = array();
135
		while ($row = $modx->db->getRow($rs)) {
136
			$result[] = $row;
137
		}
138
		return $result;
139
	}
140
141
    /**
142
     * @return void
143
     */
144
    public function prepareCategoryArrays() {
145
		foreach($this->items as $type=>$items) {
146
			foreach((array)$items as $item) {
147
				$catid = $item['catid'] ? $item['catid'] : 0;
148
				$this->categories[$catid] = $item['category'];
149
150
				$item['type'] = $type;
151
				$this->itemsPerCategory[$catid][] = $item;
152
			}
153
		}
154
155
		// Sort categories by name
156
		natcasesort($this->categories);
157
158
		// Now sort by name
159
		foreach($this->itemsPerCategory as $catid=>$items) {
160
			usort($this->itemsPerCategory[$catid], function ($a, $b) {
161
				return strcasecmp($a['name'], $b['name']);
162
			});
163
		}
164
	}
165
}
166