You are here

function fivestar_expand in Fivestar 7.2

Same name and namespace in other branches
  1. 5 fivestar.module \fivestar_expand()
  2. 6.2 fivestar.module \fivestar_expand()
  3. 6 fivestar.module \fivestar_expand()

Process callback for fivestar_element -- see fivestar_element()

1 call to fivestar_expand()
fivestar_ajax_submit in ./fivestar.module
AJAX submit handler for fivestar_custom_widget.
1 string reference to 'fivestar_expand'
fivestar_element_info in ./fivestar.module
Implements hook_elements().

File

./fivestar.module, line 613

Code

function fivestar_expand($element, $form_state, $complete_form) {
  if (!_fivestar_allow_vote($element)) {
    $element['#input'] = FALSE;
  }

  // Add CSS and JS.
  $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'],
    ));
  }

  // Display clear button only if enabled.
  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 = '&nbsp;';
      }
      $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>';

  // Add AJAX handling if necessary.
  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'],
      )),
    );
  }

  // Add validation function that considers a 0 value as empty.
  $element['#element_validate'] = array(
    'fivestar_validate',
  );
  return $element;
}