-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
208 lines (189 loc) · 5.75 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
<?php
class plugins_advantage_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 = []) {
$dateFormat = new component_format_date();
if ($config['context'] === 'all') {
switch ($config['type']) {
case 'advs':
$query = 'SELECT
ad.id_adv,
ad.icon_adv,
adc.title_adv,
adc.desc_adv,
adc.url_adv
FROM mc_advantage AS ad
JOIN mc_advantage_content AS adc USING(id_adv)
JOIN mc_lang AS lang ON(adc.id_lang = lang.id_lang)
WHERE adc.id_lang = :default_lang
ORDER BY order_adv';
break;
case 'homeAdvs':
$query = 'SELECT
ad.id_adv,
ad.icon_adv,
adc.title_adv,
adc.desc_adv,
adc.url_adv,
adc.blank_adv
FROM mc_advantage as ad
LEFT JOIN mc_advantage_content as adc USING(id_adv)
LEFT JOIN mc_lang as l USING(id_lang)
WHERE iso_lang = :lang
ORDER BY order_adv';
break;
case 'adv':
$query = 'SELECT a.*,c.*
FROM mc_advantage AS a
JOIN mc_advantage_content AS c USING(id_adv)
JOIN mc_lang AS lang ON(c.id_lang = lang.id_lang)
WHERE c.id_lang = :default_lang';
break;
case 'advContent':
$query = 'SELECT a.*,c.*
FROM mc_advantage AS a
JOIN mc_advantage_content AS c USING(id_adv)
JOIN mc_lang AS lang ON(c.id_lang = lang.id_lang)
WHERE c.id_adv = :id';
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 'advContent':
$query = 'SELECT * FROM mc_advantage_content WHERE id_adv = :id AND id_lang = :id_lang';
break;
case 'lastAdv':
$query = 'SELECT * FROM mc_advantage ORDER BY id_adv DESC LIMIT 0,1';
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 string $type
* @param array $params
* @return bool|string
*/
public function insert(string $type, array $params = []) {
$query = '';
switch ($type) {
case 'adv':
$query = 'INSERT INTO mc_advantage (icon_adv, order_adv, date_register)
SELECT :icon_adv, COUNT(id_adv), NOW() FROM mc_advantage';
break;
case 'advContent':
$query = 'INSERT INTO mc_advantage_content(id_adv, id_lang, title_adv, desc_adv, url_adv, blank_adv)
VALUES (:id_adv, :id_lang, :title_adv, :desc_adv, :url_adv, :blank_adv)';
break;
default:
return false;
}
if($query === '') return 'Unknown request asked';
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 string $type
* @param array $params
* @return bool|string
*/
public function update(string $type, array $params = []) {
$query = '';
switch ($type) {
case 'adv':
$query = 'UPDATE mc_advantage
SET
icon_adv = :icon_adv
WHERE id_adv = :id';
break;
case 'advContent':
$query = 'UPDATE mc_advantage_content
SET
title_adv = :title_adv,
desc_adv = :desc_adv,
url_adv = :url_adv,
blank_adv = :blank_adv
WHERE id_content = :id
AND id_lang = :id_lang';
break;
case 'order':
$query = 'UPDATE mc_advantage
SET order_adv = :order_adv
WHERE id_adv = :id';
break;
default:
return false;
}
if($query === '') return 'Unknown request asked';
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|string
*/
public function delete(array $config, array $params = []) {
$query = '';
switch ($config['type']) {
case 'adv':
$query = 'DELETE FROM mc_advantage
WHERE id_adv = :id';
break;
default:
return false;
}
if($query === '') return 'Unknown request asked';
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;
}
}