b2evolution PHP Cross Reference Blogging Systems

Source: /plugins/star_plugin/_star.plugin.php - 184 lines - 4373 bytes - Summary - Text - Print

Description: This file implements the Star renderer plugin for b2evolution Star formatting, like [stars:2.3/5]

   1  <?php
   2  /**

   3   * This file implements the Star renderer plugin for b2evolution

   4   *

   5   * Star formatting, like [stars:2.3/5]

   6   *

   7   * b2evolution - {@link http://b2evolution.net/}

   8   * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}

   9   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}

  10   *

  11   * @package plugins

  12   *

  13   * @version $

  14   */
  15  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  16  
  17  
  18  /**

  19   * @package plugins

  20   */
  21  class star_plugin extends Plugin
  22  {
  23      var $code = 'b2evStar';
  24      var $name = 'Star renderer';
  25      var $priority = 55;
  26      var $version = '5.0.0';
  27      var $group = 'rendering';
  28      var $short_desc;
  29      var $long_desc;
  30      var $help_url = 'http://b2evolution.net/man/technical-reference/renderer-plugins/star-plugin';
  31      var $number_of_installs = 1;
  32  
  33      /*

  34       * Internal vars

  35       */
  36      var $search_text;
  37      var $replace_func;
  38  
  39  
  40      /**

  41       * Init

  42       */
  43  	function PluginInit( & $params )
  44      {
  45          $this->short_desc = T_('Star formatting e-g [stars:2.3/5]');
  46          $this->long_desc = T_('This plugin allows to render star ratings inside blog posts and comments by using the syntax [stars:2.3/5] for example');
  47  
  48          // Pattern to search the stars

  49          $this->search_text = '#\[stars:([\d\.]+)(/\d+)?\]#';
  50          // Function to build template for stars

  51          $this->replace_func = array( $this, 'get_stars_template' );
  52      }
  53  
  54  
  55      /**

  56       * @see Plugin::SkinBeginHtmlHead()

  57       */
  58  	function SkinBeginHtmlHead()
  59      {
  60          require_css( $this->get_plugin_url( true ).'star.css', 'blog' );
  61      }
  62  
  63  
  64      /**

  65       * @see Plugin::AdminEndHtmlHead()

  66       */
  67  	function AdminEndHtmlHead()
  68      {
  69          $this->SkinBeginHtmlHead();
  70      }
  71  
  72  
  73      /**

  74       * Perform rendering

  75       *

  76       * @see Plugin::RenderItemAsHtml()

  77       */
  78  	function RenderItemAsHtml( & $params )
  79      {
  80          $content = & $params['data'];
  81          $content = replace_content_outcode( $this->search_text, $this->replace_func, $content, 'replace_content_callback' );
  82  
  83          return true;
  84      }
  85  
  86  
  87      /**

  88       * Perform rendering of Message content

  89       * 

  90       * NOTE: Use default coll settings of comments as messages settings

  91       * 

  92       * @see Plugin::RenderMessageAsHtml()

  93       */
  94  	function RenderMessageAsHtml( & $params )
  95      {
  96          $content = & $params['data'];
  97          $content = replace_content_outcode( $this->search_text, $this->replace_func, $content, 'replace_content_callback' );
  98  
  99          return true;
 100      }
 101  
 102  
 103      /**

 104       * Do the same as for HTML.

 105       *

 106       * @see RenderItemAsHtml()

 107       */
 108  	function RenderItemAsXml( & $params )
 109      {
 110          $this->RenderItemAsHtml( $params );
 111      }
 112  
 113  
 114      /**

 115       *

 116       * Render comments if required

 117       *

 118       * @see Plugin::FilterCommentContent()

 119       */
 120  	function FilterCommentContent( & $params )
 121      {
 122          $Comment = & $params['Comment'];
 123          $comment_Item = & $Comment->get_Item();
 124          $item_Blog = & $comment_Item->get_Blog();
 125          if( in_array( $this->code, $Comment->get_renderers_validated() ) )
 126          { // apply_comment_rendering is set to render
 127              $content = & $params['data'];
 128              $content = replace_content_outcode( $this->search_text, $this->replace_func, $content, 'replace_content_callback' );
 129          }
 130      }
 131  
 132  
 133      /**

 134       * Get HTML template for stars

 135       *

 136       * @param array Matches

 137       * @return string HTML stars

 138       */
 139  	function get_stars_template( $matches )
 140      {
 141          if( empty( $matches ) )
 142          { // No stars found
 143              return;
 144          }
 145  
 146          $active_stars = $matches[1];
 147  
 148          if( ! empty( $matches[2] ) )
 149          { // Get a number of stars from content
 150              $number_stars = intval( substr( $matches[2], 1 ) );
 151          }
 152          if( empty( $number_stars ) )
 153          { // Use 5 stars by default
 154              $number_stars = 5;
 155          }
 156  
 157          $active_stars_max = floor( $active_stars );
 158          $percents = round( ( $active_stars - $active_stars_max ) * 100 );
 159          $template = '<span class="star_plugin"'.( $number_stars != 5 ? ' style="width:'.( $number_stars * 16 ).'px"' : '' ).'>';
 160          for( $s = 1; $s <= $number_stars; $s++ )
 161          {
 162              $attrs = '';
 163              if( $s > $active_stars_max )
 164              { // Class for empty stars
 165                  $attrs .= ' class="empty"';
 166              }
 167              $template .= '<span'.$attrs.'>';
 168              if( $s == $active_stars_max + 1 && $percents > 0 )
 169              { // Star with a percent fill
 170                  $template .= '<span style="width:'.$percents.'%">%</span>';
 171              }
 172              else
 173              {
 174                  $template .= $s <= $active_stars_max ? '*' : '-';
 175              }
 176              $template .= '</span>';
 177          }
 178          $template .= '</span>';
 179  
 180          return $template;
 181      }
 182  }
 183  
 184  ?>

title

Description

title

Description

title

Description

title

title

Body