Revision [6333]

This is an old revision of PageAndCategoryDivisionInACategory made by NilsLindenberg on 2005-02-25 19:23:46.

 

Division between Pages and categories in a Category


25/02/2005: new code which uses jws enhanced code. still needs some adjustments in details.

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



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