0
|
1 |
<?php
|
|
2 |
/*
|
|
3 |
Plugin Name: Category list
|
|
4 |
Plugin URI: http://enanocms.org/
|
3
|
5 |
Description: A simple parser hook to display the contents of a category within another page. Syntax is {{CategoryContents:<cat name>|sub=(on|off)|pages=(on|off)}}. (Both sub [subcategories] and pages default to on)
|
0
|
6 |
Author: Dan Fuhry
|
|
7 |
Version: 1.0
|
|
8 |
Author URI: http://enanocms.org/
|
|
9 |
*/
|
|
10 |
|
|
11 |
// attach parser hook
|
2
|
12 |
$plugins->attachHook('render_wikiformat_veryearly', 'catlist_parser_hook($text);');
|
0
|
13 |
|
|
14 |
function catlist_parser_hook(&$text)
|
|
15 |
{
|
|
16 |
if ( preg_match_all('/\{\{
|
2
|
17 |
CategoryContents:
|
0
|
18 |
([^|\r\n\a\t]+?) # category name
|
2
|
19 |
(\|(?:(?:[a-z0-9_]+)(?:[\s]*)=(?:[\s]*)(?:[^\}\r\n\a\t]+)))? # parameters
|
0
|
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"
|
5
|
68 |
. " ORDER BY is_subcategory DESC, p.name ASC;");
|
0
|
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"
|
5
|
90 |
. " ORDER BY is_subcategory DESC, p.name ASC;");
|
0
|
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';
|
2
|
109 |
while ( $row = $db->fetchrow($q) )
|
0
|
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 |
}
|
4
|
138 |
$html .= '</tr>';
|
0
|
139 |
$html .= '</table></div>';
|
|
140 |
}
|
|
141 |
if ( $do_subs && $do_pages )
|
|
142 |
{
|
|
143 |
$html .= '<h3>Pages</h3>';
|
|
144 |
}
|
|
145 |
if ( $do_pages )
|
|
146 |
{
|
|
147 |
// LIST: member pages
|
|
148 |
$html .= '<div class="tblholder">';
|
|
149 |
$html .= '<table border="0" cellspacing="1" cellpadding="4">';
|
|
150 |
$html .= '<tr>';
|
|
151 |
$ticker = 0;
|
|
152 |
$have_pages = false;
|
|
153 |
$class = 'row1';
|
|
154 |
// using do-while because the last row was already fetched if we had to do subcategories
|
|
155 |
do
|
|
156 |
{
|
|
157 |
if ( !$do_subs && !isset($row) )
|
|
158 |
$row = $db->fetchrow();
|
|
159 |
|
|
160 |
if ( !$row )
|
|
161 |
break;
|
|
162 |
|
|
163 |
$have_pages = true;
|
|
164 |
|
|
165 |
if ( $ticker == 3 )
|
|
166 |
{
|
|
167 |
$ticker = 0;
|
1
|
168 |
$html .= '</tr><tr>';
|
0
|
169 |
$class = ( $class == 'row1' ) ? 'row2' : 'row1';
|
|
170 |
}
|
1
|
171 |
$ticker++;
|
|
172 |
|
0
|
173 |
$inner = '<a href="' . makeUrlNS($row['namespace'], $row['page_id'], false, true) . '">' . htmlspecialchars($row['name']) . '</a>';
|
|
174 |
$html .= '<td style="width: 33.3%;" class="' . $class . '">' . $inner . '</td>';
|
|
175 |
}
|
|
176 |
while ( $row = $db->fetchrow() );
|
|
177 |
|
|
178 |
if ( !$have_pages )
|
|
179 |
{
|
|
180 |
$ticker++;
|
|
181 |
$html .= '<td style="width: 33.3%;" class="' . $class . '">No pages in this category.</td>';
|
|
182 |
}
|
|
183 |
// fill in the rest of the table
|
|
184 |
while ( $ticker < 3 )
|
|
185 |
{
|
|
186 |
$ticker++;
|
|
187 |
$html .= '<td style="width: 33.3%;" class="' . $class . '"></td>';
|
|
188 |
}
|
4
|
189 |
$html .= '</tr>';
|
0
|
190 |
$html .= '</table></div>';
|
|
191 |
}
|
|
192 |
return $html;
|
|
193 |
}
|