PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/categorylist/pi.categorylist.php

https://github.com/bjornbjorn/categorylist.ee_addon
PHP | 163 lines | 89 code | 38 blank | 36 comment | 18 complexity | e9766f3567cbc8410eed55eead319318 MD5 | raw file
  1. <?php
  2. if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  3. $plugin_info = array(
  4. 'pi_name' => 'Category list',
  5. 'pi_version' => '1.0',
  6. 'pi_author' => 'Bjorn Borresen',
  7. 'pi_author_url' => 'http://bybjorn.com/',
  8. 'pi_description' => 'Will return a ul/li nested list of all categories',
  9. 'pi_usage' => Categorylist::usage()
  10. );
  11. /**
  12. * Memberlist Class
  13. *
  14. * @package ExpressionEngine
  15. * @category Plugin
  16. * @author Bjorn Borresen
  17. * @copyright Copyright (c) 2009, Bjorn Borresen
  18. * @link http://ee.bybjorn.com/categorylist
  19. */
  20. class Categorylist
  21. {
  22. var $return_data = "";
  23. // --------------------------------------------------------------------
  24. /**
  25. * Get HTML
  26. *
  27. * @param $arr
  28. * @param $default_css_class
  29. * @param $current_css_class
  30. * @param $path
  31. * @param $current_cat_url_title
  32. * @param $open_html
  33. * @param $ul_class_children
  34. * @param $level
  35. */
  36. function getCategoryHTML( $arr, $default_css_class, $current_css_class, $path, $current_cat_url_title, $open_html, $ul_class_children, $enclose_in_ul, $level = 0 )
  37. {
  38. $html = $open_html;
  39. foreach($arr as $category) {
  40. $css_class = ($category['cat_url_title'] == $current_cat_url_title ? $current_css_class : $default_css_class);
  41. $html .= '<li class="'.$css_class.'"><a href="'.$path.$category['cat_url_title'].'"><span>' . $category['cat_name'] . '</span></a>';
  42. if(isset($category['children'])) { // has kids!
  43. $html .= $this->getCategoryHTML( $category['children'], $default_css_class, $current_css_class, $path, $current_cat_url_title, "<ul".(($level > 0 && $ul_class_children != '')?' class="'.$ul_class_children.'">':'>'), $ul_class_children, true, $level+1 );
  44. }
  45. $html .= '</li>';
  46. }
  47. if($enclose_in_ul)
  48. {
  49. $html .= "</ul>";
  50. }
  51. return $html;
  52. }
  53. function Categorylist()
  54. {
  55. $this->EE =& get_instance();
  56. $group_id = intval($this->EE->TMPL->fetch_param('group_id'));
  57. $current_css_class = $this->EE->TMPL->fetch_param('current_css_class');
  58. $default_css_class = $this->EE->TMPL->fetch_param('default_css_class');
  59. $ul_class_children = $this->EE->TMPL->fetch_param('ul_class_children');
  60. $ul_html = $this->EE->TMPL->fetch_param('ul_html');
  61. $addtourl = $this->EE->TMPL->fetch_param('add_to_url');
  62. $enclose_in_ul = ($this->EE->TMPL->fetch_param('enclose_in_ul') != 'no');
  63. $path = $this->EE->functions->create_url(($addtourl != "" ? $addtourl."/" : "") . $this->EE->config->item('reserved_category_word')).'/';
  64. $current_cat_url_title = $this->EE->TMPL->fetch_param('current_cat_url_title');
  65. $home_link = $this->EE->TMPL->fetch_param('home_link');
  66. $query = $this->EE->db->query("SELECT * FROM exp_categories WHERE group_id='{$group_id}' ORDER BY cat_order");
  67. $categories = array();
  68. foreach($query->result_array() as $row)
  69. {
  70. $cat_id_to_cat[ $row['cat_id'] ] = $row;
  71. }
  72. // categories
  73. $categories = array();
  74. foreach($query->result_array() as $row)
  75. {
  76. $cat_id = $row['cat_id'];
  77. $parent_id = $row['parent_id'];
  78. if($parent_id == 0) {
  79. $categories[] =& $cat_id_to_cat[$cat_id];
  80. } else {
  81. if(!isset($cat_id_to_cat[$parent_id]['children'])) {
  82. $cat_id_to_cat[$parent_id]['children'] = array();
  83. }
  84. $cat_id_to_cat[$parent_id]['children'][] =& $cat_id_to_cat[$cat_id];
  85. }
  86. }
  87. $open_html = "";
  88. if($home_link != "") {
  89. $css_class = ($current_cat_url_title == '' ? $current_css_class : $default_css_class);
  90. if($enclose_in_ul)
  91. {
  92. $open_html = "<ul'.($ul_html!=''?' '.$ul_html:'').'>";
  93. }
  94. $open_html .= '<li class="'.$css_class.'"><a href="'.$home_link.'"><span>'.$this->EE->TMPL->fetch_param('home_title').'</span></a></li>';
  95. } else if($enclose_in_ul) {
  96. $open_html = '<ul'.($ul_html!=''?' '.$ul_html:'').'>';
  97. }
  98. $return_html = $this->getCategoryHTML($categories, $default_css_class, $current_css_class, $path, $current_cat_url_title, $open_html, $ul_class_children, $enclose_in_ul, 1);
  99. $this->return_data = $return_html;
  100. }
  101. // --------------------------------------------------------------------
  102. /**
  103. * Usage
  104. *
  105. * This function describes how the plugin is used.
  106. *
  107. * @access public
  108. * @return string
  109. */
  110. // Make sure and use output buffering
  111. function usage()
  112. {
  113. ob_start();
  114. ?>
  115. Will output categories as a nested <ul> / <li class=''> tags
  116. {exp:categorylist group_id="1" default_css_class="cat_item" current_css_class="current_page_item" path="archives"}
  117. <?php
  118. $buffer = ob_get_contents();
  119. ob_end_clean();
  120. return $buffer;
  121. }
  122. // END
  123. }
  124. /* End of file pi.categorylist.php */
  125. /* Location: ./system/expressionengine/third_party/categorylist/pi.categorylist.php */