You are here

function rate_views_widget in Rate 7

Same name and namespace in other branches
  1. 6.2 rate.module \rate_views_widget()
3 calls to rate_views_widget()
rate_views_widget_compact in ./rate.module
rate_views_widget_compact_disabled in ./rate.module
rate_views_widget_disabled in ./rate.module

File

./rate.module, line 1001
Rate module

Code

function rate_views_widget($value, $field, $columns, $mode = RATE_FULL) {
  if ($field->view->base_table != 'node' && $field->view->base_table != 'comment') {
    return '';
  }

  // Find the VotingAPI tag and value_type for this field.
  foreach ($field->query->table_queue[$field->relationship]['join']->extra as $votingapi_setting) {
    if ($votingapi_setting['field'] == 'tag') {
      $tag = $votingapi_setting['value'];
    }
    elseif ($votingapi_setting['field'] == 'value_type') {
      $value_type = $votingapi_setting['value'];
    }
  }
  switch ($field->view->base_table) {
    case 'node':
      $content_type = 'node';
      $content_id = $columns->nid;
      if (isset($columns->node_type)) {
        $node_type = $columns->node_type;
      }
      else {
        $node_type = db_select('node', 'n')
          ->fields('n', array(
          'type',
        ))
          ->condition('nid', $columns->nid)
          ->execute()
          ->fetchField();
      }
      break;
    case 'comment':
      $content_type = 'comment';
      $content_id = $columns->cid;
      if (isset($columns->node_type)) {
        $node_type = $columns->node_type;
      }
      else {
        $query = db_select('comment', 'c');
        $query
          ->join('node', 'n', 'n.nid = c.nid');
        $node_type = $query
          ->fields('n', array(
          'type',
        ))
          ->condition('c.cid', $columns->cid)
          ->execute()
          ->fetchField();
      }
      break;
  }
  if ($field->query->table_queue[$field->relationship]['table'] == 'votingapi_cache') {
    $displayed_rating = RATE_AVERAGE;
  }
  else {
    $displayed_rating = RATE_USER;
  }

  // Loop through all the widgets and search for a suitable widget to display.
  $widgets = variable_get(RATE_VAR_WIDGETS, array());
  foreach ($widgets as $widget_id => $widget) {
    if ((!isset($tag) || $tag == $widget->tag) && (!isset($value_type) || $value_type == $widget->value_type) && (in_array($node_type, $widget->node_types) || $field->view->base_table == 'comment')) {
      return rate_generate_widget($widget_id, $content_type, $content_id, $mode, TRUE, FALSE, $displayed_rating);
    }
  }

  // No suitable widget was found.
  return '';
}