topic.php

来自「php 开发的内容管理系统」· PHP 代码 · 共 516 行 · 第 1/2 页

PHP
516
字号
        if (!$result) {
            newbb_message("NewbbTopicHandler::getAllPosts error:" . $sql);
            return $ret;
        }
        $post_handler = &xoops_getmodulehandler('post', 'newbb');
        while ($myrow = $this->db->fetchArray($result)) {
            $post = &$post_handler->create(false);
            $post->assignVars($myrow);
            $ret[$myrow['post_id']] = $post;
            unset($post);
        }
        return $ret;
    }

    function &getPostTree(&$postArray, $pid=0)
    {
		include_once XOOPS_ROOT_PATH . "/modules/newbb/class/newbbtree.php";
        $NewBBTree = new NewBBTree('bb_posts');
        $NewBBTree->setPrefix('  ');
        $NewBBTree->setPostArray($postArray);
        $NewBBTree->getPostTree($postsArray, $pid);
        return $postsArray;
    }

    function showTreeItem(&$topic, &$postArray)
    {
        global $xoopsConfig, $xoopsModuleConfig, $viewtopic_users, $myts;

        $postArray['post_time'] = newbb_formatTimestamp($postArray['post_time']);

        if (!empty($postArray['icon'])){
            $postArray['icon'] = '<img src="' . XOOPS_URL . "/images/subject/" . htmlspecialchars($postArray['icon']) . '" alt="" />';
        }else{
            $postArray['icon'] = '<a name="' . $postArray['post_id'] . '"><img src="' . XOOPS_URL . '/images/icons/no_posticon.gif" alt="" /></a>';
        }

        if (isset($viewtopic_users[$postArray['uid']]['is_forumadmin'])){
            $postArray['subject'] = $myts->undoHtmlSpecialChars($postArray['subject']);
        }
        $postArray['subject'] = '<a href="viewtopic.php?viewmode=thread&amp;topic_id=' . $topic->getVar('topic_id') . '&amp;forum=' . $postArray['forum_id'] . '&amp;post_id=' . $postArray['post_id'] . '">' . $postArray['subject'] . '</a>';

        $isActiveUser = false;
        if (isset($viewtopic_users[$postArray['uid']]['name'])) {
	        $postArray['poster'] = $viewtopic_users[$postArray['uid']]['name'];
	        if($postArray['uid']>0)
	        $postArray['poster'] = "<a href=\"".XOOPS_URL . "/userinfo.php?uid=" . $postArray['uid'] ."\">".$viewtopic_users[$postArray['uid']]['name']."</a>";
        }else{
            $postArray['poster'] = (empty($postArray['poster_name']))?$myts->HtmlSpecialChars($xoopsConfig['anonymous']):$postArray['poster_name'];
        }

        return $postArray;
    }

    function &getAllPosters(&$topic, $isApproved = true)
    {
        $sql = 'SELECT DISTINCT uid FROM ' . $this->db->prefix('bb_posts') . "  WHERE topic_id=" . $topic->getVar('topic_id')." AND uid>0";
        if($isApproved) $sql .= ' AND approved = 1';
        $result = $this->db->query($sql);
        if (!$result) {
            newbb_message("NewbbTopicHandler::getAllPosters error:" . $sql);
            return array();
        }
        $ret = array();
        while ($myrow = $this->db->fetchArray($result)) {
            $ret[] = $myrow['uid'];
        }
        return $ret;
    }

    function delete(&$topic, $force = true){
	    $topic_id = is_object($topic)?$topic->getVar("topic_id"):intval($topic);
	    if(empty($topic_id)){
		    return false;
	    }
        $post_obj =& $this->getTopPost($topic_id);
		$post_handler =& xoops_getmodulehandler('post', 'newbb');
	    $post_handler->delete($post_obj, false, $force);
	    return true;
    }
    
    // get permission
    // parameter: $type: 'post', 'view',  'reply', 'edit', 'delete', 'addpoll', 'vote', 'attach'
    // $gperm_names = "'forum_can_post', 'forum_can_view', 'forum_can_reply', 'forum_can_edit', 'forum_can_delete', 'forum_can_addpoll', 'forum_can_vote', 'forum_can_attach', 'forum_can_noapprove'";
    function getPermission($forum, $topic_locked = 0, $type = "view")
    {
        global $xoopsUser, $xoopsModule;
        static $_cachedTopicPerms;

        if(newbb_isAdmin($forum)) return 1;

        $forum = is_object($forum)?$forum->getVar('forum_id'):intval($forum);
	    if($forum<1) return false;

        if (!isset($_cachedTopicPerms)){
            $getpermission = &xoops_getmodulehandler('permission', 'newbb');
            $_cachedTopicPerms = $getpermission->getPermissions("forum", $forum);
        }

        $type = strtolower($type);
        $perm_item = 'forum_' . $type;
        $permission = (isset($_cachedTopicPerms[$forum][$perm_item])) ? 1 : 0;

        if ($topic_locked && 'view' != $type) $permission = 0;

        return $permission;
    }
    
    /**
     * clean orphan items from database
     * 
     * @return 	bool	true on success
     */
    function cleanOrphan()
    {
	    parent::cleanOrphan($this->db->prefix("bb_forums"), "forum_id");
	    parent::cleanOrphan($this->db->prefix("bb_posts"), "topic_id");
	    
	    return true;
    }

    /**
     * clean expired objects from database
     * 
     * @param 	int 	$expire 	time limit for expiration
     * @return 	bool	true on success
     */
    function cleanExpires($expire = 0)
    {
	    $crit_expire =& new CriteriaCompo(new Criteria("approved", 0, "<="));
	    //if(!empty($expire)){
	    	$crit_expire->add(new Criteria("topic_time", time()-intval($expire), "<"));
    	//}
	    return $this->deleteAll($crit_expire, true/*, true*/);
    }
    
    function synchronization($object = null, $force = true)
    {
	    global $xoopsDB;
	    
	    if(empty($object)) {
	    	/* for MySQL 4.1+ */
	    	if($this->mysql_major_version() >= 4):
		    // Set topic_last_post_id
	        $sql = "UPDATE ".$this->table.
	        		" SET ".$this->table.".topic_last_post_id = @last_post =(".
	        		"	SELECT MAX(post_id) AS last_post ".
	        		" 	FROM " . $this->db->prefix("bb_posts") . 
	        		" 	WHERE approved=1 AND topic_id = ".$this->table.".topic_id".
	        		" )".
	        		" WHERE ".$this->table.".topic_last_post_id <> @last_post";
			$this->db->queryF($sql);
		    // Set topic_replies
	        $sql = "UPDATE ".$this->table.
	        		" SET ".$this->table.".topic_replies = @replies =(".
	        		"	SELECT count(*) AS total ".
	        		" 	FROM " . $this->db->prefix("bb_posts") . 
	        		" 	WHERE approved=1 AND topic_id = ".$this->table.".topic_id".
	        		" )".
	        		" WHERE ".$this->table.".topic_replies <> @replies";
			$this->db->queryF($sql);
	        else:
	        // for 4.0+
		    $topics = $this->getIds();
		    foreach($topics as $id){
			    if(!$obj =& $this->get($id)) continue;
			    $this->synchronization($obj);
			    unset($obj);
		    }
		    unset($topics);
	        endif;

		    /*
		    // MYSQL syntax error
		    // Set post pid for top post
	        $sql = "UPDATE ".$this->db->prefix("bb_posts").
	        		" SET ".$this->db->prefix("bb_posts").".pid = 0".
	        		" LEFT JOIN ".$this->db->prefix("bb_posts")." AS aa ON aa.topic_id = ".$this->db->prefix("bb_posts").".topic_id".
	        		" WHERE ".$this->db->prefix("bb_posts").".pid <> 0 ".
	        		" 	AND ".$this->db->prefix("bb_posts").".post_id = MIN(aa.post_id)";
			$this->db->queryF($sql);
		    // Set post pid for non-top post
	        $sql = "UPDATE ".$this->db->prefix("bb_posts").
	        		" SET ".$this->db->prefix("bb_posts").".pid = MIN(aa.post_id)".
	        		" LEFT JOIN ".$this->db->prefix("bb_posts")." AS aa ON aa.topic_id = ".$this->db->prefix("bb_posts").".topic_id".
	        		" WHERE ".$this->db->prefix("bb_posts").".pid <> 0 ".
	        		" 	AND ".$this->db->prefix("bb_posts").".post_id <> @top_post";
			$this->db->queryF($sql);
			*/			
			return;
	    }
	    if(!is_object($object)){
		    $object =& $this->get(intval($object));
	    }
	    if(!$object->getVar("topic_id")) return false;

        if($force):
        $sql = "SELECT MAX(post_id) AS last_post, COUNT(*) AS total ".
        		" FROM " . $xoopsDB->prefix("bb_posts") . 
        		" WHERE approved=1 AND topic_id = ".$object->getVar("topic_id");
        if ( $result = $xoopsDB->query($sql) )
        if ( $row = $xoopsDB->fetchArray($result) ) {
	        if($object->getVar("topic_last_post_id") != $row['last_post']){
            	$object->setVar("topic_last_post_id", $row['last_post']);
        	}
	        if($object->getVar("topic_replies") != $row['total'] -1 ){
            	$object->setVar("topic_replies", $row['total'] -1);
        	}
        }
        $this->insert($object, true);
        endif;
        
	    $time_synchronization = 30 * 24 * 3600; // in days; this should be counted since last synchronization
        if($force || $object->getVar("topic_time") > (time() - $time_synchronization)):
        $sql = "SELECT MIN(post_id) AS top_post FROM ".$xoopsDB->prefix("bb_posts")." WHERE approved = 1 AND topic_id = ".$object->getVar("topic_id");
        if ( $result = $xoopsDB->query($sql) ) {
        	list($top_post) = $xoopsDB->fetchRow($result);
        	if(empty($top_post)) return false;
	        $sql = 	"UPDATE ".$xoopsDB->prefix("bb_posts").
	        		" SET pid = 0 ".
	        		" WHERE post_id = ".$top_post.
	        		" AND pid <> 0";
	        if ( !$result = $xoopsDB->queryF($sql) ) {
	            //newbb_message("Could not set top post $top_post for topic: ".$sql);
	            //return false;
	        }
	        $sql = 	"UPDATE ".$xoopsDB->prefix("bb_posts").
	        		" SET pid = ".$top_post.
	        		" WHERE".
	        		" 	topic_id = ".$object->getVar("topic_id").
	        		" 	AND post_id <> ".$top_post.
	        		" 	AND pid = 0";
	        if ( !$result = $xoopsDB->queryF($sql) ) {
	            //newbb_message("Could not set post parent ID for topic: ".$sql);
	            //return false;
	        }
	        
	        /*
		    // MYSQL syntax error
	        $sql = 	"UPDATE ".$xoopsDB->prefix("bb_posts").
	        		" SET ".$xoopsDB->prefix("bb_posts"). ".pid = ".$top_post.
	        		" LEFT JOIN ".$xoopsDB->prefix("bb_posts"). " AS bb".
	        		" 	ON bb.post_id = ".$xoopsDB->prefix("bb_posts"). ".pid".
	        		" WHERE".
	        		" 	".$xoopsDB->prefix("bb_posts"). ".topic_id = ".$object->getVar("topic_id").
	        		" 	AND ".$xoopsDB->prefix("bb_posts"). ".post_id <> ".$top_post.
	        		" 	AND bb.topic_id <>".$object->getVar("topic_id");
	        if ( !$result = $xoopsDB->queryF($sql) ) {
	            //newbb_message("Could not concile posts for topic: ".$sql);
	            //return false;
	        }
	        */
        }
        endif;
		return true;
    }
}

?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?