-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
executable file
·210 lines (201 loc) · 6.62 KB
/
db.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
class plugins_mainsectors_db {
/**
* @var debug_logger $logger
*/
protected debug_logger $logger;
/**
* @param array $config
* @param array $params
* @return array|bool
*/
public function fetchData(array $config, array $params = []) {
if ($config['context'] === 'all') {
switch ($config['type']) {
case 'mss':
$query = "SELECT
ms.id_ms,
ms.id_page,
ms.type_ms,
COALESCE(pc.name_pages, cc.name_cat) as name_ms
FROM mc_mainsectors as ms
LEFT JOIN mc_cms_page as p ON ms.id_page = p.id_pages AND ms.type_ms = 'page'
LEFT JOIN mc_cms_page_content as pc ON p.id_pages = pc.id_pages AND pc.id_lang = :lang_p
LEFT JOIN mc_catalog_cat as c ON ms.id_page = c.id_cat AND ms.type_ms = 'category'
LEFT JOIN mc_catalog_cat_content as cc ON c.id_cat = cc.id_cat AND cc.id_lang = :lang_c
ORDER BY ms.order_ms";
break;
case 'pages':
/*$query = 'SELECT * FROM (
SELECT p.id_pages AS id, p.id_parent AS parent, pc.name_pages AS name
FROM mc_cms_page AS p
LEFT JOIN mc_cms_page_content AS pc USING ( id_pages )
LEFT JOIN mc_lang AS l ON pc.id_lang = l.id_lang
WHERE p.menu_pages = 1
AND pc.published_pages = 1
ORDER BY p.id_pages , l.default_lang DESC
) as pt
GROUP BY pt.id';*/
$query = 'SELECT
mcp.id_pages AS id,
mcp.id_parent AS parent,
mcpc.name_pages AS name
FROM mc_cms_page AS mcp
LEFT JOIN mc_cms_page_content AS mcpc USING ( id_pages )
LEFT JOIN mc_lang AS ml ON (mcpc.id_lang = ml.id_lang AND ml.default_lang = 1)
WHERE mcp.menu_pages = 1
AND mcpc.published_pages = 1
AND ml.id_lang = :default_lang
ORDER BY mcp.id_pages';
break;
case 'categories':
/*$query = 'SELECT * FROM (
SELECT p.id_cat as id, p.id_parent as parent, pc.name_cat as name
FROM mc_catalog_cat as p
LEFT JOIN mc_catalog_cat_content as pc
USING(id_cat)
LEFT JOIN mc_lang AS l ON pc.id_lang = l.id_lang
WHERE pc.published_cat =1
ORDER BY p.id_cat , l.default_lang DESC
) as pt
GROUP BY pt.id';*/
$query = 'SELECT
mcc.id_cat as id,
mcc.id_parent as parent,
mccc.name_cat as name
FROM mc_catalog_cat as mcc
LEFT JOIN mc_catalog_cat_content as mccc USING(id_cat)
LEFT JOIN mc_lang AS ml ON (mccc.id_lang = ml.id_lang AND ml.default_lang = 1)
WHERE mccc.published_cat =1 AND ml.id_lang = :default_lang
ORDER BY mcc.id_cat';
break;
case 'order':
$query = 'SELECT
id_page,
type_ms,
order_ms
FROM mc_mainsectors ORDER BY order_ms';
break;
default:
return false;
}
try {
return component_routing_db::layer()->fetchAll($query, $params);
}
catch (Exception $e) {
if(!isset($this->logger)) $this->logger = new debug_logger(MP_LOG_DIR);
$this->logger->log('statement','db',$e->getMessage(),$this->logger::LOG_MONTH);
}
}
elseif ($config['context'] === 'one') {
switch ($config['type']) {
case 'newMs':
$query = "SELECT
ms.id_ms,
ms.id_page,
ms.type_ms,
COALESCE(pc.name_pages, cc.name_cat) as name_ms
FROM mc_mainsectors as ms
LEFT JOIN mc_cms_page as p ON ms.id_page = p.id_pages AND ms.type_ms = 'page'
LEFT JOIN mc_cms_page_content as pc ON p.id_pages = pc.id_pages AND pc.id_lang = :lang_p
LEFT JOIN mc_catalog_cat as c ON ms.id_page = c.id_cat AND ms.type_ms = 'category'
LEFT JOIN mc_catalog_cat_content as cc ON c.id_cat = cc.id_cat AND cc.id_lang = :lang_c
ORDER BY ms.order_ms DESC LIMIT 0,1";
break;
case 'homeMsp':
$query = "SELECT
GROUP_CONCAT(`id_page` ORDER BY order_ms SEPARATOR ',') as ids
FROM mc_mainsectors WHERE type_ms = 'page'";
break;
case 'homeMsc':
$query = "SELECT
GROUP_CONCAT(`id_page` ORDER BY order_ms SEPARATOR ',') as ids
FROM mc_mainsectors WHERE type_ms = 'category'";
break;
default:
return false;
}
try {
return component_routing_db::layer()->fetch($query, $params);
}
catch (Exception $e) {
if(!isset($this->logger)) $this->logger = new debug_logger(MP_LOG_DIR);
$this->logger->log('statement','db',$e->getMessage(),$this->logger::LOG_MONTH);
}
}
return false;
}
/**
* @param array $config
* @param array $params
* @return bool
*/
public function insert(array $config, array $params = []): bool {
switch ($config['type']) {
case 'ms_p':
$query = 'INSERT INTO mc_mainsectors (id_page, type_ms, order_ms)
SELECT :id, :type, COUNT(order_ms) FROM mc_mainsectors';
break;
default:
return false;
}
try {
component_routing_db::layer()->insert($query,$params);
return true;
}
catch (Exception $e) {
if(!isset($this->logger)) $this->logger = new debug_logger(MP_LOG_DIR);
$this->logger->log('statement','db',$e->getMessage(),$this->logger::LOG_MONTH);
return false;
}
}
/**
* @param array $config
* @param array $params
* @return bool
*/
public function update(array $config, array $params = []): bool {
switch ($config['type']) {
case 'order':
$query = 'UPDATE mc_mainsectors
SET order_ms = :order_ms
WHERE id_ms = :id_ms';
break;
default:
return false;
}
try {
component_routing_db::layer()->update($query,$params);
return true;
}
catch (Exception $e) {
if(!isset($this->logger)) $this->logger = new debug_logger(MP_LOG_DIR);
$this->logger->log('statement','db',$e->getMessage(),$this->logger::LOG_MONTH);
return false;
}
}
/**
* @param array $config
* @param array $params
* @return bool
*/
public function delete(array $config, array $params = []): bool {
switch ($config['type']) {
case 'ms':
$query = 'DELETE FROM mc_mainsectors
WHERE id_ms = :id';
break;
default:
return false;
}
try {
component_routing_db::layer()->delete($query,$params);
return true;
}
catch (Exception $e) {
if(!isset($this->logger)) $this->logger = new debug_logger(MP_LOG_DIR);
$this->logger->log('statement','db',$e->getMessage(),$this->logger::LOG_MONTH);
return false;
}
}
}