1: <?php
2: /**
3: * XOOPS news topic
4: *
5: * You may not change or alter any portion of this comment or credits
6: * of supporting developers from this source code or any supporting source code
7: * which is considered copyrighted (c) material of the original comment or credit authors.
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
13: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14: * @package kernel
15: * @since 2.0.0
16: * @author Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
17: * @deprecated
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: * Class XoopsTopic
28: */
29: class XoopsTopic
30: {
31: /**
32: * @var \XoopsMySQLDatabase
33: */
34: public $db;
35: public $table;
36: public $topic_id;
37: public $topic_pid;
38: public $topic_title;
39: public $topic_imgurl;
40: public $prefix; // only used in topic tree
41: public $use_permission = false;
42: public $mid; // module id used for setting permission
43:
44: /**
45: * @param $table
46: * @param int|array $topicid
47: */
48: public function __construct($table, $topicid = 0)
49: {
50: $this->db = XoopsDatabaseFactory::getDatabaseConnection();
51: $this->table = $table;
52: if (is_array($topicid)) {
53: $this->makeTopic($topicid);
54: } elseif ($topicid != 0) {
55: $this->getTopic((int)$topicid);
56: } else {
57: $this->topic_id = $topicid;
58: }
59: }
60:
61: /**
62: * @param $value
63: */
64: public function setTopicTitle($value)
65: {
66: $this->topic_title = $value;
67: }
68:
69: /**
70: * @param $value
71: */
72: public function setTopicImgurl($value)
73: {
74: $this->topic_imgurl = $value;
75: }
76:
77: /**
78: * @param $value
79: */
80: public function setTopicPid($value)
81: {
82: $this->topic_pid = $value;
83: }
84:
85: /**
86: * @param $topicid
87: */
88: public function getTopic($topicid)
89: {
90: $topicid = (int)$topicid;
91: $sql = 'SELECT * FROM ' . $this->table . ' WHERE topic_id=' . $topicid . '';
92: $result = $this->db->query($sql);
93: if (!$this->db->isResultSet($result)) {
94: throw new \RuntimeException(
95: \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR
96: );
97: }
98: $array = $this->db->fetchArray($result);
99: $this->makeTopic($array);
100: }
101:
102: /**
103: * @param $array
104: */
105: public function makeTopic($array)
106: {
107: foreach ($array as $key => $value) {
108: $this->$key = $value;
109: }
110: }
111:
112: /**
113: * @param $mid
114: */
115: public function usePermission($mid)
116: {
117: $this->mid = $mid;
118: $this->use_permission = true;
119: }
120:
121: /**
122: * @return bool
123: */
124: public function store()
125: {
126: $myts = \MyTextSanitizer::getInstance();
127: $title = '';
128: $imgurl = '';
129: if (isset($this->topic_title) && $this->topic_title != '') {
130: $title = $myts->addSlashes($this->topic_title);
131: }
132: if (isset($this->topic_imgurl) && $this->topic_imgurl != '') {
133: $imgurl = $myts->addSlashes($this->topic_imgurl);
134: }
135: if (!isset($this->topic_pid) || !is_numeric($this->topic_pid)) {
136: $this->topic_pid = 0;
137: }
138: if (empty($this->topic_id)) {
139: $this->topic_id = $this->db->genId($this->table . '_topic_id_seq');
140: $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);
141: } else {
142: $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);
143: }
144: if (!$result = $this->db->query($sql)) {
145: ErrorHandler::show('0022');
146: }
147: if ($this->use_permission == true) {
148: if (empty($this->topic_id)) {
149: $this->topic_id = $this->db->getInsertId();
150: }
151: $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
152: $parent_topics = $xt->getAllParentId($this->topic_id);
153: if (!empty($this->m_groups) && \is_array($this->m_groups)) {
154: foreach ($this->m_groups as $m_g) {
155: $moderate_topics = XoopsPerms::getPermitted($this->mid, 'ModInTopic', $m_g);
156: $add = true;
157: // only grant this permission when the group has this permission in all parent topics of the created topic
158: foreach ($parent_topics as $p_topic) {
159: if (!in_array($p_topic, $moderate_topics)) {
160: $add = false;
161: continue;
162: }
163: }
164: if ($add == true) {
165: $xp = new XoopsPerms();
166: $xp->setModuleId($this->mid);
167: $xp->setName('ModInTopic');
168: $xp->setItemId($this->topic_id);
169: $xp->store();
170: $xp->addGroup($m_g);
171: }
172: }
173: }
174: if (!empty($this->s_groups) && \is_array($this->s_groups)) {
175: foreach ($s_groups as $s_g) {
176: $submit_topics = XoopsPerms::getPermitted($this->mid, 'SubmitInTopic', $s_g);
177: $add = true;
178: foreach ($parent_topics as $p_topic) {
179: if (!in_array($p_topic, $submit_topics)) {
180: $add = false;
181: continue;
182: }
183: }
184: if ($add == true) {
185: $xp = new XoopsPerms();
186: $xp->setModuleId($this->mid);
187: $xp->setName('SubmitInTopic');
188: $xp->setItemId($this->topic_id);
189: $xp->store();
190: $xp->addGroup($s_g);
191: }
192: }
193: }
194: if (!empty($this->r_groups) && \is_array($this->r_groups)) {
195: foreach ($r_groups as $r_g) {
196: $read_topics = XoopsPerms::getPermitted($this->mid, 'ReadInTopic', $r_g);
197: $add = true;
198: foreach ($parent_topics as $p_topic) {
199: if (!in_array($p_topic, $read_topics)) {
200: $add = false;
201: continue;
202: }
203: }
204: if ($add == true) {
205: $xp = new XoopsPerms();
206: $xp->setModuleId($this->mid);
207: $xp->setName('ReadInTopic');
208: $xp->setItemId($this->topic_id);
209: $xp->store();
210: $xp->addGroup($r_g);
211: }
212: }
213: }
214: }
215:
216: return true;
217: }
218:
219: public function delete()
220: {
221: $sql = sprintf('DELETE FROM %s WHERE topic_id = %u', $this->table, $this->topic_id);
222: $this->db->query($sql);
223: }
224:
225: /**
226: * @return int
227: */
228: public function topic_id()
229: {
230: return $this->topic_id;
231: }
232:
233: public function topic_pid()
234: {
235: return $this->topic_pid;
236: }
237:
238: /**
239: * @param string $format
240: *
241: * @return mixed
242: */
243: public function topic_title($format = 'S')
244: {
245: $myts = \MyTextSanitizer::getInstance();
246: switch ($format) {
247: case 'S':
248: case 'E':
249: $title = $myts->htmlSpecialChars($this->topic_title);
250: break;
251: case 'P':
252: case 'F':
253: $title = $myts->htmlSpecialChars($myts->stripSlashesGPC($this->topic_title));
254: break;
255: }
256:
257: return $title;
258: }
259:
260: /**
261: * @param string $format
262: *
263: * @return mixed
264: */
265: public function topic_imgurl($format = 'S')
266: {
267: $myts = \MyTextSanitizer::getInstance();
268: switch ($format) {
269: case 'S':
270: case 'E':
271: $imgurl = $myts->htmlSpecialChars($this->topic_imgurl);
272: break;
273: case 'P':
274: case 'F':
275: $imgurl = $myts->htmlSpecialChars($myts->stripSlashesGPC($this->topic_imgurl));
276: break;
277: }
278:
279: return $imgurl;
280: }
281:
282: public function prefix()
283: {
284: if (isset($this->prefix)) {
285: return $this->prefix;
286: }
287: return null;
288: }
289:
290: /**
291: * @return array
292: */
293: public function getFirstChildTopics()
294: {
295: $ret = array();
296: $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
297: $topic_arr = $xt->getFirstChild($this->topic_id, 'topic_title');
298: if (!empty($topic_arr) && \is_array($topic_arr)) {
299: foreach ($topic_arr as $topic) {
300: $ret[] = new XoopsTopic($this->table, $topic);
301: }
302: }
303:
304: return $ret;
305: }
306:
307: /**
308: * @return array
309: */
310: public function getAllChildTopics()
311: {
312: $ret = array();
313: $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
314: $topic_arr = $xt->getAllChild($this->topic_id, 'topic_title');
315: if (!empty($topic_arr) && \is_array($topic_arr)) {
316: foreach ($topic_arr as $topic) {
317: $ret[] = new XoopsTopic($this->table, $topic);
318: }
319: }
320:
321: return $ret;
322: }
323:
324: /**
325: * @return array
326: */
327: public function getChildTopicsTreeArray()
328: {
329: $ret = array();
330: $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
331: $topic_arr = $xt->getChildTreeArray($this->topic_id, 'topic_title');
332: if (!empty($topic_arr) && \is_array($topic_arr)) {
333: foreach ($topic_arr as $topic) {
334: $ret[] = new XoopsTopic($this->table, $topic);
335: }
336: }
337:
338: return $ret;
339: }
340:
341: /**
342: * @param int $none
343: * @param $seltopic
344: * @param string $selname
345: * @param string $onchange
346: */
347: public function makeTopicSelBox($none = 0, $seltopic = -1, $selname = '', $onchange = '')
348: {
349: $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
350: if ($seltopic != -1) {
351: $xt->makeMySelBox('topic_title', 'topic_title', $seltopic, $none, $selname, $onchange);
352: } elseif (!empty($this->topic_id)) {
353: $xt->makeMySelBox('topic_title', 'topic_title', $this->topic_id, $none, $selname, $onchange);
354: } else {
355: $xt->makeMySelBox('topic_title', 'topic_title', 0, $none, $selname, $onchange);
356: }
357: }
358:
359: //generates nicely formatted linked path from the root id to a given id
360: /**
361: * @param $funcURL
362: *
363: * @return string
364: */
365: public function getNiceTopicPathFromId($funcURL)
366: {
367: $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
368: $ret = $xt->getNicePathFromId($this->topic_id, 'topic_title', $funcURL);
369:
370: return $ret;
371: }
372:
373: /**
374: * @return array
375: */
376: public function getAllChildTopicsId()
377: {
378: $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
379: $ret = $xt->getAllChildId($this->topic_id, 'topic_title');
380:
381: return $ret;
382: }
383:
384: /**
385: * @return array
386: */
387: public function getTopicsList()
388: {
389: $sql = 'SELECT topic_id, topic_pid, topic_title FROM ' . $this->table;
390: $result = $this->db->query($sql);
391: if (!$this->db->isResultSet($result)) {
392: throw new \RuntimeException(
393: \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR
394: );
395: }
396: $ret = array();
397: $myts = \MyTextSanitizer::getInstance();
398: while (false !== ($myrow = $this->db->fetchArray($result))) {
399: $ret[$myrow['topic_id']] = array('title' => $myts->htmlSpecialChars($myrow['topic_title']), 'pid' => $myrow['topic_pid']);
400: }
401:
402: return $ret;
403: }
404:
405: /**
406: * @param $pid
407: * @param $title
408: *
409: * @return bool
410: */
411: public function topicExists($pid, $title)
412: {
413: $sql = 'SELECT COUNT(*) from ' . $this->table . ' WHERE topic_pid = ' . (int)$pid . " AND topic_title = '" . trim($title) . "'";
414: $result = $this->db->query($sql);
415: if (!$this->db->isResultSet($result)) {
416: throw new \RuntimeException(
417: \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR
418: );
419: }
420: list($count) = $this->db->fetchRow($result);
421: if ($count > 0) {
422: return true;
423: } else {
424: return false;
425: }
426: }
427: }
428: