1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19:
20: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
21:
22: $GLOBALS['xoopsLogger']->addDeprecated("'/class/xoopstopic.php' is deprecated since XOOPS 2.5.4, please create your own class instead.");
23:
24: include_once XOOPS_ROOT_PATH . '/class/xoopstree.php';
25:
26: 27: 28:
29: class XoopsTopic
30: {
31: public $table;
32: public $topic_id;
33: public $topic_pid;
34: public $topic_title;
35: public $topic_imgurl;
36: public $prefix;
37: public $use_permission = false;
38: public $mid;
39:
40: 41: 42: 43:
44: public function __construct($table, $topicid = 0)
45: {
46: $this->db = XoopsDatabaseFactory::getDatabaseConnection();
47: $this->table = $table;
48: if (is_array($topicid)) {
49: $this->makeTopic($topicid);
50: } elseif ($topicid != 0) {
51: $this->getTopic((int)$topicid);
52: } else {
53: $this->topic_id = $topicid;
54: }
55: }
56:
57: 58: 59:
60: public function setTopicTitle($value)
61: {
62: $this->topic_title = $value;
63: }
64:
65: 66: 67:
68: public function setTopicImgurl($value)
69: {
70: $this->topic_imgurl = $value;
71: }
72:
73: 74: 75:
76: public function setTopicPid($value)
77: {
78: $this->topic_pid = $value;
79: }
80:
81: 82: 83:
84: public function getTopic($topicid)
85: {
86: $topicid = (int)$topicid;
87: $sql = 'SELECT * FROM ' . $this->table . ' WHERE topic_id=' . $topicid . '';
88: $array = $this->db->fetchArray($this->db->query($sql));
89: $this->makeTopic($array);
90: }
91:
92: 93: 94:
95: public function makeTopic($array)
96: {
97: foreach ($array as $key => $value) {
98: $this->$key = $value;
99: }
100: }
101:
102: 103: 104:
105: public function usePermission($mid)
106: {
107: $this->mid = $mid;
108: $this->use_permission = true;
109: }
110:
111: 112: 113:
114: public function store()
115: {
116: $myts = MyTextSanitizer::getInstance();
117: $title = '';
118: $imgurl = '';
119: if (isset($this->topic_title) && $this->topic_title != '') {
120: $title = $myts->addSlashes($this->topic_title);
121: }
122: if (isset($this->topic_imgurl) && $this->topic_imgurl != '') {
123: $imgurl = $myts->addSlashes($this->topic_imgurl);
124: }
125: if (!isset($this->topic_pid) || !is_numeric($this->topic_pid)) {
126: $this->topic_pid = 0;
127: }
128: if (empty($this->topic_id)) {
129: $this->topic_id = $this->db->genId($this->table . '_topic_id_seq');
130: $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);
131: } else {
132: $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);
133: }
134: if (!$result = $this->db->query($sql)) {
135: ErrorHandler::show('0022');
136: }
137: if ($this->use_permission == true) {
138: if (empty($this->topic_id)) {
139: $this->topic_id = $this->db->getInsertId();
140: }
141: $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
142: $parent_topics = $xt->getAllParentId($this->topic_id);
143: if (!empty($this->m_groups) && is_array($this->m_groups)) {
144: foreach ($this->m_groups as $m_g) {
145: $moderate_topics = XoopsPerms::getPermitted($this->mid, 'ModInTopic', $m_g);
146: $add = true;
147:
148: foreach ($parent_topics as $p_topic) {
149: if (!in_array($p_topic, $moderate_topics)) {
150: $add = false;
151: continue;
152: }
153: }
154: if ($add == true) {
155: $xp = new XoopsPerms();
156: $xp->setModuleId($this->mid);
157: $xp->setName('ModInTopic');
158: $xp->setItemId($this->topic_id);
159: $xp->store();
160: $xp->addGroup($m_g);
161: }
162: }
163: }
164: if (!empty($this->s_groups) && is_array($this->s_groups)) {
165: foreach ($s_groups as $s_g) {
166: $submit_topics = XoopsPerms::getPermitted($this->mid, 'SubmitInTopic', $s_g);
167: $add = true;
168: foreach ($parent_topics as $p_topic) {
169: if (!in_array($p_topic, $submit_topics)) {
170: $add = false;
171: continue;
172: }
173: }
174: if ($add == true) {
175: $xp = new XoopsPerms();
176: $xp->setModuleId($this->mid);
177: $xp->setName('SubmitInTopic');
178: $xp->setItemId($this->topic_id);
179: $xp->store();
180: $xp->addGroup($s_g);
181: }
182: }
183: }
184: if (!empty($this->r_groups) && is_array($this->r_groups)) {
185: foreach ($r_groups as $r_g) {
186: $read_topics = XoopsPerms::getPermitted($this->mid, 'ReadInTopic', $r_g);
187: $add = true;
188: foreach ($parent_topics as $p_topic) {
189: if (!in_array($p_topic, $read_topics)) {
190: $add = false;
191: continue;
192: }
193: }
194: if ($add == true) {
195: $xp = new XoopsPerms();
196: $xp->setModuleId($this->mid);
197: $xp->setName('ReadInTopic');
198: $xp->setItemId($this->topic_id);
199: $xp->store();
200: $xp->addGroup($r_g);
201: }
202: }
203: }
204: }
205:
206: return true;
207: }
208:
209: public function delete()
210: {
211: $sql = sprintf('DELETE FROM %s WHERE topic_id = %u', $this->table, $this->topic_id);
212: $this->db->query($sql);
213: }
214:
215: 216: 217:
218: public function topic_id()
219: {
220: return $this->topic_id;
221: }
222:
223: public function topic_pid()
224: {
225: return $this->topic_pid;
226: }
227:
228: 229: 230: 231: 232:
233: public function topic_title($format = 'S')
234: {
235: $myts = MyTextSanitizer::getInstance();
236: switch ($format) {
237: case 'S':
238: case 'E':
239: $title = $myts->htmlSpecialChars($this->topic_title);
240: break;
241: case 'P':
242: case 'F':
243: $title = $myts->htmlSpecialChars($myts->stripSlashesGPC($this->topic_title));
244: break;
245: }
246:
247: return $title;
248: }
249:
250: 251: 252: 253: 254:
255: public function topic_imgurl($format = 'S')
256: {
257: $myts = MyTextSanitizer::getInstance();
258: switch ($format) {
259: case 'S':
260: case 'E':
261: $imgurl = $myts->htmlSpecialChars($this->topic_imgurl);
262: break;
263: case 'P':
264: case 'F':
265: $imgurl = $myts->htmlSpecialChars($myts->stripSlashesGPC($this->topic_imgurl));
266: break;
267: }
268:
269: return $imgurl;
270: }
271:
272: public function prefix()
273: {
274: if (isset($this->prefix)) {
275: return $this->prefix;
276: }
277: return null;
278: }
279:
280: 281: 282:
283: public function getFirstChildTopics()
284: {
285: $ret = array();
286: $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
287: $topic_arr = $xt->getFirstChild($this->topic_id, 'topic_title');
288: if (is_array($topic_arr) && count($topic_arr)) {
289: foreach ($topic_arr as $topic) {
290: $ret[] = new XoopsTopic($this->table, $topic);
291: }
292: }
293:
294: return $ret;
295: }
296:
297: 298: 299:
300: public function getAllChildTopics()
301: {
302: $ret = array();
303: $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
304: $topic_arr = $xt->getAllChild($this->topic_id, 'topic_title');
305: if (is_array($topic_arr) && count($topic_arr)) {
306: foreach ($topic_arr as $topic) {
307: $ret[] = new XoopsTopic($this->table, $topic);
308: }
309: }
310:
311: return $ret;
312: }
313:
314: 315: 316:
317: public function getChildTopicsTreeArray()
318: {
319: $ret = array();
320: $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
321: $topic_arr = $xt->getChildTreeArray($this->topic_id, 'topic_title');
322: if (is_array($topic_arr) && count($topic_arr)) {
323: foreach ($topic_arr as $topic) {
324: $ret[] = new XoopsTopic($this->table, $topic);
325: }
326: }
327:
328: return $ret;
329: }
330:
331: 332: 333: 334: 335: 336:
337: public function makeTopicSelBox($none = 0, $seltopic = -1, $selname = '', $onchange = '')
338: {
339: $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
340: if ($seltopic != -1) {
341: $xt->makeMySelBox('topic_title', 'topic_title', $seltopic, $none, $selname, $onchange);
342: } elseif (!empty($this->topic_id)) {
343: $xt->makeMySelBox('topic_title', 'topic_title', $this->topic_id, $none, $selname, $onchange);
344: } else {
345: $xt->makeMySelBox('topic_title', 'topic_title', 0, $none, $selname, $onchange);
346: }
347: }
348:
349:
350: 351: 352: 353: 354:
355: public function getNiceTopicPathFromId($funcURL)
356: {
357: $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
358: $ret = $xt->getNicePathFromId($this->topic_id, 'topic_title', $funcURL);
359:
360: return $ret;
361: }
362:
363: 364: 365:
366: public function getAllChildTopicsId()
367: {
368: $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
369: $ret = $xt->getAllChildId($this->topic_id, 'topic_title');
370:
371: return $ret;
372: }
373:
374: 375: 376:
377: public function getTopicsList()
378: {
379: $result = $this->db->query('SELECT topic_id, topic_pid, topic_title FROM ' . $this->table);
380: $ret = array();
381: $myts = MyTextSanitizer::getInstance();
382: while ($myrow = $this->db->fetchArray($result)) {
383: $ret[$myrow['topic_id']] = array('title' => $myts->htmlspecialchars($myrow['topic_title']), 'pid' => $myrow['topic_pid']);
384: }
385:
386: return $ret;
387: }
388:
389: 390: 391: 392: 393: 394:
395: public function topicExists($pid, $title)
396: {
397: $sql = 'SELECT COUNT(*) from ' . $this->table . ' WHERE topic_pid = ' . (int)$pid . " AND topic_title = '" . trim($title) . "'";
398: $rs = $this->db->query($sql);
399: list($count) = $this->db->fetchRow($rs);
400: if ($count > 0) {
401: return true;
402: } else {
403: return false;
404: }
405: }
406: }
407: