-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.php
121 lines (114 loc) · 3.04 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
<?php
class plugins_homeblock_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 'pages':
$query = 'SELECT h.*,c.*
FROM mc_homeblock AS h
JOIN mc_homeblock_content AS c USING(id_homeblock)
JOIN mc_lang AS lang ON(c.id_lang = lang.id_lang)';
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 'root':
$query = 'SELECT * FROM mc_homeblock ORDER BY id_homeblock DESC LIMIT 0,1';
break;
case 'content':
$query = 'SELECT * FROM mc_homeblock_content WHERE id_homeblock = :id AND id_lang = :id_lang';
break;
case 'page':
$query = 'SELECT *
FROM mc_homeblock as g
JOIN mc_homeblock_content as gc USING(id_homeblock)
JOIN mc_lang as l USING(id_lang)
WHERE iso_lang = :lang
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 array $config
* @param array $params
* @return bool|string
*/
public function insert(array $config, array $params = []) {
switch ($config['type']) {
case 'root':
$query = 'INSERT INTO mc_homeblock(date_register) VALUES (NOW())';
break;
case 'content':
$query = 'INSERT INTO mc_homeblock_content(id_homeblock, id_lang, name_homeblock, content_homeblock, published_homeblock)
VALUES (:id, :id_lang, :name_homeblock, :content_homeblock, :published_homeblock)';
break;
default:
return false;
}
try {
component_routing_db::layer()->insert($query,$params);
return true;
}
catch (Exception $e) {
return 'Exception : '.$e->getMessage();
}
}
/**
* @param array $config
* @param array $params
* @return bool|string
*/
public function update(array $config, array $params = []) {
switch ($config['type']) {
case 'content':
$query = 'UPDATE mc_homeblock_content
SET
name_homeblock = :name_homeblock,
content_homeblock = :content_homeblock,
published_homeblock = :published_homeblock
WHERE id_homeblock = :id
AND id_lang = :id_lang';
break;
default:
return false;
}
try {
component_routing_db::layer()->update($query,$params);
return true;
}
catch (Exception $e) {
return 'Exception : '.$e->getMessage();
}
}
}