Revision [7069]

This is an old revision of PageAndCategoryDivisionInACategory made by NilsLindenberg on 2005-04-01 19:33:28.

 

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:
  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/or 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. $output = '';                 # the final output
  64.  
  65. // get parameters
  66. if (is_array($vars))
  67. {
  68.     foreach ($vars as $param => $value)
  69.     {
  70.         switch ($param)
  71.         {
  72.             case 'forcecat':
  73.                 if (!$value) $lForceCat = FALSE;
  74.                 break;
  75.             case 'inctpl':
  76.                 if ($value) $lIncTpl = TRUE;
  77.                 break;
  78.             case 'page':
  79.                 if ($this->existsPage($value)) $lPage = $value;
  80.                 break;
  81.             case 'col':
  82.                 if ($value === (string)(int)$value && (int)$value > 0) $lCol = (int)$value;
  83.                 break;
  84.             case 'compact':
  85.                 if ($value) $lCompact = TRUE;
  86.                 break;
  87.             case 'class':
  88.                 if ('' != $value) $lClass = $value;
  89.                 break;
  90.             case 'show':
  91.                 if ($value == 'pages' || $value == 'categories') $lshow = $value;
  92.                 break;
  93.         }
  94.     }
  95. }
  96.  
  97. // filter WHICH category we (may) show the content OF
  98. if ($lForceCat)
  99. {
  100.     if (!preg_match('/^Category/',$lPage)) $lPage = 'CategoryCategory';
  101. }
  102. elseif ($lPage == '/')
  103. {
  104.     $lPage = 'CategoryCategory';
  105. }
  106. if (!$lIncTpl && preg_match('/Template$/',$lPage))
  107. {
  108.     if (!preg_match('/^Category/',$lPage)) $lPage = 'CategoryCategory';    # exception for a category that contains templates
  109. }
  110.  
  111.  
  112. // get the listed category pages
  113. if ($this->CheckMySQLVersion(4,0,1))
  114. {
  115.     $results = $this->FullCategoryTextSearch($lPage);
  116. }
  117. else
  118. {
  119.     $results = $this->FullTextSearch($lPage);
  120. }
  121.  
  122. // filter what we show AS content of the requested category
  123. if ($results !== 0)
  124. {
  125.     $pagelist = array();
  126.     $categorylist = array();
  127.     foreach ($results as $cpage)
  128.     {
  129.         // do not list top-level category as member
  130.         if ('CategoryCategory' == $cpage['tag'])
  131.         {
  132.             continue;
  133.         }
  134.         // do not list requested category as member
  135.         elseif ($cpage['tag'] == $lPage)
  136.         {
  137.             continue;
  138.         }
  139.         // unless inctpl is set, do not list template pages
  140.         elseif (!$lIncTpl && preg_match('/Template$/', $cpage['tag']))
  141.         {
  142.             // if the requested category ($lPage) is a template category, we do list its contents;
  143.             // while a page that starts with 'Category' is not (considered) a template, so we do list that as content;
  144.             // otherwise this must indeed be a template so we don't list it.
  145.             if ( ! (preg_match('/^Category.*?Template$/',$lPage) || preg_match('/^Category/',$cpage['tag'])) )
  146.             {
  147.                 continue;
  148.             }
  149.         }
  150.  
  151.         // we have a valid result: add to the list
  152.         if (preg_match('/^Category/', $cpage['tag']) && ($lshow == "all" || $lshow == "categories"))
  153.         {
  154.             if ($lCompact)
  155.                 {$categorylist[] = $this->Link($cpage['tag'],'',preg_replace('/Category/','',$cpage['tag']));}
  156.             else
  157.                 {$categorylist[] = $this->Link($cpage['tag']);}
  158.         }
  159.         else if (!preg_match('/^Category/', $cpage['tag']) && ($lshow == "all" || $lshow == "pages"))
  160.         {
  161.             if ($lCompact)
  162.                 {$pagelist[] = $this->Link($cpage['tag'],'',$cpage['tag']);}
  163.             else
  164.                 {$pagelist[] = $this->Link($cpage['tag']);}
  165.         }
  166.     }
  167. }
  168. else $output .= 'No items found for '.$lpage.".\n";
  169.  
  170. // show resulting list of categories belonging to category $lPage
  171. if ($categorylist)
  172. {
  173.     sort($categorylist);
  174.     // make simple list (useful for sidebar)
  175.     if ($lCompact)
  176.     {
  177.         $output .= ShowArrayAsList($categorylist,$lClass);
  178.     }
  179.     // make columnar overview (useful for category pages)
  180.     else
  181.     {
  182.         $categorycount = count($categorylist);
  183.         $output .= 'The following '.$categorycount.' categories belong to '.$lPage.':<br /><br />'."\n";
  184.         $output .= ShowArrayInColumns($categorylist, $lCol, $lClass);
  185.     }
  186. }
  187. else if ($lshow == "all" || $lshow == "categories")
  188. {
  189.     $output .= 'No categories found for '.$lPage.".\n";
  190. }
  191.  
  192. //divide
  193. if ($lshow == all) $output .= '<br><br>';
  194.  
  195. // show resulting list of pages belonging to category $lPage
  196. if ($pagelist)
  197. {
  198.     sort($pagelist);
  199.     // make simple list (useful for sidebar)
  200.     if ($lCompact)
  201.     {
  202.         $output .= ShowArrayAsList($pagelist,$lClass);
  203.     }
  204.     // make columnar overview (useful for category pages)
  205.     else
  206.     {
  207.         $pagecount = count($pagelist);
  208.         $output .= 'The following '.$pagecount.' pages belong to '.$lPage.':<br /><br />'."\n";
  209.         $output .= ShowArrayInColumns($pagelist, $lCol, $lClass);
  210.     }
  211.  
  212. }
  213. else if ($lshow == "all" || $lshow == "pages")
  214. {
  215.     $output .= 'No pages found for '.$lPage.".\n";
  216. }
  217.  
  218. echo $output;
  219. ?>


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