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