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 / kernel / comment.php < prev    next >
Encoding:
PHP Script  |  2007-10-19  |  16.3 KB  |  448 lines

  1. <?php
  2. // $Id: comment.php 1102 2007-10-19 02:55:52Z 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.xoops.org/ http://jp.xoops.org/  http://www.myweb.ne.jp/  //
  29. // Project: The XOOPS Project (http://www.xoops.org/)                        //
  30. // ------------------------------------------------------------------------- //
  31.  
  32. if (!defined('XOOPS_ROOT_PATH')) {
  33.     exit();
  34. }
  35.  
  36. /**
  37.  *
  38.  *
  39.  * @package     kernel
  40.  *
  41.  * @author        Kazumi Ono    <onokazu@xoops.org>
  42.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  43.  */
  44.  
  45. /**
  46.  * A Comment
  47.  *
  48.  * @package     kernel
  49.  *
  50.  * @author        Kazumi Ono    <onokazu@xoops.org>
  51.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  52.  */
  53. class XoopsComment extends XoopsObject
  54. {
  55.  
  56.     /**
  57.      * Constructor
  58.      **/
  59.     function XoopsComment()
  60.     {
  61.         $this->XoopsObject();
  62.         $this->initVar('com_id', XOBJ_DTYPE_INT, null, false);
  63.         $this->initVar('com_pid', XOBJ_DTYPE_INT, 0, false);
  64.         $this->initVar('com_modid', XOBJ_DTYPE_INT, null, false);
  65.         $this->initVar('com_icon', XOBJ_DTYPE_OTHER, null, false);
  66.         $this->initVar('com_title', XOBJ_DTYPE_TXTBOX, null, true, 255, true);
  67.         $this->initVar('com_text', XOBJ_DTYPE_TXTAREA, null, true, null, true);
  68.         $this->initVar('com_created', XOBJ_DTYPE_INT, 0, false);
  69.         $this->initVar('com_modified', XOBJ_DTYPE_INT, 0, false);
  70.         $this->initVar('com_uid', XOBJ_DTYPE_INT, 0, true);
  71.         $this->initVar('com_ip', XOBJ_DTYPE_OTHER, null, false);
  72.         $this->initVar('com_sig', XOBJ_DTYPE_INT, 0, false);
  73.         $this->initVar('com_itemid', XOBJ_DTYPE_INT, 0, false);
  74.         $this->initVar('com_rootid', XOBJ_DTYPE_INT, 0, false);
  75.         $this->initVar('com_status', XOBJ_DTYPE_INT, 0, false);
  76.         $this->initVar('com_exparams', XOBJ_DTYPE_OTHER, null, false, 255);
  77.         $this->initVar('dohtml', XOBJ_DTYPE_INT, 0, false);
  78.         $this->initVar('dosmiley', XOBJ_DTYPE_INT, 0, false);
  79.         $this->initVar('doxcode', XOBJ_DTYPE_INT, 0, false);
  80.         $this->initVar('doimage', XOBJ_DTYPE_INT, 0, false);
  81.         $this->initVar('dobr', XOBJ_DTYPE_INT, 0, false);
  82.     }
  83.  
  84.     /**
  85.      * Is this comment on the root level?
  86.      *
  87.      * @return  bool
  88.      **/
  89.     function isRoot()
  90.     {
  91.         return ($this->getVar('com_id') == $this->getVar('com_rootid'));
  92.     }
  93. }
  94.  
  95. /**
  96.  * XOOPS comment handler class.
  97.  *
  98.  * This class is responsible for providing data access mechanisms to the data source
  99.  * of XOOPS comment class objects.
  100.  *
  101.  *
  102.  * @package     kernel
  103.  * @subpackage  comment
  104.  *
  105.  * @author        Kazumi Ono    <onokazu@xoops.org>
  106.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  107.  */
  108. class XoopsCommentHandler extends XoopsObjectHandler
  109. {
  110.  
  111.     /**
  112.      * Create a {@link XoopsComment}
  113.      *
  114.      * @param    bool    $isNew  Flag the object as "new"?
  115.      *
  116.      * @return    object
  117.      */
  118.     function &create($isNew = true)
  119.     {
  120.         $comment = new XoopsComment();
  121.         if ($isNew) {
  122.             $comment->setNew();
  123.         }
  124.         return $comment;
  125.     }
  126.  
  127.     /**
  128.      * Retrieve a {@link XoopsComment}
  129.      *
  130.      * @param   int $id ID
  131.      *
  132.      * @return  object  {@link XoopsComment}, FALSE on fail
  133.      **/
  134.     function &get($id)
  135.     {
  136.         $comment = false;
  137.         $id = intval($id);
  138.         if ($id > 0) {
  139.             $sql = 'SELECT * FROM '.$this->db->prefix('xoopscomments').' WHERE com_id='.$id;
  140.             if (!$result = $this->db->query($sql)) {
  141.                 return $comment;
  142.             }
  143.             $numrows = $this->db->getRowsNum($result);
  144.             if ($numrows == 1) {
  145.                 $comment = new XoopsComment();
  146.                 $comment->assignVars($this->db->fetchArray($result));
  147.             }
  148.         }
  149.         return $comment;
  150.     }
  151.  
  152.     /**
  153.      * Write a comment to database
  154.      *
  155.      * @param   object  &$comment
  156.      *
  157.      * @return  bool
  158.      **/
  159.     function insert(&$comment)
  160.     {
  161.         /**
  162.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  163.         */
  164.         if (!is_a($comment, 'xoopscomment')) {
  165.             return false;
  166.         }
  167.         if (!$comment->isDirty()) {
  168.             return true;
  169.         }
  170.         if (!$comment->cleanVars()) {
  171.             return false;
  172.         }
  173.         foreach ($comment->cleanVars as $k => $v) {
  174.             ${$k} = $v;
  175.         }
  176.         if ($comment->isNew()) {
  177.             $com_id = $this->db->genId('xoopscomments_com_id_seq');
  178.             $sql = sprintf("INSERT INTO %s (com_id, com_pid, com_modid, com_icon, com_title, com_text, com_created, com_modified, com_uid, com_ip, com_sig, com_itemid, com_rootid, com_status, com_exparams, dohtml, dosmiley, doxcode, doimage, dobr) VALUES (%u, %u, %u, %s, %s, %s, %u, %u, %u, %s, %u, %u, %u, %u, %s, %u, %u, %u, %u, %u)", $this->db->prefix('xoopscomments'), $com_id, $com_pid, $com_modid, $this->db->quoteString($com_icon), $this->db->quoteString($com_title), $this->db->quoteString($com_text), $com_created, $com_modified, $com_uid, $this->db->quoteString($com_ip), $com_sig, $com_itemid, $com_rootid, $com_status, $this->db->quoteString($com_exparams), $dohtml, $dosmiley, $doxcode, $doimage, $dobr);
  179.         } else {
  180.             $sql = sprintf("UPDATE %s SET com_pid = %u, com_icon = %s, com_title = %s, com_text = %s, com_created = %u, com_modified = %u, com_uid = %u, com_ip = %s, com_sig = %u, com_itemid = %u, com_rootid = %u, com_status = %u, com_exparams = %s, dohtml = %u, dosmiley = %u, doxcode = %u, doimage = %u, dobr = %u WHERE com_id = %u", $this->db->prefix('xoopscomments'), $com_pid, $this->db->quoteString($com_icon), $this->db->quoteString($com_title), $this->db->quoteString($com_text), $com_created, $com_modified, $com_uid, $this->db->quoteString($com_ip), $com_sig, $com_itemid, $com_rootid, $com_status, $this->db->quoteString($com_exparams), $dohtml, $dosmiley, $doxcode, $doimage, $dobr, $com_id);
  181.         }
  182.         if (!$result = $this->db->query($sql)) {
  183.             return false;
  184.         }
  185.         if (empty($com_id)) {
  186.             $com_id = $this->db->getInsertId();
  187.         }
  188.         $comment->assignVar('com_id', $com_id);
  189.         return true;
  190.     }
  191.  
  192.     /**
  193.      * Delete a {@link XoopsComment} from the database
  194.      *
  195.      * @param   object  &$comment
  196.      *
  197.      * @return  bool
  198.      **/
  199.     function delete(&$comment)
  200.     {
  201.         /**
  202.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  203.         */
  204.         if (!is_a($comment, 'xoopscomment')) {
  205.             return false;
  206.         }
  207.         $sql = sprintf("DELETE FROM %s WHERE com_id = %u", $this->db->prefix('xoopscomments'), $comment->getVar('com_id'));
  208.         if (!$result = $this->db->query($sql)) {
  209.             return false;
  210.         }
  211.         return true;
  212.     }
  213.  
  214.     /**
  215.      * Get some {@link XoopsComment}s
  216.      *
  217.      * @param   object  $criteria
  218.      * @param   bool    $id_as_key  Use IDs as keys into the array?
  219.      *
  220.      * @return  array   Array of {@link XoopsComment} objects
  221.      **/
  222.     function getObjects($criteria = null, $id_as_key = false)
  223.     {
  224.         $ret = array();
  225.         $limit = $start = 0;
  226.         $sql = 'SELECT * FROM '.$this->db->prefix('xoopscomments');
  227.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  228.             $sql .= ' '.$criteria->renderWhere();
  229.             $sort = ($criteria->getSort() != '') ? $criteria->getSort() : 'com_id';
  230.             $sql .= ' ORDER BY '.$sort.' '.$criteria->getOrder();
  231.             $limit = $criteria->getLimit();
  232.             $start = $criteria->getStart();
  233.         }
  234.         $result = $this->db->query($sql, $limit, $start);
  235.         if (!$result) {
  236.             return $ret;
  237.         }
  238.         while ($myrow = $this->db->fetchArray($result)) {
  239.             $comment = new XoopsComment();
  240.             $comment->assignVars($myrow);
  241.             if (!$id_as_key) {
  242.                 $ret[] =& $comment;
  243.             } else {
  244.                 $ret[$myrow['com_id']] =& $comment;
  245.             }
  246.             unset($comment);
  247.         }
  248.         return $ret;
  249.     }
  250.  
  251.     /**
  252.      * Count Comments
  253.      *
  254.      * @param   object  $criteria   {@link CriteriaElement}
  255.      *
  256.      * @return  int     Count
  257.      **/
  258.     function getCount($criteria = null)
  259.     {
  260.         $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('xoopscomments');
  261.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  262.             $sql .= ' '.$criteria->renderWhere();
  263.         }
  264.         if (!$result =& $this->db->query($sql)) {
  265.             return 0;
  266.         }
  267.         list($count) = $this->db->fetchRow($result);
  268.         return $count;
  269.     }
  270.  
  271.     /**
  272.      * Delete multiple comments
  273.      *
  274.      * @param   object  $criteria   {@link CriteriaElement}
  275.      *
  276.      * @return  bool
  277.      **/
  278.     function deleteAll($criteria = null)
  279.     {
  280.         $sql = 'DELETE FROM '.$this->db->prefix('xoopscomments');
  281.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  282.             $sql .= ' '.$criteria->renderWhere();
  283.         }
  284.         if (!$result = $this->db->query($sql)) {
  285.             return false;
  286.         }
  287.         return true;
  288.     }
  289.  
  290.    /**
  291.      * Get a list of comments
  292.      *
  293.      * @param   object  $criteria   {@link CriteriaElement}
  294.      *
  295.      * @return  array   Array of raw database records
  296.      **/
  297.     function getList($criteria = null)
  298.     {
  299.         $comments = $this->getObjects($criteria, true);
  300.         $ret = array();
  301.         foreach (array_keys($comments) as $i) {
  302.             $ret[$i] = $comments[$i]->getVar('com_title');
  303.         }
  304.         return $ret;
  305.     }
  306.  
  307.     /**
  308.      * Retrieves comments for an item
  309.      *
  310.      * @param   int     $module_id  Module ID
  311.      * @param   int     $item_id    Item ID
  312.      * @param   string  $order      Sort order
  313.      * @param   int     $status     Status of the comment
  314.      * @param   int     $limit      Max num of comments to retrieve
  315.      * @param   int     $start      Start offset
  316.      *
  317.      * @return  array   Array of {@link XoopsComment} objects
  318.      **/
  319.     function getByItemId($module_id, $item_id, $order = null, $status = null, $limit = null, $start = 0)
  320.     {
  321.         $criteria = new CriteriaCompo(new Criteria('com_modid', intval($module_id)));
  322.         $criteria->add(new Criteria('com_itemid', intval($item_id)));
  323.         if (isset($status)) {
  324.             $criteria->add(new Criteria('com_status', intval($status)));
  325.         }
  326.         if (isset($order)) {
  327.             $criteria->setOrder($order);
  328.         }
  329.         if (isset($limit)) {
  330.             $criteria->setLimit($limit);
  331.             $criteria->setStart($start);
  332.         }
  333.         return $this->getObjects($criteria);
  334.     }
  335.  
  336.     /**
  337.      * Gets total number of comments for an item
  338.      *
  339.      * @param   int     $module_id  Module ID
  340.      * @param   int     $item_id    Item ID
  341.      * @param   int     $status     Status of the comment
  342.      *
  343.      * @return  array   Array of {@link XoopsComment} objects
  344.      **/
  345.     function getCountByItemId($module_id, $item_id, $status = null)
  346.     {
  347.         $criteria = new CriteriaCompo(new Criteria('com_modid', intval($module_id)));
  348.         $criteria->add(new Criteria('com_itemid', intval($item_id)));
  349.         if (isset($status)) {
  350.             $criteria->add(new Criteria('com_status', intval($status)));
  351.         }
  352.         return $this->getCount($criteria);
  353.     }
  354.  
  355.  
  356.     /**
  357.      * Get the top {@link XoopsComment}s
  358.      *
  359.      * @param   int     $module_id
  360.      * @param   int     $item_id
  361.      * @param   strint  $order
  362.      * @param   int     $status
  363.      *
  364.      * @return  array   Array of {@link XoopsComment} objects
  365.      **/
  366.     function getTopComments($module_id, $item_id, $order, $status = null)
  367.     {
  368.         $criteria = new CriteriaCompo(new Criteria('com_modid', intval($module_id)));
  369.         $criteria->add(new Criteria('com_itemid', intval($item_id)));
  370.         $criteria->add(new Criteria('com_pid', 0));
  371.         if (isset($status)) {
  372.             $criteria->add(new Criteria('com_status', intval($status)));
  373.         }
  374.         $criteria->setOrder($order);
  375.         return $this->getObjects($criteria);
  376.     }
  377.  
  378.     /**
  379.      * Retrieve a whole thread
  380.      *
  381.      * @param   int     $comment_rootid
  382.      * @param   int     $comment_id
  383.      * @param   int     $status
  384.      *
  385.      * @return  array   Array of {@link XoopsComment} objects
  386.      **/
  387.     function getThread($comment_rootid, $comment_id, $status = null)
  388.     {
  389.         $criteria = new CriteriaCompo(new Criteria('com_rootid', intval($comment_rootid)));
  390.         $criteria->add(new Criteria('com_id', intval($comment_id), '>='));
  391.         if (isset($status)) {
  392.             $criteria->add(new Criteria('com_status', intval($status)));
  393.         }
  394.         return $this->getObjects($criteria);
  395.     }
  396.  
  397.     /**
  398.      * Update
  399.      *
  400.      * @param   object  &$comment       {@link XoopsComment} object
  401.      * @param   string  $field_name     Name of the field
  402.      * @param   mixed   $field_value    Value to write
  403.      *
  404.      * @return  bool
  405.      **/
  406.     function updateByField(&$comment, $field_name, $field_value)
  407.     {
  408.         $comment->unsetNew();
  409.         $comment->setVar($field_name, $field_value);
  410.         return $this->insert($comment);
  411.     }
  412.  
  413.     /**
  414.      * Delete all comments for one whole module
  415.      *
  416.      * @param   int $module_id  ID of the module
  417.      * @return  bool
  418.      **/
  419.     function deleteByModule($module_id)
  420.     {
  421.         return $this->deleteAll(new Criteria('com_modid', intval($module_id)));
  422.     }
  423.  
  424.     /**
  425.      * Change a value in multiple comments
  426.      *
  427.      * @param   string  $fieldname  Name of the field
  428.      * @param   string  $fieldvalue Value to write
  429.      * @param   object  $criteria   {@link CriteriaElement}
  430.      *
  431.      * @return  bool
  432.      **/
  433. /*
  434.     function updateAll($fieldname, $fieldvalue, $criteria = null)
  435.     {
  436.         $set_clause = is_numeric($fieldvalue) ? $filedname.' = '.$fieldvalue : $filedname.' = '.$this->db->quoteString($fieldvalue);
  437.         $sql = 'UPDATE '.$this->db->prefix('xoopscomments').' SET '.$set_clause;
  438.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  439.             $sql .= ' '.$criteria->renderWhere();
  440.         }
  441.         if (!$result = $this->db->query($sql)) {
  442.             return false;
  443.         }
  444.         return true;
  445.     }
  446. */
  447. }
  448. ?>