home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / xoops-2.0.18.1.exe / xoops-2.0.18.1 / htdocs / class / xoopstopic.php < prev    next >
Encoding:
PHP Script  |  2007-10-19  |  10.7 KB  |  341 lines

  1. <?php
  2. // $Id: xoopstopic.php 1099 2007-10-19 01:08:14Z dugris $
  3. //  ------------------------------------------------------------------------ //
  4. //                XOOPS - PHP Content Management System                      //
  5. //                    Copyright (c) 2000 XOOPS.org                           //
  6. //                       <http://www.xoops.org/>                             //
  7. //  ------------------------------------------------------------------------ //
  8. //  This program is free software; you can redistribute it and/or modify     //
  9. //  it under the terms of the GNU General Public License as published by     //
  10. //  the Free Software Foundation; either version 2 of the License, or        //
  11. //  (at your option) any later version.                                      //
  12. //                                                                           //
  13. //  You may not change or alter any portion of this comment or credits       //
  14. //  of supporting developers from this source code or any supporting         //
  15. //  source code which is considered copyrighted (c) material of the          //
  16. //  original comment or credit authors.                                      //
  17. //                                                                           //
  18. //  This program is distributed in the hope that it will be useful,          //
  19. //  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
  20. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
  21. //  GNU General Public License for more details.                             //
  22. //                                                                           //
  23. //  You should have received a copy of the GNU General Public License        //
  24. //  along with this program; if not, write to the Free Software              //
  25. //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
  26. //  ------------------------------------------------------------------------ //
  27. // Author: Kazumi Ono (AKA onokazu)                                          //
  28. // URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
  29. // Project: The XOOPS Project                                                //
  30. // ------------------------------------------------------------------------- //
  31. if (!defined('XOOPS_ROOT_PATH')) {
  32.     exit();
  33. }
  34. include_once XOOPS_ROOT_PATH."/class/xoopstree.php";
  35.  
  36. class XoopsTopic
  37. {
  38.     var $table;
  39.     var $topic_id;
  40.     var $topic_pid;
  41.     var $topic_title;
  42.     var $topic_imgurl;
  43.     var $prefix; // only used in topic tree
  44.     var $use_permission=false;
  45.     var $mid; // module id used for setting permission
  46.  
  47.     function XoopsTopic($table, $topicid=0)
  48.     {
  49.         $this->db =& Database::getInstance();
  50.         $this->table = $table;
  51.         if ( is_array($topicid) ) {
  52.             $this->makeTopic($topicid);
  53.         } elseif ( $topicid != 0 ) {
  54.             $this->getTopic(intval($topicid));
  55.         } else {
  56.             $this->topic_id = $topicid;
  57.         }
  58.     }
  59.  
  60.     function setTopicTitle($value)
  61.     {
  62.         $this->topic_title = $value;
  63.     }
  64.  
  65.     function setTopicImgurl($value)
  66.     {
  67.         $this->topic_imgurl = $value;
  68.     }
  69.  
  70.     function setTopicPid($value)
  71.     {
  72.         $this->topic_pid = $value;
  73.     }
  74.  
  75.     function getTopic($topicid)
  76.     {
  77.         $topicid = intval($topicid);
  78.         $sql = "SELECT * FROM ".$this->table." WHERE topic_id=".$topicid."";
  79.         $array = $this->db->fetchArray($this->db->query($sql));
  80.         $this->makeTopic($array);
  81.     }
  82.  
  83.     function makeTopic($array)
  84.     {
  85.         foreach($array as $key=>$value){
  86.             $this->$key = $value;
  87.         }
  88.     }
  89.  
  90.     function usePermission($mid)
  91.     {
  92.         $this->mid = $mid;
  93.         $this->use_permission = true;
  94.     }
  95.  
  96.     function store()
  97.     {
  98.         $myts =& MyTextSanitizer::getInstance();
  99.         $title = "";
  100.         $imgurl = "";
  101.         if ( isset($this->topic_title) && $this->topic_title != "" ) {
  102.             $title = $myts->makeTboxData4Save($this->topic_title);
  103.         }
  104.         if ( isset($this->topic_imgurl) && $this->topic_imgurl != "" ) {
  105.             $imgurl = $myts->makeTboxData4Save($this->topic_imgurl);
  106.         }
  107.         if ( !isset($this->topic_pid) || !is_numeric($this->topic_pid) ) {
  108.             $this->topic_pid = 0;
  109.         }
  110.         if ( empty($this->topic_id) ) {
  111.             $this->topic_id = $this->db->genId($this->table."_topic_id_seq");
  112.             $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);
  113.         } else {
  114.             $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);
  115.         }
  116.         if ( !$result = $this->db->query($sql) ) {
  117.             ErrorHandler::show('0022');
  118.         }
  119.         if ( $this->use_permission == true ) {
  120.             if ( empty($this->topic_id) ) {
  121.                 $this->topic_id = $this->db->getInsertId();
  122.             }
  123.             $xt = new XoopsTree($this->table, "topic_id", "topic_pid");
  124.             $parent_topics = $xt->getAllParentId($this->topic_id);
  125.             if ( !empty($this->m_groups) && is_array($this->m_groups) ){
  126.                 foreach ( $this->m_groups as $m_g ) {
  127.                     $moderate_topics = XoopsPerms::getPermitted($this->mid, "ModInTopic", $m_g);
  128.                     $add = true;
  129.                     // only grant this permission when the group has this permission in all parent topics of the created topic
  130.                     foreach($parent_topics as $p_topic){
  131.                         if ( !in_array($p_topic, $moderate_topics) ) {
  132.                             $add = false;
  133.                             continue;
  134.                         }
  135.                     }
  136.                     if ( $add == true ) {
  137.                         $xp = new XoopsPerms();
  138.                         $xp->setModuleId($this->mid);
  139.                         $xp->setName("ModInTopic");
  140.                         $xp->setItemId($this->topic_id);
  141.                         $xp->store();
  142.                         $xp->addGroup($m_g);
  143.                     }
  144.                 }
  145.             }
  146.             if ( !empty($this->s_groups) && is_array($this->s_groups) ){
  147.                 foreach ( $s_groups as $s_g ) {
  148.                     $submit_topics = XoopsPerms::getPermitted($this->mid, "SubmitInTopic", $s_g);
  149.                     $add = true;
  150.                     foreach($parent_topics as $p_topic){
  151.                         if ( !in_array($p_topic, $submit_topics) ) {
  152.                             $add = false;
  153.                             continue;
  154.                         }
  155.                     }
  156.                     if ( $add == true ) {
  157.                         $xp = new XoopsPerms();
  158.                         $xp->setModuleId($this->mid);
  159.                         $xp->setName("SubmitInTopic");
  160.                         $xp->setItemId($this->topic_id);
  161.                         $xp->store();
  162.                         $xp->addGroup($s_g);
  163.                     }
  164.                 }
  165.             }
  166.             if ( !empty($this->r_groups) && is_array($this->r_groups) ){
  167.                 foreach ( $r_groups as $r_g ) {
  168.                     $read_topics = XoopsPerms::getPermitted($this->mid, "ReadInTopic", $r_g);
  169.                     $add = true;
  170.                     foreach($parent_topics as $p_topic){
  171.                         if ( !in_array($p_topic, $read_topics) ) {
  172.                             $add = false;
  173.                             continue;
  174.                         }
  175.                     }
  176.                     if ( $add == true ) {
  177.                         $xp = new XoopsPerms();
  178.                         $xp->setModuleId($this->mid);
  179.                         $xp->setName("ReadInTopic");
  180.                         $xp->setItemId($this->topic_id);
  181.                         $xp->store();
  182.                         $xp->addGroup($r_g);
  183.                     }
  184.                 }
  185.             }
  186.         }
  187.         return true;
  188.     }
  189.  
  190.     function delete()
  191.     {
  192.         $sql = sprintf("DELETE FROM %s WHERE topic_id = %u", $this->table, $this->topic_id);
  193.         $this->db->query($sql);
  194.     }
  195.  
  196.     function topic_id()
  197.     {
  198.         return $this->topic_id;
  199.     }
  200.  
  201.     function topic_pid()
  202.     {
  203.         return $this->topic_pid;
  204.     }
  205.  
  206.     function topic_title($format="S")
  207.     {
  208.         $myts =& MyTextSanitizer::getInstance();
  209.         switch($format){
  210.             case "S":
  211.                 $title = $myts->makeTboxData4Show($this->topic_title);
  212.                 break;
  213.             case "E":
  214.                 $title = $myts->makeTboxData4Edit($this->topic_title);
  215.                 break;
  216.             case "P":
  217.                 $title = $myts->makeTboxData4Preview($this->topic_title);
  218.                 break;
  219.             case "F":
  220.                 $title = $myts->makeTboxData4PreviewInForm($this->topic_title);
  221.                 break;
  222.         }
  223.         return $title;
  224.     }
  225.  
  226.     function topic_imgurl($format="S")
  227.     {
  228.         $myts =& MyTextSanitizer::getInstance();
  229.         switch($format){
  230.             case "S":
  231.                 $imgurl= $myts->makeTboxData4Show($this->topic_imgurl);
  232.                 break;
  233.             case "E":
  234.                 $imgurl = $myts->makeTboxData4Edit($this->topic_imgurl);
  235.                 break;
  236.             case "P":
  237.                 $imgurl = $myts->makeTboxData4Preview($this->topic_imgurl);
  238.                 break;
  239.             case "F":
  240.                 $imgurl = $myts->makeTboxData4PreviewInForm($this->topic_imgurl);
  241.                 break;
  242.         }
  243.         return $imgurl;
  244.     }
  245.  
  246.     function prefix()
  247.     {
  248.         if ( isset($this->prefix) ) {
  249.             return $this->prefix;
  250.         }
  251.     }
  252.  
  253.     function getFirstChildTopics()
  254.     {
  255.         $ret = array();
  256.         $xt = new XoopsTree($this->table, "topic_id", "topic_pid");
  257.         $topic_arr = $xt->getFirstChild($this->topic_id, "topic_title");
  258.         if ( is_array($topic_arr) && count($topic_arr) ) {
  259.             foreach($topic_arr as $topic){
  260.                 $ret[] = new XoopsTopic($this->table, $topic);
  261.             }
  262.         }
  263.         return $ret;
  264.     }
  265.  
  266.     function getAllChildTopics()
  267.     {
  268.         $ret = array();
  269.         $xt = new XoopsTree($this->table, "topic_id", "topic_pid");
  270.         $topic_arr = $xt->getAllChild($this->topic_id, "topic_title");
  271.         if ( is_array($topic_arr) && count($topic_arr) ) {
  272.             foreach($topic_arr as $topic){
  273.                 $ret[] = new XoopsTopic($this->table, $topic);
  274.             }
  275.         }
  276.         return $ret;
  277.     }
  278.  
  279.     function getChildTopicsTreeArray()
  280.     {
  281.         $ret = array();
  282.         $xt = new XoopsTree($this->table, "topic_id", "topic_pid");
  283.         $topic_arr = $xt->getChildTreeArray($this->topic_id, "topic_title");
  284.         if ( is_array($topic_arr) && count($topic_arr) ) {
  285.             foreach($topic_arr as $topic){
  286.                 $ret[] = new XoopsTopic($this->table, $topic);
  287.             }
  288.         }
  289.         return $ret;
  290.     }
  291.  
  292.     function makeTopicSelBox($none=0, $seltopic=-1, $selname="", $onchange="")
  293.     {
  294.         $xt = new XoopsTree($this->table, "topic_id", "topic_pid");
  295.         if ( $seltopic != -1 ) {
  296.             $xt->makeMySelBox("topic_title", "topic_title", $seltopic, $none, $selname, $onchange);
  297.         } elseif ( !empty($this->topic_id) ) {
  298.             $xt->makeMySelBox("topic_title", "topic_title", $this->topic_id, $none, $selname, $onchange);
  299.         } else {
  300.             $xt->makeMySelBox("topic_title", "topic_title", 0, $none, $selname, $onchange);
  301.         }
  302.     }
  303.  
  304.     //generates nicely formatted linked path from the root id to a given id
  305.     function getNiceTopicPathFromId($funcURL)
  306.     {
  307.         $xt = new XoopsTree($this->table, "topic_id", "topic_pid");
  308.         $ret = $xt->getNicePathFromId($this->topic_id, "topic_title", $funcURL);
  309.         return $ret;
  310.     }
  311.  
  312.     function getAllChildTopicsId()
  313.     {
  314.         $xt = new XoopsTree($this->table, "topic_id", "topic_pid");
  315.         $ret = $xt->getAllChildId($this->topic_id, "topic_title");
  316.         return $ret;
  317.     }
  318.  
  319.     function getTopicsList()
  320.     {
  321.         $result = $this->db->query('SELECT topic_id, topic_pid, topic_title FROM '.$this->table);
  322.         $ret = array();
  323.         $myts =& MyTextSanitizer::getInstance();
  324.         while ($myrow = $this->db->fetchArray($result)) {
  325.             $ret[$myrow['topic_id']] = array('title' => $myts->htmlspecialchars($myrow['topic_title']), 'pid' => $myrow['topic_pid']);
  326.         }
  327.         return $ret;
  328.     }
  329.  
  330.     function topicExists($pid, $title) {
  331.         $sql = "SELECT COUNT(*) from ".$this->table." WHERE topic_pid = ".intval($pid)." AND topic_title = '".trim($title)."'";
  332.         $rs = $this->db->query($sql);
  333.         list($count) = $this->db->fetchRow($rs);
  334.         if ($count > 0) {
  335.             return true;
  336.         } else {
  337.             return false;
  338.         }
  339.     }
  340. }
  341. ?>