Revision [6391]

This is an old revision of PageAndCategoryDivisionInACategory made by NiehLe on 2005-02-28 16:39:14.

 

Division between Pages and categories in a Category


28/02/2005: new code which fixes counting-bug. Functions for organizing the list. Still needs adjustments (for example on the "class") and documentation.

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"]}}
  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.  * @copyright       Copyright © 2004, Jason Tourtelotte
  29.  * @license     http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  30.  * @since       Wikka 1.0.0
  31.  *
  32.  * @input       integer $forcecat   optional: consider only pages that start with 'Category' (1) or
  33.  *              treat any name as category (0); default: 1
  34.  * @input       integer $inctpl     optional: include "template" pages (1) or not (0); default: 0.
  35.  * @input       integer $page       optional: category to list members of; default: current page or CategoryCategory
  36.  * @input       integer $col        optional: number of columns for table; default: 1
  37.  * @input       integer $compact    optional: use table (0) or list (1); default: 0
  38.  * @input       integer $class      optional: class(es) to determine styling of the output list of table
  39.  * @output      list of pages belonging to the specified or top-level category,
  40.  *              formatted as a list or a layout table
  41.  *
  42.  * @uses        CheckMySQLVersion()
  43.  * @uses        FullCategoryTextSearch()
  44.  * @uses        FullTextSearch()
  45.  * @uses        Link()
  46.  * @todo        - version dependency FullCategoryTextSearch( / FullTextSearch should be hidden inside call - JW 2005-01-21
  47.  *              - refactor two different presentations into function call(s) so backlinks can use the same - JW 2005-01-21
  48.  *              - possible? use a single list also for columns, using CSS to visually split up into columns - JW 2005-01-19
  49.  */
  50.  
  51. if (! function_exists('ShowArrayInColumns'))
  52. {
  53.  
  54. function ShowArrayInColumns($array, $colnumber=1, $class="")
  55. {
  56.     $str = "";
  57.     if (is_array($array))
  58.     {
  59.         $entries = count($array);
  60.         $width = (int) (100 / $colnumber);
  61.         $lines = 0;
  62.         $a = 0;
  63.        
  64.         $str .= '<div'.$class.'>'."\n";
  65.                
  66.         //how many lines with an entry in every column do we have?
  67.         while ($entries / $colnumber > 1)
  68.         {
  69.             $lines++;
  70.             $entries = $entries - $colnumber;
  71.         }
  72.        
  73.         //prepare output
  74.         for ($i=0;$i<$colnumber;$i++)
  75.         {
  76.             $str .='    <div style="width: '.$width.'%; float: left;">'."\n";
  77.             for ($j=0;$j<$lines;$j++)
  78.             {
  79.                 $str .= '       '.$array[$a].'<br />'."\n";
  80.                 $a++;  
  81.             }
  82.            
  83.             //the rest of the entries (less then the number of cols)
  84.             if ($entries)
  85.             {
  86.                 $str .= '       '.$array[$a].'<br />'."\n";
  87.                 $entries--;
  88.                 $a++;  
  89.             }
  90.             $str .="    </div>\n";
  91.    
  92.         }
  93.         $str .= '</div><br  style="clear: both;">'."\n";
  94.         return ($str);
  95.     }
  96.     $str .= 'The data delivered to the function ShowArrayInColumns was no array.';
  97.     return ($str);
  98. }  
  99.  
  100. }
  101.  
  102. if (! function_exists('ShowArrayAsList'))
  103. {
  104.  
  105. function ShowArrayAsList($array,$class="")
  106. {
  107.     $str = "";
  108.     if (is_array($array))
  109.     {
  110.         $entries = count($array);
  111.         //$classattr = ('' != $class) ? ' class="linklist '.$class.'"' : ' class="linklist"';
  112.         $str .= '<div'.$class.'>'."\n";
  113.         $str .= "<ul>\n";
  114.         for ($i=0;$i<$entries;$i++)
  115.         {
  116.             $str .= '   <li>'.$array[$i].'</li>';
  117.         }
  118.         $str .= "</ul>\n</div>\n";
  119.         return ($str);
  120.     }
  121.     $str .= "The data delivered to the function ShowArrayAsList was no array.";
  122.     return ($str);
  123. }
  124.  
  125. }
  126.  
  127. // set defaults
  128. $lForceCat  = TRUE;         # only pages starting with 'Category' are considered category pages
  129. $lIncTpl    = FALSE;        # do not show template pages or treat a template as a category
  130. $lPage      = $this->tag;   # current page is default category
  131. $lCol       = 1;            # one column for table
  132. $lCompact   = FALSE;        # use table, not list
  133. $lClass     = '';           # no class
  134.  
  135. // get parameters
  136. if (is_array($vars))
  137. {
  138.     foreach ($vars as $param => $value)
  139.     {
  140.         switch ($param)
  141.         {
  142.             case 'forcecat':
  143.                 if (!$value) $lForceCat = FALSE;
  144.                 break;
  145.             case 'inctpl':
  146.                 if ($value) $lIncTpl = TRUE;
  147.                 break;
  148.             case 'page':
  149.                 if ($this->existsPage($value)) $lPage = $value;
  150.                 break;
  151.             case 'col':
  152.                 if ($value === (string)(int)$value && (int)$value > 0) $lCol = (int)$value;
  153.                 break;
  154.             case 'compact':
  155.                 if ($value) $lCompact = TRUE;
  156.                 break;
  157.             case 'class':
  158.                 if ('' != $value) $lClass = $value;
  159.                 break;
  160.         }
  161.     }
  162. }
  163.  
  164. // filter WHICH category we (may) show the content OF
  165. if ($lForceCat)
  166. {
  167.     if (!preg_match('/^Category/',$lPage)) $lPage = 'CategoryCategory';
  168. }
  169. elseif ($lPage == '/')
  170. {
  171.     $lPage = 'CategoryCategory';
  172. }
  173. if (!$lIncTpl && preg_match('/Template$/',$lPage))
  174. {
  175.     if (!preg_match('/^Category/',$lPage)) $lPage = 'CategoryCategory'; # exception for a category that contains templates
  176. }
  177.  
  178.  
  179. // get the listed category pages
  180. if ($this->CheckMySQLVersion(4,0,1))
  181. {
  182.     $results = $this->FullCategoryTextSearch($lPage);
  183. }
  184. else
  185. {
  186.     $results = $this->FullTextSearch($lPage);
  187. }
  188.  
  189. // filter what we show AS content of the requested category
  190. if ($results)
  191. {
  192.     $pagelist = array();
  193.     $categorylist = array();
  194.     foreach ($results as $cpage)
  195.     {
  196.         // do not list top-level category as member
  197.         if ('CategoryCategory' == $cpage['tag'])
  198.         {
  199.             continue;
  200.         }
  201.         // do not list requested category as member
  202.         elseif ($cpage['tag'] == $lPage)
  203.         {
  204.             continue;
  205.         }
  206.         // unless inctpl is set, do not list template pages
  207.         elseif (!$lIncTpl && preg_match('/Template$/', $cpage['tag']))
  208.         {
  209.             // if the requested category ($lPage) is a template category, we do list its contents;
  210.             // while a page that starts with 'Category' is not (considered) a template, so we do list that as content;
  211.             // otherwise this must indeed be a template so we don't list it.
  212.             if ( ! (preg_match('/^Category.*?Template$/',$lPage) || preg_match('/^Category/',$cpage['tag'])) )
  213.             {
  214.                 continue;
  215.             }
  216.         }
  217.         // we have a valid result: add to the list
  218.         if ($lCompact)
  219.         {
  220.             if (preg_match('/^Category/', $cpage['tag'])) $categorylist[] = $this->Link($cpage['tag'],'',preg_replace('/Category/','',$cpage['tag']));
  221.             else $pagelist[] = $this->Link($cpage['tag'],'',$cpage['tag']);
  222.         }
  223.         else
  224.         {
  225.             if (preg_match('/^Category/', $cpage['tag'])) $categorylist[] = $this->Link($cpage['tag']);
  226.             else $pagelist[] = $this->Link($cpage['tag']);
  227.         }
  228.     }
  229.     sort($categorylist);
  230.     sort($pagelist);
  231. }
  232.  
  233. // show resulting list of categories belonging to category $lPage
  234. if ($categorylist)
  235. {
  236.     $categorystr ='';
  237.     // make simple list (useful for sidebar)
  238.     if ($lCompact)
  239.     {
  240.         $categorystr .= ShowArrayAsList($categorylist,$lClass);
  241.     }
  242.     // make columnar overview (useful for category pages)
  243.     else
  244.     {
  245.         $categorycount = count($categorylist);
  246.         $categorystr .= 'The following '.$categorycount.' categories belong to '.$lPage.':<br /><br />'."\n";
  247.         $categorystr .= ShowArrayInColumns($categorylist, $lCol, $lClass); 
  248.     }
  249. }
  250. else
  251. {
  252.     $categorystr .= 'Sorry, no categories found for ' . $lPage .'.';
  253. }
  254.  
  255. // show resulting list of pages belonging to category $lPage
  256. if ($pagelist)
  257. {
  258.     $pagestr ='';
  259.     // make simple list (useful for sidebar)
  260.     if ($lCompact)
  261.     {
  262.         $pagestr .= ShowArrayAsList($pagelist,$lClass);
  263.     }
  264.     // make columnar overview (useful for category pages)
  265.     else
  266.     {
  267.         $pagecount = count($pagelist);
  268.         $pagestr .= 'The following '.$pagecount.' pages belong to '.$lPage.':<br /><br />'."\n";
  269.         $pagestr .= ShowArrayInColumns($pagelist, $lCol, $lClass);
  270.     }
  271.  
  272. }
  273. else
  274. {
  275.     $pagestr .= 'Sorry, no items found for ' . $lPage .'.';
  276. }
  277.  
  278. echo $categorystr;
  279. echo '<br /><br />';
  280. echo $pagestr;
  281. ?>



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