Revision [6441]

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

 

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. function ShowArrayInColumns($array, $colnumber=1, $class="")
  4. {
  5.     $str = "";
  6.     if (is_array($array))
  7.     {
  8.         $entries = count($array);
  9.         $width = (int) (100 / $colnumber);
  10.         $lines = 0;
  11.         $a = 0;
  12.        
  13.         $str .= '<div'.$class.'>'."\n";
  14.                
  15.         //how many lines with an entry in every column do we have?
  16.         while ($entries / $colnumber > 1)
  17.         {
  18.             $lines++;
  19.             $entries = $entries - $colnumber;
  20.         }
  21.        
  22.         //prepare output
  23.         for ($i=0;$i<$colnumber;$i++)
  24.         {
  25.             $str .='    <div style="width: '.$width.'%; float: left;">'."\n";
  26.             for ($j=0;$j<$lines;$j++)
  27.             {
  28.                 $str .= '       '.$array[$a].'<br />'."\n";
  29.                 $a++;  
  30.             }
  31.            
  32.             //the rest of the entries (less then the number of cols)
  33.             if ($entries)
  34.             {
  35.                 $str .= '       '.$array[$a].'<br />'."\n";
  36.                 $entries--;
  37.                 $a++;  
  38.             }
  39.             $str .="    </div>\n";
  40.    
  41.         }
  42.         $str .= '</div><br  style="clear: both;">'."\n";
  43.         return ($str);
  44.     }
  45.     $str .= 'The data delivered to the function ShowArrayInColumns was no array.';
  46.     return ($str);
  47. }  
  48.  
  49.  
  50.  
  51. function ShowArrayAsList($array,$class="")
  52. {
  53.     $str = "";
  54.     if (is_array($array))
  55.     {
  56.         $entries = count($array);
  57.         //$classattr = ('' != $class) ? ' class="linklist '.$class.'"' : ' class="linklist"';
  58.         $str .= '<div'.$class.'>'."\n";
  59.         $str .= "<ul>\n";
  60.         for ($i=0;$i<$entries;$i++)
  61.         {
  62.             $str .= '   <li>'.$array[$i].'</li>';
  63.         }
  64.         $str .= "</ul>\n</div>\n";
  65.         return ($str);
  66.     }
  67.     $str .= "The data delivered to the function ShowArrayAsList was no array.";
  68.     return ($str);
  69. }
  70. ?>



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