-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
executable file
·167 lines (154 loc) · 4.43 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
<?php
class plugins_homepages_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
hs.id_hs,
hs.id_pages,
pc.name_pages as name_hs
FROM mc_homepages as hs
LEFT JOIN mc_cms_page as p ON (hs.id_pages = p.id_pages)
LEFT JOIN mc_cms_page_content as pc ON (p.id_pages = pc.id_pages AND pc.id_lang = :id_lang)
ORDER BY hs.order_hs ASC";
break;
case 'pages':
$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
ORDER BY mcp.id_pages';
break;
case 'order':
$query = 'SELECT
id_pages,
order_hs
FROM mc_homepages ORDER BY order_hs ASC';
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 'newHs':
$query = "SELECT
hs.id_hs,
hs.id_pages,
pc.name_pages as name_hs
FROM mc_homepages as hs
LEFT JOIN mc_cms_page as p ON (hs.id_pages = p.id_pages)
LEFT JOIN mc_cms_page_content as pc ON (p.id_pages = pc.id_pages AND pc.id_lang = :id_lang)
ORDER BY hs.order_hs DESC LIMIT 0,1";
break;
case 'homeMsp':
$query = "SELECT
GROUP_CONCAT(`id_pages` ORDER BY order_hs SEPARATOR ',') as hsids
FROM mc_homepages";
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|string
*/
public function insert(array $config, array $params = []) {
switch ($config['type']) {
case 'homepages':
$query = 'INSERT INTO mc_homepages (id_pages, order_hs)
SELECT :id, COUNT(order_hs) FROM mc_homepages';
break;
default:
return false;
}
try {
component_routing_db::layer()->insert($query,$params);
return true;
}
catch (Exception $e) {
return 'Exception reçue : '.$e->getMessage();
}
}
/**
* @param array $config
* @param array $params
* @return bool|string
*/
public function update(array $config, array $params = []) {
switch ($config['type']) {
case 'order':
$query = 'UPDATE mc_homepages
SET order_hs = :order_hs
WHERE id_hs = :id_hs';
break;
default:
return false;
}
try {
component_routing_db::layer()->update($query,$params);
return true;
}
catch (Exception $e) {
return 'Exception reçue : '.$e->getMessage();
}
}
/**
* @param $config
* @param array $params
* @return bool|string
*/
public function delete($config, $params = array())
{
if (!is_array($config)) return '$config must be an array';
$query = '';
switch ($config['type']) {
case 'homepages':
$query = 'DELETE FROM mc_homepages
WHERE id_hs = :id';
break;
}
if($query === '') return 'Unknown request asked';
try {
component_routing_db::layer()->delete($query,$params);
return true;
}
catch (Exception $e) {
return 'Exception reçue : '.$e->getMessage();
}
}
}