PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/pi.entry_category_count.php

https://github.com/erikreagan/er.entry_category_count.ee_addon
PHP | 127 lines | 71 code | 34 blank | 22 comment | 9 complexity | 3d1c5e95fb5e503b75977efc4e9f4961 MD5 | raw file
  1. <?php
  2. /**
  3. * Entry Category Count
  4. *
  5. * This file must be placed in the
  6. * system/plugins/ folder in your ExpressionEngine installation.
  7. *
  8. * @package EntryCategoryCount
  9. * @version 1.0.1
  10. * @author Erik Reagan http://erikreagan.com
  11. * @copyright Copyright (c) 2010 Erik Reagan
  12. * @see http://erikreagan.com/projects/entry-category-count/
  13. * @license http://creativecommons.org/licenses/by-sa/3.0/ Attribution-Share Alike 3.0 Unported
  14. */
  15. $plugin_info = array(
  16. 'pi_name' => 'Entry Category Count',
  17. 'pi_version' => '1.0.1',
  18. 'pi_author' => 'Erik Reagan',
  19. 'pi_author_url' => 'http://erikreagan.com',
  20. 'pi_description' => 'Returns the number of categories assigned to any entry',
  21. 'pi_usage' => Entry_category_count::usage()
  22. );
  23. class Entry_category_count
  24. {
  25. var $return_data = "";
  26. function Entry_category_count()
  27. {
  28. global $DB, $TMPL;
  29. $entry_id = ($TMPL->fetch_param('entry_id') != '') ? $TMPL->fetch_param('entry_id') : '0';
  30. $TMPL->log_item('Entry Category Count Plugin: Entry: '.$entry_id);
  31. $show_group = ($TMPL->fetch_param('show_group') != '') ? $TMPL->fetch_param('show_group') : FALSE ;
  32. $TMPL->log_item('Entry Category Count Plugin: Show Group: '.$show_group);
  33. $show = ($TMPL->fetch_param('show') != '') ? $TMPL->fetch_param('show') : FALSE ;
  34. $TMPL->log_item('Entry Category Count Plugin: Show Categories: '.$show);
  35. // Build our query now
  36. $query = "SELECT COUNT(*) FROM exp_category_posts WHERE entry_id = '".$DB->escape_str($entry_id)."'";
  37. if($show)
  38. {
  39. $query .= " AND cat_id ";
  40. $cat_ids = str_replace('|',',',$show);
  41. if(strpos($show,'not') !== FALSE)
  42. {
  43. $cat_ids = str_replace('not ','',$cat_ids);
  44. $query .= "NOT ";
  45. }
  46. $query .= "IN ($cat_ids)";
  47. }
  48. if($show_group)
  49. {
  50. $query .= " AND cat_id ";
  51. $group_ids = str_replace('|',',',$show_group);
  52. if(strpos($show_group,'not') !== FALSE)
  53. {
  54. $group_ids = str_replace('not ','',$group_ids);
  55. $query .= "NOT ";
  56. }
  57. $query .= "IN (SELECT cat_id FROM exp_categories WHERE group_id IN ($group_ids))";
  58. }
  59. $TMPL->log_item('Entry Category Count Plugin: Query: '.$query);
  60. $category_count = $DB->query($query);
  61. $this->return_data = $category_count->result[0]['COUNT(*)'];
  62. }
  63. /**
  64. * Plugin Usage
  65. */
  66. // This function describes how the plugin is used.
  67. // Make sure and use output buffering
  68. function usage()
  69. {
  70. ob_start();
  71. ?>
  72. This very simple plugin just returns the total categories assigned to the entry based on the entry_id parameter. Here are a few sample uses
  73. Total Assigned Categories: {exp:entry_category_count entry_id="{entry_id}"}
  74. {if {exp:entry_category_count entry_id="{entry_id}"} > 1}
  75. There are multiple categories assigned to this entry
  76. {/if}
  77. {if {exp:entry_category_count entry_id="{entry_id}"} == 0}
  78. There are no categories assigned to this entry
  79. {/if}
  80. ============
  81. You can hardcode in an entry ID if you like
  82. {if {exp:entry_category_count entry_id="124"} > 1}
  83. Your very awesome code here
  84. {/if}
  85. <?php
  86. $buffer = ob_get_contents();
  87. ob_end_clean();
  88. return $buffer;
  89. }
  90. // END
  91. }
  92. /* End of file pi.entry_category_count.php */
  93. /* Location: ./system/plugins/pi.er_entry_category_count.php */