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

Categories::addCategory()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 2
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Class to handle the modx-categories
5
 */
6
class Categories
0 ignored issues
show
Coding Style introduced by
The property $db_tbl is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
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...
7
{
8
    /**
9
     * @var DBAPI
10
     */
11
    public $db;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $db. 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...
12
    public $db_tbl = array();
13
    public $elements = array('templates', 'tmplvars', 'htmlsnippets', 'snippets', 'plugins', 'modules');
14
15
    public function __construct()
16
    {
17
        global $modx;
18
19
        $this->db = &$modx->db;
20
        $this->db_tbl['categories'] = $modx->getFullTableName('categories');
21
22
        foreach ($this->elements as $element) {
23
            $this->db_tbl[$element] = $modx->getFullTableName('site_' . $element);
24
        }
25
    }
26
27
28
    /**
29
     * Get all categories
30
     * @return  array   $categories / array contains all categories
31
     */
32
    public function getCategories()
33
    {
34
        $categories = $this->db->makeArray(
35
            $this->db->select(
36
                '*',
37
                $this->db_tbl['categories'],
38
                '1',
39
                '`rank`,`category`'
40
            )
41
        );
42
43
        return empty($categories) ? array() : $categories;
44
    }
45
46
    /**
47
     * @param string $search
48
     * @param string $where
49
     * @return array|bool|object|stdClass
50
     */
51 View Code Duplication
    public function getCategory($search, $where = 'category')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        $category = $this->db->getRow(
54
            $this->db->select(
55
                '*',
56
                $this->db_tbl['categories'],
57
                "`" . $where . "` = '" . $this->db->escape($search) . "'"
58
            )
59
        );
60
61
        return $category;
62
    }
63
64
    /**
65
     * @param string $value
66
     * @param string $search
67
     * @param string $where
68
     * @return bool|int|string
69
     */
70 View Code Duplication
    public function getCategoryValue($value, $search, $where = 'category')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $_value = $this->db->getValue(
73
            $this->db->select(
74
                '`' . $value . '`',
75
                $this->db_tbl['categories'],
76
                "`" . $where . "` = '" . $this->db->escape($search) . "'"
77
            )
78
        );
79
80
        return $_value;
81
    }
82
83
    /**
84
     * @param int $category_id
85
     * @param string $element
86
     * @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...
87
     */
88
    public function getAssignedElements($category_id, $element)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $category_id is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
89
    {
90
        if (in_array($element, $this->elements, true)) {
91
            $elements = $this->db->makeArray(
92
                $this->db->select(
93
                    '*',
94
                    $this->db_tbl[$element],
95
                    "`category` = '" . (int)$category_id . "'"
96
                )
97
            );
98
99
            // correct the name of templates
100
            if ($element === 'templates') {
101
                $_elements_count = count($elements);
102
                for ($i = 0; $i < $_elements_count; $i++) {
103
                    $elements[$i]['name'] = $elements[$i]['templatename'];
104
                }
105
            }
106
107
            return $elements;
108
        }
109
110
        return false;
111
    }
112
113
    /**
114
     * @param int $category_id
115
     * @return array
116
     */
117
    public function getAllAssignedElements($category_id)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $category_id is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
118
    {
119
        $elements = array();
120
        foreach ($this->elements as $element) {
121
            $elements[$element] = $this->getAssignedElements($category_id, $element);
122
        }
123
124
        return $elements;
125
    }
126
127
    /**
128
     * @param int $category_id
129
     * @return bool
130
     */
131
    public function deleteCategory($category_id)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $category_id is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
132
    {
133
        $_update = array('category' => 0);
134
        foreach ($this->elements as $element) {
135
            $this->db->update(
136
                $_update,
137
                $this->db_tbl[$element],
138
                "`category` = '" . (int)$category_id . "'"
139
            );
140
        }
141
142
        $this->db->delete(
143
            $this->db_tbl['categories'],
144
            "`id` = '" . (int)$category_id . "'"
145
        );
146
147
        return $this->db->getAffectedRows() === 1;
148
    }
149
150
    /**
151
     * @param int $category_id
152
     * @param array $data
153
     * @return bool
154
     */
155
    public function updateCategory($category_id, $data = array())
0 ignored issues
show
Coding Style Naming introduced by
The parameter $category_id is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
156
    {
157
        if (empty($data) || empty($category_id)) {
158
            return false;
159
        }
160
161
        $_update = array(
162
            'category' => $this->db->escape($data['category']),
163
            'rank'     => (int)$data['rank']
164
        );
165
166
        $this->db->update(
167
            $_update,
168
            $this->db_tbl['categories'],
169
            "`id` = '" . (int)$category_id . "'"
170
        );
171
172
        if ($this->db->getAffectedRows() === 1) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $this->db->getAffectedRows() === 1;.
Loading history...
173
            return true;
174
        }
175
176
        return false;
177
    }
178
179
    /**
180
     * @param string $category_name
181
     * @param int $category_rank
182
     * @return bool|int|mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use false|integer.

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...
183
     */
184
    public function addCategory($category_name, $category_rank)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $category_name is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The parameter $category_rank is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
185
    {
186
        if ($this->isCategoryExists($category_name)) {
187
            return false;
188
        }
189
190
        $_insert = array(
191
            'category' => $this->db->escape($category_name),
192
            'rank'     => (int)$category_rank
193
        );
194
195
        $this->db->insert(
196
            $_insert,
197
            $this->db_tbl['categories']
198
        );
199
200
        if ($this->db->getAffectedRows() === 1) {
201
            return $this->db->getInsertId();
202
        }
203
204
        return false;
205
    }
206
207
    /**
208
     * @param string $category_name
209
     * @return bool|int|string
210
     */
211
    public function isCategoryExists($category_name)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $category_name is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
212
    {
213
        $category = $this->db->escape($category_name);
214
215
        $category_id = $this->db->getValue(
216
            $this->db->select(
217
                '`id`',
218
                $this->db_tbl['categories'],
219
                "`category` = '" . $category . "'"
220
            )
221
        );
222
223
        if ($this->db->getAffectedRows() === 1) {
224
            return $category_id;
225
        }
226
227
        return false;
228
    }
229
}
230