source: branches/1.0/content/plugins/categories/categories.php @ 1186

Revision 1186, 15.0 KB checked in by shibuya246, 3 years ago (diff)

[Branch] add "/" to end of meta description and keywords, W3C valid

Line 
1<?php
2/**
3 * name: Categories
4 * description: Enables categories for posts
5 * version: 1.3
6 * folder: categories
7 * class: Categories
8 * type: categories
9 * requires: sb_base 0.1, submit 1.9, category_manager 0.7
10 * hooks: sb_base_theme_index_top, header_include, pagehandling_getpagename, sb_base_functions_preparelist, sb_base_show_post_author_date, header_end, breadcrumbs, header_meta
11 * author: Nick Ramsay
12 * authorurl: http://hotarucms.org/member.php?1-Nick
13 *
14 * PHP version 5
15 *
16 * LICENSE: Hotaru CMS is free software: you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation, either version 3 of
19 * the License, or (at your option) any later version.
20 *
21 * Hotaru CMS is distributed in the hope that it will be useful, but WITHOUT
22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 * FITNESS FOR A PARTICULAR PURPOSE.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with Hotaru CMS. If not, see http://www.gnu.org/licenses/.
27 *
28 * @category  Content Management System
29 * @package   HotaruCMS
30 * @author    Nick Ramsay <admin@hotarucms.org>
31 * @copyright Copyright (c) 2009, Hotaru CMS
32 * @license   http://www.gnu.org/copyleft/gpl.html GNU General Public License
33 * @link      http://www.hotarucms.org/
34 */
35
36class Categories
37{
38    /**
39     * Determine if we are filtering to a category
40     * Categories might be numeric, e.g. category=3 or safe names, e.g. category=news_and_business
41     * We also test for urls like domain.com/News/ where "News" is a category
42     */
43    public function sb_base_theme_index_top($h)
44    {
45        // if there's a "category" key in the url...
46       
47        if ($h->cage->get->keyExists('category'))
48        {
49            $category = $h->cage->get->noTags('category');
50            if (is_numeric($category)) {
51                // category is numeric
52                $h->vars['category_id'] = $category;
53                $h->vars['category_name'] = $h->getCatName($category);
54                $h->vars['category_safe_name'] = $h->getCatSafeName($category);
55                $h->pageTitle = $h->vars['category_name'];
56            } else {
57                // category should be a safe name
58                $h->vars['category_id'] = $h->getCatId($category);
59                $h->vars['category_name'] = $h->getCatName(0, $category);
60                $h->vars['category_safe_name'] = $category;
61                $h->pageTitle = $h->vars['category_name'];
62            }
63            if (!$h->pageName) { $h->pageName = 'index'; }
64            $h->subPage = 'category';
65            $h->pageType = 'list';
66        }
67        elseif (!$h->pageType)  // only do this if we don't know the pageType yet...
68        {
69            if ($h->pageName == 'all') { return false; } // when sorting to "all", we don't want to filter to the "all" category!
70
71            /*  if $h->pageName is set, then there must be an odd number of query vars where
72                the first one is the page name. Let's see if it's a category safe name... */
73            $sql = "SELECT category_id, category_name FROM " . TABLE_CATEGORIES . " WHERE category_safe_name = %s LIMIT 1";
74            $exists = $h->db->get_row($h->db->prepare($sql, $h->pageName));
75            if ($exists) {
76                $h->vars['category_id'] = $exists->category_id;
77                $h->vars['category_name'] = $exists->category_name;
78                $h->vars['category_safe_name'] = $h->pageName;
79                $h->pageTitle = $h->vars['category_name'];
80                $h->subPage = 'category';  // overwrite the current pageName which is the category name
81                $h->pageType = 'list';
82            }
83        }
84    }
85   
86   
87    /**
88     * Include CSS and JavaScript files for this plugin
89     */
90    public function header_include($h)
91    {
92        // include a files that match the name of the plugin folder:
93        $h->includeJs('categories', 'suckerfish');
94        $h->includeCss();
95    }
96   
97   
98   
99    /**
100     * Checks if url query string is /category_name/post_name/
101     *
102     * @return bool
103     *
104     * Only used for friendly urls. This is necessary because if a url
105     * is /people/top-10-longest-beards/ there's no actual mention of "category" there!
106     */
107    public function pagehandling_getpagename($h)
108    {
109        // Can't get keys from the url with Inspekt, so must get the whole query string instead.
110        $query_string = $h->cage->server->sanitizeTags('QUERY_STRING');
111
112        // no query string? exit...
113        if (!$query_string) { return false; }
114       
115        // we actually only need the first pair, so won't bother looping.
116        $query_string = preg_replace('/&amp;/', '&', $query_string);
117        $pairs = explode('&', $query_string);
118       
119        // no pairs or equal sign? exit...
120        if (!$pairs[0] || !strpos($pairs[0], '=')) { return false; }
121       
122        list($key, $value) = explode('=', $pairs[0]);
123       
124        // no key or no value? exit...
125        if (!$key || !$value) { return false; }
126
127        $sql = "SELECT category_id FROM " . TABLE_CATEGORIES . " WHERE category_safe_name = %s LIMIT 1";
128        $exists = $h->db->get_var($h->db->prepare($sql, $key));
129       
130        // no category? exit...
131        if (!$exists) { return false; }
132       
133        // Now we know that $key is a category so $value must be the post name. Go get the post_id...
134        $h->post->id = $h->post->isPostUrl($h, $value);
135       
136        // no post? exit...
137        if (!$h->post->id) { return false; }
138       
139        $h->post->readPost($h, $h->post->id);
140        $h->pageName = $h->post->url; // slug for page title
141        $h->pageTitle = $h->post->title;
142        $h->pageType = 'post';
143        return true;
144    }
145   
146   
147    /**
148     * Also changes meta when browsing a category page
149     */
150    public function header_meta($h)
151    {   
152        if ($h->subPage == 'category')
153        {
154            $cat_meta = $h->getCatMeta($h->vars['category_id']);
155           
156            if ($cat_meta->category_desc) {
157                echo '<meta name="description" content="' . urldecode($cat_meta->category_desc) . '" />' . "\n";
158            } else {
159                echo '<meta name="description" content="' . $h->lang['header_meta_description'] . '" />' . "\n";  // default meta tags
160            }
161           
162            if ($cat_meta->category_keywords) {
163                echo '<meta name="keywords" content="' . urldecode($cat_meta->category_keywords) . '" />' . "\n";
164            } else {
165                echo '<meta name="description" content="' . $h->lang['header_meta_keywords'] . '" />' . "\n";  // default meta tags
166            }
167
168            return true;
169        }
170    }
171   
172   
173    /**
174     * Read category settings
175     */
176    public function post_read_post_1()
177    {
178        //categories
179        if (($this->getSetting('submit_categories') == 'checked')
180            && ($this->isActive())) {
181            $h->post->vars['useCategories'] = true;
182        } else {
183            $h->post->vars['useCategories'] = false;
184        }
185    }
186
187     /* ********************************************************************
188     * *********************************************************************
189     * ******************* FUNCTIONS FOR SHOWING POSTS *********************
190     * *********************************************************************
191     * ****************************************************************** */
192   
193    /**
194     * Gets a category from the url and sets the filter for get_posts
195     *
196     * @return bool
197     */
198    public function sb_base_functions_preparelist($h)
199    {
200        if ($h->subPage == 'category')
201        {
202            // When a user clicks a parent category, we need to show posts from all child categories, too.
203            // This only works for one level of sub-categories.
204            $filter_string = '(post_category = %d';
205            $values = array($h->vars['category_id']);
206            $parent = $h->getCatParent($h->vars['category_id']);
207            if ($parent == 1) {
208                $children = $h->getCatChildren($h->vars['category_id']);
209                if ($children) {
210                    foreach ($children as $child_id) {
211                        $filter_string .= ' || post_category = %d';
212                        array_push($values, $child_id->category_id);
213                    }
214                }
215            }
216            $filter_string .= ')';
217            $h->vars['filter'][$filter_string] = $values;
218            $h->vars['filter']['post_archived = %s'] = 'N'; // don't include archived posts
219        }
220    }
221   
222   
223    /**
224     * Shows categories before post title in breadcrumbs
225     */
226    public function breadcrumbs($h)
227    {
228        $crumbs = '';
229               
230        if ($h->subPage == 'category') // the pageType is "list"
231        {
232            $parent_id = $h->getCatParent($h->vars['category_id']);
233            if ($parent_id > 1) {
234                $parent_name = $h->getCatName($parent_id);
235                $parent_name = stripslashes(htmlentities($parent_name, ENT_QUOTES, 'UTF-8'));
236                $crumbs .= "<a href='" . $h->url(array('category'=>$parent_id)) . "'>";
237                $crumbs .= $parent_name . "</a> &raquo; \n";
238            }
239   
240            $crumbs .= "<a href='" . $h->url(array('category'=>$h->vars['category_id'])) . "'>\n";
241            $crumbs .= $h->vars['category_name'] . "</a>\n ";
242
243            $crumbs .= $h->rssBreadcrumbsLink('', array('category'=>$h->vars['category_id']));
244        }
245        elseif ($h->pageType == 'post') // the pageName is the post slug (post_url)
246        {
247            $parent_id = $h->getCatParent($h->post->category);
248            if ($parent_id > 1) {
249                $parent_name = $h->getCatName($parent_id);
250                $parent_name = stripslashes(htmlentities($parent_name, ENT_QUOTES, 'UTF-8'));
251                $crumbs .= "<a href='" . $h->url(array('category'=>$parent_id)) . "'>";
252                $crumbs .= $parent_name . "</a> &raquo; \n";
253            }
254   
255            $crumbs .= "<a href='" . $h->url(array('category'=>$h->post->category)) . "'>\n";
256            $crumbs .= $h->getCatName($h->post->category) . "</a> &raquo; \n";
257            $crumbs .= "<a href='" . $h->url(array('page'=>$h->post->id)) . "'>" . $h->post->title . "</a>\n";
258        }
259       
260        if ($crumbs) { return $crumbs; } else { return false; }
261    }
262   
263   
264    /**
265     * Shows category in each post
266     */
267    public function sb_base_show_post_author_date($h)
268    {
269        if ($h->post->category != 1) {
270
271            $cat_name = $h->getCatName($h->post->category);
272           
273            echo " " . $h->lang["sb_base_post_in"] . " ";
274            echo "<a href='" . $h->url(array('category'=>$h->post->category)) . "'>" . $cat_name . "</a>\n";
275        }       
276    }
277
278
279     /* ********************************************************************
280     * *********************************************************************
281     * ************************* EXTRA FUNCTIONS ***************************
282     * *********************************************************************
283     * ****************************************************************** */
284
285
286    /**
287     * Category Bar - shows categories as a drop-down menu
288     *
289     * Adapted from:
290     * @link http://www.cssnewbie.com/easy-css-dropdown-menus/
291     */
292    public function header_end($h)
293    {
294        $output = '';
295       
296        // get all top-level categories
297        $sql    = "SELECT * FROM " . TABLE_CATEGORIES . " WHERE category_id != %d AND category_parent = %d ORDER BY category_order ASC";
298        $query = $h->db->prepare($sql, 1, 1);
299        $h->smartCache('on', 'categories', 60, $query); // start using cache
300        $categories = $h->db->get_results($query);
301       
302        if($categories)
303        {
304            foreach ($categories as $category)
305            {
306                $parent = $category->category_id;
307               
308                // Check for children
309                $sql = "SELECT count(*) FROM " . TABLE_CATEGORIES . " WHERE category_parent = %d";
310                $countchildren = $h->db->get_var($h->db->prepare($sql, $parent));
311                   
312                // If children, go to a recursive function to build links for all children of this top-level category
313                if ($countchildren) {
314                    $depth = 1;
315                    $output = $this->buildMenuBar($h, $category, $output, $parent, $depth);
316                    $output .= "</ul>";
317                } else { 
318                    $output = $this->categoryLink($h, $category, $output);
319                }
320               
321                $output .= "</li>\n";
322            }
323           
324            // Output the category bar
325            $h->vars['output'] = $output;   
326            $h->displayTemplate('category_bar');
327        }
328       
329        $h->smartCache('off'); // stop using cache
330    }
331   
332
333    /**
334     * Build Category Menu Bar - recursive function
335     *
336     * @param array $category 
337     * @param string $output 
338     * @param int $parent
339     * @return string $output
340     */
341    public function buildMenuBar($h, $category, $output, $parent, $depth)
342    {
343        $output = $this->categoryLink($h, $category, $output);
344
345        $sql = "SELECT count(*) FROM " . TABLE_CATEGORIES . " WHERE category_parent = %d";
346        $countchildren = $h->db->get_var($h->db->prepare($sql, $category->category_id));
347
348        if ($countchildren) {
349            $output .=  "<ul class='children'>\n";
350           
351            $sql = "SELECT * FROM " . TABLE_CATEGORIES . " WHERE category_parent = %d ORDER BY category_order ASC";
352            $children = $h->db->get_results($h->db->prepare($sql, $category->category_id));
353           
354            $depth++;
355            foreach ($children as $child) {
356                if ($depth < 3) {
357                    $output = $this->buildMenuBar($h, $child, $output, $child->category_id, $depth);
358                }
359            }
360            $output .= "";
361            return $output;
362        } 
363        $output .= "</li>";
364        return $output;
365    }
366
367
368    /**
369     * HTML link for each category
370     *
371     * @param array $category 
372     * @param string $output 
373     * @return string $output
374     */
375    public function categoryLink($h, $category, $output)
376    {
377        if (FRIENDLY_URLS == "true") { 
378            $link = $category->category_safe_name; 
379        } else {
380            $link = $category->category_id;
381        }
382   
383        $category = stripslashes(html_entity_decode(urldecode($category->category_name), ENT_QUOTES,'UTF-8'));
384        $output .= '<li><a href="' . $h->url(array('category'=>$link)) .'">' . $category . "</a>\n";
385       
386        return $output;
387    }
388
389}
390
391?>
Note: See TracBrowser for help on using the repository browser.