View source
<?php
include_once dirname(__FILE__) . '/includes/fivestar.field.inc';
function fivestar_help($path, $arg) {
switch ($path) {
case 'admin/config/content/fivestar':
$output = t('This page is used to configure site-wide features of the Fivestar module.');
return $output;
case 'admin/help#fivestar':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The <a href="@fivestar">Fivestar</a> voting module is a very simple rating module that provides the possibility to rate items with stars or similar items. This gives you the possibilities to rate various items or even to create online forms for evaluations and assessments with different questions to be answered.
For more information, see the online documentation for the <a href="@doco">Fivestar module</a>.', array(
'@fivestar' => 'http://drupal.org/project/fivestar',
'@doco' => 'https://drupal.org/handbook/modules/fivestar',
)) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('General') . '</dt>';
$output .= '<dd>' . t('The Fivestar module can be used to easily rate various types of content on your website. These ratings can be used on the content itself or even from the comments of that piece of content.') . '</dd>';
$output .= '<dt>' . t('Basic Concepts and Features') . '</dt>';
$output .= '<dd>' . t('Fivestar is an excellent voting widget first made available for use on Drupal 5 websites. The D5 module included the ability to create a voting widget for nodes. With Drupal 6 came the ability to add comments. And with Drupal 7, the web developer was given the ability to create the voting widget with any entity.') . '</dd>';
$output .= '<dt>' . t('Advanced Use Cases') . '</dt>';
$output .= '<dd>' . t('There are many excellent resources online describing the many Fivestar features, such as the excellent article <a href="@features">"In-Depth features of Drupal Fivestar" </a>.', array(
'@features' => 'https://www.greengeeks.com/kb/3233/drupal-fivestar/',
)) . '</dd>';
$output .= '</dl>';
return $output;
}
}
function fivestar_menu() {
$items = array();
$items['admin/config/content/fivestar'] = array(
'title' => 'Fivestar',
'description' => 'Configure site-wide widgets used for Fivestar rating.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fivestar_settings',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'includes/fivestar.admin.inc',
);
return $items;
}
function fivestar_microdata_suggestions() {
$mappings = array();
$mappings['fields']['fivestar']['schema.org'] = array(
'#itemprop' => array(
'aggregateRating',
),
'#is_item' => TRUE,
'#itemtype' => array(
'http://schema.org/AggregateRating',
),
'average_rating' => array(
'#itemprop' => array(
'ratingValue',
),
),
'rating_count' => array(
'#itemprop' => array(
'ratingCount',
),
),
);
return $mappings;
}
function fivestar_permission() {
return array(
'rate content' => array(
'title' => t('Use Fivestar to rate content'),
),
);
}
function fivestar_theme() {
return array(
'fivestar' => array(
'render element' => 'element',
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_select' => array(
'render element' => 'element',
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_static' => array(
'variables' => array(
'rating' => NULL,
'stars' => 5,
'tag' => 'vote',
'widget' => array(
'name' => 'default',
'css' => '',
),
),
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_static_element' => array(
'variables' => array(
'star_display' => NULL,
'title' => NULL,
'description' => NULL,
),
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_summary' => array(
'variables' => array(
'user_rating' => NULL,
'average_rating' => NULL,
'votes' => 0,
'stars' => 5,
'microdata' => array(),
),
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_preview' => array(
'variables' => array(
'style' => NULL,
'text' => NULL,
'stars' => NULL,
'unvote' => NULL,
'title' => NULL,
),
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_preview_widget' => array(
'variables' => array(
'css' => NULL,
'name' => NULL,
),
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_preview_wrapper' => array(
'variables' => array(
'content' => NULL,
'type' => 'direct',
),
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_settings' => array(
'render element' => 'form',
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_color_form' => array(
'render element' => 'form',
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_formatter_default' => array(
'render element' => 'element',
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_formatter_rating' => array(
'render element' => 'element',
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_formatter_percentage' => array(
'render element' => 'element',
'file' => 'includes/fivestar.theme.inc',
),
);
}
function fivestar_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'fivestar') . '/includes',
);
}
function _fivestar_variables() {
return array(
'fivestar',
'fivestar_unvote',
'fivestar_style',
'fivestar_stars',
'fivestar_comment',
'fivestar_position',
'fivestar_position_teaser',
'fivestar_labels_enable',
'fivestar_labels',
'fivestar_text',
'fivestar_title',
'fivestar_feedback',
);
}
function _fivestar_cast_vote($entity_type, $id, $value, $tag = NULL, $uid = NULL, $skip_validation = FALSE, $vote_source = NULL) {
global $user;
$tag = empty($tag) ? 'vote' : $tag;
$uid = isset($uid) ? $uid : $user->uid;
if (!$skip_validation && !fivestar_validate_target($entity_type, $id, $tag, $uid)) {
return array();
}
if (is_numeric($id) && is_numeric($value)) {
if ($value > 100) {
$value = 100;
}
$criteria = array(
'entity_type' => $entity_type,
'entity_id' => $id,
'tag' => $tag,
'uid' => $uid,
);
if ($vote_source != NULL) {
$user_criteria = array(
'vote_source' => $vote_source,
);
$limit = 1;
}
else {
$user_criteria = votingapi_current_user_identifier();
$limit = 0;
}
$user_votes = votingapi_select_votes($criteria + $user_criteria, $limit);
if ($value == 0) {
votingapi_delete_votes($user_votes);
}
else {
$votes = $criteria += array(
'value' => $value,
);
votingapi_set_votes($votes);
}
votingapi_recalculate_results($entity_type, $id);
return fivestar_get_votes($entity_type, $id, $tag, $uid);
}
}
function fivestar_get_votes_multiple($entity_type, $ids, $tag = 'vote', $uid = NULL) {
global $user;
if (!isset($uid)) {
$uid = $user->uid;
}
$criteria = array(
'entity_type' => $entity_type,
'entity_id' => $ids,
'value_type' => 'percent',
'tag' => $tag,
);
$votes = array();
foreach ($ids as $id) {
$votes[$entity_type][$id] = array(
'average' => array(),
'count' => array(),
'user' => array(),
);
}
$results = votingapi_select_results($criteria);
$user_votes = array();
if (!empty($results)) {
foreach (votingapi_select_votes($criteria += array(
'uid' => $uid,
)) as $uv) {
$user_votes[$uv['entity_type']][$uv['entity_id']] = $uv;
}
}
foreach ($results as $result) {
if ($result['function'] == 'average') {
$votes[$result['entity_type']][$result['entity_id']]['average'] = $result;
}
if ($result['function'] == 'count') {
$votes[$result['entity_type']][$result['entity_id']]['count'] = $result;
}
if ($uid) {
if (!empty($user_votes[$result['entity_type']][$result['entity_id']])) {
$votes[$result['entity_type']][$result['entity_id']]['user'] = $user_votes[$result['entity_type']][$result['entity_id']];
$votes[$result['entity_type']][$result['entity_id']]['user']['function'] = 'user';
}
}
else {
$votes[$result['entity_type']][$result['entity_id']]['user'] = array(
'value' => 0,
);
}
}
return $votes;
}
function fivestar_get_votes($entity_type, $id, $tag = 'vote', $uid = NULL) {
$multiple = fivestar_get_votes_multiple($entity_type, array(
$id,
), $tag, $uid);
return $multiple[$entity_type][$id];
}
function fivestar_validate_target($entity_type, $id, $tag, $uid = NULL) {
if (!isset($uid)) {
$uid = $GLOBALS['user']->uid;
}
$access = module_invoke_all('fivestar_access', $entity_type, $id, $tag, $uid);
foreach ($access as $result) {
if ($result == TRUE) {
return TRUE;
}
if ($result === FALSE) {
return FALSE;
}
}
}
function fivestar_fivestar_access($entity_type, $id, $tag, $uid) {
$fields = field_read_fields(array(
'module' => 'fivestar',
));
foreach ($fields as $field) {
if ($field['settings']['axis'] == $tag) {
$params = array(
'entity_type' => $entity_type,
'field_name' => $field['field_name'],
);
$instance = field_read_instances($params);
if (!empty($instance)) {
return TRUE;
}
}
}
}
function fivestar_form_comment_form_alter(&$form, &$form_state, $form_id) {
$fivestar_field_keys = array();
if (isset($form['comment_output_below'])) {
foreach ($form['comment_output_below'] as $key => $value) {
if (is_array($value) && !empty($value['#field_type']) && $value['#field_type'] == 'fivestar') {
$fivestar_field_keys[] = $key;
}
}
}
if ($fivestar_field_keys) {
foreach ($fivestar_field_keys as $key) {
unset($form['comment_output_below'][$key]);
}
}
}
function fivestar_fivestar_widgets() {
$widgets =& drupal_static(__FUNCTION__);
if (!isset($widgets)) {
$cache = cache_get(__FUNCTION__);
if ($cache) {
$widgets = $cache->data;
}
}
if (!isset($widgets)) {
$widgets_directory = drupal_get_path('module', 'fivestar') . '/widgets';
$files = file_scan_directory($widgets_directory, '/\\.css$/');
foreach ($files as $file) {
if (strpos($file->filename, '-rtl.css') === FALSE) {
$widgets[$file->uri] = drupal_ucfirst(str_replace('-color', '', $file->name));
}
}
cache_set(__FUNCTION__, $widgets);
}
return $widgets;
}
function fivestar_custom_widget($form, &$form_state, $values, $settings) {
$form = array(
'#attributes' => array(
'class' => array(
'fivestar-widget',
),
),
);
$form['#submit'][] = 'fivestar_form_submit';
$form_state['settings'] = $settings;
$default_settings = array(
'allow_clear' => FALSE,
'allow_ownvote' => TRUE,
'allow_revote' => TRUE,
'autosubmit' => TRUE,
'description' => '',
'required' => FALSE,
'stars' => 5,
'tag' => 'vote',
'widget' => array(
'name' => 'default',
'css' => 'default',
),
);
$settings = $settings + $default_settings;
$form['vote'] = array(
'#type' => 'fivestar',
'#stars' => $settings['stars'],
'#auto_submit' => $settings['autosubmit'],
'#allow_clear' => $settings['allow_clear'],
'#allow_revote' => $settings['allow_revote'],
'#allow_ownvote' => $settings['allow_ownvote'],
'#required' => $settings['required'],
'#widget' => $settings['widget'],
'#values' => $values,
'#settings' => $settings,
'#description' => $settings['description'],
);
$form['fivestar_submit'] = array(
'#type' => 'submit',
'#value' => t('Rate'),
'#attributes' => array(
'class' => array(
'fivestar-submit',
),
),
);
return $form;
}
function fivestar_form_submit($form, &$form_state) {
_fivestar_cast_vote($form_state['settings']['content_type'], $form_state['settings']['content_id'], $form_state['values']['vote'], $form_state['settings']['tag']);
if ($form_state['values']['vote'] === '0') {
drupal_set_message(t('Your vote has been cleared.'));
}
elseif (is_numeric($form_state['values']['vote'])) {
drupal_set_message(t('Thank you for your vote.'));
}
}
function fivestar_ajax_submit($form, $form_state) {
if (!empty($form_state['settings']['content_id'])) {
$entity = entity_load($form_state['settings']['entity_type'], array(
$form_state['settings']['entity_id'],
));
$entity = reset($entity);
_fivestar_update_field_value($form_state['settings']['content_type'], $entity, $form_state['settings']['field_name'], $form_state['settings']['langcode'], $form_state['values']['vote']);
$votes = _fivestar_cast_vote($form_state['settings']['entity_type'], $form_state['settings']['content_id'], $form_state['values']['vote'], $form_state['settings']['tag']);
}
$values = array();
$values['user'] = isset($votes['user']['value']) ? $votes['user']['value'] : 0;
$values['average'] = isset($votes['average']['value']) ? $votes['average']['value'] : 0;
$values['count'] = isset($votes['count']['value']) ? $votes['count']['value'] : 0;
$form['vote']['#values'] = $values;
$new_element = fivestar_expand($form['vote'], $form_state, $form_state['complete form']);
$form['vote']['vote']['#description'] = isset($new_element['vote']['#description']) ? $new_element['vote']['#description'] : '';
if (!$form['vote']['#allow_revote'] && !$form['vote']['#allow_clear']) {
$form['vote'] = $new_element;
}
return array(
'#type' => 'ajax',
'#commands' => array(
array(
'command' => 'fivestarUpdate',
'data' => drupal_render($form['vote']),
),
),
);
}
function fivestar_element_info() {
$type['fivestar'] = array(
'#input' => TRUE,
'#stars' => 5,
'#allow_clear' => FALSE,
'#allow_revote' => FALSE,
'#allow_ownvote' => FALSE,
'#auto_submit' => FALSE,
'#process' => array(
'fivestar_expand',
),
'#theme_wrappers' => array(
'form_element',
),
'#widget' => array(
'name' => 'default',
'css' => 'default',
),
'#values' => array(
'user' => 0,
'average' => 0,
'count' => 0,
),
'#settings' => array(
'style' => 'user',
'text' => 'none',
),
);
return $type;
}
function fivestar_expand($element, $form_state, $complete_form) {
if (!_fivestar_allow_vote($element)) {
$element['#input'] = FALSE;
}
$path = drupal_get_path('module', 'fivestar');
$element['#attached']['js'][] = $path . '/js/fivestar.js';
$element['#attached']['css'][] = $path . '/css/fivestar.css';
$settings = $element['#settings'];
$values = $element['#values'];
$class[] = 'clearfix';
$title = t('it');
if (isset($element['#settings']['entity_id']) && isset($element['#settings']['entity_type'])) {
$entity_id = $element['#settings']['entity_id'];
$entity = entity_load($element['#settings']['entity_type'], array(
$entity_id,
));
$entity = $entity[$entity_id];
if (isset($entity->title)) {
$title = $entity->title;
}
}
elseif (isset($complete_form['#node'])) {
$title = $complete_form['#node']->title;
}
$options = array(
'-' => t('Select rating'),
);
for ($i = 1; $i <= $element['#stars']; $i++) {
$this_value = ceil($i * 100 / $element['#stars']);
$options[$this_value] = t('Give @title @star/@count', array(
'@title' => $title,
'@star' => $i,
'@count' => $element['#stars'],
));
}
if ($element['#allow_clear'] == TRUE) {
$options[0] = t('Cancel rating');
}
$element['vote'] = array(
'#type' => 'select',
'#options' => $options,
'#required' => $element['#required'],
'#attributes' => $element['#attributes'],
'#theme' => _fivestar_allow_vote($element) ? 'fivestar_select' : 'fivestar_static',
'#default_value' => _fivestar_get_element_default_value($element),
'#weight' => -2,
);
if (isset($element['#parents'])) {
$element['vote']['#parents'] = $element['#parents'];
}
switch ($settings['text']) {
case 'user':
$element['vote']['#description'] = theme('fivestar_summary', array(
'user_rating' => $values['user'],
'votes' => $settings['style'] == 'dual' ? NULL : $values['count'],
'stars' => $settings['stars'],
'microdata' => $settings['microdata'],
));
$class[] = 'fivestar-user-text';
break;
case 'average':
$element['vote']['#description'] = $settings['style'] == 'dual' ? NULL : theme('fivestar_summary', array(
'average_rating' => $values['average'],
'votes' => $values['count'],
'stars' => $settings['stars'],
'microdata' => $settings['microdata'],
));
$class[] = 'fivestar-average-text';
break;
case 'smart':
$element['vote']['#description'] = $settings['style'] == 'dual' && !$values['user'] ? NULL : theme('fivestar_summary', array(
'user_rating' => $values['user'],
'average_rating' => $values['user'] ? NULL : $values['average'],
'votes' => $settings['style'] == 'dual' ? NULL : $values['count'],
'stars' => $settings['stars'],
'microdata' => $settings['microdata'],
));
$class[] = 'fivestar-smart-text';
$class[] = $values['user'] ? 'fivestar-user-text' : 'fivestar-average-text';
break;
case 'dual':
$element['vote']['#description'] = theme('fivestar_summary', array(
'user_rating' => $values['user'],
'average_rating' => $settings['style'] == 'dual' ? NULL : $values['average'],
'votes' => $settings['style'] == 'dual' ? NULL : $values['count'],
'stars' => $settings['stars'],
'microdata' => $settings['microdata'],
));
$class[] = ' fivestar-combo-text';
break;
case 'none':
$element['vote']['#description'] = NULL;
break;
}
switch ($settings['style']) {
case 'average':
$class[] = 'fivestar-average-stars';
break;
case 'user':
$class[] = 'fivestar-user-stars';
break;
case 'smart':
$class[] = 'fivestar-smart-stars ' . ($values['user'] ? 'fivestar-user-stars' : 'fivestar-average-stars');
break;
case 'dual':
$class[] = 'fivestar-combo-stars';
$static_average = theme('fivestar_static', array(
'rating' => $values['average'],
'stars' => $settings['stars'],
'tag' => $settings['tag'],
'widget' => $settings['widget'],
));
if ($settings['text'] != 'none') {
$static_description = theme('fivestar_summary', array(
'average_rating' => $settings['text'] == 'user' ? NULL : (isset($values['average']) ? $values['average'] : 0),
'votes' => isset($values['count']) ? $values['count'] : 0,
'stars' => $settings['stars'],
));
}
else {
$static_description = ' ';
}
$element['average'] = array(
'#type' => 'markup',
'#markup' => theme('fivestar_static_element', array(
'star_display' => $static_average,
'title' => '',
'description' => $static_description,
)),
'#weight' => -1,
);
break;
}
$class[] = 'fivestar-form-item';
$class[] = 'fivestar-' . $element['#widget']['name'];
if ($element['#widget']['name'] != 'default') {
$element['#attached']['css'][] = $element['#widget']['css'];
}
$element['#prefix'] = '<div ' . drupal_attributes(array(
'class' => $class,
)) . '>';
$element['#suffix'] = '</div>';
if (!empty($element['#auto_submit'])) {
$element['vote']['#ajax'] = array(
'callback' => 'fivestar_ajax_submit',
);
$element['vote']['#attached']['js'][] = $path . '/js/fivestar.ajax.js';
}
if (empty($element['#input'])) {
$static_stars = theme('fivestar_static', array(
'rating' => $element['vote']['#default_value'],
'stars' => $settings['stars'],
'tag' => $settings['tag'],
'widget' => $settings['widget'],
));
$element['vote'] = array(
'#type' => 'markup',
'#markup' => theme('fivestar_static_element', array(
'star_display' => $static_stars,
'title' => '',
'description' => $element['vote']['#description'],
)),
);
}
$element['#element_validate'] = array(
'fivestar_validate',
);
return $element;
}
function _fivestar_get_element_default_value($element) {
if (isset($element['#default_value'])) {
$default_value = $element['#default_value'];
}
else {
switch ($element['#settings']['style']) {
case 'average':
$default_value = $element['#values']['average'];
break;
case 'user':
$default_value = $element['#values']['user'];
break;
case 'smart':
$default_value = !empty($element['#values']['user']) ? $element['#values']['user'] : $element['#values']['average'];
break;
case 'dual':
$default_value = $element['#values']['user'];
break;
default:
$default_value = $element['#values']['average'];
}
}
for ($i = 0; $i <= $element['#stars']; $i++) {
$this_value = ceil($i * 100 / $element['#stars']);
$next_value = ceil(($i + 1) * 100 / $element['#stars']);
if ($this_value < $default_value && $next_value > $default_value) {
$default_value = $next_value;
}
}
return $default_value;
}
function fivestar_validate($element, &$form_state) {
if ($element['#required'] && (empty($element['#value']) || $element['#value'] == '-')) {
form_error($element, t('@name field is required.', array(
'@name' => $element['#title'],
)));
}
}
function fivestar_votingapi_metadata_alter(&$data) {
foreach (fivestar_get_tags() as $tag) {
$data['tags'][$tag] = array(
'name' => t($tag),
'description' => t('@tag used by fivestar.', array(
'@tag' => $tag,
)),
'module' => 'fivestar',
);
}
}
function fivestar_get_tags() {
$tags_txt = variable_get('fivestar_tags', 'vote');
$tags_exploded = explode(',', $tags_txt);
$tags = array();
$got_vote = FALSE;
foreach ($tags_exploded as $tag) {
$tag_trimmed = trim($tag);
if ($tag_trimmed) {
$tags[$tag_trimmed] = $tag_trimmed;
if ($tag_trimmed == 'vote') {
$got_vote = TRUE;
}
}
}
if (!$got_vote) {
$tags['vote'] = 'vote';
}
return $tags;
}
function fivestar_get_targets($field, $instance, $key = FALSE, $entity = FALSE, $langcode = LANGUAGE_NONE) {
$options = array();
$targets = module_invoke_all('fivestar_target_info', $field, $instance);
if ($key == FALSE) {
foreach ($targets as $target => $info) {
$options[$target] = $info['title'];
}
return $options;
}
else {
if (isset($targets[$key]) && !empty($targets[$key]['callback']) && function_exists($targets[$key]['callback'])) {
return call_user_func($targets[$key]['callback'], $entity, $field, $instance, $langcode);
}
}
}
function fivestar_fivestar_target_info($field, $instance) {
$entity_type = $instance['entity_type'];
$bundle = $instance['bundle'];
$options = array(
'none' => array(
'title' => t('None'),
),
);
if (module_exists('node_reference')) {
$field_names = array_keys(field_read_fields(array(
'module' => 'node_reference',
)));
if (!empty($field_names)) {
$field_instances = field_read_instances(array(
'entity_type' => $entity_type,
'bundle' => $bundle,
'field_name' => $field_names,
));
if (!empty($field_instances)) {
foreach ($field_instances as $field_instance) {
$options[$field_instance['field_name']] = array(
'title' => t('Node reference: @field', array(
'@field' => $field_instance['field_name'],
)),
'callback' => '_fivestar_target_node_reference',
);
}
}
}
}
if (module_exists('entityreference')) {
$field_names = array_keys(field_read_fields(array(
'module' => 'entityreference',
)));
if (!empty($field_names)) {
$field_instances = field_read_instances(array(
'entity_type' => $entity_type,
'bundle' => $bundle,
'field_name' => $field_names,
));
if (!empty($field_instances)) {
foreach ($field_instances as $field_instance) {
$options[$field_instance['field_name']] = array(
'title' => t('Entity reference: @field', array(
'@field' => $field_instance['field_name'],
)),
'callback' => '_fivestar_target_entityreference',
);
}
}
}
}
if (module_exists('user_reference')) {
$field_names = array_keys(field_read_fields(array(
'module' => 'user_reference',
)));
if (!empty($field_names)) {
$field_instances = field_read_instances(array(
'entity_type' => $entity_type,
'bundle' => $bundle,
'field_name' => $field_names,
));
if (!empty($field_instances)) {
foreach ($field_instances as $field_instance) {
$options[$field_instance['field_name']] = array(
'title' => t('User reference: @field', array(
'@field' => $field_instance['field_name'],
)),
'callback' => '_fivestar_target_user_reference',
);
}
}
}
}
if ($instance['entity_type'] == 'comment') {
$options['parent_node'] = array(
'title' => t('Parent Node'),
'callback' => '_fivestar_target_comment_parent_node',
);
}
return $options;
}
function _fivestar_target_node_reference($entity, $field, $instance, $langcode) {
$target = array();
$node_reference = $instance['settings']['target'];
if (isset($entity->{$node_reference}[$langcode][0]) && is_numeric($entity->{$node_reference}[$langcode][0]['nid'])) {
$target['entity_id'] = $entity->{$node_reference}[$langcode][0]['nid'];
$target['entity_type'] = 'node';
}
return $target;
}
function _fivestar_target_entityreference($entity, $field, $instance, $langcode) {
$target = array();
$entityreference = $instance['settings']['target'];
$field_info = field_info_field($entityreference);
if (isset($entity->{$entityreference}[$langcode][0]) && isset($entity->{$entityreference}[$langcode][0]['target_id']) && is_numeric($entity->{$entityreference}[$langcode][0]['target_id'])) {
$target['entity_id'] = $entity->{$entityreference}[$langcode][0]['target_id'];
$target['entity_type'] = $field_info['settings']['target_type'];
}
return $target;
}
function _fivestar_target_user_reference($entity, $field, $instance, $langcode) {
$target = array();
$user_reference = $instance['settings']['target'];
if (isset($entity->{$user_reference}[$langcode][0]) && is_numeric($entity->{$user_reference}[$langcode][0]['uid'])) {
$target['entity_id'] = $entity->{$user_reference}[$langcode][0]['uid'];
$target['entity_type'] = 'user';
}
return $target;
}
function _fivestar_target_comment_parent_node($entity, $field, $instance, $langcode) {
return array(
'entity_id' => $entity->nid,
'entity_type' => 'node',
);
}
function _fivestar_allow_vote($element) {
global $user;
$can_revote = FALSE;
if ($element['#allow_revote']) {
$can_revote = TRUE;
}
else {
$criteria = array(
'entity_id' => isset($element['#settings']['content_id']) ? $element['#settings']['content_id'] : NULL,
'entity_type' => isset($element['#settings']['content_type']) ? $element['#settings']['content_type'] : NULL,
'tag' => isset($element['#settings']['tag']) ? $element['#settings']['tag'] : NULL,
'uid' => $user->uid,
);
$can_revote = !votingapi_select_votes($criteria);
}
if (!$can_revote) {
return FALSE;
}
if ($element['#allow_ownvote']) {
return TRUE;
}
if (!isset($element['#settings']['entity_id']) || !isset($element['#settings']['entity_type'])) {
return TRUE;
}
$entity_id = $element['#settings']['entity_id'];
$entity = entity_load($element['#settings']['entity_type'], array(
$entity_id,
));
$entity = $entity[$entity_id];
$uid1 = $entity->uid;
$uid2 = $user->uid;
return $entity->uid != $user->uid;
}
function fivestar_ctools_content_subtype_alter(&$subtype, &$plugin) {
if ($plugin['name'] == 'entity_field' && isset($subtype['subtype_id'])) {
list($entity_type, $field_name) = explode(':', $subtype['subtype_id'], 2);
$field = field_info_field($field_name);
if ($field['type'] == 'fivestar') {
$subtype['render callback'] = 'fivestar_fivestar_field_content_type_render';
}
}
}
function fivestar_fivestar_field_content_type_render($subtype, $conf, $panel_args, $context) {
if (empty($context) || empty($context->data)) {
return;
}
$entity = $context->data;
list($entity_type, $field_name) = explode(':', $subtype, 2);
$ids = entity_extract_ids($entity_type, $entity);
$field = field_info_instance($entity_type, $field_name, $ids[2]);
if (empty($field)) {
return;
}
$language = field_language($entity_type, $entity, $field_name);
if (empty($conf['label']) || $conf['label'] == 'title') {
$label = 'hidden';
$conf['label'] = 'title';
}
else {
$label = $conf['label'];
}
$field_settings = array(
'label' => $label,
'type' => $conf['formatter'],
'pane_settings' => $conf,
);
if (!empty($conf['formatter_settings'])) {
$field_settings['settings'] = $conf['formatter_settings'];
}
$all_values = field_get_items($entity_type, $entity, $field_name, $language);
if (!$all_values) {
$all_values = array();
}
if (isset($conf['delta_reversed']) && $conf['delta_reversed']) {
$all_values = array_reverse($all_values, TRUE);
}
if (isset($conf['delta_limit'])) {
$offset = intval($conf['delta_offset']);
$limit = !empty($conf['delta_limit']) ? $conf['delta_limit'] : NULL;
$all_values = array_slice($all_values, $offset, $limit, TRUE);
}
$clone = clone $entity;
$clone->{$field_name}[$language] = $all_values;
$field_output = field_view_field($entity_type, $clone, $field_name, $field_settings, $language);
if (!empty($field_output) && !empty($conf['override_title'])) {
$field_output['#title'] = filter_xss_admin($conf['override_title_text']);
}
$block = new stdClass();
$block->module = 'entity_field';
if ($conf['label'] == 'title' && isset($field_output['#title'])) {
$block->title = $field_output['#title'];
}
$block->content = $field_output;
$block->delta = $ids[0];
return $block;
}