Revision [6397]

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

 

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



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