plugins/CategoryList.php
changeset 0 70fa9fdd6f25
child 1 eaeb479c52c9
equal deleted inserted replaced
-1:000000000000 0:70fa9fdd6f25
       
     1 <?php
       
     2 /*
       
     3 Plugin Name: Category list
       
     4 Plugin URI: http://enanocms.org/
       
     5 Description: A simple parser hook to display the contents of a category within another page. Syntax is {{Category:<cat name>|sub=(on|off)|pages=(on|off)}}. (Both sub [subcategories] and pages default to on)
       
     6 Author: Dan Fuhry
       
     7 Version: 1.0
       
     8 Author URI: http://enanocms.org/
       
     9 */
       
    10 
       
    11 // attach parser hook
       
    12 $plugins->attachHook('render_wikiformat_pre', 'catlist_parser_hook($text);');
       
    13 
       
    14 function catlist_parser_hook(&$text)
       
    15 {
       
    16   if ( preg_match_all('/\{\{
       
    17                          Category:
       
    18                          ([^|\r\n\a\t]+?)                                              # category name
       
    19                          (\|(?:(?:[a-z0-9_]+)(?:[\s]*)=(?:[\s]*)(?:[^\}\r\n\a\t]+)))   # parameters
       
    20                        \}\}/x', $text, $matches) )
       
    21   {
       
    22     foreach ( $matches[0] as $i => $match )
       
    23     {
       
    24       $cat_name =& $matches[1][$i];
       
    25       $params =& $matches[2][$i];
       
    26       $params = catlist_parse_params($params);
       
    27       
       
    28       $do_subs = ( isset($params['sub']) && $params['sub'] === 'off' ) ? false : true;
       
    29       $do_pages = ( isset($params['pages']) && $params['pages'] === 'off' ) ? false : true;
       
    30       
       
    31       $result = catlist_print_category($cat_name, $do_subs, $do_pages);
       
    32       $text = str_replace($match, $result, $text);
       
    33     }
       
    34   }
       
    35 }
       
    36 
       
    37 function catlist_parse_params($params)
       
    38 {
       
    39   $params = trim($params, '|');
       
    40   $params = explode('|', $params);
       
    41   $return = array();
       
    42   foreach ( $params as $val )
       
    43   {
       
    44     if ( preg_match('/^([a-z0-9_]+)(?:[\s]*)=(?:[\s]*)([^\}\r\n\a\t]+)$/', $val, $match) )
       
    45     {
       
    46       $return[ $match[1] ] = $match[2];
       
    47     }
       
    48   }
       
    49   return $return;
       
    50 }
       
    51 
       
    52 function catlist_print_category($cat_name, $do_subs, $do_pages)
       
    53 {
       
    54   // nicely format and print the category out, then return HTML
       
    55   global $db, $session, $paths, $template, $plugins; // Common objects
       
    56   
       
    57   // make sane the category ID
       
    58   $cat_id = $db->escape(sanitize_page_id($cat_name));
       
    59   
       
    60   // if we're doing both, use the more complicated query
       
    61   if ( $do_subs && $do_pages )
       
    62   {
       
    63     
       
    64     $q = $db->sql_query('SELECT c.page_id, c.namespace, p.name, ( c.namespace = \'Category\' ) AS is_subcategory FROM ' . table_prefix . "categories AS c\n"
       
    65                       . "  LEFT JOIN " . table_prefix . "pages AS p\n"
       
    66                       . "    ON ( p.urlname = c.page_id AND p.namespace = c.namespace )\n"
       
    67                       . "  WHERE c.category_id = '$cat_id'\n"
       
    68                       . "  ORDER BY is_subcategory DESC;");
       
    69   }
       
    70   else
       
    71   {
       
    72     // nice little where clause...
       
    73     if ( $do_subs && !$do_pages )
       
    74     {
       
    75       $where = 'c.namespace = \'Category\'';
       
    76     }
       
    77     else if ( $do_pages && !$do_subs )
       
    78     {
       
    79       $where = 'c.namespace != \'Category\'';
       
    80     }
       
    81     else
       
    82     {
       
    83       // ok, subs = off AND pages = off. some people are dummies.
       
    84       return '';
       
    85     }
       
    86     $q = $db->sql_query('SELECT c.page_id, c.namespace, p.name, ( c.namespace = \'Category\' ) AS is_subcategory FROM ' . table_prefix . "categories AS c\n"
       
    87                       . "  LEFT JOIN " . table_prefix . "pages AS p\n"
       
    88                       . "    ON ( p.urlname = c.page_id AND p.namespace = c.namespace )\n"
       
    89                       . "  WHERE c.category_id = '$cat_id' AND $where\n"
       
    90                       . "  ORDER BY is_subcategory DESC;");
       
    91   }
       
    92   if ( !$q )
       
    93     $db->_die();
       
    94   
       
    95   $html = '';
       
    96   if ( $do_subs && $do_pages )
       
    97   {
       
    98     $html .= '<h3>Subcategories</h3>';
       
    99   }
       
   100   if ( $do_subs )
       
   101   {
       
   102     // LIST: subcategories
       
   103     $html .= '<div class="tblholder">';
       
   104     $html .= '<table border="0" cellspacing="1" cellpadding="4">';
       
   105     $html .= '<tr>';
       
   106     $ticker = 0;
       
   107     $have_subcats = false;
       
   108     $class = 'row1';
       
   109     while ( $row = $db->fetchrow() )
       
   110     {
       
   111       if ( empty($row['is_subcategory']) )
       
   112         break;
       
   113       
       
   114       $have_subcats = true;
       
   115       
       
   116       $ticker++;
       
   117       if ( $ticker == 3 )
       
   118       {
       
   119         $ticker = 0;
       
   120         $html = '</tr><tr>';
       
   121         $class = ( $class == 'row1' ) ? 'row2' : 'row1';
       
   122       }
       
   123       $inner = '<a href="' . makeUrlNS($row['namespace'], $row['page_id'], false, true) . '">' . htmlspecialchars($row['name']) . '</a>';
       
   124       $html .= '<td style="width: 33.3%;" class="' . $class . '">' . $inner . '</td>';
       
   125     }
       
   126     if ( !$have_subcats )
       
   127     {
       
   128       $ticker++;
       
   129       $html .= '<td style="width: 33.3%;" class="' . $class . '">No subcategories.</td>';
       
   130     }
       
   131     // fill in the rest of the table
       
   132     while ( $ticker < 3 )
       
   133     {
       
   134       $ticker++;
       
   135       $html .= '<td style="width: 33.3%;" class="' . $class . '"></td>';
       
   136     }
       
   137     $html .= '</table></div>';
       
   138   }
       
   139   if ( $do_subs && $do_pages )
       
   140   {
       
   141     $html .= '<h3>Pages</h3>';
       
   142   }
       
   143   if ( $do_pages )
       
   144   {
       
   145     // LIST: member pages
       
   146     $html .= '<div class="tblholder">';
       
   147     $html .= '<table border="0" cellspacing="1" cellpadding="4">';
       
   148     $html .= '<tr>';
       
   149     $ticker = 0;
       
   150     $have_pages = false;
       
   151     $class = 'row1';
       
   152     // using do-while because the last row was already fetched if we had to do subcategories
       
   153     do
       
   154     {
       
   155       if ( !$do_subs && !isset($row) )
       
   156         $row = $db->fetchrow();
       
   157       
       
   158       if ( !$row )
       
   159         break;
       
   160       
       
   161       $have_pages = true;
       
   162       
       
   163       $ticker++;
       
   164       if ( $ticker == 3 )
       
   165       {
       
   166         $ticker = 0;
       
   167         $html = '</tr><tr>';
       
   168         $class = ( $class == 'row1' ) ? 'row2' : 'row1';
       
   169       }
       
   170       $inner = '<a href="' . makeUrlNS($row['namespace'], $row['page_id'], false, true) . '">' . htmlspecialchars($row['name']) . '</a>';
       
   171       $html .= '<td style="width: 33.3%;" class="' . $class . '">' . $inner . '</td>';
       
   172     }
       
   173     while ( $row = $db->fetchrow() );
       
   174     
       
   175     if ( !$have_pages )
       
   176     {
       
   177       $ticker++;
       
   178       $html .= '<td style="width: 33.3%;" class="' . $class . '">No pages in this category.</td>';
       
   179     }
       
   180     // fill in the rest of the table
       
   181     while ( $ticker < 3 )
       
   182     {
       
   183       $ticker++;
       
   184       $html .= '<td style="width: 33.3%;" class="' . $class . '"></td>';
       
   185     }
       
   186     $html .= '</table></div>';
       
   187   }
       
   188   return $html;
       
   189 }