Revision [6442]

This is an old revision of PageAndCategoryDivisionInACategory made by NilsLindenberg on 2005-03-03 14:08:53.

 

Division between Pages and categories in a Category


28/02/2005: new option show="categories|pages|all" and a seperate file for the two functions.

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:
  1. <?php
  2. /**
  3.  * Generates a list of pages belonging to the specified or top-level category.
  4.  *
  5.  * If no category page is specified, the current page is assumed to be a category (unless filtered).
  6.  *
  7.  * Unless 'forcecat' is set to 0, only pages starting with 'Category' are considered to be Category
  8.  * pages and if this action is not on a category page, it will list the contents of
  9.  * CategoryCategory instead.
  10.  *
  11.  * Template pages (page name ending with 'Template') may contain category names; they are filtered
  12.  * out automatically unless 'inctpl' is set to 1. One exception: pagenames that start with
  13.  * 'Category' and end with 'Template' are considered a proper category so templates can themselves
  14.  * be categorized.
  15.  *
  16.  * Note: the list view ('compact' = 1) is nice for a sidebar while the columnar view
  17.  * ('compact' = 0) is more suited for a category page.
  18.  *
  19.  * Syntax:
  20.  * {{category [forcecat="0|1"] [inctpl="0|1"] [page="categoryname"] [col="n"] [compact="0|1"] [class="class"] [show="pages|categories|all"]}}
  21.  *
  22.  * @package Actions
  23.  * @subpackage  SystemContent
  24.  * @name    Category
  25.  *
  26.  * @author  {@link http://wikka.jsnx.com/JsnX JsnX}
  27.  * @author  {@link http://wikka.jsnx.com/JavaWoman JavaWoman} (rewrite with filtering and table-less columns)
  28.  * @author  {@link http://wikka.jsnx.com/NilsLindenberg NilsLindenberg} (seperation of categories, functions for output)
  29.  * @copyright   Copyright © 2004, Jason Tourtelotte
  30.  * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  31.  * @since   Wikka 1.0.0
  32.  *
  33.  * @input   integer $forcecat   optional: consider only pages that start with 'Category' (1) or
  34.  *              treat any name as category (0); default: 1
  35.  * @input   integer $inctpl optional: include "template" pages (1) or not (0); default: 0.
  36.  * @input   integer $page   optional: category to list members of; default: current page or CategoryCategory
  37.  * @input   integer $col    optional: number of columns for table; default: 1
  38.  * @input   integer $compact    optional: use table (0) or list (1); default: 0
  39.  * @input   integer $class  optional: class(es) to determine styling of the output list of table
  40.  * @input   string  $show       optional: show "pages" "categories" od "all"; default: all 
  41.  * @output  list of categories and pages belonging to the specified or top-level category,
  42.  *              formatted as a list or in columns
  43.  *
  44.  * @uses        CheckMySQLVersion()
  45.  * @uses        FullCategoryTextSearch()
  46.  * @uses        FullTextSearch()
  47.  * @uses        Link()
  48.  * @todo        - version dependency FullCategoryTextSearch/ FullTextSearch should be hidden inside call - JW 2005-01-21
  49.  *      - possible? use a single list also for columns, using CSS to visually split up into columns - JW 2005-01-19
  50.  */
  51.  
  52. //including the needed functions
  53. require_once 'library/common/showarray.php';
  54.  
  55. // set defaults
  56. $lForceCat  = TRUE;         # only pages starting with 'Category' are considered category pages
  57. $lIncTpl    = FALSE;            # do not show template pages or treat a template as a category
  58. $lPage  = $this->tag;       # current page is default category
  59. $lCol       = 1;                # one column for table
  60. $lCompact   = FALSE;            # use table, not list
  61. $lClass = '';               # no class
  62. $lshow  = 'all';            # show pages and categories
  63.  
  64. // get parameters
  65. if (is_array($vars))
  66. {
  67.     foreach ($vars as $param => $value)
  68.     {
  69.         switch ($param)
  70.         {
  71.             case 'forcecat':
  72.                 if (!$value) $lForceCat = FALSE;
  73.                 break;
  74.             case 'inctpl':
  75.                 if ($value) $lIncTpl = TRUE;
  76.                 break;
  77.             case 'page':
  78.                 if ($this->existsPage($value)) $lPage = $value;
  79.                 break;
  80.             case 'col':
  81.                 if ($value === (string)(int)$value && (int)$value > 0) $lCol = (int)$value;
  82.                 break;
  83.             case 'compact':
  84.                 if ($value) $lCompact = TRUE;
  85.                 break;
  86.             case 'class':
  87.                 if ('' != $value) $lClass = $value;
  88.                 break;
  89.             case 'show':
  90.                 if ($value == 'pages' || $value == 'categories') $lshow = $value;
  91.                 break;
  92.         }
  93.     }
  94. }
  95.  
  96. // filter WHICH category we (may) show the content OF
  97. if ($lForceCat)
  98. {
  99.     if (!preg_match('/^Category/',$lPage)) $lPage = 'CategoryCategory';
  100. }
  101. elseif ($lPage == '/')
  102. {
  103.     $lPage = 'CategoryCategory';
  104. }
  105. if (!$lIncTpl && preg_match('/Template$/',$lPage))
  106. {
  107.     if (!preg_match('/^Category/',$lPage)) $lPage = 'CategoryCategory'; # exception for a category that contains templates
  108. }
  109.  
  110.  
  111. // get the listed category pages
  112. if ($this->CheckMySQLVersion(4,0,1))
  113. {
  114.     $results = $this->FullCategoryTextSearch($lPage);
  115. }
  116. else
  117. {
  118.     $results = $this->FullTextSearch($lPage);
  119. }
  120.  
  121. // filter what we show AS content of the requested category
  122. if ($results)
  123. {
  124.     $pagelist = array();
  125.     $categorylist = array();
  126.     foreach ($results as $cpage)
  127.     {
  128.         // do not list top-level category as member
  129.         if ('CategoryCategory' == $cpage['tag'])
  130.         {
  131.             continue;
  132.         }
  133.         // do not list requested category as member
  134.         elseif ($cpage['tag'] == $lPage)
  135.         {
  136.             continue;
  137.         }
  138.         // unless inctpl is set, do not list template pages
  139.         elseif (!$lIncTpl && preg_match('/Template$/', $cpage['tag']))
  140.         {
  141.             // if the requested category ($lPage) is a template category, we do list its contents;
  142.             // while a page that starts with 'Category' is not (considered) a template, so we do list that as content;
  143.             // otherwise this must indeed be a template so we don't list it.
  144.             if ( ! (preg_match('/^Category.*?Template$/',$lPage) || preg_match('/^Category/',$cpage['tag'])) )
  145.             {
  146.                 continue;
  147.             }
  148.         }
  149.         // we have a valid result: add to the list
  150.         if ($lCompact)
  151.         {
  152.             if (preg_match('/^Category/', $cpage['tag'])) $categorylist[] = $this->Link($cpage['tag'],'',preg_replace('/Category/','',$cpage['tag']));
  153.             else $pagelist[] = $this->Link($cpage['tag'],'',$cpage['tag']);
  154.         }
  155.         else
  156.         {
  157.             if (preg_match('/^Category/', $cpage['tag'])) $categorylist[] = $this->Link($cpage['tag']);
  158.             else $pagelist[] = $this->Link($cpage['tag']);
  159.         }
  160.     }
  161.     sort($categorylist);
  162.     sort($pagelist);
  163. }
  164.  
  165. // show resulting list of categories belonging to category $lPage
  166. if ($categorylist && ($lshow == 'categories' || $lshow == 'all'))
  167. {
  168.     $categorystr ='';
  169.     // make simple list (useful for sidebar)
  170.     if ($lCompact)
  171.     {
  172.         $categorystr .= ShowArrayAsList($categorylist,$lClass);
  173.     }
  174.     // make columnar overview (useful for category pages)
  175.     else
  176.     {
  177.         $categorycount = count($categorylist);
  178.         $categorystr .= 'The following '.$categorycount.' categories belong to '.$lPage.':<br /><br />'."\n";
  179.         $categorystr .= ShowArrayInColumns($categorylist, $lCol, $lClass); 
  180.     }
  181. }
  182. else
  183. {
  184.     $categorystr .= 'Sorry, no categories found for ' . $lPage .'.';
  185. }
  186.  
  187. // show resulting list of pages belonging to category $lPage
  188. if ($pagelist && ($lshow == 'pages' || $lshow == 'all'))
  189. {
  190.     $pagestr ='';
  191.     // make simple list (useful for sidebar)
  192.     if ($lCompact)
  193.     {
  194.         $pagestr .= ShowArrayAsList($pagelist,$lClass);
  195.     }
  196.     // make columnar overview (useful for category pages)
  197.     else
  198.     {
  199.         $pagecount = count($pagelist);
  200.         $pagestr .= 'The following '.$pagecount.' pages belong to '.$lPage.':<br /><br />'."\n";
  201.         $pagestr .= ShowArrayInColumns($pagelist, $lCol, $lClass);
  202.     }
  203.  
  204. }
  205. else
  206. {
  207.     $pagestr .= 'Sorry, no items found for ' . $lPage .'.';
  208. }
  209.  
  210. if ($lshow == 'categories' || $lshow == 'all')
  211. {
  212.     echo $categorystr;
  213.     echo '<br /><br />';
  214. }
  215. if ($lshow == 'pages' || $lshow == 'all') echo $pagestr;
  216. ?>


The following file has to be saved as library/common/showarray.php:
  1. <?php
  2. /**
  3.  * Sorts a given array in a given number of columns.
  4.  *
  5.  * @package     Library
  6.  * @subpackage  Common
  7.  * @name        ShowArray
  8.  *
  9.  * @author      {@link http://wikka.jsnx.com/JavaWoman JavaWoman} (table-less columns)
  10.  * @author      {@link http://wikka.jsnx.com/NilsLindenberg NilsLindenberg} (function and "divison" of the array)
  11.  * @copyright   Copyright © 2004,
  12.  * @license     http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  13.  * @since       Wikka 1.0.0
  14.  *
  15.  * @input       array       $array  necessary: the data to be shown
  16.  * @input       integer $colnumber  optional: number of colums; default: 0.
  17.  * @input       string  $class  optional: class(es) to determine styling of the output list of table; default: none.
  18.  * @output      list of data from an array, formatted as in columns
  19.  *
  20.  * @todo        - possible? use a single list also for columns, using CSS to visually split up into columns - JW 2005-01-19
  21.  */
  22.  
  23. function ShowArrayInColumns($array, $colnumber=1, $class="")
  24. {
  25.     $str = "";
  26.     if (is_array($array))
  27.     {
  28.         $entries = count($array);
  29.         $width = (int) (100 / $colnumber);
  30.         $lines = 0;
  31.         $a = 0;
  32.        
  33.         $str .= '<div'.$class.'>'."\n";
  34.                
  35.         //how many lines with an entry in every column do we have?
  36.         while ($entries / $colnumber > 1)
  37.         {
  38.             $lines++;
  39.             $entries = $entries - $colnumber;
  40.         }
  41.        
  42.         //prepare output
  43.         for ($i=0;$i<$colnumber;$i++)
  44.         {
  45.             $str .='    <div style="width: '.$width.'%; float: left;">'."\n";
  46.             for ($j=0;$j<$lines;$j++)
  47.             {
  48.                 $str .= '       '.$array[$a].'<br />'."\n";
  49.                 $a++;  
  50.             }
  51.            
  52.             //the rest of the entries (less then the number of cols)
  53.             if ($entries)
  54.             {
  55.                 $str .= '       '.$array[$a].'<br />'."\n";
  56.                 $entries--;
  57.                 $a++;  
  58.             }
  59.             $str .="    </div>\n";
  60.    
  61.         }
  62.         $str .= '</div><br  style="clear: both;">'."\n";
  63.         return ($str);
  64.     }
  65.     $str .= 'The data delivered to the function ShowArrayInColumns was no array.';
  66.     return ($str);
  67. }  
  68.  
  69. /**
  70.  * Sorts a given array as a list.
  71.  *
  72.  * @package     Library
  73.  * @subpackage  Common
  74.  * @name        ShowArray
  75.  *
  76.  * @author      {@link http://wikka.jsnx.com/JsnX JsnX} (list)
  77.  * @author      {@link http://wikka.jsnx.com/NilsLindenberg NilsLindenberg} (function and "divison" of the array)
  78.  * @copyright   Copyright © 2004,
  79.  * @license     http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  80.  * @since       Wikka 1.0.0
  81.  *
  82.  * @input       array       $array  necessary: the data to be shown
  83.  * @input       string  $class  optional: class(es) to determine styling of the output list of table; default: none.
  84.  * @output      list of data from an array, formatted as a list
  85.  *
  86.  */
  87.  
  88. function ShowArrayAsList($array,$class="")
  89. {
  90.     $str = "";
  91.     if (is_array($array))
  92.     {
  93.         $entries = count($array);
  94.         $str .= '<div'.$class.'>'."\n";
  95.         $str .= "<ul>\n";
  96.         for ($i=0;$i<$entries;$i++)
  97.         {
  98.             $str .= '   <li>'.$array[$i].'</li>';
  99.         }
  100.         $str .= "</ul>\n</div>\n";
  101.         return ($str);
  102.     }
  103.     $str .= "The data delivered to the function ShowArrayAsList was no array.";
  104.     return ($str);
  105. }
  106. ?>



CategoryDevelopment
There are 22 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki