You are here

class RateDefaultWidget in Rate 7.2

Hierarchy

Expanded class hierarchy of RateDefaultWidget

1 string reference to 'RateDefaultWidget'
rate_rate_widgets in ./rate.module
Implements hook_rate_widgets().

File

classes/default.inc, line 3

View source
class RateDefaultWidget extends RateWidget {
  protected $wid;
  protected $layout;
  protected $css_file;
  protected $js_file;
  protected $desc_norating;
  protected $desc_notvoted;
  protected $desc_voted;
  protected $desc_justvoted;
  protected $desc_mouseover;
  protected $sprites;
  protected $highlight_voted;
  protected $highlight_mouseover;

  /**
   * Load the widget from database.
   *
   * Set up the buttons, tag, value type etc.
   */
  protected function load() {
    $widget = db_select('rate_widget', 'w')
      ->fields('w', array(
      'wid',
      'type',
      'name',
      'mode',
      'sprites',
      'highlight_voted',
      'highlight_mouseover',
      'desc_norating',
      'desc_notvoted',
      'desc_voted',
      'desc_justvoted',
      'desc_mouseover',
      'css_file',
      'js_file',
    ))
      ->condition('w.type', $this->type)
      ->execute()
      ->fetchObject();
    if ($widget) {
      $this->wid = $widget->wid;
      $this->value_type = $widget->mode;
      $this->css_file = $widget->css_file;
      $this->js_file = $widget->js_file;
      $this->desc_norating = $widget->desc_norating;
      $this->desc_notvoted = $widget->desc_notvoted;
      $this->desc_voted = $widget->desc_voted;
      $this->desc_justvoted = $widget->desc_justvoted;
      $this->desc_mouseover = $widget->desc_mouseover;
      $this->sprites = (bool) $widget->sprites;
      $this->highlight_voted = $widget->highlight_voted;
      $this->highlight_mouseover = $widget->highlight_mouseover;
      $this
        ->loadButtons();
    }
  }

  /**
   * Load buttons from database.
   */
  protected function loadButtons() {
    $buttons = db_select('rate_widget_button', 'b')
      ->fields('b', array(
      'num',
      'label',
      'value',
      'width',
      'height',
      'img_default',
      'img_highlighted',
      'img_default_voted',
      'img_highlighted_voted',
      'img_disabled',
      'img_disabled_voted',
    ))
      ->condition('b.wid', $this->wid)
      ->orderBy('b.num', 'asc')
      ->execute()
      ->fetchAll();

    // Loop the result to find files to load.
    $fids = array();
    foreach ($buttons as $button) {
      if (!$this->sprites) {
        $img = $button->img_default;
        if (is_numeric($img)) {
          $fids[] = $img;
        }
      }
    }
    if ($fids) {
      $fids = array_filter(array_unique($fids));
      $files = file_load_multiple($fids);
    }

    // Second loop to generate buttons.
    foreach ($buttons as $button) {
      $this->elements["button{$button->num}"] = new RateButton($button->num, $button->label, $button->value, $this
        ->getToken($button->num), FALSE, '');
    }
  }

  /**
   * Initialize the widget.
   *
   * Load layout from database and generate default elements like vote count and description.
   */
  protected function init() {
    $this->layout = array();

    // Get numeric display mode as used in the elements table.
    $modes = array(
      'format_full' => 1,
      'format_compact' => 2,
      'format_full_disabled' => 4,
      'format_compact_disabled' => 8,
      'format_closed' => 16,
    );
    $mode = isset($modes[$this->mode]) ? $modes[$this->mode] : 0;
    $elements = db_select('rate_widget_element', 'e')
      ->fields('e', array(
      'type',
      'prefix',
      'suffix',
      'mode',
    ))
      ->condition('e.wid', $this->wid)
      ->orderBy('e.weight', 'asc')
      ->execute()
      ->fetchAll();
    foreach ($elements as $element) {
      if ($element->mode & $mode) {
        $this->layout[] = $element;
      }
    }
    foreach ($this->elements as $element) {
      if ($element instanceof RateButton) {
        $highlight = FALSE;
        if ($this->rating == $element
          ->getValue() && $this->highlight_voted & 1) {
          $highlight = TRUE;
        }
        if ($this->rating > $element
          ->getValue() && $this->highlight_voted & 2) {
          $highlight = TRUE;
        }
        if ($this->rating < $element
          ->getValue() && $this->highlight_voted & 4) {
          $highlight = TRUE;
        }
        if ($highlight) {
          $element
            ->addClass('highlighted');
        }
      }
    }
    $this->elements['description'] = $this
      ->getDescription();
  }

  /**
   * Generate HTML code for widget.
   *
   * @return string
   */
  public function __toString() {
    $output = '';

    // Add generic javascript.
    drupal_add_js(array(
      'rate' => array(
        'basePath' => url('rate/vote/js'),
        'destination' => drupal_get_destination(),
      ),
    ), array(
      'type' => 'setting',
    ));
    drupal_add_js(drupal_get_path('module', 'rate') . '/rate.js');

    // Add widget specific javascript and CSS.
    if ($this->js_file) {
      drupal_add_js($this->js_file);
    }
    if ($this->css_file) {
      drupal_add_css($this->css_file);
    }
    foreach ($this->layout as $element) {
      if (isset($this->elements[$element->type])) {
        $output .= $element->prefix;
        $output .= (string) $this->elements[$element->type];
        $output .= $element->suffix;
      }
    }
    $classes = array(
      'rate-widget',
      "rate-widget-{$this->type}",
    );
    $classes[] = $this->displayed == RATE_AVERAGE ? 'average-rating' : 'user-rating';
    $classes[] = $this->voted ? 'voted' : 'not-voted';
    if ($this->mode == 'format_full_disabled' || $this->mode == 'format_compact_disabled' || $this->mode == 'format_closed') {
      $classes[] = 'disabled';
    }
    else {
      $classes[] = 'enabled';
    }
    if ($this->mode == 'format_closed') {
      $classes[] = 'closed';
    }
    if ($this->mode == 'format_compact' || $this->mode == 'format_compact_disabled') {
      $classes[] = 'compact';
    }
    $attributes = array(
      'class' => $classes,
      'data-entity-type' => $this->entity_type,
      'data-entity-id' => $this->entity_id,
      'data-field' => $this->field_name,
      'data-mode' => $this->mode,
      'data-highlight-mouseover' => $this->highlight_mouseover,
      'data-desc-mouseover' => $this->desc_mouseover,
    );
    $output = "<div" . drupal_attributes($attributes) . ">{$output}</div>";
    return $output;
  }

  /**
   * Get description for this widget.
   *
   * @return string
   */
  protected function getDescription() {
    $user_vote = NULL;
    foreach ($this->results as $result) {
      if ($result['function'] == 'user') {
        $user_vote = $this
          ->getButtonLabelByValue($result['value']);
      }
    }
    if ($this->count == 0) {
      $description = $this->desc_norating;
    }
    elseif (is_null($user_vote)) {
      $description = $this->desc_notvoted;
    }
    elseif ($this->just_voted) {
      $description = $this->desc_justvoted;
    }
    else {
      $description = $this->desc_voted;
    }
    $description = t($description, array(
      '@rating' => $this->rating,
      '@vote' => $user_vote,
    ));
    $description = "<span class=\"rate-description\">{$description}</span>";
    return $description;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RateDefaultWidget::$css_file protected property
RateDefaultWidget::$desc_justvoted protected property
RateDefaultWidget::$desc_mouseover protected property
RateDefaultWidget::$desc_norating protected property
RateDefaultWidget::$desc_notvoted protected property
RateDefaultWidget::$desc_voted protected property
RateDefaultWidget::$highlight_mouseover protected property
RateDefaultWidget::$highlight_voted protected property
RateDefaultWidget::$js_file protected property
RateDefaultWidget::$layout protected property
RateDefaultWidget::$sprites protected property
RateDefaultWidget::$wid protected property
RateDefaultWidget::getDescription protected function Get description for this widget.
RateDefaultWidget::init protected function Initialize the widget. Overrides RateWidget::init
RateDefaultWidget::load protected function Load the widget from database. Overrides RateWidget::load
RateDefaultWidget::loadButtons protected function Load buttons from database.
RateDefaultWidget::__toString public function Generate HTML code for widget. Overrides RateWidget::__toString
RateWidget::$account protected property Voters account.
RateWidget::$count protected property Vote count.
RateWidget::$displayed protected property Displayed rating.
RateWidget::$displayed_just_voted protected property Rating to display directly after voting.
RateWidget::$elements protected property Elements of the rate widget as keyed array where the names are the element identifiers (like 'button1') and the values are the elements, which must be casted to strings.
RateWidget::$entity_id protected property Entity id.
RateWidget::$entity_type protected property Entity type.
RateWidget::$field_name protected property Field API field name.
RateWidget::$just_voted protected property Did the user just click on a rate button?
RateWidget::$mode protected property Display mode / formatter name (i.e. "rate_full").
RateWidget::$rating protected property Displayed rating.
RateWidget::$results protected property Voting result for display.
RateWidget::$tag protected property VotingAPI tag.
RateWidget::$type protected property Machine name for this widget.
RateWidget::$value_type protected property Voting API value type.
RateWidget::$voted protected property Has the user voted?
RateWidget::calculateResults protected function Add elements with results.
RateWidget::clickButton public function Save vote.
RateWidget::clickedButton protected function Check if the user has clicked a button on this widget and return that button.
RateWidget::getAverageResults protected function Get average voting results.
RateWidget::getButtonLabelByValue protected function
RateWidget::getCSS public function Get CSS files required for the widget.
RateWidget::getJS public function Get JS scripts required for the widget.
RateWidget::getToken final protected function Generate a rate token
RateWidget::getUserResults protected function Get users vote.
RateWidget::processVote public function Process the users vote (if applicable).
RateWidget::viewAccess public function Check if user may view this widget.
RateWidget::vote public function Save a vote.
RateWidget::__construct public function Create a new Rate widget.