function rate_generate_widget in Rate 7
Same name and namespace in other branches
- 6.2 rate.module \rate_generate_widget()
Generate a widget.
Parameters
int $widget_id Widget id:
string $content_type "node" or "comment":
int $content_id Node id (nid) or comment id (cid):
bool $teaser:
bool $include_div:
Return value
array
6 calls to rate_generate_widget()
- rate_comment_view_alter in ./
rate.module - Implements hook_comment_view_alter().
- rate_embed in ./
rate.module - Get the widget code to embed.
- rate_node_view in ./
rate.module - Implements hook_node_view().
- rate_panels_rate_widget_render in plugins/
content_types/ rate_widget.inc - rate_views_widget in ./
rate.module
File
- ./
rate.module, line 211 - Rate module
Code
function rate_generate_widget($widget_id, $content_type, $content_id, $mode = RATE_FULL, $include_div = TRUE, $just_voted = FALSE, $displayed = NULL) {
global $user;
// Check input.
if (!is_numeric($widget_id) || empty($content_type) || !is_numeric($content_id)) {
return NULL;
}
$widgets = variable_get(RATE_VAR_WIDGETS, array());
$widget = $widgets[$widget_id];
_rate_check_widget($widget);
// Determine if the user may vote.
$entities = entity_load($content_type, array(
$content_id,
));
$node = $entities[$content_id];
$permission_status = _rate_check_permissions($widget, $node);
// This option should be available, check for legacy.
isset($widget->noperm_behaviour) or $widget->noperm_behaviour = RATE_NOPERM_REDIRECT_WITH_MESSAGE;
isset($widget->displayed) or $widget->displayed = RATE_AVERAGE;
if ($permission_status != RATE_PERMISSION_OK && $widget->noperm_behaviour == RATE_NOPERM_HIDE_WIDGET) {
return NULL;
}
elseif ($permission_status == RATE_PERMISSION_DISALLOWED_ROLE && $widget->noperm_behaviour == RATE_NOPERM_SHOW_DISABLED_WIDGET) {
$mode = $mode == RATE_COMPACT ? RATE_COMPACT_DISABLED : RATE_DISABLED;
}
elseif ($permission_status == RATE_PERMISSION_DISALLOWED_AUTHOR) {
$mode = $mode == RATE_COMPACT ? RATE_COMPACT_DISABLED : RATE_DISABLED;
}
// Store the display mode in $widget, so it can be altered by hook_rate_widget.
$widget->mode = $mode;
// Let other modules alter the rate widget.
$context = array(
'content_type' => $content_type,
'content_id' => $content_id,
);
drupal_alter('rate_widget', $widget, $context);
$div_id = "rate-{$content_type}-{$content_id}-{$widget_id}-{$mode}";
$theme_name = str_replace('-', '_', $widget->name);
$theme = array(
'rate_widget__' . $theme_name,
'rate_widget',
);
if (isset($widget->theme)) {
// Insert the general widget template suggestion at second position to go
// after the specific widget template and before the overall general
// template.
array_splice($theme, 1, 0, $widget->theme);
}
// Get voting results.
$results = rate_get_results($content_type, $content_id, $widget_id);
$results['empty'] = $results['count'] ? FALSE : TRUE;
// Handle interaction modes.
if (is_null($displayed)) {
$displayed = RATE_AVERAGE;
if ($just_voted) {
if ($widget->displayed_just_voted == RATE_USER) {
$displayed = RATE_USER;
}
}
else {
if (isset($results['user_vote']) && $widget->displayed == RATE_USER_OR_AVERAGE) {
$displayed = RATE_USER;
}
if ($widget->displayed == RATE_USER) {
$displayed = RATE_USER;
}
}
}
elseif ($displayed == RATE_USER_OR_AVERAGE && isset($results['user_vote'])) {
$displayed = RATE_USER;
}
elseif ($displayed == RATE_USER) {
$displayed = RATE_USER;
}
else {
$displayed = RATE_AVERAGE;
}
if ($displayed == RATE_USER) {
$results['rating'] = isset($results['user_vote']) ? $results['user_vote'] : 0;
if (!isset($results['user_vote'])) {
// We should display an empty rating in this case.
$results['empty'] = TRUE;
}
}
// Add generic javascript.
$dest = drupal_get_destination();
drupal_add_js(array(
'rate' => array(
'basePath' => url('rate/vote/js'),
'destination' => $dest['destination'],
),
), array(
'type' => 'setting',
));
drupal_add_js(drupal_get_path('module', 'rate') . '/rate.js');
$links = array();
foreach ($widget->options as $option) {
// This name must be unique for all submit buttons across the page.
$id = "opt-{$widget_id}-{$content_type}-{$content_id}-{$option[0]}";
$token = rate_get_token($id);
if (isset($_GET['rate']) && $_GET['rate'] == $token) {
rate_save_vote($widget, $content_type, $content_id, $option[0]);
drupal_goto($_GET['q'], array(
'query' => _rate_get_query(),
));
}
isset($widget->translate) or $widget->translate = TRUE;
$link_text = $widget->translate ? t($option[1]) : $option[1];
$link_href = url($_GET['q'], array(
'query' => _rate_get_query($token),
));
if (isset($results['options']) && isset($results['options'][$option[0]])) {
$link_votes = $results['options'][$option[0]];
}
else {
$link_votes = NULL;
}
$disabled = FALSE;
if ($widget->mode == RATE_DISABLED || $widget->mode == RATE_COMPACT_DISABLED || $widget->mode == RATE_CLOSED) {
$disabled = TRUE;
}
$links[] = array(
'text' => $link_text,
'href' => $disabled ? NULL : $link_href,
'value' => $option[0],
'votes' => $link_votes,
);
}
if (isset($widget->css)) {
drupal_add_css($widget->css);
}
if (isset($widget->js)) {
drupal_add_js($widget->js);
}
if ($widget->mode == RATE_CLOSED && ($mode == RATE_COMPACT || $mode == RATE_DISABLED || $mode == RATE_COMPACT_DISABLED)) {
// The widget was closed by hook_rate_widget, but we want to display a compact or disabled
// widget. Set the mode back to compact / disabled in order to display a compact / disabled
// widget with disabled voting buttons.
$widget->mode = $mode == RATE_DISABLED ? RATE_DISABLED : RATE_COMPACT_DISABLED;
}
$display_options = array(
'description' => '',
'title' => check_plain($widget->title),
);
if ($widget->mode != RATE_COMPACT && $widget->mode != RATE_COMPACT_DISABLED || $widget->description_in_compact) {
$display_options['description'] = check_plain($widget->description);
}
$widget_html = theme($theme, array(
'links' => $links,
'results' => $results,
'mode' => $widget->mode,
'just_voted' => $just_voted,
'content_type' => $content_type,
'content_id' => $content_id,
'display_options' => $display_options,
));
if ($include_div) {
$classes = array(
'rate-widget-' . $widget_id,
'rate-widget',
'clear-block',
);
$classes[] = $displayed == RATE_AVERAGE ? 'rate-average' : 'rate-user';
if (!empty($widget->template)) {
$classes[] = 'rate-widget-' . $widget->template;
}
// This token is required for the AHAH callback when using arbitrary values.
$id = "rate-{$widget_id}-{$content_type}-{$content_id}";
$token = _rate_get_token($id);
$classes[] = 'rate-' . $token;
return '<div class="' . implode(' ', $classes) . ' ' . $div_id . '" id="' . drupal_html_id($div_id) . '">' . $widget_html . '</div>';
}
else {
// We do not want the div for AJAX callbacks, we would have 2 div's otherwise.
return $widget_html;
}
}