Revision [6989]
This is an old revision of PageAndCategoryDivisionInACategory made by NilsLindenberg on 2005-03-29 13:39:41.
Division between Pages and categories in a Category
29/03/2005: small code change (building arrays only when needed and better organzation of the output).
If you use the following code to replace your actions/category.php, Categories are listed seperate from pages, and the first ones without the "Category" in front.
here's the code:
- <?php
- /**
- * Generates a list of pages belonging to the specified or top-level category.
- *
- * If no category page is specified, the current page is assumed to be a category (unless filtered).
- *
- * Unless 'forcecat' is set to 0, only pages starting with 'Category' are considered to be Category
- * pages and if this action is not on a category page, it will list the contents of
- * CategoryCategory instead.
- *
- * Template pages (page name ending with 'Template') may contain category names; they are filtered
- * out automatically unless 'inctpl' is set to 1. One exception: pagenames that start with
- * 'Category' and end with 'Template' are considered a proper category so templates can themselves
- * be categorized.
- *
- * Note: the list view ('compact' = 1) is nice for a sidebar while the columnar view
- * ('compact' = 0) is more suited for a category page.
- *
- * Syntax:
- * {{category [forcecat="0|1"] [inctpl="0|1"] [page="categoryname"] [col="n"] [compact="0|1"] [class="class"] [show="pages|categories|all"]}}
- *
- * @package Actions
- * @subpackage SystemContent
- * @name Category
- *
- * @author {@link http://wikka.jsnx.com/JsnX JsnX}
- * @author {@link http://wikka.jsnx.com/JavaWoman JavaWoman} (rewrite with filtering and table-less columns)
- * @author {@link http://wikka.jsnx.com/NilsLindenberg NilsLindenberg} (seperation of categories, functions for output)
- * @copyright Copyright © 2004, Jason Tourtelotte
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
- * @since Wikka 1.0.0
- *
- * @input integer $forcecat optional: consider only pages that start with 'Category' (1) or
- * treat any name as category (0); default: 1
- * @input integer $inctpl optional: include "template" pages (1) or not (0); default: 0.
- * @input integer $page optional: category to list members of; default: current page or CategoryCategory
- * @input integer $col optional: number of columns for table; default: 1
- * @input integer $compact optional: use table (0) or list (1); default: 0
- * @input integer $class optional: class(es) to determine styling of the output list of table
- * @input string $show optional: show "pages" "categories" od "all"; default: all
- * @output list of categories and/or pages belonging to the specified or top-level category,
- * formatted as a list or in columns
- *
- * @uses CheckMySQLVersion()
- * @uses FullCategoryTextSearch()
- * @uses FullTextSearch()
- * @uses Link()
- * @todo - version dependency FullCategoryTextSearch/ FullTextSearch should be hidden inside call - JW 2005-01-21
- * - possible? use a single list also for columns, using CSS to visually split up into columns - JW 2005-01-19
- */
- //including the needed functions
- require_once 'library/common/showarray.php';
- // set defaults
- $lForceCat = TRUE; # only pages starting with 'Category' are considered category pages
- $lIncTpl = FALSE; # do not show template pages or treat a template as a category
- $lPage = $this->tag; # current page is default category
- $lCol = 1; # one column for table
- $lCompact = FALSE; # use table, not list
- $lClass = ''; # no class
- $lshow = 'all'; # show pages and categories
- $output = ''; # the final output
- // get parameters
- {
- foreach ($vars as $param => $value)
- {
- switch ($param)
- {
- case 'forcecat':
- if (!$value) $lForceCat = FALSE;
- break;
- case 'inctpl':
- if ($value) $lIncTpl = TRUE;
- break;
- case 'page':
- if ($this->existsPage($value)) $lPage = $value;
- break;
- case 'col':
- if ($value === (string)(int)$value && (int)$value > 0) $lCol = (int)$value;
- break;
- case 'compact':
- if ($value) $lCompact = TRUE;
- break;
- case 'class':
- if ('' != $value) $lClass = $value;
- break;
- case 'show':
- if ($value == 'pages' || $value == 'categories') $lshow = $value;
- break;
- }
- }
- }
- // filter WHICH category we (may) show the content OF
- if ($lForceCat)
- {
- }
- elseif ($lPage == '/')
- {
- $lPage = 'CategoryCategory';
- }
- {
- if (!preg_match('/^Category/',$lPage)) $lPage = 'CategoryCategory'; # exception for a category that contains templates
- }
- // get the listed category pages
- if ($this->CheckMySQLVersion(4,0,1))
- {
- $results = $this->FullCategoryTextSearch($lPage);
- }
- else
- {
- $results = $this->FullTextSearch($lPage);
- }
- // filter what we show AS content of the requested category
- if ($results)
- {
- foreach ($results as $cpage)
- {
- // do not list top-level category as member
- if ('CategoryCategory' == $cpage['tag'])
- {
- continue;
- }
- // do not list requested category as member
- elseif ($cpage['tag'] == $lPage)
- {
- continue;
- }
- // unless inctpl is set, do not list template pages
- {
- // if the requested category ($lPage) is a template category, we do list its contents;
- // while a page that starts with 'Category' is not (considered) a template, so we do list that as content;
- // otherwise this must indeed be a template so we don't list it.
- {
- continue;
- }
- }
- // we have a valid result: add to the list
- {
- if ($lCompact)
- else
- }
- {
- if ($lCompact)
- else
- }
- }
- }
- else $output .= 'No items found for '. $lpage .'.';
- // show resulting list of categories belonging to category $lPage
- if ($categorylist)
- {
- // make simple list (useful for sidebar)
- if ($lCompact)
- {
- $output .= ShowArrayAsList($categorylist,$lClass);
- }
- // make columnar overview (useful for category pages)
- else
- {
- $output .= 'The following '.$categorycount.' categories belong to '.$lPage.':<br /><br />'."\n";
- $output .= ShowArrayInColumns($categorylist, $lCol, $lClass);
- }
- }
- else if ($lshow == "all" || $lshow == "categories")
- {
- $output .= 'No categories found for ' . $lPage .'.';
- }
- //divide
- if ($lshow == all) $output .= '<br><br>';
- // show resulting list of pages belonging to category $lPage
- if ($pagelist)
- {
- // make simple list (useful for sidebar)
- if ($lCompact)
- {
- $output .= ShowArrayAsList($pagelist,$lClass);
- }
- // make columnar overview (useful for category pages)
- else
- {
- $output .= 'The following '.$pagecount.' pages belong to '.$lPage.':<br /><br />'."\n";
- $output .= ShowArrayInColumns($pagelist, $lCol, $lClass);
- }
- }
- else if ($lshow == "all" || $lshow == "pages")
- {
- $output .= 'No pages found for ' . $lPage .'.';
- }
- echo $output;
- ?>
The following file has to be saved as library/common/showarray.php:
- <?php
- /**
- * Sorts a given array in a given number of columns.
- *
- * @package Library
- * @subpackage Common
- * @name ShowArray
- *
- * @author {@link http://wikka.jsnx.com/JavaWoman JavaWoman} (table-less columns)
- * @author {@link http://wikka.jsnx.com/NilsLindenberg NilsLindenberg} (function and "divison" of the array)
- * @copyright Copyright © 2004,
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
- * @since Wikka 1.0.0
- *
- * @input array $array necessary: the data to be shown
- * @input integer $colnumber optional: number of colums; default: 0.
- * @input string $class optional: class(es) to determine styling of the output list of table; default: none.
- * @output list of data from an array, formatted as in columns
- *
- * @todo - possible? use a single list also for columns, using CSS to visually split up into columns - JW 2005-01-19
- */
- function ShowArrayInColumns($array, $colnumber=1, $class="")
- {
- $str = "";
- {
- $width = (int) (100 / $colnumber);
- $lines = 0;
- $a = 0;
- $str .= '<div'.$class.'>'."\n";
- //how many lines with an entry in every column do we have?
- while ($entries / $colnumber > 1)
- {
- $lines++;
- $entries = $entries - $colnumber;
- }
- //prepare output
- for ($i=0;$i<$colnumber;$i++)
- {
- $str .=' <div style="width: '.$width.'%; float: left;">'."\n";
- for ($j=0;$j<$lines;$j++)
- {
- $str .= ' '.$array[$a].'<br />'."\n";
- $a++;
- }
- //the rest of the entries (less then the number of cols)
- if ($entries)
- {
- $str .= ' '.$array[$a].'<br />'."\n";
- $entries--;
- $a++;
- }
- $str .=" </div>\n";
- }
- $str .= '</div><br style="clear: both;">'."\n";
- return ($str);
- }
- $str .= 'The data delivered to the function ShowArrayInColumns was no array.';
- return ($str);
- }
- /**
- * Sorts a given array as a list.
- *
- * @package Library
- * @subpackage Common
- * @name ShowArray
- *
- * @author {@link http://wikka.jsnx.com/JsnX JsnX} (list)
- * @author {@link http://wikka.jsnx.com/NilsLindenberg NilsLindenberg} (function and "divison" of the array)
- * @copyright Copyright © 2004,
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
- * @since Wikka 1.0.0
- *
- * @input array $array necessary: the data to be shown
- * @input string $class optional: class(es) to determine styling of the output list of table; default: none.
- * @output list of data from an array, formatted as a list
- *
- */
- function ShowArrayAsList($array,$class="")
- {
- $str = "";
- {
- $str .= '<div'.$class.'>'."\n";
- $str .= "<ul>\n";
- for ($i=0;$i<$entries;$i++)
- {
- $str .= ' <li>'.$array[$i].'</li>';
- }
- $str .= "</ul>\n</div>\n";
- return ($str);
- }
- $str .= "The data delivered to the function ShowArrayAsList was no array.";
- return ($str);
- }
- ?>
CategoryDevelopment