ModelDesignLayout::getLayouts()   B
last analyzed

Complexity

Conditions 9
Paths 20

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 18
c 0
b 0
f 0
nc 20
nop 1
dl 0
loc 36
rs 8.0555
1
<?php
2
3
/* 	Divine CMS - Open source CMS for widespread use.
4
    Copyright (c) 2019 Mykola Burakov ([email protected])
5
6
    See SOURCE.txt for other and additional information.
7
8
    This file is part of Divine CMS.
9
10
    This program is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
15
    This program is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
    GNU General Public License for more details.
19
20
    You should have received a copy of the GNU General Public License
21
    along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23
class ModelDesignLayout extends \Divine\Engine\Core\Model
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...
24
{
25
    public function addLayout($data)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
26
    {
27
        $this->db->query("
28
			INSERT INTO layout 
29
			SET name = '" . $this->db->escape($data['name']) . "'
30
		");
31
32
        $layout_id = $this->db->getLastId();
33
34
        if (isset($data['layout_route'])) {
35
            foreach ($data['layout_route'] as $layout_route) {
36
                $this->db->query("
37
					INSERT INTO layout_route 
38
					SET layout_id = '" . (int)$layout_id . "', 
39
						route = '" . $this->db->escape($layout_route['route']) . "'
40
				");
41
            }
42
        }
43
44
        if (isset($data['layout_module'])) {
45
            foreach ($data['layout_module'] as $layout_module) {
46
                $this->db->query("
47
					INSERT INTO layout_module 
48
					SET layout_id = '" . (int)$layout_id . "', 
49
						code = '" . $this->db->escape($layout_module['code']) . "', 
50
						position = '" . $this->db->escape($layout_module['position']) . "', 
51
						sort_order = '" . (int)$layout_module['sort_order'] . "'
52
				");
53
            }
54
        }
55
56
        return $layout_id;
57
    }
58
59
    public function editLayout($layout_id, $data)
60
    {
61
        $this->db->query("
62
			UPDATE layout 
63
			SET name = '" . $this->db->escape($data['name']) . "' 
64
			WHERE layout_id = '" . (int)$layout_id . "'
65
		");
66
67
        $this->db->query("
68
			DELETE 
69
			FROM layout_route 
70
			WHERE layout_id = '" . (int)$layout_id . "'
71
		");
72
73
        if (isset($data['layout_route'])) {
74
            foreach ($data['layout_route'] as $layout_route) {
75
                $this->db->query("
76
					INSERT INTO layout_route 
77
					SET layout_id = '" . (int)$layout_id . "', 
78
						route = '" . $this->db->escape($layout_route['route']) . "'
79
				");
80
            }
81
        }
82
83
        $this->db->query("
84
			DELETE 
85
			FROM layout_module 
86
			WHERE layout_id = '" . (int)$layout_id . "'
87
		");
88
89
        if (isset($data['layout_module'])) {
90
            foreach ($data['layout_module'] as $layout_module) {
91
                $this->db->query("
92
					INSERT INTO layout_module 
93
					SET layout_id = '" . (int)$layout_id . "', 
94
						code = '" . $this->db->escape($layout_module['code']) . "', 
95
						position = '" . $this->db->escape($layout_module['position']) . "', 
96
						sort_order = '" . (int)$layout_module['sort_order'] . "'
97
				");
98
            }
99
        }
100
    }
101
102
    public function deleteLayout($layout_id)
103
    {
104
        $this->db->query("
105
			DELETE 
106
			FROM layout 
107
			WHERE layout_id = '" . (int)$layout_id . "'
108
		");
109
        $this->db->query("
110
			DELETE 
111
			FROM layout_route 
112
			WHERE layout_id = '" . (int)$layout_id . "'
113
		");
114
        $this->db->query("
115
			DELETE 
116
			FROM layout_module 
117
			WHERE layout_id = '" . (int)$layout_id . "'
118
		");
119
        $this->db->query("
120
			DELETE 
121
			FROM category_to_layout 
122
			WHERE layout_id = '" . (int)$layout_id . "'
123
		");
124
        $this->db->query("
125
			DELETE 
126
			FROM product_to_layout 
127
			WHERE layout_id = '" . (int)$layout_id . "'
128
		");
129
        $this->db->query("
130
			DELETE 
131
			FROM information_to_layout 
132
			WHERE layout_id = '" . (int)$layout_id . "'
133
		");
134
    }
135
136
    public function getLayout($layout_id)
137
    {
138
        $query = $this->db->query("
139
			SELECT DISTINCT * 
140
			FROM layout 
141
			WHERE layout_id = '" . (int)$layout_id . "'
142
		");
143
144
        return $query->row;
145
    }
146
147
    public function getLayouts($data = array())
148
    {
149
        $sql = "
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal \n SELECT * \... FROM layout\n does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
150
			SELECT * 
151
			FROM layout
152
		";
153
154
        $sort_data = array('name');
155
156
        if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
157
            $sql .= " ORDER BY " . $data['sort'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ORDER BY does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
158
        } else {
159
            $sql .= " ORDER BY name";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ORDER BY name does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
160
        }
161
162
        if (isset($data['order']) && ($data['order'] == 'DESC')) {
163
            $sql .= " DESC";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal DESC does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
164
        } else {
165
            $sql .= " ASC";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ASC does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
166
        }
167
168
        if (isset($data['start']) || isset($data['limit'])) {
169
            if ($data['start'] < 0) {
170
                $data['start'] = 0;
171
            }
172
173
            if ($data['limit'] < 1) {
174
                $data['limit'] = 20;
175
            }
176
177
            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal LIMIT does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal , does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
178
        }
179
180
        $query = $this->db->query($sql);
181
182
        return $query->rows;
183
    }
184
185
    public function getLayoutRoutes($layout_id)
186
    {
187
        $query = $this->db->query("
188
			SELECT * 
189
			FROM layout_route 
190
			WHERE layout_id = '" . (int)$layout_id . "'
191
		");
192
193
        return $query->rows;
194
    }
195
196
    public function getLayoutModules($layout_id)
197
    {
198
        $query = $this->db->query("
199
			SELECT * 
200
			FROM layout_module 
201
			WHERE layout_id = '" . (int)$layout_id . "' 
202
			ORDER BY position ASC, sort_order ASC
203
		");
204
205
        return $query->rows;
206
    }
207
208
    public function getTotalLayouts()
209
    {
210
        $query = $this->db->query("
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal \n SELECT COU... FROM layout\n does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
211
			SELECT COUNT(*) AS total 
212
			FROM layout
213
		");
214
215
        return $query->row['total'];
216
    }
217
}
218