Revision [6386]

This is an old revision of PageAndCategoryDivisionInACategory made by NilsLindenberg on 2005-02-28 14:52:19.

 

Division between Pages and categories in a Category


28/02/2005: new code which fixes counting-bug. First draft of functions for output. Still needs adjustments.

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



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