1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Newbb; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* NewBB 5.0x, the forum module for XOOPS project |
7
|
|
|
* |
8
|
|
|
* @copyright XOOPS Project (https://xoops.org) |
9
|
|
|
* @license GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html) |
10
|
|
|
* @author Taiwen Jiang (phppp or D.J.) <[email protected]> |
11
|
|
|
* @since 4.00 |
12
|
|
|
* @package module::newbb |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Type object handler class. |
19
|
|
|
* @package module::newbb |
20
|
|
|
* |
21
|
|
|
* @author D.J. (phppp) |
22
|
|
|
* @copyright copyright © 2006 XOOPS Project |
23
|
|
|
*/ |
24
|
|
|
class TypeHandler extends \XoopsPersistableObjectHandler |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param null|\XoopsDatabase $db |
28
|
|
|
*/ |
29
|
|
|
public function __construct(\XoopsDatabase $db = null) |
30
|
|
|
{ |
31
|
|
|
parent::__construct($db, 'newbb_type', Type::class, 'type_id', 'type_name'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get types linked to a forum |
36
|
|
|
* |
37
|
|
|
* @param mixed $forums single forum ID or an array of forum IDs |
38
|
|
|
* @return array associative array of types (name, color, order) |
39
|
|
|
*/ |
40
|
|
|
public function getByForum($forums = null) |
41
|
|
|
{ |
42
|
|
|
$ret = []; |
43
|
|
|
|
44
|
|
|
$forums = (\is_array($forums) ? \array_filter(\array_map('\intval', \array_map('\trim', $forums))) : (empty($forums) ? 0 : [(int)$forums])); |
45
|
|
|
|
46
|
|
|
$sql = ' SELECT o.type_id, o.type_name, o.type_color, l.type_order' |
47
|
|
|
. ' FROM ' |
48
|
|
|
. $this->db->prefix('newbb_type_forum') |
49
|
|
|
. ' AS l ' |
50
|
|
|
. " LEFT JOIN {$this->table} AS o ON o.{$this->keyName} = l.{$this->keyName} " |
51
|
|
|
. ' WHERE ' |
52
|
|
|
. ' l.forum_id ' |
53
|
|
|
. (empty($forums) ? 'IS NOT NULL' : 'IN (' . \implode(', ', $forums) . ')') |
|
|
|
|
54
|
|
|
. ' ORDER BY l.type_order ASC'; |
55
|
|
|
if (false === ($result = $this->db->query($sql))) { |
56
|
|
|
//xoops_error($this->db->error()); |
57
|
|
|
return $ret; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
while (false !== ($myrow = $this->db->fetchArray($result))) { |
61
|
|
|
$ret[$myrow[$this->keyName]] = [ |
62
|
|
|
'type_id' => $myrow[$this->keyName], |
63
|
|
|
'type_order' => $myrow['type_order'], |
64
|
|
|
'type_name' => \htmlspecialchars($myrow['type_name'], \ENT_QUOTES | \ENT_HTML5), |
65
|
|
|
'type_color' => \htmlspecialchars($myrow['type_color'], \ENT_QUOTES | \ENT_HTML5), |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $ret; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Update types linked to a forum |
74
|
|
|
* |
75
|
|
|
* @param int $forum_id |
76
|
|
|
* @param array $types |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function updateByForum($forum_id, $types) |
80
|
|
|
{ |
81
|
|
|
$forum_id = (int)$forum_id; |
82
|
|
|
if (empty($forum_id)) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$types_existing = $this->getByForum($forum_id); |
87
|
|
|
$types_valid = []; |
88
|
|
|
$types_add = []; |
89
|
|
|
$types_update = []; |
90
|
|
|
foreach (\array_keys($types_existing) as $key) { |
91
|
|
|
if (empty($types[$key])) { |
92
|
|
|
continue; |
93
|
|
|
} |
94
|
|
|
$types_valid[] = $key; |
95
|
|
|
if ($types[$key] !== $types_existing[$key]['type_order']) { |
96
|
|
|
$types_update[] = $key; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
foreach (\array_keys($types) as $key) { |
100
|
|
|
if (!empty($types[$key]) && !isset($types_existing[$key])) { |
101
|
|
|
$types_add[] = $key; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
$types_valid = \array_filter($types_valid); |
105
|
|
|
$types_add = \array_filter($types_add); |
106
|
|
|
$types_update = \array_filter($types_update); |
107
|
|
|
|
108
|
|
|
if (!empty($types_valid)) { |
109
|
|
|
$sql = 'DELETE FROM ' . $this->db->prefix('newbb_type_forum') . ' WHERE ' . ' forum_id = ' . $forum_id . ' AND ' . // irmtfan bug fix: delete other forums types when update the type for a specific forum |
110
|
|
|
" {$this->keyName} NOT IN (" . \implode(', ', $types_valid) . ')'; |
111
|
|
|
if (false === ($result = $this->db->queryF($sql))) { |
|
|
|
|
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (!empty($types_update)) { |
116
|
|
|
$type_query = []; |
|
|
|
|
117
|
|
|
foreach ($types_update as $key) { |
118
|
|
|
$order = $types[$key]; |
119
|
|
|
if ($types_existing[$key]['type_order'] == $order) { |
120
|
|
|
continue; |
121
|
|
|
} |
122
|
|
|
$sql = 'UPDATE ' . $this->db->prefix('newbb_type_forum') . " SET type_order = {$order}" . " WHERE {$this->keyName} = {$key} AND forum_id = {$forum_id}"; |
123
|
|
|
if (false === ($result = $this->db->queryF($sql))) { |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (!empty($types_add)) { |
129
|
|
|
$type_query = []; |
130
|
|
|
foreach ($types_add as $key) { |
131
|
|
|
$order = $types[$key]; |
132
|
|
|
//if (!in_array($key, $types_add)) continue; |
133
|
|
|
$type_query[] = "({$key}, {$forum_id}, {$order})"; |
134
|
|
|
} |
135
|
|
|
$sql = 'INSERT INTO ' . $this->db->prefix('newbb_type_forum') . ' (type_id, forum_id, type_order) ' . ' VALUES ' . \implode(', ', $type_query); |
136
|
|
|
if (false === ($result = $this->db->queryF($sql))) { |
137
|
|
|
//xoops_error($this->db->error()); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return true; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* delete an object as well as links relying on it |
146
|
|
|
* |
147
|
|
|
* @param Type|\XoopsObject $object {@link Type} |
148
|
|
|
* @param bool $force flag to force the query execution despite security settings |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
|
|
public function delete(\XoopsObject $object, $force = true) |
152
|
|
|
{ |
153
|
|
|
if (!\is_object($object) || !$object->getVar($this->keyName)) { |
154
|
|
|
return false; |
155
|
|
|
} |
156
|
|
|
$queryFunc = empty($force) ? 'query' : 'queryF'; |
157
|
|
|
|
158
|
|
|
/* |
159
|
|
|
* Remove forum-type links |
160
|
|
|
*/ |
161
|
|
|
$sql = 'DELETE' . ' FROM ' . $this->db->prefix('newbb_type_forum') . ' WHERE ' . $this->keyName . ' = ' . $object->getVar($this->keyName); |
162
|
|
|
if (false === ($result = $this->db->{$queryFunc}($sql))) { |
|
|
|
|
163
|
|
|
// xoops_error($this->db->error()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/* |
167
|
|
|
* Reset topic type linked to this type |
168
|
|
|
*/ |
169
|
|
|
$sql = 'UPATE' . ' ' . $this->db->prefix('newbb_topics') . ' SET ' . $this->keyName . '=0' . ' WHERE ' . $this->keyName . ' = ' . $object->getVar($this->keyName); |
170
|
|
|
if (false === ($result = $this->db->{$queryFunc}($sql))) { |
171
|
|
|
//xoops_error($this->db->error()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return parent::delete($object, $force); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* clean orphan links from database |
179
|
|
|
* |
180
|
|
|
* @param string $table_link |
181
|
|
|
* @param string $field_link |
182
|
|
|
* @param string $field_object |
183
|
|
|
* @return bool true on success |
184
|
|
|
*/ |
185
|
|
|
public function cleanOrphan($table_link = '', $field_link = '', $field_object = '') //cleanOrphan() |
186
|
|
|
{ |
187
|
|
|
/* clear forum-type links */ |
188
|
|
|
$sql = 'DELETE FROM ' . $this->db->prefix('newbb_type_forum') . " WHERE ({$this->keyName} NOT IN ( SELECT DISTINCT {$this->keyName} FROM {$this->table}) )"; |
189
|
|
|
$this->db->queryF($sql); |
190
|
|
|
|
191
|
|
|
/* reconcile topic-type link */ |
192
|
|
|
$sql = 'UPATE ' . $this->db->prefix('newbb_topics') . " SET {$this->keyName} = 0" . " WHERE ({$this->keyName} NOT IN ( SELECT DISTINCT {$this->keyName} FROM {$this->table}) )"; |
193
|
|
|
$this->db->queryF($sql); |
194
|
|
|
|
195
|
|
|
return true; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|