XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
xoopstopic.php
1<?php
13
14if (!defined('XOOPS_ROOT_PATH')) {
15 exit();
16}
17include_once XOOPS_ROOT_PATH . '/class/xoopstree.php';
18
19class XoopsTopic
20{
21 public $table;
22 public $topic_id;
23 public $topic_pid;
24 public $topic_title;
25 public $topic_imgurl;
26 public $prefix; // only used in topic tree
27 public $use_permission=false;
28 public $mid; // module id used for setting permission
29
30 public function __construct($table, $topicid=0)
31 {
32 $this->db =& Database::getInstance();
33 $this->table = $table;
34 if (is_array($topicid)) {
35 $this->makeTopic($topicid);
36 } elseif (0 !== $topicid) {
37 $this->getTopic((int)$topicid);
38 } else {
39 $this->topic_id = $topicid;
40 }
41 }
42 public function XoopsTopic($table, $topicid=0)
43 {
44 return $this->__construct($table, $topicid);
45 }
46
47 public function setTopicTitle($value)
48 {
49 $this->topic_title = $value;
50 }
51
52 public function setTopicImgurl($value)
53 {
54 $this->topic_imgurl = $value;
55 }
56
57 public function setTopicPid($value)
58 {
59 $this->topic_pid = $value;
60 }
61
62 public function getTopic($topicid)
63 {
64 $sql = 'SELECT * FROM ' . $this->table . ' WHERE topic_id=' . $topicid . '';
65 $array = $this->db->fetchArray($this->db->query($sql));
66 $this->makeTopic($array);
67 }
68
69 public function makeTopic($array)
70 {
71 foreach ($array as $key=>$value) {
72 $this->$key = $value;
73 }
74 }
75
76 public function usePermission($mid)
77 {
78 $this->mid = $mid;
79 $this->use_permission = true;
80 }
81
82 public function store()
83 {
85 $title = '';
86 $imgurl = '';
87 if (isset($this->topic_title) && '' !== $this->topic_title) {
88 $title = $myts->makeTboxData4Save($this->topic_title);
89 }
90 if (isset($this->topic_imgurl) && '' !== $this->topic_imgurl) {
91 $imgurl = $myts->makeTboxData4Save($this->topic_imgurl);
92 }
93 if (!isset($this->topic_pid) || !is_numeric($this->topic_pid)) {
94 $this->topic_pid = 0;
95 }
96 if (empty($this->topic_id)) {
97 $this->topic_id = $this->db->genId($this->table . '_topic_id_seq');
98 $sql = sprintf("INSERT INTO %s (topic_id, topic_pid, topic_imgurl, topic_title) VALUES (%u, %u, '%s', '%s')", $this->table, $this->topic_id, $this->topic_pid, $imgurl, $title);
99 } else {
100 $sql = sprintf("UPDATE %s SET topic_pid = %u, topic_imgurl = '%s', topic_title = '%s' WHERE topic_id = %u", $this->table, $this->topic_pid, $imgurl, $title, $this->topic_id);
101 }
102 if (!$result = $this->db->query($sql)) {
103 (new ErrorHandler)->show('0022');
104 }
105 if (true == $this->use_permission) {
106 if (empty($this->topic_id)) {
107 $this->topic_id = $this->db->getInsertId();
108 }
109 $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
110 $parent_topics = $xt->getAllParentId($this->topic_id);
111 if (!empty($this->m_groups) && is_array($this->m_groups)) {
112 foreach ($this->m_groups as $m_g) {
113 $moderate_topics = XoopsPerms::getPermitted($this->mid, 'ModInTopic', $m_g);
114 $add = true;
115 // only grant this permission when the group has this permission in all parent topics of the created topic
116 foreach ($parent_topics as $p_topic) {
117 if (!in_array($p_topic, $moderate_topics, true)) {
118 $add = false;
119 continue;
120 }
121 }
122 if (true == $add) {
123 $xp = new XoopsPerms();
124 $xp->setModuleId($this->mid);
125 $xp->setName('ModInTopic');
126 $xp->setItemId($this->topic_id);
127 $xp->store();
128 $xp->addGroup($m_g);
129 }
130 }
131 }
132 if (!empty($this->s_groups) && is_array($this->s_groups)) {
133 foreach ($s_groups as $s_g) {
134 $submit_topics = XoopsPerms::getPermitted($this->mid, 'SubmitInTopic', $s_g);
135 $add = true;
136 foreach ($parent_topics as $p_topic) {
137 if (!in_array($p_topic, $submit_topics, true)) {
138 $add = false;
139 continue;
140 }
141 }
142 if (true == $add) {
143 $xp = new XoopsPerms();
144 $xp->setModuleId($this->mid);
145 $xp->setName('SubmitInTopic');
146 $xp->setItemId($this->topic_id);
147 $xp->store();
148 $xp->addGroup($s_g);
149 }
150 }
151 }
152 if (!empty($this->r_groups) && is_array($this->r_groups)) {
153 foreach ($r_groups as $r_g) {
154 $read_topics = XoopsPerms::getPermitted($this->mid, 'ReadInTopic', $r_g);
155 $add = true;
156 foreach ($parent_topics as $p_topic) {
157 if (!in_array($p_topic, $read_topics, true)) {
158 $add = false;
159 continue;
160 }
161 }
162 if (true == $add) {
163 $xp = new XoopsPerms();
164 $xp->setModuleId($this->mid);
165 $xp->setName('ReadInTopic');
166 $xp->setItemId($this->topic_id);
167 $xp->store();
168 $xp->addGroup($r_g);
169 }
170 }
171 }
172 }
173 return true;
174 }
175
176 public function delete()
177 {
178 $sql = sprintf('DELETE FROM %s WHERE topic_id = %u', $this->table, $this->topic_id);
179 $this->db->query($sql);
180 }
181
182 public function topic_id()
183 {
184 return $this->topic_id;
185 }
186
187 public function topic_pid()
188 {
189 return $this->topic_pid;
190 }
191
192 public function topic_title($format= 'S')
193 {
195 switch ($format) {
196 case 'S':
197 $title = $myts->makeTboxData4Show($this->topic_title);
198 break;
199 case 'E':
200 $title = $myts->makeTboxData4Edit($this->topic_title);
201 break;
202 case 'P':
203 $title = $myts->makeTboxData4Preview($this->topic_title);
204 break;
205 case 'F':
206 $title = $myts->makeTboxData4PreviewInForm($this->topic_title);
207 break;
208 }
209 return $title;
210 }
211
212 public function topic_imgurl($format= 'S')
213 {
215 switch ($format) {
216 case 'S':
217 $imgurl= $myts->makeTboxData4Show($this->topic_imgurl);
218 break;
219 case 'E':
220 $imgurl = $myts->makeTboxData4Edit($this->topic_imgurl);
221 break;
222 case 'P':
223 $imgurl = $myts->makeTboxData4Preview($this->topic_imgurl);
224 break;
225 case 'F':
226 $imgurl = $myts->makeTboxData4PreviewInForm($this->topic_imgurl);
227 break;
228 }
229 return $imgurl;
230 }
231
232 public function prefix()
233 {
234 if (isset($this->prefix)) {
235 return $this->prefix;
236 }
237 }
238
239 public function getFirstChildTopics()
240 {
241 $ret = [];
242 $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
243 $topic_arr = $xt->getFirstChild($this->topic_id, 'topic_title');
244 if (is_array($topic_arr) && count($topic_arr)) {
245 foreach ($topic_arr as $topic) {
246 $ret[] = new XoopsTopic($this->table, $topic);
247 }
248 }
249 return $ret;
250 }
251
252 public function getAllChildTopics()
253 {
254 $ret = [];
255 $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
256 $topic_arr = $xt->getAllChild($this->topic_id, 'topic_title');
257 if (is_array($topic_arr) && count($topic_arr)) {
258 foreach ($topic_arr as $topic) {
259 $ret[] = new XoopsTopic($this->table, $topic);
260 }
261 }
262 return $ret;
263 }
264
265 public function getChildTopicsTreeArray()
266 {
267 $ret = [];
268 $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
269 $topic_arr = $xt->getChildTreeArray($this->topic_id, 'topic_title');
270 if (is_array($topic_arr) && count($topic_arr)) {
271 foreach ($topic_arr as $topic) {
272 $ret[] = new XoopsTopic($this->table, $topic);
273 }
274 }
275 return $ret;
276 }
277
278 public function makeTopicSelBox($none=0, $seltopic=-1, $selname= '', $onchange= '')
279 {
280 $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
281 if (-1 !== $seltopic) {
282 $xt->makeMySelBox('topic_title', 'topic_title', $seltopic, $none, $selname, $onchange);
283 } elseif (!empty($this->topic_id)) {
284 $xt->makeMySelBox('topic_title', 'topic_title', $this->topic_id, $none, $selname, $onchange);
285 } else {
286 $xt->makeMySelBox('topic_title', 'topic_title', 0, $none, $selname, $onchange);
287 }
288 }
289
290 //generates nicely formatted linked path from the root id to a given id
291 public function getNiceTopicPathFromId($funcURL)
292 {
293 $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
294 $ret = $xt->getNicePathFromId($this->topic_id, 'toppic_title', $funcURL);
295 return $ret;
296 }
297
298 public function getAllChildTopicsId()
299 {
300 $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
301 $ret = $xt->getAllChildId($this->topic_id, 'toppic_title');
302 return $ret;
303 }
304
305 public function &getTopicsList()
306 {
307 $result = $this->db->query('SELECT topic_id, topic_pid, topic_title FROM '.$this->table);
308 $ret = [];
310 while ($myrow = $this->db->fetchArray($result)) {
311 $ret[$myrow['topic_id']] = ['title' => $myts->htmlSpecialChars($myrow['topic_title']), 'pid' => $myrow['topic_pid']];
312 }
313 return $ret;
314 }
315
316 public function topicExists($pid, $title)
317 {
318 $sql = 'SELECT COUNT(*) from ' . $this->table . ' WHERE topic_pid = ' . (int)$pid . " AND topic_title = '" . trim($title) . "'";
319 $rs = $this->db->query($sql);
320 list($count) = $this->db->fetchRow($rs);
321 return $count > 0;
322 }
323}