コード例 #1
0
ファイル: topic.php プロジェクト: trabisdementia/xuups
art_parse_class('
class [CLASS_PREFIX]TopicHandler extends XoopsPersistableObjectHandler
{
    /**
     * Constructor
     *
     * @param object $db reference to the {@link XoopsDatabase} object     
     **/
    function [CLASS_PREFIX]TopicHandler(&$db)
    {
        $this->XoopsPersistableObjectHandler($db, art_DB_prefix("topic", true), "Xtopic", "top_id", "top_title");
    }

    /**
     * get a list of topics including a specified article and matching a condition
     * 
     * {@link Permission}
     *
     * @param    int        $art_id     article ID
     * @param     object    $criteria     {@link CriteriaElement} to match
     * @return     array of topics {@link Xtopoic}
     */
    function &getByArticle($art_id, $criteria = null)
    {
        $_cachedTop=array();
        $ret = null;
        if (empty($art_id)) {
            return $ret;
        }

        $sql = "SELECT t.top_id, t.top_title FROM " . art_DB_prefix("topic") . " AS t";
        $sql .= " LEFT JOIN " . art_DB_prefix("arttop") . " AS at ON at.top_id=t.top_id";
        $sql .= " WHERE at.art_id =" . intval($art_id);
        mod_loadFunctions("user", $GLOBALS["artdirname"]);
        if (!art_isAdministrator()) {
            $permission_handler =& xoops_getmodulehandler("permission", $GLOBALS["artdirname"]);
            $allowed_cats =& $permission_handler->getCategories();
            if (count($allowed_cats) == 0) return null;
            $sql .= " AND t.cat_id IN (" . implode(",", $allowed_cats) . ")";
        }
        $limit = $start = null;
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $sql .= " AND " . $criteria->render();
            if ($criteria->getSort() != "") {
                $sql .= " ORDER BY " . $criteria->getSort() . " " . $criteria->getOrder();
                $orderSet = true;
            }
            $limit = $criteria->getLimit();
            $start = $criteria->getStart();
        }
        if (empty($orderSet))  $sql .= " ORDER BY t.cat_id, t.top_order, t.top_time DESC";
        if (!$result = $this->db->query($sql, $limit, $start)) {
            return $ret;
        }
        while ($row = $this->db->fetchArray($result)) {
            $topic =& $this->create(false);
            $topic->assignVars($row);
            $_cachedTop[$topic->getVar("top_id")] = $topic;
            unset($topic);
        }
        return $_cachedTop;
    }

    /**
     * get a list of topics matching a condition of a category
     * 
     * @param    mixed    $cat_id     category ID(s)
     * @param     int       $limit      Max number of objects to fetch
     * @param     int       $start      Which record to start at
     * @param     object    $criteria     {@link CriteriaElement} to match
     * @param     array    $tags         variables to fetch
     * @param     bool    $asObject     flag indicating as object, otherwise as array
     * @return     array of topics {@link Xtopoic}
     */
    function &getByCategory($cat_id = 0, $limit = 0, $start = 0, $criteria = null, $tags = null, $asObject = true)
    {
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
        } else {
            $criteria = new CriteriaCompo();
        }
        $criteria->setLimit($limit);
        $criteria->setStart($start);

        if (is_array($cat_id) && count($cat_id) > 0) {
            $cat_id = array_map("intval",$cat_id);
            $criteria->add(new criteria("cat_id", "(" . implode(",", $cat_id) . ")", "IN"));
        } elseif (intval($cat_id)) {
            $criteria->add(new criteria("cat_id", intval($cat_id)));
        }
        $ret = $this->getAll($criteria, $tags, $asObject);
        return $ret;
    }

    /**
     * count topics matching a condition of a category (categories)
     * 
     * @param     mixed     $category array or {@link Xcategory}
     * @param object $criteria {@link CriteriaElement} to match
     * @return int count of topics
     */
    function getCountByCategory($cat_id = 0, $criteria = null)
    {
        $sql = "SELECT COUNT(*) AS count FROM " . art_DB_prefix("topic");
        if (is_array($cat_id) && count($cat_id)>0) {
            $cat_id = array_map("intval", $cat_id);
            $sql .= " WHERE cat_id IN (" . implode(",", $cat_id) . ")";
        } elseif (intval($cat_id)) {
            $sql .= " WHERE cat_id = " . intval($cat_id);
        } else {
            $sql .= " WHERE 1=1";
        }
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $sql .= " AND " . $criteria->render();
        }
        if (!$result = $this->db->query($sql)) {
            return false;
        }
        $myrow = $this->db->fetchArray($result);
        return intval($myrow["count"]);
    }

    /**
     * count topics matching a condition grouped by category
     * 
     * @param     object     $criteria {@link CriteriaElement} to match
     * @return array    associative array of count of topics and category ID
     */
    function getCountsByCategory($criteria = null)
    {
        $sql = "SELECT cat_id, COUNT(*) as count FROM " . art_DB_prefix("topic") . " GROUP BY cat_id";
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $sql .= " " . $criteria->renderWhere();
        }
        $result = $this->db->query($sql);
        $ret = array();
        while ($myrow = $this->db->fetchArray($result)) {
            $ret[$myrow["cat_id"]] = $myrow["count"];
        }
        return $ret;
    }

    /**
     * get articles matching a condition of a topic
     * 
     * {@link Article} 
     *
     * @param mixed        $topic         topic ID or {@link Xtopic}
     * @param int       $limit      Max number of objects to fetch
     * @param int       $start      Which record to start at
     * @param object    $criteria     {@link CriteriaElement} to match
     * @param array        $tags         variables to fetch
     * @return array of articles {@link Article}
     */
    function &getArticles($topic, $limit = 0, $start = 0, $criteria = null, $tags = null)
    {
        $top_id = (is_object($topic)) ? $topic->getVar("top_id") : intval($topic);
        $article_handler =& xoops_getmodulehandler("article", $GLOBALS["artdirname"]);
        $ret =& $article_handler->getByTopic($top_id, $limit, $start, $criteria, $tags);
        return $ret;
    }

    /**
     * count articles matching a condition of a cateogy (categories)
     * 
     * @param     mixed    $top_id    array or {@link Xtopic}
     * @param     object     $criteria {@link CriteriaElement} to match
     * @return     int     count of articles
     */
    function getArticleCount($top_id, $criteria = null)
    {
        $sql = "SELECT COUNT(*) as count FROM " . art_DB_prefix("arttop");
        if (is_array($top_id) && count($top_id) > 0) {
            $sql .= " WHERE top_id IN (" . implode(",", $top_id) . ")";
        } elseif (intval($top_id)) {
            $sql .= " WHERE top_id = " . intval($top_id);
        } else {
            $sql .= " WHERE 1=1";
        }
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $sql .= " AND " . $criteria->render();
        }
        $result = $this->db->query($sql);
        $myrow = $this->db->fetchArray($result);
        return intval($myrow["count"]);
    }

    /**
     * count articles matching a condition of a list of topics, respectively
     * 
     * @param     mixed    $top_id array or {@link Xtopic}
     * @param     object     $criteria {@link CriteriaElement} to match
     * @return     array     associative array topic ID and article count
     */
    function getArticleCounts($top_id, $criteria = null)
    {
        $sql = "SELECT top_id, COUNT(*) as count FROM " . art_DB_prefix("arttop");
        if (is_array($top_id) && count($top_id) > 0) {
            $sql .= " WHERE top_id IN (" . implode(",", $top_id) . ")";
        } elseif (intval($top_id)) {
            $sql .= " WHERE top_id = " . intval($top_id);
        } else {
            $sql .= " WHERE 1=1";
        }
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $sql .= " AND " . $criteria->render();
        }
        $sql .= " GROUP BY top_id";
        $result = $this->db->query($sql);
        $ret = array();
        while ($myrow = $this->db->fetchArray($result)) {
            $ret[$myrow["top_id"]] = $myrow["count"];
        }
        return $ret;
    }

    /**
     * check permission of the topic
     * 
     * {@link Xcategory} 
     *
     * @param     object    $topic     {@link Xtopic}
     * @param     string     $type     permission type
     * @return     bool     true on accessible
     */
    function getPermission(&$topic, $type = "access")
    {
        if (!is_object($topic)) $topic=& $this->get(intval($topic));
        $category_handler =& xoops_getmodulehandler("category", $GLOBALS["artdirname"]);
        $category_obj =& $category_handler->get($topic->getVar("cat_id"));
        return $category_handler->getPermission($category_obj, $type);
    }
    
    /**
     * clean orphan topics from database
     * 
     * @return     bool    true on success
     */
    function cleanOrphan()
    {
        parent::cleanOrphan(art_DB_prefix("category"), "cat_id");
        
        /* for MySQL 4.1+ */
        if (version_compare( mysql_get_server_info(), "4.1.0", "ge" )):
        $sql = "DELETE FROM " . art_DB_prefix("arttop").
                " WHERE (top_id NOT IN ( SELECT DISTINCT top_id FROM " . art_DB_prefix("topic") . ") )";
        else:
        $sql =     "DELETE " . art_DB_prefix("arttop") . " FROM " . art_DB_prefix("arttop") .
                " LEFT JOIN " . art_DB_prefix("topic") . " AS aa ON " . art_DB_prefix("arttop") . ".top_id = aa.top_id ".
                " WHERE (aa.top_id IS NULL)";
        endif;
        if (!$result = $this->db->queryF($sql)) {
            //xoops_error("cleanOrphan: ". $sql);
            //return false;
        }
        
        return true;
    }
}
');
コード例 #2
0
ファイル: keywords.php プロジェクト: trabisdementia/xuups
art_parse_class('
class [CLASS_PREFIX]KeywordsHandler /*extends XoopsObjectHandler*/
{
    var $keywords;
    var $skip_tags = array("A", "IMG", "PRE", "QUOTE", "CODE",
                            "H1", "H2", "H3", "H4", "H5", "H6" 
                            );    //add here more, if you want to filter them

    function init()
    {
        $this->getKeywords();
        if (count($this->keywords) == 0) return false;
        else return true;
    }

    function getKeywords()
    {
        global $xoopsModuleConfig;
        static $keywords = array();
        if (count($keywords) > 0) return $keywords;
        $_keywords = art_parseLinks($xoopsModuleConfig["keywords"]);

        foreach ($_keywords as $_keyword) {
            $this->keywords[strtolower($_keyword["title"])] = $_keyword["url"];
        }
    }

    function highlighter($matches)
    {
        if (!in_array(strtoupper($matches[2]), $this->skip_tags)) {
            $replace = "<a href=\\"" . $this->keywords[strtolower($matches[3])] . "\\">" . $matches[3] . "</a>";
            $proceed =  preg_replace("#\\b(" . $matches[3] . ")\\b#si", $replace, $matches[0]);
        } else {
            $proceed = $matches[0];
        }
        return stripslashes($proceed);
    }

    function &process(&$text)
    {
        foreach ($this->keywords as $keyword => $rep) {
            $text = preg_replace_callback("#(<([A-Za-z]+)[^>]*[\\>]*)*\\s(" . $keyword . ")\\s(.*?)(<\\/\\2>)*#si", array(&$this, "highlighter"), $text);
        }
        return $text;
    }
}
');
コード例 #3
0
ファイル: pingback.php プロジェクト: trabisdementia/xuups
art_parse_class('
class [CLASS_PREFIX]PingbackHandler extends XoopsPersistableObjectHandler
{
    function [CLASS_PREFIX]PingbackHandler(&$db)
    {
        $this->XoopsPersistableObjectHandler($db, art_DB_prefix("pingback", true), "Pingback", "pb_id", "pb_url");
    }

    function &getByArticle($art_id)
    {
        $sql = "SELECT * FROM " . art_DB_prefix("pingback") . " WHERE art_id = ". intval($art_id);
        $result = $this->db->query($sql);
        $ret = array();
        while ($myrow = $this->db->fetchArray($result)) {
            $pingback =& $this->create(false);
            $pingback->assignVars($myrow);
            $ret[$myrow["pb_id"]] = $pingback;
            unset($pingback);
        }
        return $ret;
    }

    function deleteByArticle($art_id)
    {
        $pingbacks = $this->getByArticle($art_id);
        if (count($pingbacks)>0) {
            foreach($pingbacks as $pb_id => $pingback) {
                $this->delete($pingback);
            }
        }
        return true;
    }

    /**
     * clean orphan items from database
     * 
     * @return     bool    true on success
     */
    function cleanOrphan()
    {
        return parent::cleanOrphan(art_DB_prefix("article"), "art_id");
    }
}
');
コード例 #4
0
ファイル: trackback.php プロジェクト: trabisdementia/xuups
art_parse_class('
class [CLASS_PREFIX]TrackbackHandler extends XoopsPersistableObjectHandler
{
    function [CLASS_PREFIX]TrackbackHandler(&$db)
    {
        $this->XoopsPersistableObjectHandler($db, art_DB_prefix("trackback", true), "Trackback", "tb_id", "tb_url");
    }

    function &getByArticle($art_id, $isApproved = true)
    {
        $sql = "SELECT * FROM " . art_DB_prefix("trackback") . " WHERE art_id = ". intval($art_id);
        if ($isApproved) {
            $sql .= " AND tb_status > 0";
        }
        $result = $this->db->query($sql);
        $ret = array();
        while ($myrow = $this->db->fetchArray($result)) {
            $trackback =& $this->create(false);
            $trackback->assignVars($myrow);
            $ret[$myrow["tb_id"]] = $trackback;
            unset($trackback);
        }
        return $ret;
    }

    function deleteIds($ids)
    {
        $ids = array_map("intval", $ids);

        $sql = "DELETE FROM " . art_DB_prefix("trackback") . " WHERE tb_id IN (" . implode(",", $ids) . ")";
        if (!$result = $this->db->queryF($sql)) {
            //xoops_error("delete trackbacks error:" . $sql);
            return false;
        }
        return true;
    }

    function approveIds($ids)
    {
        $ids = array_map("intval", $ids);

        $sql = "UPDATE " . art_DB_prefix("trackback") . " SET tb_status=1 WHERE tb_id IN (" . implode(",", $ids) . ")";
        if (!$result = $this->db->queryF($sql)) {
            //xoops_error("approve trackback error:" . $sql);
            return false;
        }
        return true;
    }

    function deleteByArticle($art_id)
    {
        $trackbacks = $this->getByArticle($art_id);
        if (count($trackbacks) > 0) {
            $this->deleteIds(array_keys($trackbacks));
        }
        return true;
    }

    function getStatus()
    {
        return 1;
    }

    /**
     * clean orphan items from database
     * 
     * @return     bool    true on success
     */
    function cleanOrphan()
    {
        parent::cleanOrphan(art_DB_prefix("article"), "art_id");
    }
}
');
コード例 #5
0
ファイル: file.php プロジェクト: trabisdementia/xuups
art_parse_class('
class [CLASS_PREFIX]FileHandler extends XoopsPersistableObjectHandler
{
    function [CLASS_PREFIX]FileHandler(&$db)
    {
        $this->XoopsPersistableObjectHandler($db, art_DB_prefix("file", true), "Xfile", "file_id", "file_name");
    }
    
    function getCountOrphan()
    {
        $sql = "SELECT COUNT(*) as count FROM " . art_DB_prefix("file") . " WHERE art_id NOT IN ( SELECT DISTINCT art_id FROM " . art_DB_prefix("article") . ")";
        $result = $this->db->query($sql);
        $myrow = $this->db->fetchArray($result);
        return intval($myrow["count"]);
    }

    function &getOrpan($criteria = null, $tags = false)
    {
	    if (is_array($tags) && count($tags) > 0) {
		    if(!in_array("file_id", $tags)) $tags[] = "file_id";
		    $select = implode(",", $tags);
	    }
	    else $select = "*";
	    $limit = $start = null;
        $sql = "SELECT $select FROM " . art_DB_prefix("file") . " WHERE art_id NOT IN ( SELECT DISTINCT art_id FROM " . art_DB_prefix("article") . ")";
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $sql .= " " . $criteria->renderWhere();
            if ($criteria->getSort() != "") {
                $sql .= " ORDER BY " . $criteria->getSort() . " " . $criteria->getOrder();
            }
            $limit = $criteria->getLimit();
            $start = $criteria->getStart();
        }
        $result = $this->db->query($sql, $limit, $start);
        $ret = array();
        while ($myrow = $this->db->fetchArray($result)) {
            $file =& $this->create(false);
            $file->assignVars($myrow);

            $ret[$myrow["file_id"]] = $file;
            unset($file);
        }
        return $ret;
    }

   	function &getOrphanByLimit($limit = 1, $start = 0, $criteria = null, $tags = false)
   	{
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
        } elseif (!empty($limit)) {
			$criteria = new CriteriaCompo();
	        $criteria->setLimit($limit);
	        $criteria->setStart($start);
        }
        $ret =& $this->getAll($criteria, $tags);
        return $ret;
   	}

    function &getByArticle($art_id)
    {
        $sql = "SELECT * FROM " . art_DB_prefix("file") . " WHERE art_id = " . intval($art_id);
        $result = $this->db->query($sql);
        $ret = array();
        while ($myrow = $this->db->fetchArray($result)) {
            $file =& $this->create(false);
            $file->assignVars($myrow);
            $ret[$myrow["file_id"]] = $file;
            unset($file);
        }
        return $ret;
    }

    function delete(&$file)
    {
	    global $xoopsModuleConfig;

        $sql = "DELETE FROM " . $file->table . " WHERE file_id =" . $file->getVar("file_id");
        if (!$result = $this->db->queryF($sql)) {
            return false;
        }
        @unlink($xoopsModuleConfig["path_file"] . "/" . $file->getVar("file_name"));
        unset($file);
        return true;
    }

    function deleteByArticle($art_id)
    {
	    $files = $this->getByArticle($art_id);
	    if (count($files) > 0) {
		    foreach ($files as $file_id => $file) {
			    $this->delete($file);
		    }
	    }
	    return true;
    }
}
');
コード例 #6
0
ファイル: writer.php プロジェクト: trabisdementia/xuups
if (!class_exists("Writer")) {
    class Writer extends XoopsObject
    {
        function Writer($id = null)
        {
            //$this->ArtObject();
            //$this->table = art_DB_prefix("writer");
            $this->initVar("writer_id", XOBJ_DTYPE_INT, null);
            $this->initVar("uid", XOBJ_DTYPE_INT);
            // submitter for the author
            //$this->initVar("writer_uid", XOBJ_DTYPE_INT, 0, true); // uid of the author if registered
            $this->initVar("writer_name", XOBJ_DTYPE_TXTBOX);
            $this->initVar("writer_avatar", XOBJ_DTYPE_TXTBOX);
            $this->initVar("writer_profile", XOBJ_DTYPE_TXTAREA);
            $this->initVar("dohtml", XOBJ_DTYPE_INT, 1);
            $this->initVar("dosmiley", XOBJ_DTYPE_INT, 1);
            $this->initVar("doxcode", XOBJ_DTYPE_INT, 1);
            $this->initVar("doimage", XOBJ_DTYPE_INT, 1);
            $this->initVar("dobr", XOBJ_DTYPE_INT, 1);
        }
    }
}
art_parse_class('
class [CLASS_PREFIX]WriterHandler extends  XoopsPersistableObjectHandler
{
    function [CLASS_PREFIX]WriterHandler(&$db)
    {
        $this->XoopsPersistableObjectHandler($db, art_DB_prefix("writer", true), "Writer", "writer_id", "writer_name");
    }
}
');
コード例 #7
0
ファイル: text.php プロジェクト: trabisdementia/xuups
art_parse_class('
class [CLASS_PREFIX]TextHandler extends XoopsPersistableObjectHandler
{
    function [CLASS_PREFIX]TextHandler(&$db)
    {
        $this->XoopsPersistableObjectHandler($db, art_DB_prefix("text", true), "Text", "text_id", "text_title");
    } 

    function &getByArticle($art_id, $page = 0, $tags = null)
    {
        $text = false;
        $page = intval($page);
        if (is_array($tags) && count($tags) > 0) {
            if (!in_array("text_id",$tags)) $tags[] = "text_id";
            $select = implode(",", $tags);
        } else $select = "*";

        if ($page) {
            $sql = "SELECT $select FROM " . art_DB_prefix("text") . " WHERE art_id = " . intval($art_id) . " ORDER BY text_id";
            $result = $this->db->query($sql, 1, $page-1);
            if ($result && $myrow = $this->db->fetchArray($result)) {
                $text =& $this->create(false);
                $text->assignVars($myrow);
                return $text;
            } else {
                //xoops_error($this->db->error());
                return $text;
            }
        } else {
            $sql = "SELECT $select FROM " . art_DB_prefix("text") . " WHERE art_id = " . intval($art_id) . " ORDER BY text_id";
            $result = $this->db->query($sql);
            $ret = array();
            while ($myrow = $this->db->fetchArray($result)) {
                $text =& $this->create(false);
                $text->assignVars($myrow);
                $ret[$myrow["text_id"]] = $text;
                unset($text);
            }
            return $ret;
        }
    }

    /*
    function getIdByArticle($art_id, $page = 0)
    {
        $page = intval($page);
        if ($page) {
            $sql = "SELECT text_id FROM " . art_DB_prefix("text") . " WHERE art_id = ". intval($art_id) ." ORDER BY text_id LIMIT ".(intval($page)-1).", 1";
            $result = $this->db->query($sql);
            while ($myrow = $this->db->fetchArray($result)) {
                $ret = $myrow["text_id"];
                return $ret;
            }
            $ret = null;
            return $ret;
        } else {
            $sql = "SELECT text_id FROM " . art_DB_prefix("text") . " WHERE art_id = ". intval($art_id) ." ORDER BY text_id";
            $result = $this->db->query($sql);
            $ret = array();
            while ($myrow = $this->db->fetchArray($result)) {
                $ret[] = $myrow["text_id"];
                unset($text);
            }
            return $ret;
        }
    }

    function getForPDF(&$text)
    {
        return $text->getBody(true);
    }

    function getForPrint(&$text)
    {
        return $text->getBody();
    }

    function deleteByArticle($art_id)
    {
        $sql = "DELETE FROM ".art_DB_prefix("text")." WHERE art_id = ".intval($art_id);
        if (!$result = $this->db->queryF($sql)) {
              //xoops_error($this->db->error());
            return false;
        }
        return true;
    }
    */

    /**
     * clean orphan text from database
     * 
     * @return     bool    true on success
     */
    function cleanOrphan()
    {
        return parent::cleanOrphan(art_DB_prefix("article"), "art_id");
    }
}
');
コード例 #8
0
ファイル: permission.php プロジェクト: trabisdementia/xuups
art_parse_class('
class [CLASS_PREFIX]PermissionHandler extends XoopsGroupPermHandler
{
    function deleteByCategory($cat_id)
    {
        global $xoopsModule;

        if (is_object($xoopsModule) && $xoopsModule->getVar("dirname") == $GLOBALS["artdirname"]) {
            $module_id = $xoopsModule->getVar("mid") ;
        } else {
            $module_handler =& xoops_gethandler("module");
            $artModule =& $module_handler->getByDirname($GLOBALS["artdirname"]);
            $module_id = $artModule->getVar("mid") ;
            unset($artModule);
        }

        $gperm_handler =& xoops_gethandler("groupperm");
        $criteria = new CriteriaCompo(new Criteria("gperm_modid", $module_id));
        $gperm_names = "(\'" . implode( "\', \'", array_keys( $GLOBALS["perms_category"] ) ) . "\')";
        $criteria->add(new Criteria("gperm_name", $gperm_names, "IN"));
        $criteria->add(new Criteria("gperm_itemid", $cat_id));
        $gperm_handler->deleteAll($criteria);

        $this->createPermData();
        return true;
    }

    function getPermission($gperm_name = "access", $id = 0)
    {
        global $xoopsUser, $xoopsModule;
        
        if ($GLOBALS["xoopsUserIsAdmin"] && $xoopsModule->getVar("dirname") == $GLOBALS["artdirname"]) {
            return true;
        }
        
        $groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : array(XOOPS_GROUP_ANONYMOUS);
        if ( !$groups ) return false;

        if ( !$allowed_groups = $this->getGroups($gperm_name, $id) ) return false;
        
        return count(array_intersect($allowed_groups, $groups));
    }

    function &getCategories($perm_name = "access")
    {
        global $xoopsUser;

        $ret = array();

        $groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : array(XOOPS_GROUP_ANONYMOUS);
        if (count($groups) < 1)    return $ret;

        $_cachedPerms = $this->loadPermData($perm_name);
        
        $allowed_cats = array();
        foreach ($_cachedPerms as $cat => $cats) {
            if ($cat == 0 || empty($cats)) continue;
            
            if (array_intersect($groups, $cats)) {
                $allowed_cats[$cat] = 1;
            }
        }
        unset($_cachedPerms);
        $ret = array_keys($allowed_cats);
        
        return $ret;
    }

    function &getGroups($gperm_name, $id = 0)
    {
        $_cachedPerms = $this->loadPermData($gperm_name);
        $groups = empty($_cachedPerms[$id]) ? array() : $_cachedPerms[$id];
        unset($_cachedPerms);
        return $groups;
    }

    function createPermData($perm_name = "")
    {
        global $xoopsModule;

        if (is_object($xoopsModule) && $xoopsModule->getVar("dirname") == $GLOBALS["artdirname"]) {
            $module_id = $xoopsModule->getVar("mid") ;
        } else {
            $module_handler =& xoops_gethandler("module");
            $artModule =& $module_handler->getByDirname($GLOBALS["artdirname"]);
            $module_id = $artModule->getVar("mid") ;
            unset($artModule);
        }

        $category_handler =& xoops_getmodulehandler("category", $GLOBALS["artdirname"]);
        $cat_ids = $category_handler->getIds();

        $gperm_handler =& xoops_gethandler("groupperm");
        $member_handler =& xoops_gethandler("member");
        $glist =& $member_handler->getGroupList();

        load_functions("cache");
        $perms = array();
        
        if ( empty($perm_name) || in_array($perm_name, array_keys($GLOBALS["perms_global"])) ):
        foreach (array_keys($glist) as $i) {
            $ids = $gperm_handler->getItemIds("global", $i, $module_id);
            foreach ($ids as $id) {
                foreach ($GLOBALS["perms_global"] as  $permname => $perm_info) {
                    if ( !empty($perm_name) && $perm_name != $permname ) continue;
                    if ($perm_info["id"] == $id) $perms[$permname][0][] = $i;
                }
            }
        }
        endif;
        
        if ( empty($perm_name) || $perm_name == "all" || in_array($perm_name, array_keys($GLOBALS["perms_category"])) ):
        foreach (array_keys($GLOBALS["perms_category"]) as $permname) {
            if ( !empty($perm_name) && $perm_name != "all" && $perm_name != $permname) continue;
            foreach (array_keys($glist) as $i) {
                $cats = $gperm_handler->getItemIds($permname, $i, $module_id);
                foreach ($cats as $cat) {
                    $perms[$permname][$cat][] = $i;
                    if (empty($perm_name) || $perm_name == "all") {
                        $perms["all"][$cat][] = $i;
                    }
                }
            }
        }
        endif;
        
        foreach (array_keys($perms) as $perm) {
            mod_createCacheFile($perms[$perm], "permission_{$perm}", $GLOBALS["artdirname"]);
        }
        
        $ret = !empty($perm_name) ? @$perms[$perm_name] : $perms;
        return $ret;
    }

    function &loadPermData($perm_name = "access")
    {
        load_functions("cache");
        if (!$perms = mod_loadCacheFile("permission_{$perm_name}", $GLOBALS["artdirname"])) {
            $perms = $this->createPermData($perm_name);
        }
        
        return $perms;
    }
    
    function validateRight($perm, $itemid, $groupid, $mid = null)
    {
        if (empty($mid)) {
            if (is_object($GLOBALS["xoopsModule"]) && $GLOBALS["xoopsModule"]->getVar("dirname") == $GLOBALS["artdirname"]) {
                $mid = $GLOBALS["xoopsModule"]->getVar("mid");
            } else {
                $module_handler =& xoops_gethandler("module");
                $mod =& $module_handler->getByDirname($GLOBALS["artdirname"]);
                $mid = $mod->getVar("mid");
                unset($mod);
            }
        }
        if ($this->_checkRight($perm, $itemid, $groupid, $mid)) return true;
        $this->addRight($perm, $itemid, $groupid, $mid);
        return true;
    }

    /**
     * Check permission (directly)
     * 
     * @param    string    $gperm_name       Name of permission
     * @param    int       $gperm_itemid     ID of an item
     * @param    int/array $gperm_groupid    A group ID or an array of group IDs
     * @param    int       $gperm_modid      ID of a module
     * 
     * @return    bool    TRUE if permission is enabled
     */
    function _checkRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1)
    {
        $criteria = new CriteriaCompo(new Criteria("gperm_modid", $gperm_modid));
        $criteria->add(new Criteria("gperm_name", $gperm_name));
        $gperm_itemid = intval($gperm_itemid);
        if ($gperm_itemid > 0) {
            $criteria->add(new Criteria("gperm_itemid", $gperm_itemid));
        }
        if (is_array($gperm_groupid)) {
            $criteria2 = new CriteriaCompo();
            foreach ($gperm_groupid as $gid) {
                $criteria2->add(new Criteria("gperm_groupid", $gid), "OR");
            }
            $criteria->add($criteria2);
        } else {
            $criteria->add(new Criteria("gperm_groupid", $gperm_groupid));
        }
        if ($this->getCount($criteria) > 0) {
            return true;
        }
        return false;
    }
    
    function deleteRight($perm, $itemid, $groupid, $mid = null)
    {
        if (empty($mid)) {
            if (is_object($GLOBALS["xoopsModule"]) && $GLOBALS["xoopsModule"]->getVar("dirname") == $GLOBALS["artdirname"]) {
                $mid = $GLOBALS["xoopsModule"]->getVar("mid");
            } else {
                $module_handler =& xoops_gethandler("module");
                $mod =& $module_handler->getByDirname($GLOBALS["artdirname"]);
                $mid = $mod->getVar("mid");
                unset($mod);
            }
        }
        if (is_callable(array(&$this->XoopsGroupPermHandler, "deleteRight"))) {
            return parent::deleteRight($perm, $itemid, $groupid, $mid);
        } else {
            $criteria = new CriteriaCompo(new Criteria("gperm_name", $perm));
            $criteria->add(new Criteria("gperm_groupid", $groupid));
            $criteria->add(new Criteria("gperm_itemid", $itemid));
            $criteria->add(new Criteria("gperm_modid", $mid));
            $perms_obj = $this->getObjects($criteria);
            if (!empty($perms_obj)) {
                foreach ($perms_obj as $perm_obj) {
                    $this->delete($perm_obj);
                }
            }
            unset($criteria, $perms_obj);
        }
        return true;
    }        
        
    function applyTemplate($category, $mid=null)
    {
        $perm_template = $this->getTemplate();
        if (empty($perm_template)) return false;
        
        if (empty($mid)) {
            if (is_object($GLOBALS["xoopsModule"]) && $GLOBALS["xoopsModule"]->getVar("dirname") == $GLOBALS["artdirname"]) {
                $mid = $GLOBALS["xoopsModule"]->getVar("mid");
            } else {
                $module_handler =& xoops_gethandler("module");
                $mod =& $module_handler->getByDirname($GLOBALS["artdirname"]);
                $mid = $mod->getVar("mid");
                unset($mod);
            }
        }
        
        $member_handler =& xoops_gethandler("member");
        $glist = $member_handler->getGroupList();
        $perms = array_keys($GLOBALS["perms_category"]);
        foreach (array_keys($glist) as $group) {
            foreach ($perms as $perm) {
                if (!empty($perm_template[$group][$perm])) {
                    $this->validateRight($perm, $category, $group, $mid);
                } else {
                    $this->deleteRight($perm, $category, $group, $mid);
                }
            }
        }
        return true;
    }
    
    function &getTemplate()
    {
        load_functions("cache");
        $perms = mod_loadCacheFile("perm_template", $GLOBALS["artdirname"]);
        return $perms;
    }
    
    function setTemplate($perms)
    {
        load_functions("cache");
        return mod_createCacheFile($perms, "perm_template", $GLOBALS["artdirname"]);
    }
}
');
コード例 #9
0
ファイル: category.php プロジェクト: trabisdementia/xuups
art_parse_class('
class [CLASS_PREFIX]CategoryHandler extends XoopsPersistableObjectHandler
{
    /**
     * Constructor
     *
     * @param object $db reference to the {@link XoopsDatabase} object     
     **/
    function [CLASS_PREFIX]CategoryHandler(&$db)
    {
        $this->XoopsPersistableObjectHandler($db, art_DB_prefix("category", true), "Xcategory", "cat_id", "cat_title");
    }

    /**
     * get IDs of latest articles of the category
     * 
     * @param     category    $category     {@link Xcategory}
     * @param     int           $limit      Max number of article IDs to fetch
     * @return     array of article IDs
     */
    function &getLastArticleIds(&$category, $limit = 0)
    {
        $art_ids = $category->getVar("cat_lastarticles");
        if ($limit > 0) @array_splice($art_ids, $limit);
        return $art_ids;
    }

    /**#@+
     *
     * set IDs of latest articles of the category in database
     * 
     * {@link CriteriaCompo} 
     *
     * @return     bool    true on success
     */
    function setLastArticleIds($cat_id = null)
    {
        if (is_array($cat_id) && count($cat_id) > 0) {
            foreach ($cat_id as $id) {
                $cat =& $this->get($id);
                $this->_setLastArticleIds($cat);
                unset($cat);
            }
        } elseif ($cat_id = intval($cat_id)) {
            $cat =& $this->get($cat_id);
            $this->_setLastArticleIds($cat);
            unset($cat);
        } else {
            $cats = $this->getAllByPermission("access", array("cat_id"));
            foreach ($cats as $id => $cat) {
                $cat =& $this->get($id);
                $this->_setLastArticleIds($cat);
            }
            unset($cats);
        }
        return true;
    }

    function _setLastArticleIds(&$category)
    {
        $criteria = new CriteriaCompo(new Criteria("ac.ac_publish", 0, ">"));
        $criteria->setSort("ac.ac_publish");
        $criteria->setOrder("DESC");
        $artConfig = art_load_config();
        $limit = MAX($artConfig["articles_perpage"], 10);
        $article_handler =& xoops_getmodulehandler("article", $GLOBALS["artdirname"]);
        $articleIds = $article_handler->getIdsByCategory($category, $limit, 0, $criteria);
        $category->setVar("cat_lastarticles", $articleIds);
        unset($articleIds);
        return $this->insert($category, true);
    }
    /**#@-*/

    /**
     * insert a new category into the database
     * 
     * @param    object    $category     {@link Xcategory} reference to Xcategory
     * @param     bool     $force         flag to force the query execution despite security settings
     * @return     int     category ID
     */
    function insert(&$category, $force = true)
    {
        $cat_id = parent::insert($category, $force);
        if (!empty($category->vars["cat_pid"]["changed"])) {
            $this->updateTracks($category);
        }
        return $cat_id;
    }

    /**
     * delete an article from the database
     * 
     * {@link Article}
     * {@link Xtopic}
     * {@link Permission}
     *
     * @param    object    $category             {@link Xcategory} reference to Xcategory
     * @param     bool     $force                 flag to force the query execution despite security settings
     * @param     bool     $forceDelete         flag to force deleting articles/subcategories, otherwise move to its parent category
     * @return     bool     true on success
     */
    function delete(&$category, $force = true, $forceDelete = false)
    {
        $des_cat = ($category->getVar("cat_pid")) ? $category->getVar("cat_pid") : 0; // move to parent category
        $article_handler =& xoops_getmodulehandler("article", $GLOBALS["artdirname"]);
        $articles = $article_handler->getByCategory($category);
        
        if (!empty($forceDelete) || empty($des_cat)) {
            foreach (array_keys($articles) as $id) {
                $article_handler->terminateCategory($articles[$id], $category->getVar("cat_id"));
                if (is_object($articles[$id]) && $articles[$id]->getVar("cat_id") != $category->getVar("cat_id")) {
                    $article_handler->updateCategories($articles[$id]);
                }
            }
        } else {
            $article_handler->updateAll("cat_id", $des_cat, new Criteria("cat_id", $category->getVar("cat_id")), true);
            foreach (array_keys($articles) as $id) {
                if ($des_cat > 0 && $articles[$id]->getVar("cat_id") == $category->getVar("cat_id")) {
                    $article_handler->moveCategory($articles[$id], $des_cat, $category->getVar("cat_id"));
                } else {
                    $article_handler->terminateCategory($articles[$id], $category->getVar("cat_id"));
                }
                $article_handler->updateCategories($articles[$id]);
            }
        }
        unset($articles);
        
        if (empty($forceDelete)) {
            $this->updateAll("cat_pid", $des_cat, new Criteria("cat_pid", $category->getVar("cat_id")), true);
            if (!empty($des_cat)) {
                $this->setLastArticleIds($des_cat);
            }
        } else {
            $cats = $this->getChildCategories($category->getVar("cat_id"));        
            foreach (array_keys($cats) as $id) {
                $this->delete($cats[$id], $force, $forceDelete);
            }
            unset($cats);
        }
        
        $queryFunc = empty($force) ? "query" : "queryF";
        $sql = "DELETE FROM " . art_DB_prefix("artcat") . " WHERE cat_id = " . $category->getVar("cat_id");
        $result = $this->db->{$queryFunc}($sql);
        
        $topic_handler =& xoops_getmodulehandler("topic", $GLOBALS["artdirname"]);
        $topic_handler->deleteAll(new Criteria("cat_id", $category->getVar("cat_id")));
        
        xoops_notification_deletebyitem($GLOBALS["xoopsModule"]->getVar("mid"), "category", $category->getVar("cat_id"));
        
        $sql = "DELETE FROM " . $category->table . " WHERE cat_id = " . $category->getVar("cat_id");
        if ($result = $this->db->{$queryFunc}($sql)) {

            $permission_handler =& xoops_getmodulehandler("permission", $GLOBALS["artdirname"]);
            $permission_handler->deleteByCategory($category->getVar("cat_id"));
        
            return true;
        } else {
            //xoops_error("delte category error: ".$sql);
            return false;
        }
    }

    /**
     * get a list of categories including a specified article and matching a condition
     * 
     * {@link Permission}
     *
     * @param    mixed    $art_id     article ID(s)
     * @param     object    $criteria     {@link CriteriaElement} to match
     * @return     array of categories {@link Xcategory}
     */
    function &getByArticle($art_id, $criteria = null)
    {
        $_cachedTop = array();
        $ret = array();
        if (empty($art_id)) {
            return $ret;
        }

        $sql = "SELECT c.cat_id, c.cat_title FROM " . art_DB_prefix("category") . " AS c";
        $sql .= " LEFT JOIN " . art_DB_prefix("artcat") . " AS ac ON ac.cat_id=c.cat_id";
        if (is_array($art_id) && count($art_id) > 0) {
            $sql .= " WHERE ac.art_id IN (" . implode(",", $art_id) . ")";
        } elseif (intval($art_id)) {
            $sql .= " WHERE ac.art_id = " . intval($art_id);
        } else {
            $sql .= " WHERE 1=1";
        }
        
        mod_loadFunctions("user", $GLOBALS["artdirname"]);
        if (!art_isAdministrator()) {
            $permission_handler =& xoops_getmodulehandler("permission", $GLOBALS["artdirname"]);
            $allowed_cats =& $permission_handler->getCategories();
            if (count($allowed_cats) == 0) return $ret;
            $sql .= " AND c.cat_id IN (" . implode(",", $allowed_cats) . ")";
        }
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $sql .= " AND " . $criteria->render();
            if ($criteria->getSort() != "") {
                $sql .= " ORDER BY " . $criteria->getSort() . " " . $criteria->getOrder();
                $orderSet = true;
            }
        }
        if (empty($orderSet))  $sql .= " ORDER BY c.cat_id, c.cat_order";
        if (!$result = $this->db->query($sql)) {
            //xoops_error($this->db->error());
            return $ret;
        }
        while ($row = $this->db->fetchArray($result)) {
            $category =& $this->create(false);
            $category->assignVars($row);
            $ret[$category->getVar("cat_id")] = $category;
            unset($category);
        }
        return $ret;
    }

    /**
     * get all categories with specified permission
     * 
     * @param string    $permission Permission type
     * @param array        $tags     variables to fetch
     * @return    array of categories {@link Xcategory}
     */
    function &getAllByPermission($permission = "access", $tags = null)
    {
        $ret = array();
        
        mod_loadFunctions("user", $GLOBALS["artdirname"]);
        if (!art_isAdministrator()) {
            $permission_handler =&  xoops_getmodulehandler("permission", $GLOBALS["artdirname"]);
            $allowed_cats =& $permission_handler->getCategories($permission);
            if ( count($allowed_cats) == 0 ) return $ret;
            $criteria = new Criteria("cat_id", "(" . implode(",", $allowed_cats) . ")", "IN");
        } else {
            $criteria = new Criteria("1", 1);
        }
        $criteria->setSort("cat_pid ASC, cat_order");
        $criteria->setOrder("ASC");
        
        $ret = parent::getAll($criteria, $tags);
        return $ret;
    }
    
    /**
     * get child categories with specified permission
     * 
     * @param int    $category category ID
     * @param string    $permission Permission type
     * @return    array of categories {@link Xcategory}
     */
    function &getChildCategories($category = 0, $permission = "access", $tags = null)
    {
        $pid = intval($category);
        $ret = array();
        
        $criteria = new CriteriaCompo(new Criteria("cat_pid", $pid));
        $criteria->setSort("cat_order");
        mod_loadFunctions("user", $GLOBALS["artdirname"]);
        if (!art_isAdministrator()) {
            $permission_handler =&  xoops_getmodulehandler("permission", $GLOBALS["artdirname"]);
            $allowed_cats =& $permission_handler->getCategories($permission);
            if (count($allowed_cats) == 0) return $ret;
            $criteria->add(new Criteria("cat_id", "(" . implode(", ", $allowed_cats) . ")", "IN"));
        }
        
        $ret = parent::getAll($criteria, $tags);
        unset($criteria);
        return $ret;
    }

    /**
     * get all subcategories with specified permission
     * 
     * @param     int        $pid 
     * @param     string    $permission Permission type
     * @param     array    $tags         variables to fetch
     * @return    array of categories {@link Xcategory}
     */
    function &getSubCategories($pid = 0, $permission = "access", $tags = null)
    {
        $pid = intval($pid);
        $perm_string = (empty($permission)) ? "access" : $permission;
        if (!is_array($tags) || count($tags) == 0) $tags = array("cat_id", "cat_pid", "cat_title", "cat_order");
        $categories = $this->getAllByPermission($perm_string, $tags);

        require_once XOOPS_ROOT_PATH . "/modules/" . $GLOBALS["artdirname"] . "/class/tree.php";
        $tree = new artTree($categories);
        $category_array = $tree->getAllChild(0);
        unset($categories);
        return $category_array;
    }

    /**#@+
     * get all parent categories
     * 
     * @param     object    $category {@link Xcategory}
     * @param     bool    $reverse    flag for reverse order
     * @return    array of categories {@link Xcategory}
     */
    function &getSupCategories(&$category, $reverse = true)
    {
        if (!is_object($category))  {
            $category =& $this->get(intval($category));
        }
        $pid = $category->getVar("cat_pid");
        $category_array=array();
        $this->_getSup($category_array, $pid);
        if ($reverse) $category_array = array_reverse($category_array);

        return $category_array;
    }

    function _getSup(&$category_array, $id = 0)
    {
        if (empty($id)) return null;
        $category = $this->get(intval($id));
        $category_array[] = intval($id);
        $pid = $category->getVar("cat_pid");
        unset($category);
        $this->_getSup($category_array, $pid);
        return true;
    }
    /**#@-*/

    /**
     * recursively update breadcrumbs of the category and its subcategories
     * 
     * @param     object    $category {@link Xcategory}
     * @param     array    $tracks    array of parent category IDs
     * @return    bool    true on success
     */
    function updateTracks(&$category, $tracks = null)
    {
        if ($tracks === null)  $tracks = $this->getSupCategories($category);
        $this->setTrack($category, $tracks);
        $subCats = $this->getChildCategories($category->getVar("cat_id"));
        $tracks[] = $category->getVar("cat_id");
        foreach ($subCats as $id => $cat) {
            $this->updateTracks($cat, $tracks);
        }
        unset($subCats, $tracks);
        return true;
    }

    /**
     * set breadcrumbs of the category
     * 
     * @param     object    $category {@link Xcategory}
     * @param     array    $ids    array of parent category IDs
     * @return    bool    true on success
     */
    function setTrack(&$category, $ids = array())
    {
         if (!is_array($ids)) return false;
         $ids = array_map("intval", $ids);
         $track = $this->db->quoteString(serialize($ids));
        $sql = "UPDATE " . $category->table . " SET cat_track = $track WHERE cat_id=" . $category->getVar("cat_id");
        if (!$result = $this->db->queryF($sql)) {
            //xoops_error($this->db->error());
            return false;
        }
        return true;
    }

    /**
     * get breadcrumbs of the category
     * 
     * @param     object    $category {@link Xcategory}
     * @param     bool    $incCurrent    flag for including current category
     * @return    array    associative array of category IDs and titles
     */
    function &getTrack(&$category, $incCurrent = false)
    {
        $ret = array();
        if (!is_object($category)) {
            return $ret;
        }
        $tracks = $category->getVar("cat_track");
        if (!empty($tracks)) {
            $criteria = new Criteria("cat_id", "(" . implode(",", $tracks) . ")", "IN");
            $cats = $this->getList($criteria);
            foreach ($tracks as $id) {
                $ret[]=array(
                    "id"    => $id,
                    "title"    => $cats[$id]
                    );
            }
        }
        if ($incCurrent) {
            $ret[] = array(
                "id"    => $category->getVar("cat_id"),
                "title"    => $category->getVar("cat_title")
                );
        }
        return $ret;
    }

    /**
     * get a hierarchical tree of categories
     * 
     * {@link artTree} 
     *
     * @param     int        $pid     Top category ID
     * @param     string    $permission    permission type
     * @param     string    $prefix        prefix for display
     * @param     string    $tags        variables to fetch
     * @return    array    associative array of category IDs and sanitized titles
     */
    function &getTree($pid = 0, $permission = "access", $prefix = "--", $tags = array())
    {
        $pid = intval($pid);
        $perm_string = $permission;
        if (!is_array($tags) || count($tags) == 0) $tags = array("cat_id", "cat_pid", "cat_title", "cat_order");
        $categories = $this->getAllByPermission($perm_string, $tags);

        require_once XOOPS_ROOT_PATH . "/modules/" . $GLOBALS["artdirname"] . "/class/tree.php";
        $tree = new artTree($categories);
        $category_array =& $tree->makeTree($prefix, $pid, $tags);
        return $category_array;
    }

    /**
     * get a hierarchical array tree of categories
     * 
     * {@link artTree} 
     *
     * @param     int        $pid     Top category ID
     * @param     string    $permission    permission type
     * @param     string    $tags        variables to fetch
     * @param   integer    $depth    level of subcategories
     * @return    array    associative array of category IDs and sanitized titles
     */
    function &getArrayTree($pid = 0, $permission = "access", $tags = null, $depth = 0)
    {
        $pid = intval($pid);
        $perm_string = $permission;
        if (!is_array($tags) || count($tags) == 0) $tags = array("cat_id", "cat_pid", "cat_title", "cat_order");
        $categories = $this->getAllByPermission($perm_string, $tags);

        require_once XOOPS_ROOT_PATH . "/modules/" . $GLOBALS["artdirname"] . "/class/tree.php";
        $tree = new artTree($categories);
        $category_array =& $tree->makeArrayTree($pid, $tags, $depth);
        return $category_array;
    }

    /**
     * get articles matching a condition of a category
     * 
     * @param mixed        $category     array or {@link Xcategory}
     * @param int       $limit      Max number of objects to fetch
     * @param int       $start      Which record to start at
     * @param object    $criteria     {@link CriteriaElement} to match
     * @param array        $tags         variables to fetch
     * @param bool        $asObject     flag indicating as object, otherwise as array
     * @return array of articles {@link Article}
     */
       function &getArticles(&$category, $limit = 0, $start = 0, $criteria = null, $tags = null, $asObject = true)
       {
        if (is_array($tags) && count($tags) > 0) {
            $key_artid = array_search("art_id", $tags);
            if (is_numeric($key_artid)) {
                $tags[$key_artid] = "a.art_id";
            }
            if (!in_array("a.art_id", $tags)) {
                $tags[] = "a.art_id";
            }
            $select = implode(",", $tags);
        } else { 
            $select = "*";
        }
        $sql = "SELECT $select FROM " . art_DB_prefix("article") . " AS a";
        $sql .= " LEFT JOIN " . art_DB_prefix("artcat") . " AS ac ON ac.art_id=a.art_id";
        $sql .= " WHERE a.art_time_submit > 0";
        $sql .= " AND (a.cat_id = ac.cat_id OR a.art_time_publish > 0)";
        if (is_array($category) && count($category) > 0) {
            $category = array_map("intval", $category);
            $sql .= " AND ac.cat_id IN (" . implode(",", $category) . ")";
        } else {
            $cat_id = (is_object($category)) ? $category->getVar("cat_id") : intval($category);
            if ($cat_id > 0) $sql .= " AND ac.cat_id = " . intval($cat_id);
        }
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $sql .= " AND " . $criteria->render();
            if ($criteria->getSort() != "") {
                $sql .= " ORDER BY " . $criteria->getSort() . " " . $criteria->getOrder();
                $orderSet = true;
            }
        }
        if (empty($orderSet)) $sql .= " ORDER BY ac.ac_publish DESC";
        $result = $this->db->query($sql, intval($limit), intval($start));
        $ret = array();
        $article_handler =& xoops_getmodulehandler("article", $GLOBALS["artdirname"]);
           while ($myrow = $this->db->fetchArray($result)) {
            $article =& $article_handler->create(false);
            $article->assignVars($myrow);
            if ($asObject) {
                $ret[] = $article;
            } else {
                $_ret = array();
                foreach ($myrow as $key => $val) {
                    $_ret[$key] = isset($article->vars[$key]) ? $article->getVar($key) : $val;
                }
                //$ret[] = $article->getValues(array_keys($myrow));
                $ret[] = $_ret;
            }
            unset($article);
        }
        return $ret;
       }
       
    /**
     * count featured articles matching a condition of a category
     * 
     * {@link CriteriaCompo} 
     *
     * @param     mixed    $cat_id array or {@link Xcategory}
     * @param     object     $criteria {@link CriteriaElement} to match
     * @return     int     count of articles
     */
       function getArticleCountFeatured(&$cat_id, $criteria = null)
       {
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $criteria->add(new Criteria("ac.ac_feature", 0, ">"));
        } else {
            $criteria = new CriteriaCompo(new Criteria("ac.ac_feature", 0, ">"));
        }
        return $this->getArticleCount($cat_id, $criteria);
       }

    /**
     * count published articles matching a condition of a category
     * 
     * {@link CriteriaCompo} 
     *
     * @param     mixed    $cat_id array or {@link Xcategory}
     * @param     object     $criteria {@link CriteriaElement} to match
     * @return     int     count of articles
     */
       function getArticleCountPublished(&$cat_id, $criteria = null)
       {
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $criteria->add(new Criteria("ac.ac_publish", 0, ">"));
        } else {
            $criteria = new CriteriaCompo(new Criteria("ac.ac_publish", 0, ">"));
        }
        return $this->getArticleCount($cat_id, $criteria);
       }

    /**
     * count registered articles matching a condition of a category
     * 
     * {@link CriteriaCompo} 
     *
     * @param     mixed    $cat_id array or {@link Xcategory}
     * @param     object     $criteria {@link CriteriaElement} to match
     * @return     int     count of articles
     */
       function getArticleCountRegistered(&$cat_id, $criteria = null)
       {
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $criteria->add(new Criteria("ac.ac_register", 0, ">"));
        } else {
            $criteria = new CriteriaCompo(new Criteria("ac.ac_register", 0, ">"));
        }
        $criteria->add(new Criteria("ac.ac_publish", 0));
        return $this->getArticleCount($cat_id, $criteria);
       }

    /**
     * count articles matching a condition of a category
     * 
     * @param     mixed    $cat_id array or {@link Xcategory}
     * @param     object     $criteria {@link CriteriaElement} to match
     * @return     int     count of articles
     */
    function getArticleCount(&$cat_id, $criteria = null)
    {
        $sql = "SELECT COUNT(*) as count FROM " . art_DB_prefix("artcat") . " AS ac";
        if (is_array($cat_id) && count($cat_id) > 0) {
            $sql .= " WHERE ac.cat_id IN (" . implode(",", $cat_id) . ")";
        } elseif (is_object($cat_id)) {
            $sql .= " WHERE ac.cat_id = " . $cat_id->getVar("cat_id");
        } elseif (intval($cat_id)) {
            $sql .= " WHERE ac.cat_id = " . intval($cat_id);
        } else {
            $sql .= " WHERE 1=1";
        }
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $sql .= " AND " . $criteria->render();
        }
        $result = $this->db->query($sql);
        $myrow = $this->db->fetchArray($result);
        return intval($myrow["count"]);
    }

    /**
     * count registered articles matching a condition of a list of categories, respectively
     * 
     * @param     int        $cat_id array of category IDs
     * @param     object     $criteria {@link CriteriaElement} to match
     * @return     int     count of articles
     */
       function getArticleCountsRegistered($cat_id = 0, $criteria = null)
       {
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $criteria->add(new Criteria("ac.ac_register", 0, ">"));
        } else {
            $criteria = new CriteriaCompo(new Criteria("ac.ac_register", 0, ">"));
        }
        $criteria->add(new Criteria("ac.ac_publish", 0));
        return $this->getArticleCounts($cat_id, $criteria);
       }

    /**
     * count articles matching a condition of a list of categories, respectively
     * 
     * @param     mixed    $cat_id array or {@link Xcategory}
     * @param     object     $criteria {@link CriteriaElement} to match
     * @return     array     associative array category ID and article count
     */
    function &getArticleCounts($cat_id = null, $criteria = null)
    {
        $sql = "SELECT ac.cat_id, COUNT(*) as count FROM " . art_DB_prefix("artcat") . " AS ac";
        if (is_array($cat_id) && count($cat_id) > 0) {
            $sql .= " WHERE ac.cat_id IN (" . implode(",", $cat_id) . ")";
        } elseif (intval($cat_id)) {
            $sql .= " WHERE ac.cat_id = " . intval($cat_id);
        } else {
            $sql .= " WHERE 1=1";
        }
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $sql .= " AND " . $criteria->render();
        }
        $sql .= " GROUP BY ac.cat_id";
        $result = $this->db->query($sql);
        $ret = array();
        while ($myrow = $this->db->fetchArray($result)) {
            $ret[$myrow["cat_id"]] = $myrow["count"];
        }
        return $ret;
    }

    /**
     * count categories with specified permission of a list of parent categories, respectively
     * 
     * @param     mixed    $cat_pid     array or category ID
     * @param     string     $permission    permission type
     * @return     array     associative array category IDs and subcategory counts
     */
    function &getCategoryCounts($cat_pid=0, $permission = "access")
    {
        $sql = "SELECT cat_pid, COUNT(*) as count FROM " . $this->table;
        if (is_array($cat_pid) && count($cat_pid) > 0) {
            $sql .= " WHERE cat_pid IN (" . implode(",", $cat_pid) . ")";
        } elseif (intval($cat_pid)) {
            $sql .= " WHERE cat_pid = " . intval($cat_pid);
        } else {
            $sql .= " WHERE 1=1";
        }
        mod_loadFunctions("user", $GLOBALS["artdirname"]);
        if (!art_isAdministrator()) {
            $permission_handler =&  xoops_getmodulehandler("permission", $GLOBALS["artdirname"]);
            $allowed_cats =& $permission_handler->getCategories($permission);
            if (count($allowed_cats) == 0) {
                $ret = array();
                return $ret;
            }
            $sql .= " AND cat_id IN (" . implode(",", $allowed_cats) . ")";
        }
        $sql .= " GROUP BY cat_pid";
        $result = $this->db->query($sql);
        $ret = array();
        while ($myrow = $this->db->fetchArray($result)) {
            $ret[$myrow["cat_pid"]] = $myrow["count"];
        }
        return $ret;
    }

    /**
     * check permission of the category
     * 
     * {@link Permission} 
     *
     * @param     mixed    $category     category ID or {@link Xcategory}
     * @param     string     $type         permission type
     * @return     bool     true on accessible
     */
    function getPermission(&$category, $type = "access")
    {
        global $xoopsUser, $xoopsModule;
        static $_cachedPerms;

        if ($GLOBALS["xoopsUserIsAdmin"] && $xoopsModule->getVar("dirname") == $GLOBALS["artdirname"]) {
            return true;
        }
        $cat_id = is_object($category)? $category->getVar("cat_id") : intval($category);

        $type = strtolower($type);
        if ("moderate" == $type) {
            mod_loadFunctions("user", $GLOBALS["artdirname"]);
            $permission = art_isModerator($category);
        } else {
            if (!isset($_cachedPerms[$type][$cat_id])) {
                $getpermission =&  xoops_getmodulehandler("permission", $GLOBALS["artdirname"]);
                $_cachedPerms[$type][$cat_id] = $getpermission->getPermission($type, $cat_id);
            }
            $permission = (!empty($_cachedPerms[$type][$cat_id])) ? 1 : 0;
        }
        return $permission;
    }

    /**
     * clean orphan art-cat links from database
     * 
     * @return     bool    true on success
     */
    function cleanOrphan()
    {
        /* for MySQL 4.1+ */
        if (version_compare( mysql_get_server_info(), "4.1.0", "ge" )):
        $sql =     "DELETE " . $this->table . " FROM " . $this->table.
                " LEFT JOIN " . $this->table . " AS aa ON " . $this->table . ".cat_pid = aa.cat_id ".
                " WHERE " . $this->table . ".cat_pid>0 AND (aa.cat_id IS NULL)";
        else:
        $this->identifierName = "cat_pid";
        $category_list = $this->getList(new Criteria("cat_pid", 0, ">"));
        $this->identifierName = "cat_title";
        if ($parent_categories = @array_values($category_list)) {
            $parent_list = $this->getIds(new Criteria("cat_id", "(" . implode(", ", $parent_categories) . ")", "IN"));
            foreach ($category_list as $cat_id => $parent_category) {
                if (in_array($parent_category, $parent_list)) continue;
                $category_obj =& $this->get($cat_id);
                $this->delete($category_obj);
                unset($category_obj);
            }
        }
        endif;
        
        if (!empty($sql) && !$result = $this->db->queryF($sql)) {
            //xoops_error("cleanOrphan error:". $sql);
        }
        
        /* for MySQL 4.1+ */
        if (version_compare( mysql_get_server_info(), "4.1.0", "ge" )):
        $sql = "DELETE FROM " . art_DB_prefix("artcat") .
                " WHERE (cat_id NOT IN ( SELECT DISTINCT cat_id FROM " . $this->table . ") )";
        else:
        $sql =     "DELETE " . art_DB_prefix("artcat") . " FROM " . art_DB_prefix("artcat") .
                " LEFT JOIN " . $this->table . " AS aa ON " . art_DB_prefix("artcat") . ".cat_id = aa.cat_id ".
                " WHERE (aa.cat_id IS NULL)";
        endif;
        if (!$result = $this->db->queryF($sql)) {
            //xoops_error("cleanOrphan error:". $sql);
        }
        return true;
    }
    
    function updateTrack($category = null, $tracks = null)
    {
        if (empty($category)) {
            $categories_obj = $this->getObjects(new Criteria("cat_pid", 0), true);
            foreach (array_keys($categories_obj) as $key) {
                $this->updateTracks($categories_obj[$key]);
            }
            unset($categories_obj);
            return true;
        }
        if ($tracks === null)  $tracks = $this->getSupCategories($category);
        $this->setTrack($category, $tracks);
        $subCats = $this->getChildCategories($category->getVar("cat_id"));
        $tracks[] = $category->getVar("cat_id");
        foreach ($subCats as $id => $cat) {
            $this->updateTracks($cat, $tracks);
        }
        unset($subCats, $tracks);
        return true;
    }
}
');
コード例 #10
0
ファイル: rate.php プロジェクト: trabisdementia/xuups
art_parse_class('
class [CLASS_PREFIX]RateHandler extends XoopsPersistableObjectHandler
{
    function [CLASS_PREFIX]RateHandler(&$db)
    {
        $this->XoopsPersistableObjectHandler($db, art_DB_prefix("rate", true), "Rate", "rate_id");
    }
    
    function &getByArticle($art_id, $criteria = null)
    {
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $criteria->add(new Criteria("art_id", intval($art_id)), "AND");
        } else {
            $criteria = new CriteriaCompo(new Criteria("art_id", intval($art_id)));
        }
        $ret =& $this->getAll($criteria);
        return $ret;
    }

    function deleteByArticle($art_id)
    {
        if (is_array($art_id)) {
            if (count($art_id) > 0) {
                $art_id = array_map("intval", $art_id);
                $where = " WHERE art_id IN (" . implode(",", $art_id) . ")";
            } else {
                return false;
            }
        } elseif (!empty($art_id)) {
            $where = " WHERE art_id= " . intval($art_id);
        } else {
            return false;
        }
        $sql = "DELETE FROM " . art_DB_prefix("rate") . $where;
        if (!$result = $this->db->queryF($sql)) {
            return false;
        }
        return true;
    }

    /**
     * clean orphan items from database
     * 
     * @return     bool    true on success
     */
    function cleanOrphan()
    {
        return parent::cleanOrphan(art_DB_prefix("article"), "art_id");
    }
}
');
コード例 #11
0
ファイル: xmlpc.php プロジェクト: trabisdementia/xuups
}
art_parse_class('
class [CLASS_PREFIX]XmlrpcHandler
{
    function &get($type = "c")
    {
        switch (strtolower($type)) {
        case "s":
        case "server":
            return new Xmlrpc_server();
        case "c":
        case "client":
            return new Xmlrpc_client();
        }
    }

    function display(&$feed, $filename = "")
    {
        if (!is_object($feed)) return null;
        $filename = empty($filename) ? $feed->filename : $filename;
        echo $feed->saveFeed($feed->version, $filename);
    }

    function utf8_encode(&$feed)
    {
        if (!is_object($feed)) return null;
        $text = xoops_utf8_encode(serialize($feed));
        $feed = unserialize($text);
    }
}
');
コード例 #12
0
ファイル: article.php プロジェクト: trabisdementia/xuups
art_parse_class('
class [CLASS_PREFIX]ArticleHandler extends XoopsPersistableObjectHandler
{
    /**
     * Constructor
     *
     * @param object $db reference to the {@link XoopsDatabase} object     
     **/
    function [CLASS_PREFIX]ArticleHandler(&$db)
    {
        $this->XoopsPersistableObjectHandler($db, art_DB_prefix("article", true), "Article", "art_id", "art_title");
    }

    /**
     * Get the previous article and the next article of an articles
     * 
     * @param   object    $article    reference to the article
     * @param   array    $category    article scope
     * @return  array
     **/
    // To be optimized
    function &getSibling(&$article, &$category)
    {
        $ret = array();
        
        $sql_cat = "";
        if (is_array($category) && count($category) > 0) {
            $category = array_map("intval", $category);
            $sql_cat = " AND ac.cat_id IN (" . implode(",", $category) . ")";
        } else {
            $cat_id = (is_object($category)) ? $category->getVar("cat_id") : intval($category);
            if ($cat_id > 0) $sql_cat = " AND ac.cat_id = " . intval($cat_id);
        }

        $sql = "
            SELECT a.art_id as prev_id, a.art_title
                FROM " . art_DB_prefix("article") . " AS a
                LEFT JOIN " . art_DB_prefix("artcat") . " AS ac
                ON ac.art_id=a.art_id
                WHERE a.art_id < " . $article->getVar("art_id") . "
                " . $sql_cat . "
                ORDER BY a.art_id DESC LIMIT 1
        ";
        $result = $this->db->query($sql);
        $myrow = $this->db->fetchArray($result);
        if (!empty($myrow["prev_id"])) {
            $ret["previous"] = array("id" => $myrow["prev_id"], "title" => $myrow["art_title"]);
        }

        $sql = "
            SELECT a.art_id as next_id, a.art_title
                FROM " . art_DB_prefix("article") . " AS a
                LEFT JOIN " . art_DB_prefix("artcat") . " AS ac
                ON ac.art_id=a.art_id
                WHERE a.art_id > " . $article->getVar("art_id") . "
                " . $sql_cat . "
                ORDER BY a.art_id ASC LIMIT 1
        ";
        $result = $this->db->query($sql);
        $myrow = $this->db->fetchArray($result);
        if (!empty($myrow["next_id"])) {
            $ret["next"] = array("id" => $myrow["next_id"], "title" => $myrow["art_title"]);
        }

        return $ret;
    }

    /**
     * count articles matching a condition of a category (categories)
     * 
     * @param     mixed     $category array or {@link Xcategory}
     * @param     object     $criteria {@link CriteriaElement} to match
     * @return int count of articles
     */
    function getCountByCategory(&$category, $criteria = null)
    {
        $sql = "SELECT COUNT(DISTINCT a.art_id) as count FROM " . art_DB_prefix("article") . " AS a LEFT JOIN " . art_DB_prefix("artcat") . " AS ac ON a.art_id=ac.art_id WHERE 1=1 ";
        if (is_array($category) && count($category) > 0) {
            $category = array_map("intval", $category);
            $sql .= " AND ac.cat_id IN (" . implode(",", $category) . ")";
        } else {
            $cat_id = (is_object($category)) ? $category->getVar("cat_id") : intval($category);
            if ($cat_id > 0) $sql .= " AND ac.cat_id = " . intval($cat_id);
        }
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $sql .= " AND " . $criteria->render();
        }
        $result = $this->db->query($sql);
        $myrow = $this->db->fetchArray($result);
        return intval($myrow["count"]);
    }

    /**
     * count articles matching a condition of a topic
     * 
     * {@link Xtopic} 
     *
     * @param     mixed     $top_id        topic ID(s)
     * @param     object     $criteria     {@link CriteriaElement} to match
     * @return int count of articles
     */
    function getCountByTopic($top_id, $criteria = null)
    {
        $topic_handler =& xoops_getmodulehandler("topic", $GLOBALS["artdirname"]);
        return $topic_handler->getArticleCount($top_id, $criteria);
    }

    /**
     * get articles matching a condition of a topic
     * 
     * @param int       $top_id     Topic ID
     * @param int       $limit      Max number of objects to fetch
     * @param int       $start      Which record to start at
     * @param object    $criteria     {@link CriteriaElement} to match
     * @param array        $tags         variables to fetch
     * @param bool        $asObject     flag indicating as object, otherwise as array
     * @return array of articles {@link Article}
     */
    function &getByTopic($top_id, $limit = 0, $start = 0, $criteria = null, $tags = null, $asObject = true)
    {
        if (is_array($tags) && count($tags) > 0) {
            if (!in_array("art_id", $tags) && !in_array("a.art_id", $tags)) $tags[] = "a.art_id";
            $select = implode(",", $tags);
        }
        else $select = "*";
        $sql = "SELECT $select FROM " . art_DB_prefix("article") . " AS a";
        $sql .= " LEFT JOIN " . art_DB_prefix("arttop") . " AS at ON at.art_id=a.art_id";
        $sql .= " WHERE a.art_time_submit > 0 AND a.art_time_publish > 0";
        if (is_array($top_id) && count($top_id)>0) {
            $sql .= " AND at.top_id IN (" . implode(",", $top_id) . ")";
        } elseif (intval($top_id)) {
            $sql .= " AND at.top_id = " . intval($top_id);
        }
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $sql .= " AND " . $criteria->render();
            if ($criteria->getSort() != "") {
                $sql .= " ORDER BY " . $criteria->getSort() . " " . $criteria->getOrder();
                $orderSet = true;
            }
        }
        if (empty($orderSet)) $sql .= " ORDER BY at.at_time DESC";

        $result = $this->db->query($sql, intval($limit), intval($start));
        $ret = array();
        while ($myrow = $this->db->fetchArray($result)) {
            $article =& $this->create(false);
            $article->assignVars($myrow);
            if ($asObject) {
                $ret[$myrow["art_id"]] = $article;
            } else {
                $ret[$myrow[$this->keyName]] = $article->getValues(array_keys($myrow));
            }
            unset($article);
        }
        return $ret;
    }

    /**
     * get articles matching a condition of a category (categories)
     * 
     * @param mixed       $category   Category ID or object
     * @param int       $limit      Max number of objects to fetch
     * @param int       $start      Which record to start at
     * @param object    $criteria     {@link CriteriaElement} to match
     * @param array        $tags         variables to fetch
     * @param bool        $asObject     flag indicating as object, otherwise as array
     * @return array of articles {@link Article}
     */
    function &getByCategory($category, $limit = 0, $start = 0, $criteria = null, $tags = null, $asObject = true)
    {
        if (is_array($tags) && count($tags) > 0) {
            $key_artid = array_search("art_id", $tags);
            if (is_numeric($key_artid)) {
                unset($tags[$key_artid]);
            }
            $key_artid = array_search("a.art_id", $tags);
            if (is_numeric($key_artid)) {
                unset($tags[$key_artid]);
            }
            if (strtolower($tags[0]) != "distinct a.art_id") {
                array_unshift($tags, "DISTINCT a.art_id");
            }
            $select = implode(",", $tags);
        }
        else $select = "*";
        $sql = "SELECT {$select} FROM " . art_DB_prefix("article") . " AS a";
        $sql .= " LEFT JOIN " . art_DB_prefix("artcat") . " AS ac ON ac.art_id=a.art_id";
        $sql .= " WHERE a.art_time_submit > 0";
        $sql .= " AND (a.cat_id = ac.cat_id OR a.art_time_publish > 0)";
        if (is_array($category) && count($category) > 0) {
            $category = array_map("intval", $category);
            $sql .= " AND ac.cat_id IN (" . implode(",", $category) . ")";
        } else {
            $cat_id = (is_object($category)) ? $category->getVar("cat_id") : intval($category);
            if ($cat_id > 0) $sql .= " AND ac.cat_id = " . intval($cat_id);
        }
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $sql .= " AND " . $criteria->render();
            if ($criteria->getSort() != "") {
                $sql .= " ORDER BY " . $criteria->getSort() . " " . $criteria->getOrder();
                $orderSet = true;
            }
        }
        if (empty($orderSet)) $sql .= " ORDER BY a.art_id DESC";
        $result = $this->db->query($sql, intval($limit), intval($start));
        $ret = array();
        while ($myrow = $this->db->fetchArray($result)) {
            $article =& $this->create(false);
            $article->assignVars($myrow);
            if ($asObject) {
                $ret[$myrow["art_id"]] = $article;
            } else {
                $ret[$myrow[$this->keyName]] = $article->getValues(array_keys($myrow));
            }
            unset($article);
        }
        return $ret;
    }

    /**
     * get IDs of articles matching a condition of a category (categories)
     * 
     * @param mixed       $category   Category ID or object
     * @param int       $limit      Max number of objects to fetch
     * @param int       $start      Which record to start at
     * @param object    $criteria     {@link CriteriaElement} to match
     * @return array of article IDs
     */
    function &getIdsByCategory($category, $limit = 0, $start = 0, $criteria = null)
    {
        $sql = "SELECT DISTINCT a.art_id FROM " . art_DB_prefix("article") . " AS a LEFT JOIN " . art_DB_prefix("artcat") . " AS ac ON ac.art_id=a.art_id";
        $sql .= " WHERE 1=1";
        if (is_array($category) && count($category) > 0) {
            $category = array_filter(array_map("intval", $category));
            if (count($category) > 0) {
                $sql .= " AND ac.cat_id IN (" . implode(",", $category) . ")";
            }
        } else {
            $cat_id = (is_object($category))? $category->getVar("cat_id") : intval($category);
            if ($cat_id > 0) $sql .= " AND ac.cat_id = " . intval($cat_id);
        }
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $sql .= " AND " . $criteria->render();
            if ($criteria->getSort() != "") {
                $sql .= " ORDER BY " . $criteria->getSort() . " " . $criteria->getOrder();
                $orderSet = true;
            }
        }
        if (empty($orderSet)) $sql .= " ORDER BY ac.ac_publish DESC";
        $result = $this->db->query($sql, $limit, $start);
        $ret = array();
        while ($myrow = $this->db->fetchArray($result)) {
            $ret[] = $myrow["art_id"];
        }
        $ret = array_unique($ret);
        return $ret;
    }

    /**
     * Update category ids of the article
     * 
     * {@link CriteriaCompo} 
     * {@link Criteria} 
     *
     * @param object    $article     {@link Article}
     * @return bool
     */
    function updateCategories(&$article)
    {
        $criteria = new CriteriaCompo(new Criteria("ac_publish", 0, ">"));
        $ids_new = $this->getCategoryIds($article, $criteria);
        $ids_curr = $article->getVar("art_categories");
        if (strcmp(serialize($ids_new), serialize($ids_curr))) {
            $article->setVar("art_categories", $ids_new, true);
            $this->insert($article);
        }
        return true;
    }

    /**
     * Update topic ids of the article
     * 
     * @param object    $article     {@link Article}
     * @return bool
     */
    function updateTopics(&$article)
    {
        $ids_new = $this->getTopicIds($article);
        $ids_curr =$article->getVar("art_topics");
        if (strcmp(serialize($ids_new), serialize($ids_curr))) {
            $article->setVar("art_topics", $ids_new, true);
            $this->insert($article);
        }
        return true;
    }

    /**
     * get IDs of categories matching a condition of the article
     * 
     * @param object    $article     {@link Article} reference to Article
     * @param object    $criteria     {@link CriteriaElement} to match
     * @return array     array of category IDs
     */
    function getCategoryIds($article, $criteria = null)
    {
        $ret = array();
        $art_id = is_object($article) ? $article->getVar("art_id") : intval($article);
        if ( $art_id ==0 ) return $ret;

        $sql = "SELECT cat_id FROM " . art_DB_prefix("artcat") . " WHERE art_id=" . $art_id;
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
            $sql .= " AND " . $criteria->render();
        }
        $result = $this->db->query($sql);
        while ($myrow = $this->db->fetchArray($result)) {
            $ret[] = $myrow["cat_id"];
        }
        return $ret;
    }

    /**
     * get status of categories of the article
     * 
     * @param object    $article     {@link Article} reference to Article
     * @return array     associative array of category IDs and status
     */
    function &getCategoryArray(&$article)
    {
        $_cachedCats = array();
        $art_id = is_object($article) ? $article->getVar("art_id") : intval($article);
        $sql = "SELECT cat_id, ac_publish AS approved FROM " . art_DB_prefix("artcat") . " WHERE art_id =" . $art_id;
        if (!$result = $this->db->query($sql)) {
            return $_cachedCats;
        }
        while ($myrow = $this->db->fetchArray($result)) {
            $_cachedCats[$myrow["cat_id"]] = $myrow["approved"];
        }
        return $_cachedCats;
    }

    /**
     * get IDs of active topics of the article
     * 
     * @param object    $article     {@link Article} reference to Article
     * @return array     array of topic IDs
     */
    function &getTopicIds(&$article, $criteria = null)
    {
        $ret = array();
        $art_id = is_object($article) ? $article->getVar("art_id") : intval($article);
        $topic_handler =& xoops_getmodulehandler("topic", $GLOBALS["artdirname"]);
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
        } else {
            $criteria = new CriteriaCompo(new Criteria("t.top_expire", time(), ">"));
        }
        if ($topics_obj =& $topic_handler->getByArticle($art_id, $criteria)) {
            $ret = array_keys($topics_obj);
        }
        return $ret;
    }

    /**
     * insert a new article into the database
     * 
     * @param    object    $article     {@link Article} reference to Article
     * @param     bool     $force         flag to force the query execution despite security settings
     * @return     int     article ID
     */
    function insert(&$article, $force = true)
    {
        if (!$art_id = parent::insert($article, $force)) {
            return false;
        }
        
        // placeholder for user stats - to be implemented
       if (!empty($article->vars["art_time_publish"]["changed"]) && $article->getVar("uid")) {
               /*
            $member_handler =& xoops_gethandler("member");
            $user =& $member_handler->getUser($article->getVar("uid"));
            if (is_object($user) && $user->isActive()) {
                   $posts = $article->getVar("art_time_publish") ? $user->getVar("posts") + 1 : $user->getVar("posts") - 1;
                $member_handler->updateUserByField($user, "posts", $posts);
            }
            */
        }
        
        if (!empty($article->vars["art_keywords"]["changed"])) {
            $this->updateKeywords($article);
        }
        
        return $art_id;
    }

    /**
     * Update keyword-article links of the article
     * 
     * @param    object    $article     {@link Article} reference to Article
     * @return     bool     true on success
     */
    function updateKeywords(&$article)
    {
        if (!@include_once XOOPS_ROOT_PATH . "/modules/tag/include/functions.php") {
            return false;
        }
        if (!$tag_handler =& tag_getTagHandler()) {
            return false;
        }
        $tag_handler->updateByItem($article->getVar("art_keywords", "n"), $article->getVar("art_id"), $GLOBALS["artdirname"]);
        return true;
    }

    /**
     * Delete keyword-article links of the article from database
     * 
     * @param    object    $article     {@link Article} reference to Article
     * @return     bool     true on success
     */
    function deleteKeywords(&$article)
    {
        if (!@include_once XOOPS_ROOT_PATH . "/modules/tag/include/functions.php") {
            return false;
        }
        if (!$tag_handler =& tag_getTagHandler()) {
            return false;
        }
        $tag_handler->updateByItem(array(), $article->getVar("art_id"), $GLOBALS["artdirname"]);
        return true;
    }

    /**
     * delete an article from the database
     * 
     * {@link Text}
     *
     * @param    object    $article     {@link Article} reference to Article
     * @param     bool     $force         flag to force the query execution despite security settings
     * @return     bool     true on success
     */
    function delete(&$article, $force = true)
    {
        if (empty($force) && xoops_comment_count($GLOBALS["xoopsModule"]->getVar("mid"), $article->getVar("art_id"))) {
            return false;
        }
        
        // placeholder for files
        /*
        $file_handler =&  xoops_getmodulehandler("file", $GLOBALS["artdirname"]);
        $file_handler->deleteAll(new Criteria("art_id", $article->getVar("art_id")));
        */

        $text_handler =& xoops_getmodulehandler("text", $GLOBALS["artdirname"]);
        $text_handler->deleteAll(new Criteria("art_id", $article->getVar("art_id")));

        $rate_handler =& xoops_getmodulehandler("rate", $GLOBALS["artdirname"]);
        $rate_handler->deleteAll(new Criteria("art_id", $article->getVar("art_id")));

        $this->terminateCategory($article, $article->getCategories(), false);
        $this->terminateTopic($article);

        $this->deleteKeywords($article);
        
        xoops_comment_delete($GLOBALS["xoopsModule"]->getVar("mid"), $article->getVar("art_id"));
        xoops_notification_deletebyitem($GLOBALS["xoopsModule"]->getVar("mid"), "article", $article->getVar("art_id"));

        parent::delete($article, $force);

        $article = null;
        unset($article);
        return true;
    }

    /**
     * Set article-category status of the article
     * 
     * {@link Xcategory}
     *
     * @param    mixed        $art_id     article ID
     * @param    mixed        $cat_id     category ID
     * @param    int            $status     status value
     * @param    int            $time         register, publish or feature time; 0 for time()
     * @return     bool     true on success
     */
    function setCategoryStatus(&$article, $cat_id, $status = 1, $time = 0)
    {
        if (!is_object($article)) {
            $art_id = intval($article);
            $article_obj =& $this->get($article);
        } else {
            $article_obj =& $article;
            $art_id = $article->getVar("art_id");
        }
        if (empty($art_id)) return false;

        $where = " WHERE ac.art_id = " . $art_id;
        $table = art_DB_prefix("artcat") . " AS ac";
        switch ($status) {
            case 2:
                $time = empty($time) ? time() : $time;
                // Multi-table update only supported by MySQL 4.0.4+
                // Version check and update by scottlai
                if (version_compare($this->mysql_server_version(), "4.0.4", "<")) {
                    if ($article_obj->getVar("art_time_publish") > 0) {
                        $value = "ac_feature = {$time}, ac_publish = if ( ac_publish = 0, {$time}, ac_publish )";
                    } else {
                        $value = "ac_feature = 0";
                    }
                } else {
                    $table .= ", ".art_DB_prefix("article")." AS a";
                    $value = "ac.ac_feature = {$time}, ac.ac_publish = if ( ac.ac_publish = 0, {$time}, ac.ac_publish )";
                    $where .= " AND a.art_id = ac.art_id AND a.art_time_publish > 0";
                }
                break;
            case 0:
                $value = "ac.ac_feature = 0, ac.ac_publish = 0";
                break;
            default:
            case 1:
                $time_feature = empty($time) ? 0 : $time;
                $time_publish = empty($time) ? time() : $time;
                // Multi-table update only supported by MySQL 4.0.4+
                // Version check and update by scottlai
                if (version_compare($this->mysql_server_version(), "4.0.4", "<")) {
                    $art_time_publish = $article_obj->getVar("art_time_publish");
                    $a_cat_id = $article_obj->getVar("cat_id");
                    $value .= " ac.ac_publish = if ( ac.ac_publish = 0, if ( {$a_cat_id} = ac.cat_id, {$time_publish}, if ( {$art_time_publish} > 0, {$time_publish}, 0 ) ), {$time_publish}),";
                    $value .= " ac.ac_feature = if ( ac.ac_feature = 0, 0, {$time_feature} )";
                    
                    /* Update art_time_publish manually instead of using multi-table update */
                    if (!$art_time_publish) {
                        $article_obj->setVar("art_time_publish", $time_publish);
                        $this->insert($article_obj, false);
                    }
                } else {
                    $table .= ", " . art_DB_prefix("article") . " AS a";
                    $value  = "a.art_time_publish = if ( a.art_time_publish = 0, if ( a.cat_id = ac.cat_id, {$time_publish}, 0 ), a.art_time_publish ), ";
                    $value .= " ac.ac_publish = if ( ac.ac_publish = 0, if ( a.cat_id = ac.cat_id, {$time_publish}, if ( a.art_time_publish > 0, {$time_publish}, 0 ) ), {$time_publish} ),";
                    $value .= " ac.ac_feature = if ( ac.ac_feature = 0, 0, {$time_feature} )";
                    $where .= " AND a.art_id = ac.art_id";
                }
                break;
        }
        if (!empty($cat_id)) {
            $cat_id = is_array($cat_id) ? $cat_id : array($cat_id);
            $where .= " AND ac.cat_id IN (" . implode(",", array_map("intval", $cat_id)) . ")";
        }

        $sql = "UPDATE " . $table . " SET " . $value . $where;
        
        /* filter all table short-name when mysql version below 4.0.4 */
        if (version_compare($this->mysql_server_version(), "4.0.4", "<")) {
            $pattern = array("AS ac", "AS a", "ac.", "a.");
            $sql = str_replace($pattern, array(), $sql);
        }

        if (!$result = $this->db->queryF($sql)) {
            //xoops_error("update article-category error:" . $sql);
            return false;
        }
        
        $category_handler =& xoops_getmodulehandler("category", $GLOBALS["artdirname"]);
        $category_handler->setLastArticleIds($cat_id);

        return true;
    }

    /**
     * get article-category status of a list articles
     * 
     * @param    int            $cat_id     category ID
     * @param    mixed        $art_id     article ID
     * @param    int            $status     status value
     * @return     array         associative array of article IDs and status
     */
    function getCategoryStatus($cat_id, $art_id = null)
    {
        if (empty($cat_id)) return null;
        if (!is_array($art_id)) {
            $art_id = array($art_id);
            $isSingle = true;
        }
        if (count($art_id) == 0) return null;
        $art_id = array_map("intval", $art_id);

        $sql = "SELECT DISTINCT art_id, ac_register AS register, ac_publish AS publish, ac_feature AS feature FROM " . art_DB_prefix("artcat") . " WHERE art_id IN (" . implode(",", $art_id) . ")";
        if (!$result = $this->db->query($sql)) {
            //xoops_error("query article-category error: " . $sql);
            return null;
        }
        $ret = array();
        while ($myrow = $this->db->fetchArray($result)) {
            $status = ($myrow["feature"]) ? 2 : ( ($myrow["publish"]) ? 1 : (($myrow["register"]) ? 0 : null) );
            if (!empty($isSingle)) return $status;
            $ret[$myrow["art_id"]] = $status;
        }
        return $ret;
    }

    /**
     * Set article-category status as published (value = 1) 
     * 
     * @param    mixed        $art_id     article ID
     * @param    mixed        $cat_id     category ID
     * @return     bool     true on success
     */
    function publishCategory(&$article, $cats)
    {
        if (!$ret = $this->setCategoryStatus($article, $cats, 1)) return false;
        
        if (!empty($GLOBALS["xoopsModuleConfig"]["notification_enabled"])) {
            $category_handler =& xoops_getmodulehandler("category", $GLOBALS["artdirname"]);
            $notification_handler =& xoops_gethandler("notification");
            if (!is_object($article)) $article_obj =& $this->get($article);
            else $article_obj =& $article; 
            $cats = is_array($cats)?$cats:array($cats);
            $tags = array();
            $tags["ARTICLE_ACTION"] = art_constant("MD_NOT_ACTION_PUBLISHED");
            $tags["ARTICLE_TITLE"] = $article_obj->getVar("art_title");
            foreach ($cats as $id) {
                $tags["ARTICLE_URL"] = XOOPS_URL . "/modules/" . $GLOBALS["artdirname"] . "/view.article.php" . URL_DELIMITER . $article_obj->getVar("art_id") . "/c" . $id;
                $category_obj =& $category_handler->get($id); 
                $tags["CATEGORY_TITLE"] = $category_obj->getVar("cat_title");
                $notification_handler->triggerEvent("global", 0, "article_new", $tags);
                $notification_handler->triggerEvent("global", 0, "article_monitor", $tags);
                $notification_handler->triggerEvent("category", $id, "article_new", $tags);
                $notification_handler->triggerEvent("article", $article_obj->getVar("art_id"), "article_approve", $tags);
                unset($category_obj);
            }
        }
        
        return true;
        
    }

    /**
     * Set article-category status as dismissed(pending) (value = 0) 
     * 
     * @param    mixed        $art_id     article ID
     * @param    mixed        $cat_id     category ID
     * @return     bool     true on success
     */
    function unPublishCategory(&$article, $cats)
    {
        return $this->setCategoryStatus($article, $cats, 0);
    }

    /**
     * Set article-category status as featured (value = 2) 
     * 
     * @param    mixed        $art_id     article ID
     * @param    mixed        $cat_id     category ID
     * @return     bool     true on success
     */
    function featureCategory(&$article, $cats)
    {
        return $this->setCategoryStatus($article, $cats, 2);
    }

    /**
     * Set article-category status as normal (value = 1) 
     * 
     * @param    mixed        $art_id     article ID
     * @param    mixed        $cat_id     category ID
     * @return     bool     true on success
     */
    function unFeatureCategory(&$article, $cats)
    {
        return $this->setCategoryStatus($article, $cats, 1);
    }

    /**
     * register an article to categories
     *
     * TODO: refactoring with publishCategory
     * 
     * {@link Xcategory}
     *
     * @param    object    $article     {@link Article} reference to Article
     * @param     array     $cats         array of category IDs
     * @return     bool     true on success
     */
    function registerCategory(&$article, $cats)
    {
        $art_id = is_object($article) ? $article->getVar("art_id") : intval($art_id);
        if ( empty($art_id) || empty($cats) ) return false;
        $cats_pub = array();
        $cat_str=array();
        foreach ($cats as $id => $cat) {
            $status = @intval($cat["status"]);
            if ($status > 2) $status = 2;
            if ($status < 0) $status = 0;
            if ($status > 0) $cats_pub[] = $id;
            switch($status) {
                // Publish and feature
                case 2:
                    $value = time() . "," . time() . "," . time();
                    break;
                // Register
                case 0:
                    $value = time() . ", 0, 0";
                    break;
                // Publish
                default:
                case 1:
                    $value = time() . "," . time() . ", 0";
                    break;
            }

            $cat_str[] = "(" . $art_id.", " . intval($id) . ", " . $value . ", " . intval($cat["uid"]) . ")";
        }
        $values = implode(",", $cat_str);

        $sql = "INSERT INTO " . art_DB_prefix("artcat") . " (art_id, cat_id, ac_register, ac_publish, ac_feature, uid) VALUES " . $values;
        if (!$result = $this->db->queryF($sql)) {
            //xoops_error("Insert article-category error:" . $sql);
            return false;
        }
        
        $category_handler =& xoops_getmodulehandler("category", $GLOBALS["artdirname"]);
        if (!empty($GLOBALS["xoopsModuleConfig"]["notification_enabled"])) {
            $notification_handler =& xoops_gethandler("notification");
            if (!is_object($article)) $article_obj =& $this->get($article);
            else $article_obj =& $article; 
            $tags = array();
            $tags["ARTICLE_TITLE"] = $article_obj->getVar("art_title");
        }
        if (count($cats_pub) > 0) {
            $category_handler->setLastArticleIds($cats_pub);
            if (!empty($GLOBALS["xoopsModuleConfig"]["notification_enabled"])) {
                $cats = $cats_pub;
                foreach ($cats as $id) {
                    $tags["ARTICLE_URL"] = XOOPS_URL . "/modules/" . $GLOBALS["artdirname"] . "/view.article.php" . URL_DELIMITER . $article_obj->getVar("art_id") . "/c" . $id;
                    $category_obj =& $category_handler->get($id); 
                    $tags["CATEGORY_TITLE"] = $category_obj->getVar("cat_title");
                    $notification_handler->triggerEvent("global", 0, "article_new", $tags);
                    $notification_handler->triggerEvent("global", 0, "article_monitor", $tags);
                    $notification_handler->triggerEvent("category", $id, "article_new", $tags);
                    $notification_handler->triggerEvent("article", $article_obj->getVar("art_id"), "article_approve", $tags);
                    unset($category_obj);
                }
            }
        }
        $cats_reg = array_diff(array_keys($cats), $cats_pub);
        if (count($cats_reg) > 0) {
            if (!empty($GLOBALS["xoopsModuleConfig"]["notification_enabled"])) {
                $cats = $cats_reg;
                foreach ($cats as $id) {
                    $tags["ARTICLE_URL"] = XOOPS_URL . "/modules/" . $GLOBALS["artdirname"] . "/edit.article.php?article=" . $article_obj->getVar("art_id") . "&category=" . $id;
                    $category_obj =& $category_handler->get($id); 
                    $tags["CATEGORY_TITLE"] = $category_obj->getVar("cat_title");
                    $notification_handler->triggerEvent("global", 0, "article_submit", $tags);
                    $notification_handler->triggerEvent("category", $id, "article_submit", $tags);
                    unset($category_obj);
                }
            }
        }
        
        return true;
    }

    /**
     * terminate an article from categories
     * 
     * {@link Xcategory}
     *
     * @param    mixed    $article     array or {@link Article} reference to Article
     * @param     mixed     $cat_id     array of category IDs
     * @return     bool     true on success
     */
    function terminateCategory(&$article, $cat_id = null, $check_basic = true)
    {
        if ( empty($cat_id) ) return false;
        if (!is_array($cat_id)) $cat_id = array($cat_id);
        if (is_object($article)) {
            $art_obj =& $article;
            $art_id = $article->getVar("art_id");
        } else {
            $art_id = intval($article);
            $art_obj =& $this->get($art_id);
            if (!is_object($art_obj) || !$art_id = $art_obj->getVar("art_id")) {
                return false;
            }
        }
        if (empty($art_id)) {
            //xoops_error("empty art_id");
            return false;
        }
        $cat_id = array_map("intval", $cat_id);
        
        $remove_all = false;
        if ($check_basic):
        // The basic category is to remove
        if (in_array($art_obj->getVar("cat_id"), $cat_id)) {
            $remove_all = true;
        } else {
            $cats = $art_obj->getCategories();
        // Or all categories are to remove
            if (array_intersect($cat_id, $cats) == $cats) {
                $remove_all = true;
            }            
        }
        endif;

        $where = " WHERE art_id = " . $art_id;
        if (empty($remove_all)) {
            $where .= " AND cat_id IN (" . implode(",", $cat_id) . ")";
        }

        // remove article-category links
        $sql = "DELETE FROM " . art_DB_prefix("artcat") . $where;
        if (!$result = $this->db->queryF($sql)) {
            //xoops_error("terminate article-category error:" . $sql);
            return false;
        }
        // update last-articles for relevant categories
        $category_handler =& xoops_getmodulehandler("category", $GLOBALS["artdirname"]);
        $category_handler->setLastArticleIds($cat_id);
        
        if (!empty($remove_all)) {
            $this->delete($art_obj, true);
        }
        
        return true;
    }

    /**
     * move an article=
     * 
     * {@link Xcategory}
     *
     * @param    object    $article     {@link Article} reference to Article
     * @param     int     $cat_to     destination category
     * @param     int     $cat_from     source category
     * @return     bool     true on success
     */
    function moveCategory(&$article, $cat_to, $cat_from)
    {
        if (in_array($cat_to, $article->getCategories())) {
            return true;
        }

        $sql = "UPDATE " . art_DB_prefix("artcat") . " SET cat_id = " . intval($cat_to) . " WHERE art_id= " . $article->getVar("art_id") . " AND cat_id=" . intval($cat_from);
        if (!$result = $this->db->queryF($sql)) {
            //xoops_error("moveCategory error:" . $sql);
            return false;
        }
        return true;
    }

    /**
     * register an article to topics
     * 
     * @param    object    $article     {@link Article} reference to Article
     * @param     mixed     $top_id     array of topic IDs
     * @return     bool     true on success
     */
    function registerTopic(&$article, $top_id)
    {
        if (is_array($top_id)) {
            if (count($top_id) > 0) {
                $top_str=array();
                $top_id = array_map("intval", $top_id);
                foreach ($top_id as $top) {
                    $top_str[] = "(" . $article->getVar("art_id") . ", " . $article->getVar("uid") . ", {$top}," . time() . ")";
                }
                $values = implode(",", $top_str);
                $top_id_value = " top_id IN (" . implode(",", $top_id) . ")";
            } else {
                return false;
            }
        } else {
            $values = "(" . $article->getVar("art_id") . ", " . $article->getVar("uid") . ", " . intval($top_id) . "," . time() . ")";
            $top_id_value = " top_id =" . intval($top_id);
        }

        $sql = "INSERT INTO " . art_DB_prefix("arttop") . " (art_id, uid, top_id, at_time) VALUES " . $values;
        if (!$result = $this->db->queryF($sql)) {
            //xoops_error("Insert article-topic error:" . $sql);
            return false;
        }

        $sql = "UPDATE " . art_DB_prefix("topic") . " SET top_time=" . time() . " WHERE " . $top_id_value;
        if (!$result = $this->db->queryF($sql)) {
            //xoops_error("Update topic time error:" . $sql);
            return false;
        }
        return true;
    }

    /**
     * terminate an article from topics
     * 
     * @param    mixed    $article     array or {@link Article} reference to Article
     * @param     mixed     $top_id     array of topic IDs
     * @return     bool     true on success
     */
    function terminateTopic(&$article, $top_id=null)
    {
        if (is_object($article)) {
            $art_id = $article->getVar("art_id");
        } else {
            $art_id =intval($article);
        }
        if (empty($art_id)) return false;
        $where = " WHERE art_id =" . $art_id;

        if (!is_array($top_id) && !empty($top_id)) {
            $top_id = array($top_id);
        }
        if (count($top_id) > 0) {
            $top_id = array_map("intval", $top_id);
            $where .= " AND top_id IN (" . implode(",", $top_id) . ")";
        }

        $sql = "DELETE FROM " . art_DB_prefix("arttop") . $where;
        if (!$result = $this->db->queryF($sql)) {
            //xoops_error("terminate article-topic error:" . $sql);
            return false;
        }
        return true;
    }

    /**
     * retrieve global article stats of the module
     * 
     * @return     array    array of authors, article view count, article rates
     */
    function &getStats()
    {
        $sql = "SELECT COUNT(DISTINCT uid) AS authors, SUM(art_counter) AS views, SUM(art_rates) AS rates FROM " . art_DB_prefix("article");
        $result = $this->db->query($sql);
        $myrow = $this->db->fetchArray($result);
        return $myrow;
    }

    /**
     * insert a trackback item of the article into database
     * 
     * @param    object    $article     {@link Article} reference to Article
     * @param     array     $trackback     associative array of time, url
     * @return     bool     true on success
     */
    function addTracked(&$article, $trackback)
    {
        $sql = "INSERT INTO " . art_DB_prefix("tracked") . " (art_id, td_time, td_url) VALUES (" . $article->getVar("art_id") . ", " . $trackback["time"] . ", " . $this->db->quoteString($trackback["url"]) . ")";
        if (!$result = $this->db->queryF($sql)) {
            //xoops_error("Add tracked error:" . $sql);
            return false;
        }
        return true;
    }

    /**
     * update tracked trackbacks of the article
     * 
     * @param    object    $article     {@link Article} reference to Article
     * @param     mixed     $td_id         trackback id
     * @return     bool     true on success
     */
    function updateTracked(&$article, $td_id)
    {
        if (is_array($tb_id) && count($tb_id) > 0) $id = "td_id IN (" . implode(",", $td_id) . ")";
        else $id = "td_id = " . $td_id;
        $sql = "UPDATE " . art_DB_prefix("tracked") . " SET td_time=" . time() . " WHERE art_id = " . $article->getVar("art_id") . " AND " . $id;
        if (!$result = $this->db->queryF($sql)) {
            //xoops_error("update tracked error:" . $sql);
            return false;
        }
        return true;
    }

    /**
     * retrieve trackbacks of the article from database
     * 
     * @param    object    $article     {@link Article} reference to Article
     * @param     string     $type         type of trackbacks
     * @return     array     associative array of trackback ID and values
     */
    function getTracked(&$article, $type = "all")
    {
        $sql = "SELECT * FROM " . art_DB_prefix("tracked") . " WHERE art_id = " . $article->getVar("art_id");
        if ($type=="tracked") $sql .= "AND td_time > 0";
        if ($type=="untracked") $sql .= "AND td_time = 0";
        $result = $this->db->query($sql);
        $res = array();
        while ($myrow = $this->db->fetchArray($result)) {
            $res[$myrow["td_id"]] = $myrow;
        }
        return $res;
    }

    /**
     * clean expired articles from database
     * 
     * @param     int     $expire     time limit for expiration
     * @return     bool    true on success
     */
    function cleanExpires($expire)
    {
        $sql = "DELETE FROM " . $this->table .
                " WHERE art_time_create < ". ( time() - intval($expire) ) .
                " AND art_time_submit =0";
        if (!$result = $this->db->queryF($sql)) {
            //xoops_error("cleanExpires error:". $sql);
            return false;
        }
        return true;
    }

    /**
     * clean orphan articles from database
     * 
     * @return     bool    true on success
     */
    function cleanOrphan()
    {
        /* for MySQL 4.1+ */
        if ( version_compare( mysql_get_server_info(), "4.1.0", "ge" ) ):
        $sql = "DELETE FROM " . $this->table .
                " WHERE " .
                " (" . $this->table . ".cat_id > 0 AND cat_id NOT IN ( SELECT DISTINCT cat_id FROM " . art_DB_prefix("category") . ") )";
        else:
        $sql =     "DELETE " . $this->table . " FROM " . $this->table.
                " LEFT JOIN " . art_DB_prefix("category") . " AS aa ON " . $this->table . ".cat_id = aa.cat_id " .
                " WHERE " .
                " (" . $this->table . ".cat_id>0 AND aa.cat_id IS NULL)";
        endif;
        
        if (!$result = $this->db->queryF($sql)) {
            //xoops_error("cleanOrphan error: ". $sql);
        }
        
        /* for MySQL 4.1+ */
        if (version_compare( mysql_get_server_info(), "4.1.0", "ge" )):
        $sql = "DELETE FROM " . art_DB_prefix("artcat") .
                " WHERE " .
                " (" . art_DB_prefix("artcat") . ".art_id = 0 OR art_id NOT IN ( SELECT DISTINCT art_id FROM " . $this->table . ") )";
        else:
        $sql =  "DELETE " . art_DB_prefix("artcat") . " FROM " . art_DB_prefix("artcat") .
                " LEFT JOIN " . $this->table . " AS aa ON " . art_DB_prefix("artcat") . ".art_id = aa.art_id " .
                " WHERE " .
                " (" . art_DB_prefix("artcat") . ".art_id = 0 OR aa.art_id IS NULL)";
        endif;
        
        if (!$result = $this->db->queryF($sql)) {
            //xoops_error("cleanOrphan error: ". $sql);
        }
        
        /* for MySQL 4.1+ */
        if (version_compare( mysql_get_server_info(), "4.1.0", "ge" )):
        $sql = "DELETE FROM " . art_DB_prefix("arttop") .
                " WHERE " .
                " (" . art_DB_prefix("arttop") . ".art_id = 0 OR art_id NOT IN ( SELECT DISTINCT art_id FROM " . $this->table . ") )";
        else:
        $sql =  "DELETE " . art_DB_prefix("arttop") . " FROM " . art_DB_prefix("arttop") .
                " LEFT JOIN " . $this->table . " AS aa ON " . art_DB_prefix("arttop") . ".art_id = aa.art_id " .
                " WHERE " .
                " (" . art_DB_prefix("arttop") . ".art_id = 0 OR aa.art_id IS NULL)";
        endif;
        
        if (!$result = $this->db->queryF($sql)) {
            //xoops_error("cleanOrphan error:". $sql);
        }
        
        /* for MySQL 4.1+ */
        if (version_compare( mysql_get_server_info(), "4.1.0", "ge" )):
        $sql = "DELETE FROM " . art_DB_prefix("tracked") .
                " WHERE " .
                " (" . art_DB_prefix("tracked") . ".art_id = 0 OR art_id NOT IN ( SELECT DISTINCT art_id FROM " . $this->table . ") )";
        else:
        $sql =  "DELETE " . art_DB_prefix("tracked") . " FROM " . art_DB_prefix("tracked") .
                " LEFT JOIN " . $this->table . " AS aa ON " . art_DB_prefix("tracked") . ".art_id = aa.art_id " .
                " WHERE " .
                " (" . art_DB_prefix("tracked") . ".art_id = 0 OR aa.art_id IS NULL)";
        endif;
        
        if (!$result = $this->db->queryF($sql)) {
            //xoops_error("cleanOrphan error:". $sql);
        }
        
        return true;
    }
}
');
コード例 #13
0
ファイル: spotlight.php プロジェクト: trabisdementia/xuups
art_parse_class('
class [CLASS_PREFIX]SpotlightHandler extends XoopsPersistableObjectHandler
{    
    function [CLASS_PREFIX]SpotlightHandler(&$db)
    {
        $this->XoopsPersistableObjectHandler($db, art_DB_prefix("spotlight", true), "Spotlight", "sp_id");
    }
    
    function &get()
    {
        $Spotlight =& $this->create();
        $sql = "SELECT * FROM " . art_DB_prefix("spotlight") . " ORDER BY sp_id DESC LIMIT 1";
        if (!$result = $this->db->query($sql)) {
            return $Spotlight;
        }
        $array = $this->db->fetchArray($result);
        if (empty($array)) {
            return $Spotlight;
        }
        $Spotlight->assignVars($array);
        $Spotlight->unsetNew();

        return $Spotlight;
    }
    
    /**
     * Get spotlight article
     * 
      * {@link XoopsPersistableObjectHandler} 
      *
     * @param    bool    $asArticleId    retrun article ID
     * @param    bool    $specifiedOnly    only return article market as spotlight by editors; in this case, null is returned if "recent article" is selected in spotlight admin
     * @return    array    spotlight content
     */
    function &getContent($asArticleId = true, $specifiedOnly = false)
    {
        $content = array();
        $spotlight =& $this->get();
        if (!is_object($spotlight) || !$spotlight->getVar("art_id")) {
            $content["sp_note"] = "";
            $content["image"] = null;
            $art_id = 0;
            $categories = null;
        } else {
            $content["sp_note"] = $spotlight->getVar("sp_note");
            $content["image"] = $spotlight->getImage();
            $art_id = $spotlight->getVar("art_id");
            $categories = $spotlight->getVar("sp_categories");
        }
        if (empty($art_id) && !empty($specifiedOnly)) {
            return $content;
        }
        
        $article_handler =& xoops_getmodulehandler("article", $GLOBALS["artdirname"]);
        if (empty($art_id)) {
            $criteria = new CriteriaCompo(new Criteria("ac.ac_publish", 0, ">"));
            $arts =& $article_handler->getIdsByCategory($categories, 1, 0, $criteria);
            $art_id = empty($arts[0])?0:$arts[0];
        }
        
        $content["art_id"] = $art_id;
        if ($asArticleId) {
        } elseif ($art_id>0) {
            $article_obj =& $article_handler->get($art_id);
            if (!is_object($article_obj)) {
                unset($content["art_id"]);
                return $content;
            }
            $content["image"]    = empty($content["image"]) ? $article_obj->getImage() : $content["image"];
            $content["title"]    = $article_obj->getVar("art_title");
            $content["uid"]        = $article_obj->getVar("uid") ;
            $content["writer_id"] = $article_obj->getVar("writer_id") ;
            $content["time"]    = $article_obj->getTime();
            $content["views"]    = $article_obj->getVar("art_counter") ;
            $content["comments"]= $article_obj->getVar("art_comments") + $article_obj->getVar("art_trackbacks");
            $content["summary"]    = $article_obj->getSummary(true);
        } else {
            $content["summary"] = "";
        }
        return $content;
    }

    /**
     * clean orphan items from database
     * 
     * @return     bool    true on success
     */
    function cleanOrphan()
    {
        return true; // skip this step since it will remove all spotlight with "art_id = 0";
        
        //return parent::cleanOrphan(art_DB_prefix("article"), "art_id");
    }
}
');
コード例 #14
0
ファイル: xml.php プロジェクト: trabisdementia/xuups
art_parse_class('
class [CLASS_PREFIX]XmlHandler
{
    function &create($format = "RSS0.91")
    {
        $xmlfeed = new Xmlfeed($format);
        return $xmlfeed;
    }

    function display(&$feed, $filename = "", $display = false)
    {
        if (!is_object($feed)) return null;
        if ($display) {
            $filename = empty($filename) ? $feed->filename : $filename;
            $feed->saveFeed($feed->version, $filename);
        } elseif (empty($filename)) {
            $content = $feed->createFeed($feed->version);
            return $content;
        }
    }

    function insert(&$feed)
    {
        $xml_data = array();
        $xml_data["version"] = $feed->version;
        $xml_data["encoding"] = $feed->encoding;
        $xml_data["image"] = $feed->image;
        $xml_data["items"] = $feed->items;

        return $xml_data;
    }

    function &get(&$feed)
    {
        $xml_data = array();
        $xml_data["version"] = $feed->version;
        $xml_data["encoding"] = $feed->encoding;
        $xml_data["image"] = $feed->image;
        $xml_data["items"] = $feed->items;

        return $xml_data;
    }
}
');