0
|
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 |
if ( $ticker == 3 )
|
|
117 |
{
|
|
118 |
$ticker = 0;
|
1
|
119 |
$html .= '</tr><tr>';
|
0
|
120 |
$class = ( $class == 'row1' ) ? 'row2' : 'row1';
|
|
121 |
}
|
1
|
122 |
$ticker++;
|
|
123 |
|
0
|
124 |
$inner = '<a href="' . makeUrlNS($row['namespace'], $row['page_id'], false, true) . '">' . htmlspecialchars($row['name']) . '</a>';
|
|
125 |
$html .= '<td style="width: 33.3%;" class="' . $class . '">' . $inner . '</td>';
|
|
126 |
}
|
|
127 |
if ( !$have_subcats )
|
|
128 |
{
|
|
129 |
$ticker++;
|
|
130 |
$html .= '<td style="width: 33.3%;" class="' . $class . '">No subcategories.</td>';
|
|
131 |
}
|
|
132 |
// fill in the rest of the table
|
|
133 |
while ( $ticker < 3 )
|
|
134 |
{
|
|
135 |
$ticker++;
|
|
136 |
$html .= '<td style="width: 33.3%;" class="' . $class . '"></td>';
|
|
137 |
}
|
|
138 |
$html .= '</table></div>';
|
|
139 |
}
|
|
140 |
if ( $do_subs && $do_pages )
|
|
141 |
{
|
|
142 |
$html .= '<h3>Pages</h3>';
|
|
143 |
}
|
|
144 |
if ( $do_pages )
|
|
145 |
{
|
|
146 |
// LIST: member pages
|
|
147 |
$html .= '<div class="tblholder">';
|
|
148 |
$html .= '<table border="0" cellspacing="1" cellpadding="4">';
|
|
149 |
$html .= '<tr>';
|
|
150 |
$ticker = 0;
|
|
151 |
$have_pages = false;
|
|
152 |
$class = 'row1';
|
|
153 |
// using do-while because the last row was already fetched if we had to do subcategories
|
|
154 |
do
|
|
155 |
{
|
|
156 |
if ( !$do_subs && !isset($row) )
|
|
157 |
$row = $db->fetchrow();
|
|
158 |
|
|
159 |
if ( !$row )
|
|
160 |
break;
|
|
161 |
|
|
162 |
$have_pages = true;
|
|
163 |
|
|
164 |
if ( $ticker == 3 )
|
|
165 |
{
|
|
166 |
$ticker = 0;
|
1
|
167 |
$html .= '</tr><tr>';
|
0
|
168 |
$class = ( $class == 'row1' ) ? 'row2' : 'row1';
|
|
169 |
}
|
1
|
170 |
$ticker++;
|
|
171 |
|
0
|
172 |
$inner = '<a href="' . makeUrlNS($row['namespace'], $row['page_id'], false, true) . '">' . htmlspecialchars($row['name']) . '</a>';
|
|
173 |
$html .= '<td style="width: 33.3%;" class="' . $class . '">' . $inner . '</td>';
|
|
174 |
}
|
|
175 |
while ( $row = $db->fetchrow() );
|
|
176 |
|
|
177 |
if ( !$have_pages )
|
|
178 |
{
|
|
179 |
$ticker++;
|
|
180 |
$html .= '<td style="width: 33.3%;" class="' . $class . '">No pages in this category.</td>';
|
|
181 |
}
|
|
182 |
// fill in the rest of the table
|
|
183 |
while ( $ticker < 3 )
|
|
184 |
{
|
|
185 |
$ticker++;
|
|
186 |
$html .= '<td style="width: 33.3%;" class="' . $class . '"></td>';
|
|
187 |
}
|
|
188 |
$html .= '</table></div>';
|
|
189 |
}
|
|
190 |
return $html;
|
|
191 |
}
|