category.php

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

PHP
868
字号
            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");
	    if(count($subCats)>0) 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";

⌨️ 快捷键说明

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