Revision [7092]

This is an old revision of PageAndCategoryDivisionInACategory made by NilsLindenberg on 2005-04-04 10:19:12.

 

Division between Pages and categories in a Category


04/04/2005:version 2.0 (see below).

this version builds on the beta-version from JavaWomen (running on this wiki) and has in total the following cchanges to the official action:


Todo:


Explanations

The new search-method uses two queries instead of one: one search for subcategories (searching for the category-action) and another for pages (searching for the new link version) belonging to a category (they only load the name, since we know from the search if its a category or not).

This allows any page to become a category, their names don't have to start with "Category" anymore. For this reason, i took the "forcecat" param out (if you put the action, for example, on the SandBox without "categorylinking" any page it will simply show that nothing could be found).


Comments?



Installation


Three things have to be changed:

1. addition to formatters/wakka.php

right before
                // forced links
                // \S : any character that is not a whitespace character
                // \s : any whitespace character


add the following:
                //links to category pages
                else if (preg_match("/^\[\[\(cat\)(\S+)(\s(.+))*\]\]$/s", $thing, $matches))
                {
                        list(, $categorylink, $text) = $matches;
                        if ($categorylink)
                        {
                            if (!$help) $help = $categorylink;
                            return $result.$wakka->Link($categorylink, "", $text);
                        }
                        else
                        {
                            return "";
                        }
                }


2. library/common/showarray.php

The following file has to be saved as library/common/showarray.php:
  1. <?php
  2. /**
  3.  * Sorts a given array in a given number of columns.
  4.  *
  5.  * @package     Library
  6.  * @subpackage  Common
  7.  * @name        ShowArray
  8.  *
  9.  * @author      {@link http://wikka.jsnx.com/JavaWoman JavaWoman} (table-less columns)
  10.  * @author      {@link http://wikka.jsnx.com/NilsLindenberg NilsLindenberg} (function and "divison" of the array)
  11.  * @copyright   Copyright © 2004,
  12.  * @license     http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  13.  * @since       Wikka 1.0.0
  14.  *
  15.  * @input       array       $array  necessary: the data to be shown
  16.  * @input       integer $colnumber  optional: number of colums; default: 0.
  17.  * @input       string  $class  optional: class(es) to determine styling of the output list of table; default: none.
  18.  * @output      list of data from an array, formatted as in columns
  19.  *
  20.  * @todo        - possible? use a single list also for columns, using CSS to visually split up into columns - JW 2005-01-19
  21.  */
  22.  
  23. function ShowArrayInColumns($array, $colnumber=1, $class="")
  24. {
  25.     $str = "";
  26.     if (is_array($array))
  27.     {
  28.         $entries = count($array);
  29.         $width = (int) (100 / $colnumber);
  30.         $lines = 0;
  31.         $a = 0;
  32.        
  33.         $str .= '<div'.$class.'>'."\n";
  34.                
  35.         //how many lines with an entry in every column do we have?
  36.         while ($entries / $colnumber > 1)
  37.         {
  38.             $lines++;
  39.             $entries = $entries - $colnumber;
  40.         }
  41.        
  42.         //prepare output
  43.         for ($i=0;$i<$colnumber;$i++)
  44.         {
  45.             $str .='    <div style="width: '.$width.'%; float: left;">'."\n";
  46.             for ($j=0;$j<$lines;$j++)
  47.             {
  48.                 $str .= '       '.$array[$a].'<br />'."\n";
  49.                 $a++;  
  50.             }
  51.            
  52.             //the rest of the entries (less then the number of cols)
  53.             if ($entries)
  54.             {
  55.                 $str .= '       '.$array[$a].'<br />'."\n";
  56.                 $entries--;
  57.                 $a++;  
  58.             }
  59.             $str .="    </div>\n";
  60.    
  61.         }
  62.         $str .= '</div><br  style="clear: both;">'."\n";
  63.         return ($str);
  64.     }
  65.     $str .= 'The data delivered to the function ShowArrayInColumns was no array.';
  66.     return ($str);
  67. }  
  68.  
  69. /**
  70.  * Sorts a given array as a list.
  71.  *
  72.  * @package     Library
  73.  * @subpackage  Common
  74.  * @name        ShowArray
  75.  *
  76.  * @author      {@link http://wikka.jsnx.com/JsnX JsnX} (list)
  77.  * @author      {@link http://wikka.jsnx.com/NilsLindenberg NilsLindenberg} (function and "divison" of the array)
  78.  * @copyright   Copyright © 2004,
  79.  * @license     http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  80.  * @since       Wikka 1.0.0
  81.  *
  82.  * @input       array       $array  necessary: the data to be shown
  83.  * @input       string  $class  optional: class(es) to determine styling of the output list of table; default: none.
  84.  * @output      list of data from an array, formatted as a list
  85.  *
  86.  */
  87.  
  88. function ShowArrayAsList($array,$class="")
  89. {
  90.     $str = "";
  91.     if (is_array($array))
  92.     {
  93.         $entries = count($array);
  94.         $str .= '<div'.$class.'>'."\n";
  95.         $str .= "<ul>\n";
  96.         for ($i=0;$i<$entries;$i++)
  97.         {
  98.             $str .= '   <li>'.$array[$i].'</li>';
  99.         }
  100.         $str .= "</ul>\n</div>\n";
  101.         return ($str);
  102.     }
  103.     $str .= "The data delivered to the function ShowArrayAsList was no array.";
  104.     return ($str);
  105. }
  106. ?>


3. Replacement of actions/category.php

<?php
/**
 * Generates a list of pages belonging to the specified or top-level category.
 *
 * If no category page is specified, the current page is assumed to be a category.
 *
 * Template pages (page name ending with 'Template') may contain category names; they are filtered
 * out automatically unless 'inctpl' is set to 1. One exception: pagenames that start with
 * 'Category' and end with 'Template' are considered a proper category so templates can themselves
 * be categorized.
 *
 * Note: the list view ('compact' = 1) is nice for a sidebar while the columnar view
 * ('compact' = 0) is more suited for a category page.
 *
 * Syntax:
 * {{category [inctpl="0|1"] [page="categoryname"] [col="n"] [compact="0|1"] [class="class"] [show="pages|categories|all"]}}
 *
 * @package      Actions
 * @subpackage   SystemContent
 * @name             Category
 *
 * @author       {@link http://wikka.jsnx.com/JsnX JsnX}
 * @author       {@link http://wikka.jsnx.com/JavaWoman JavaWoman} (rewrite with filtering and table-less columns)
 * @author       {@link http://wikka.jsnx.com/NilsLindenberg NilsLindenberg} (seperation of categories, functions for output)
 * @copyright    Copyright © 2004, Jason Tourtelotte
 * @license      http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
 * @since        Wikka 1.0.0
 * @version      2.0
 *
 * @input           integer    $inctpl   optional: include "template" pages (1) or not (0); default: 0.
 * @input           integer    $page     optional: category to list members of; default: current page or CategoryCategory
 * @input           integer    $col      optional: number of columns for table; default: 1
 * @input           integer    $compact  optional: use table (0) or list (1); default: 0
 * @input           integer    $class    optional: class(es) to determine styling of the output list of table
 * @input           string     $show     optional: show "pages" "categories" or "all"; default: all
 * @output          list of categories and/or pages belonging to the specified or top-level category,
 *               formatted as a list or in columns
 *
 * @uses        LoadAll()
 * @uses        Link()
 * @todo        - possible? use a single list also for columns, using CSS to visually split up into columns - JW 2005-01-19
 */


//including the needed functions
require_once 'library/common/showarray.php'; #the two functions showing the output in a list/ in columns

// set defaults
$lIncTpl    = FALSE;            # do not show template pages or treat a template as a category
$lPage    = $this->tag;        # current page is default category
$lCol        = 1;                # one column for table
$lCompact    = FALSE;            # use table, not list
$lClass    = '';                # no class
$lShow    = 'all';            # show pages and categories
$lOutput = '';                 # the final output

// get parameters
if (is_array($vars))
{
    foreach ($vars as $param => $value)
    {
        switch ($param)
        {
            case 'inctpl':
                if ($value) $lIncTpl = TRUE;
                break;
            case 'page':
                if ($this->existsPage($value)) $lPage = $value;
                break;
            case 'col':
                if ($value === (string)(int)$value && (int)$value > 0) $lCol = (int)$value;
                break;
            case 'compact':
                if ($value) $lCompact = TRUE;
                break;
            case 'class':
                if ('' != $value) $lClass = $value;
                break;
            case 'show':
                if ($value == 'pages' || $value == 'categories') $lShow = $value;
                break;
        }
    }
}

// filter WHICH category we (may) show the content OF
if ($lPage == '/')
{
    $lPage = 'CategoryCategory';
}
if (!$lIncTpl && preg_match('/Template$/',$lPage))
{
    if (!preg_match('/^Category/',$lPage)) $lPage = 'CategoryCategory';    # exception for a category that contains templates
}

//searching for categories belonging to the requested category, if necessary, and preparing the output
if ($lShow == 'all' || $lShow == 'categories')
{
    $lCategorySearchphrase = "SELECT tag FROM ".$this->config['table_prefix']."pages WHERE latest = 'Y' AND body LIKE '%[[(cat)".$lPage."%' AND body LIKE '%{{category%'";
    $categoryresults = $this->LoadAll($lCategorySearchphrase);
    //print_r($categoryresults); //DEBUG
    if ($categoryresults)
    {
         $categorylist = array();

         // filter what we show AS content of the requested category
         foreach ($categoryresults as $ccategory)
         {
             // do not list top-level category as member
             if ('CategoryCategory' == $ccategory['tag'])
             {
                 continue;
             }
             // do not list requested category as member
             if ($ccategory['tag'] == $lPage)
             {
                 continue;
             }
             // unless inctpl is set, do not list template pages
             elseif (!$lIncTpl && preg_match('/Template$/', $ccategory['tag']))
             {
                 // if the requested category ($lPage) is a template category, we do list its contents;
                 // otherwise this must indeed be a template so we don't list it.
                 if ( ! (preg_match('/^Category.*?Template$/',$lPage) || preg_match('/^Category/',$cpage['tag'])) )
                 {
                    continue;
                 }
             }

             // we have a valid result: add to the list
             if ($lCompact)
             {
                 $categorylist[] = $this->Link($ccategory['tag'],'',preg_replace( "/Category/", "",$ccategory['tag']));
             }
             else
             {
                 $categorylist[] = $this->Link($ccategory['tag'],'',preg_replace( "/Category/", "",$ccategory['tag']));
             }
         }
         sort($categorylist);

         // make simple list (useful for sidebar)
         if ($lCompact)
         {
             $lOutput .= ShowArrayAsList($categorylist,$lClass);
         }
         // make columnar overview (useful for category pages)
         else
         {
             $categorycount = count($categorylist);
             $lOutput .= 'The following '.$categorycount.' categories belong to '.$lPage.':<br /><br />'."\n"; #i18n
             $lOutput .= ShowArrayInColumns($categorylist, $lCol, $lClass);
         }
    }
    else
    {
        $lOutput .= 'No categories found for '.$lPage.".\n"; #i18n
    }
}

//if necessary, a division between the categories and the pages
if ($lShow == all) $lOutput .= '<br /><br />';

//searching for pages belonging to the requested category, if necessary, and preparing the output
if ($lShow == 'all' || $lShow == 'pages')
{
    $lPageSearchphrase = "SELECT tag FROM ".$this->config['table_prefix']."pages WHERE latest = 'Y' and body LIKE '%[[(cat)".$lPage."%' AND body NOT LIKE '%{{category%'";
    $pageresults = $this->LoadAll($lPageSearchphrase);
    //print_r($pageresults); //DEBUG
    if ($pageresults)
    {
         $pagelist = array();

         // filter what we show AS content of the requested category
         foreach ($pageresults as $cpage)
         {
             // do not list requested category as member
             if ($cpage['tag'] == $lPage)
             {
                 continue;
             }
             // unless inctpl is set, do not list template pages
             elseif (!$lIncTpl && preg_match('/Template$/', $cpage['tag']))
             {
                 // if the requested category ($lPage) is a template category, we do list its contents;
                 // otherwise this must indeed be a template so we don't list it.
                 if ( ! preg_match('/^Category.*?Template$/',$lPage))
                 {
                    continue;
                 }
             }

             // we have a valid result: add to the list
             if ($lCompact)
             {
                 $pagelist[] = $this->Link($cpage['tag'],'',$cpage['tag']);
             }
             else
             {
                 $pagelist[] = $this->Link($cpage['tag']);
             }
         }
         sort($pagelist);

         // make simple list (useful for sidebar)
         if ($lCompact)
         {
             $lOutput .= ShowArrayAsList($pagelist,$lClass);
         }
         // make columnar overview (useful for category pages)
         else
         {
             $pagecount = count($pagelist);
             $lOutput .= 'The following '.$pagecount.' pages belong to '.$lPage.':<br /><br />'."\n"; #i18n
             $lOutput .= ShowArrayInColumns($pagelist, $lCol, $lClass);
         }
    }
    else
    {
        $lOutput .= 'No pages found for '.$lPage.".\n"; #i18n
    }
}

echo $lOutput;
?>



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