article.php

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

PHP
1,497
字号
<?php
/**
 * Article management
 *
 * @copyright	The XOOPS project http://www.xoops.org/
 * @license		http://www.fsf.org/copyleft/gpl.html GNU public license
 * @author		Taiwen Jiang (phppp or D.J.) <php_pp@hotmail.com>
 * @since		1.00
 * @version		$Id$
 * @package		module::article
 */
 
if (!defined("XOOPS_ROOT_PATH")) {
	exit();
}
include_once dirname(dirname(__FILE__))."/include/vars.php";
mod_loadFunctions("parse", $GLOBALS["artdirname"]);

/**
 * Article 
 * 
 * @author D.J. (phppp)
 * @copyright copyright &copy; 2005 XoopsForge.com
 * @package module::article
 *
 * {@link XoopsObject} 
 **/

if(!class_exists("Article")){
class Article extends ArtObject
{
    /**
     * @var array
     */
    var $headings = array();
    /**
     * @var array
     */
    var $notes = array();

    /**
     * Constructor
     *
     */
    function Article()
    {
	    $this->ArtObject();
        $this->table = art_DB_prefix("article");
        $this->initVar("art_id", 			XOBJ_DTYPE_INT, null, false);				// auto_increment unique ID
        $this->initVar("cat_id", 			XOBJ_DTYPE_INT, 0, true);					// base category ID

        $this->initVar("uid", 				XOBJ_DTYPE_INT, 0);							// submitter's UID
        $this->initVar("writer_id", 		XOBJ_DTYPE_INT, 0); 						// Original writer's ID
        //$this->initVar("art_author", 		XOBJ_DTYPE_TXTBOX, "");
        //$this->initVar("art_profile",		XOBJ_DTYPE_TXTAREA, "");
        $this->initVar("art_source", 		XOBJ_DTYPE_TXTBOX, ""); 					// Original URL or resource

        $this->initVar("art_title", 		XOBJ_DTYPE_TXTBOX, "");						// article title
        $this->initVar("art_keywords", 		XOBJ_DTYPE_TXTBOX, "");						// keywords, in raw format
        $this->initVar("art_summary", 		XOBJ_DTYPE_TXTAREA, "");					// article summary

        $this->initVar("art_image", 		XOBJ_DTYPE_ARRAY, "");						// head image: file name, caption
        $this->initVar("art_template", 		XOBJ_DTYPE_TXTBOX, "");						// specified article template, overwriting the module and category -wide setting

        $this->initVar("art_pages", 		XOBJ_DTYPE_ARRAY, "");						// associative array of pages: text ID, papage title
        $this->initVar("art_categories",	XOBJ_DTYPE_ARRAY, "");						// categories ID
        $this->initVar("art_topics", 		XOBJ_DTYPE_ARRAY, "");						// topics ID
        $this->initVar("art_elinks", 		XOBJ_DTYPE_TXTAREA, ""); 					// external links, in raw format
        $this->initVar("art_forum", 		XOBJ_DTYPE_INT, 0);							// forum ID the comments will be located

        $this->initVar("art_time_create", 	XOBJ_DTYPE_INT);							// time of creation
        $this->initVar("art_time_submit", 	XOBJ_DTYPE_INT);							// time of submission

        $this->initVar("art_time_publish",	XOBJ_DTYPE_INT);							// time of publish
        $this->initVar("art_counter", 		XOBJ_DTYPE_INT, 0);							// click count
        $this->initVar("art_rating", 		XOBJ_DTYPE_INT);							// rating value, in sum
        $this->initVar("art_rates", 		XOBJ_DTYPE_INT, 0);							// rating count
        $this->initVar("art_comments", 		XOBJ_DTYPE_INT, 0);							// comment count
        $this->initVar("art_trackbacks", 	XOBJ_DTYPE_INT, 0);							// trackback count

        /*
         * For summary
         *
         */
        $this->initVar("dohtml", 			XOBJ_DTYPE_INT, 1);
    }

    /**
     * get a list of categories
     * 
     * @return array of category ID
     */
    function &getCategories()
    {
	    $categories = $this->getVar("art_categories");
	    if(!in_array($this->getVar("cat_id"), $categories)){
		    array_unshift($categories, $this->getVar("cat_id"));
	    }
		return $categories;
    }

    /**
     * get verified image of the article: url, caption
     * 
     * @param 	bool	$complete	flag for retrieving image url
     * @return	mixed	array or null
     */
    function getImage($complete = true)
    {
	    $image = $this->getVar("art_image");
		if(!empty($image["file"])){
			if(!empty($complete)){
				mod_loadFunctions("url", $GLOBALS["artdirname"]);
				$image["url"] = art_getImageUrl($image["file"]);
			}
		}else{
			$image = null;
		}
		return $image;
    }

    /**
     * get writer info of the article
     * 
     * @return array associative array of writer name, avatar and his profile
     */
    function &getWriter()
    {
	    $writer = array();
		if($writer_id = $this->getVar("writer_id")){
			$writer_handler =& xoops_getmodulehandler("writer", $GLOBALS["artdirname"]);
			$writer_obj =& $writer_handler->get($writer_id);
			$writer["name"] = $writer_obj->getVar("writer_name");
			mod_loadFunctions("url", $GLOBALS["artdirname"]);
			$writer["avatar"] = art_getImageLink($writer_obj->getVar("writer_avatar"));
			$writer["profile"] = $writer_obj->getVar("writer_profile");
			unset($writer_obj);
		}
		return $writer;
    }

    /**
     * get author info of the article
     * 
 	 * {@link XoopsUser} 
 	 *
     * @param 	bool	$retrieveUname	flag for retrieving user name based on user ID
     * @return array associative array of registered author id and his name
     */
    function &getAuthor($retrieveUname = false)
    {
	    /*
	    $author["author"] = $this->getVar("art_author");
		$author["profile"] = $this->getVar("art_profile");
		*/
    	$author["uid"] = $this->getVar("uid");
    	if($retrieveUname){
    		$author["name"] = XoopsUser::getUnameFromId($author["uid"]) ;
		}
		return $author;
    }

    /**
     * get formatted publish time of the article
     * 
 	 * {@link Config} 
 	 *
     * @param string $format format of time
     * @return string
     */
    function getTime($format = "c")
    {
		mod_loadFunctions("time", $GLOBALS["artdirname"]);
	    $time = art_formatTimestamp($this->getVar("art_time_publish"), $format);
		return $time;
    }

    
    /**
     * get summary of the article
     * 
     * @param bool 	$actionOnEmpty flag for truncating content if summary is empty
     * @return string
     */
    function &getSummary($actionOnEmpty = false, $dohtml = true)
    {
		$myts =& MyTextSanitizer::getInstance();
	    $summary = $this->getVar("art_summary", "n");
	    if(empty($summary) && !empty($actionOnEmpty)){
		    $pages = $this->getPages();
			if(count($pages)>1){
				$text_handler =& xoops_getmodulehandler("text", $GLOBALS["artdirname"]);
				$texts =array_filter($text_handler->getList(new Criteria("text_id", "(".implode(",", $pages).")", "IN")), "trim"); // fixed by Steven Chu
				$summary = implode( $dohtml ? "<br />" : ". ", $texts);
			}else{
				$text = $this->getText(0, "n");
				mod_loadFunctions("render", $GLOBALS["artdirname"]);
			    //$summary = $myts->displayTarea($text["body"], 0, 0, 0, 0, 0);
			    $summary = art_html2text($text["body"]);
				if(!empty($GLOBALS["xoopsModuleConfig"]["length_excerpt"])){
					$summary = $myts->htmlspecialchars( xoops_substr($summary, 0, $GLOBALS["xoopsModuleConfig"]["length_excerpt"]) );
				}
			}
	    }else{
		    if(!$dohtml) {
				mod_loadFunctions("render", $GLOBALS["artdirname"]);
			    //$summary = $myts->displayTarea($summary, 0, 0, 0, 0, 0);
			    $summary = art_html2text($summary);
		    }else{
			    $summary = $myts->displayTarea($summary, 1);
		    }
	    }
	    return $summary;
    }

    /**
     * get the text ID of a specified page of the article
     * 
     * @param int $page truncate content if summary is empty
     * @return int page ID (text_id)
     */
    function getPage($page = 0, $searchAll = false)
    {
	    if($this->getVar("art_id") == 0) return null;
	    $pages = $this->getPages(false, $searchAll);
	    $page = isset($pages[intval($page)])?$pages[intval($page)]:null;
	    return $page;
    }

    /**
     * get array of text IDs and titles  of the article
     * 
     * @return array	associative array of ID and title
     */
    function &getPages($withTitle = false, $searchAll = false)
    {
	    if($this->getVar("art_id") == 0) return array();
	    $pages_id = $this->getVar("art_pages");
	    if(empty($withTitle) && empty($searchAll)){
		    return $pages_id;
	    }
		$text_handler =& xoops_getmodulehandler("text", $GLOBALS["artdirname"]);
		if($searchAll){
			$criteria_pages = new Criteria("art_id", $this->getVar("art_id"));
		}else{
			$criteria_pages = new Criteria("text_id", "(".implode(",", $pages_id).")", "IN");
		}
		if(empty($withTitle)){
    		$text_handler->identifierName = false;
		}
		$pages = $text_handler->getList($criteria_pages);
	    foreach($pages_id as $id){
		    if(!isset($pages[$id])) continue;
		    if(empty($withTitle)){
		    	$ret[] = $id;
		    }else{
		    	$ret[] = array("id"=>$id, "title"=>$pages[$id]);
	    	}
	    }
	    foreach(array_keys($pages) as $id){
		    if(in_array($id, $pages_id)) continue;
		    if(empty($withTitle)){
		    	$ret[] = $id;
		    }else{
		    	$ret[] = array("id"=>$id, "title"=>$pages[$id]);
	    	}
	    }
	    unset($criteria_pages, $pages);
	    return $ret;
    }

    /**
     * pages count of the article
     * 
     * @return int
     */
    function getPageCount($searchAll = false)
    {
	    if($this->getVar("art_id") == 0) return 0;
	    if(empty($searchAll)){
		    return count($this->getVar("art_pages"));
	    }
		$text_handler =& xoops_getmodulehandler("text", $GLOBALS["artdirname"]);
		$criteria_pages = new Criteria("art_id", $this->getVar("art_id"));
		$count = $text_handler->getCount($criteria_pages);		
		unset($criteria_pages);
		return $count;
    }

    /**
     * get rating average of the article
     * 
     * @param int $decimals decimal length
     * @return numeric
     */
    function getRatingAverage($decimals = 2)
    {
	    $ave = 0;
	    if($this->getVar("art_rates")){
	    	$ave = number_format($this->getVar("art_rating")/$this->getVar("art_rates"), $decimals);
    	}
	    return $ave;
    }

    /**
     * get text content of a specified page of the article
     * 
     * @param int $page page no
     * @param string $format text format
     * @return array
     */
    function &getText($page = -1, $format = "s")
    {
	    global $xoopsModuleConfig;

	    $format = strtolower($format);
		$text = $this->_getText($page, $format);
		if(empty($text)){
			return $text;
		}
		if($format=="e" || $format == "edit" || $format == "n" || $format == "none") {
			return $text;
		}
		if($format=="raw"){
			mod_loadFunctions("render", $GLOBALS["artdirname"]);
			$ret =array(
				"title"	=> art_htmlSpecialChars($text["title"]),
				"body"	=> art_displayTarea($text["body"])
				);
			return $ret;
		}

		$body =& $text["body"];
		$body = $this->parseNotes($body);
		$body = $this->parseHeadings($body);

		$ret = array("title" => $text["title"], "body" => $body);
		return $ret;
    }
    
    /**
     * Generate sanitized text and headings of the article
     * 
     * @param string $text text content
     * @return string 
     */
    function &parseHeadings(&$text)
    {
	    $this->headings = array();
		if(empty($GLOBALS["xoopsModuleConfig"]["do_heading"]) || empty($text)) {
			return $text;
		}
		$text = preg_replace_callback( "/<h([1-7])>(.*)<\/h\\1>/isU", array(&$this, "_convertHeadings"), $text );
		return $text;
    }

    /**
     * Generate heading of the article
     * 
     * @param array $matches matched items
     * @return string 
     */
    function _convertHeadings($matches)
    {
	    static $ii=0;
	    $ii ++;
	    $this->headings[] = "<a href=\"#heading".$ii."\">". $matches[2]. "</a>";
	    return "<a name=\"heading".$ii."\" id=\"heading".$ii."\"></a><h".$matches[1].">". $matches[2]. "</h".$matches[1].">";
    }
    
    /**
     * Generate sanitized text and footnotes of the article
     * 
     * @param string $text text content
     * @return string 

⌨️ 快捷键说明

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