View source
<?php
define('VUD_WIDGET_MESSAGE_ERROR', 0);
define('VUD_WIDGET_MESSAGE_DENIED', 1);
define('VUD_REQUIRED_CTOOLS_API', '2.0-alpha');
function vud_ctools_plugin_type() {
return array(
'widgets' => array(
'process' => 'vud_width_plugin_defaults',
'load themes' => TRUE,
'cache' => FALSE,
),
);
}
function vud_width_plugin_defaults(&$plugin, $info) {
$defaults = array(
'render function' => 'theme_render_template',
'extension' => '.tpl.php',
'css' => $plugin['name'] . '.css',
);
$plugin += $defaults;
}
function vud_widget_get($name) {
ctools_include('plugins');
return ctools_get_plugins('vud', 'widgets', $name);
}
function vud_widget_get_all() {
ctools_include('plugins');
return ctools_get_plugins('vud', 'widgets');
}
function vud_widget_get_names() {
$names = array();
foreach (vud_widget_get_all() as $name => $plugin) {
$names[$name] = $plugin['title'];
}
asort($names);
return $names;
}
function vud_theme() {
return array(
'vud_widget' => array(
'function' => 'vud_widget_proxy',
'variables' => array(
'entity_id' => NULL,
'type' => NULL,
'tag' => NULL,
'widget_theme' => NULL,
'readonly' => NULL,
'widget_message_code' => NULL,
),
),
'vud_votes' => array(
'function' => 'vud_votes_proxy',
'variables' => array(
'entity_id' => NULL,
'type' => NULL,
'tag' => NULL,
'widget_theme' => NULL,
),
),
);
}
function vud_pseudo_theming($vote_entity, $template_type, $plugin, &$variables) {
$variables_type = 'vud_' . $template_type . '_template_variables';
drupal_alter($variables_type, $variables);
if ($function = ctools_plugin_get_function($plugin, 'alter template variables')) {
$function($template_type, $variables);
}
$template_file = $plugin['path'] . '/' . $plugin[$template_type . ' template'] . $plugin['extension'];
return $template_file;
$suggestions = module_invoke('vud_' . $vote_entity, 'template_suggestions', $template_type, $plugin, $variables['entity_id']);
if (empty($suggestions)) {
$template_file = $plugin['path'] . '/' . $plugin[$template_type . ' template'] . $plugin['extension'];
}
else {
global $theme_key;
$themes = list_themes();
$current_theme = $themes[$theme_key];
$paths = array(
$plugin['path'],
dirname($current_theme->filename),
);
}
return $template_file;
}
function vud_widget_proxy($variables) {
global $user;
$plugin = vud_widget_get($variables['widget_theme']);
if (empty($plugin) || empty($plugin['widget template'])) {
return;
}
$variables += array(
'widget_message_code' => VUD_WIDGET_MESSAGE_ERROR,
'readonly' => FALSE,
'plugin' => $plugin,
'tag' => variable_get('vud_tag', 'vote'),
'id' => 'widget-' . $variables['type'] . '-' . $variables['entity_id'],
'link_class_up' => 'vud-link-up',
'link_class_down' => 'vud-link-down',
'link_class_reset' => 'vud-link-reset',
'link_up' => '',
'link_down' => '',
'class_up' => 'up-active',
'class_down' => 'down-active',
'class_reset' => 'reset-inactive',
'show_links' => FALSE,
'show_reset' => FALSE,
'show_up_as_link' => FALSE,
'show_down_as_link' => FALSE,
'reset_long_text' => t('Reset your vote'),
'reset_short_text' => t('(reset)'),
);
$type = $variables['type'];
$entity_id = $variables['entity_id'];
$tag = $variables['tag'];
$widget_theme = $variables['widget_theme'];
$readonly = $variables['readonly'];
drupal_add_library('system', 'drupal.ajax');
ctools_add_js('ajax-responder');
ctools_include('ajax');
vud_add_files('css', $plugin);
vud_add_files('js', $plugin);
$user_vote = 0;
$up_access = $down_access = $reset_access = FALSE;
if (user_access('use vote up/down')) {
$criteria = array(
'entity_type' => $type,
'entity_id' => $entity_id,
'tag' => $variables['tag'],
) + votingapi_current_user_identifier();
$previous_vote = votingapi_select_single_vote_value($criteria);
$user_vote = isset($previous_vote) ? $previous_vote : 0;
if (!$readonly) {
$up_access = $user_vote <= 0;
$down_access = $user_vote >= 0;
$reset_access = $user_vote != 0 && vud_access_callback('reset vote up/down votes', $type, $entity_id, NULL, $tag);
if (!$up_access && !$down_access) {
$readonly = $variables['readonly'] = TRUE;
}
}
}
$message_on_deny = variable_get('vud_message_on_deny', FALSE);
if (!$readonly || $readonly && $message_on_deny) {
if ($up_access) {
$variables['class_up'] = 'up-inactive';
$variables['show_up_as_link'] = TRUE;
}
if ($down_access) {
$variables['class_down'] = 'down-inactive';
$variables['show_down_as_link'] = TRUE;
}
if ($reset_access) {
$variables['show_reset'] = TRUE;
$variables['class_reset'] = 'reset-active';
}
$variables['show_links'] = TRUE;
}
if (!$readonly) {
if ($up_access) {
$token_up = drupal_get_token("vote/{$type}/{$entity_id}/1/{$tag}/{$widget_theme}");
$variables['link_up'] = url("vote/{$type}/{$entity_id}/1/{$tag}/{$widget_theme}/{$token_up}/nojs");
$variables['link_class_up'] .= ' use-ajax';
}
if ($down_access) {
$token_down = drupal_get_token("vote/{$type}/{$entity_id}/-1/{$tag}/{$widget_theme}");
$variables['link_down'] = url("vote/{$type}/{$entity_id}/-1/{$tag}/{$widget_theme}/{$token_down}/nojs");
$variables['link_class_down'] .= ' use-ajax';
}
if ($reset_access) {
$token_reset = drupal_get_token("votereset/{$type}/{$entity_id}/{$tag}/{$widget_theme}");
$variables['link_reset'] = url("votereset/{$type}/{$entity_id}/{$tag}/{$widget_theme}/{$token_reset}");
$variables['link_class_reset'] .= ' use-ajax';
}
}
elseif ($readonly && $message_on_deny) {
ctools_include('modal');
ctools_modal_add_js();
if ($up_access) {
$variables['link_class_up'] .= ' denied ctools-use-modal';
}
else {
$variables['link_class_down'] .= ' denied ctools-use-modal';
}
if ($up_access || $down_access) {
$variables['link_class_reset'] .= ' denied ctools-use-modal';
}
$widget_message_code = VUD_WIDGET_MESSAGE_DENIED;
$variables['link_up'] = url(sprintf('vud/nojs/denied/%d', $widget_message_code));
$variables['link_down'] = url(sprintf('vud/nojs/denied/%d', $widget_message_code));
$variables['link_reset'] = url(sprintf('vud/nojs/denied/%d', $widget_message_code));
}
else {
$variables['link_up'] = '#';
$variables['link_down'] = '#';
$variables['link_reset'] = '#';
}
$result_criteria = array(
'entity_type' => $type,
'entity_id' => $entity_id,
'value_type' => 'points',
'tag' => $tag,
'function' => 'sum',
);
$raw_points = votingapi_select_single_result_value($result_criteria);
$variables['raw_points'] = $raw_points;
$vote_result = (int) $raw_points;
$criteria = array(
'entity_type' => $type,
'entity_id' => $entity_id,
'value_type' => 'points',
'tag' => $tag,
'function' => 'count',
);
$vote_count = (int) votingapi_select_single_result_value($criteria);
$variables['vote_count'] = $vote_count;
$variables['unsigned_points'] = $vote_result;
if ($vote_result > 0) {
$variables['class'] = 'positive';
$variables['points'] = '+' . $vote_result;
}
else {
$variables['points'] = $vote_result;
if ($vote_result < 0) {
$variables['class'] = 'negative';
}
else {
$variables['class'] = 'neutral';
}
}
$variables['vote_label'] = format_plural(abs($vote_result), 'vote', 'votes');
$template_file = vud_pseudo_theming($type, 'widget', $plugin, $variables);
return $plugin['render function']($template_file, $variables);
}
function vud_votes_proxy($variables) {
$plugin = vud_widget_get($variables['widget_theme']);
if (empty($plugin) || empty($plugin['votes template'])) {
return;
}
$template_file = $plugin['path'] . '/' . $plugin['votes template'] . $plugin['extension'];
$variables['plugin'] = $plugin;
$variables['id'] = 'votes-' . $variables['type'] . '-' . $variables['entity_id'];
$criteria = array(
'entity_type' => $variables['type'],
'entity_id' => $variables['entity_id'],
'value_type' => 'points',
'tag' => $variables['tag'],
'function' => 'sum',
);
$vote_result = (int) votingapi_select_single_result_value($criteria);
$variables['unsigned_points'] = $vote_result;
if ($vote_result > 0) {
$variables['class'] = 'positive';
$variables['points'] = '+' . $vote_result;
}
else {
$variables['points'] = $vote_result;
if ($vote_result < 0) {
$variables['class'] = 'negative';
}
else {
$variables['class'] = 'neutral';
}
}
$variables['vote_label'] = format_plural(abs($vote_result), 'vote', 'votes');
vud_add_files('css', $plugin);
vud_add_files('js', $plugin);
$template_file = vud_pseudo_theming($variables['type'], 'votes', $plugin, $variables);
if (function_exists($plugin['render function'])) {
return $plugin['render function']($template_file, $variables);
}
return '';
}
function vud_add_files($type, $plugin) {
$function = 'drupal_add_' . $type;
if (empty($plugin[$type])) {
return;
}
if (is_string($plugin[$type])) {
$css = array(
$plugin[$type],
);
}
if (is_array($plugin[$type])) {
$css = $plugin[$type];
}
if (!empty($css)) {
foreach ($css as $file) {
$function($plugin['path'] . '/' . $file);
}
}
}
function vud_vote($type, $entity_id, $value, $tag, $widget, $token, $ajax = 'ajax') {
if (!is_numeric($value) || !drupal_valid_token($token, "vote/{$type}/{$entity_id}/{$value}/{$tag}/{$widget}", TRUE)) {
return MENU_ACCESS_DENIED;
}
$vote = array();
$casted_vote_criteria = array(
'entity_type' => $type,
'entity_id' => $entity_id,
'tag' => $tag,
) + votingapi_current_user_identifier();
$casted_vote = votingapi_select_single_vote_value($casted_vote_criteria);
if ($value > 0) {
$value = 1;
}
elseif ($value < 0) {
$value = -1;
}
else {
watchdog('vud', 'Invalid vote on @type @content_id, with value @value, tag @tag and token @token', array(
'@type' => $type,
'@content_id' => $content_id,
'@value' => $value,
'@tag' => $tag,
'@token' => $token,
));
return;
}
$vote['value'] = $value;
$vote['value_type'] = 'points';
$tag = $tag ? $tag : variable_get('vud_tag', 'vote');
$vote['tag'] = $tag;
$vote['entity_id'] = $entity_id;
$vote['entity_type'] = $type;
$votes = array(
0 => $vote,
);
drupal_alter('vud_votes', $votes);
if ($casted_vote == $votes[0]['value']) {
return;
}
votingapi_set_votes($votes);
if ($ajax == 'ajax') {
$plugin = vud_widget_get($widget);
$commands = array();
if ($function = ctools_plugin_get_function($plugin, 'ajax render')) {
$commands = $function($type, $entity_id, $value, $tag, $token, $widget);
}
else {
$variables = array(
'entity_id' => $entity_id,
'type' => $type,
'tag' => $tag,
'widget_theme' => $widget,
);
if (!empty($plugin['widget template'])) {
$commands[] = ajax_command_replace("#widget-{$type}-{$entity_id}", theme('vud_widget', $variables));
}
if (!empty($plugin['votes template'])) {
$commands[] = ajax_command_replace("#votes-{$type}-{$entity_id}", theme('vud_votes', $variables));
}
}
return ajax_deliver(array(
'#type' => 'ajax',
'#commands' => $commands,
));
}
else {
drupal_goto($_SERVER['HTTP_REFERER']);
}
}
function vud_reset($type, $entity_id, $tag, $widget, $token, $ajax = 'ajax') {
if (drupal_valid_token($token, "votereset/{$type}/{$entity_id}/{$tag}/{$widget}", TRUE)) {
$criteria = array(
'entity_type' => $type,
'entity_id' => $entity_id,
'tag' => $tag,
) + votingapi_current_user_identifier();
votingapi_delete_votes(votingapi_select_votes($criteria));
votingapi_recalculate_results($type, $entity_id);
}
else {
watchdog('vud', 'Could not reset votes on @type @entity_id, tag @tag and token @token', array(
'@type' => $type,
'@entity_id' => $entity_id,
'@tag' => $tag,
'@token' => $token,
));
drupal_set_message(t('Oops! There was an error resetting your vote!'), 'warning');
}
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
if ($ajax == 'ajax') {
$plugin = vud_widget_get($widget);
$commands = array();
if ($function = ctools_plugin_get_function($plugin, 'ajax render')) {
$commands = $function($type, $entity_id, $value, $tag, $token, $widget);
}
else {
$variables = array(
'entity_id' => $entity_id,
'type' => $type,
'tag' => $tag,
'widget_theme' => $widget,
);
if (!empty($plugin['widget template'])) {
$commands[] = ajax_command_replace("#widget-{$type}-{$entity_id}", theme('vud_widget', $variables));
}
if (!empty($plugin['votes template'])) {
$commands[] = ajax_command_replace("#votes-{$type}-{$entity_id}", theme('vud_votes', $variables));
}
}
return ajax_deliver(array(
'#type' => 'ajax',
'#commands' => $commands,
));
}
}
else {
drupal_goto($_SERVER['HTTP_REFERER']);
}
}