You are here

function fivestar_expand in Fivestar 5

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

Process callback for fivestar_element -- see fivestar_element()

File

./fivestar.module, line 1481
A simple n-star voting widget, usable in other forms.

Code

function fivestar_expand($element) {
  static $fivestar_id = 0;
  if (isset($element['#vote_count'])) {
    $element['vote_count'] = array(
      '#type' => 'hidden',
      '#value' => $element['#vote_count'],
      '#id' => 'edit-vote-count-' . $fivestar_id,
    );
  }
  if (isset($element['#vote_average'])) {
    $element['vote_average'] = array(
      '#type' => 'hidden',
      '#value' => $element['#vote_average'],
      '#id' => 'edit-vote-average-' . $fivestar_id,
    );
  }
  if ($element['#auto_submit'] && !empty($element['#auto_submit_path'])) {
    $element['auto_submit_path'] = array(
      '#type' => 'hidden',
      '#value' => url($element['#auto_submit_path']),
      '#attributes' => array(
        'class' => 'fivestar-path',
      ),
      '#id' => 'edit-auto-submit-path-' . $fivestar_id,
    );
    $element['auto_submit_token'] = array(
      '#type' => 'hidden',
      '#value' => fivestar_get_token($element['#auto_submit_path']),
      '#attributes' => array(
        'class' => 'fivestar-token',
      ),
      '#id' => 'edit-auto-submit-token-' . $fivestar_id,
    );
  }
  if (!isset($element['#default_value'])) {
    $element['#default_value'] = 0;
  }
  $options = array(
    '-' => t('Select rating'),
  );
  $default_value = $element['#default_value'];
  for ($i = 0; $i <= $element['#stars']; $i++) {
    $this_value = ceil($i * 100 / $element['#stars']);
    $next_value = ceil(($i + 1) * 100 / $element['#stars']);

    // Display clear button only if enabled.
    if ($element['#allow_clear'] == TRUE && $i == 0) {
      $options[$this_value] = isset($element['#labels'][$i]) ? t(filter_xss_admin($element['#labels'][$i])) : t('Cancel rating');
    }

    // Display a normal star value.
    if ($i > 0) {
      if (isset($element['#labels'][$i])) {
        $options[$this_value] = $element['#labels'][$i] == '' ? $i : t(filter_xss_admin($element['#labels'][$i]), array(
          '@star' => $i,
          '@count' => $element['#stars'],
        ));
      }
      else {
        $options[$this_value] = t('Give it @star/@count', array(
          '@star' => $i,
          '@count' => $element['#stars'],
        ));
      }
    }

    // Round up the default value to the next exact star value if needed.
    if ($this_value < $element['#default_value'] && $next_value > $element['#default_value']) {
      $default_value = $next_value;
    }
  }
  $element['vote'] = array(
    '#type' => 'select',
    '#options' => $options,
    '#required' => $element['#required'],
    '#default_value' => $default_value,
    '#parents' => $element['#parents'],
    '#id' => 'edit-vote-' . $fivestar_id,
    '#theme' => 'fivestar_select',
    '#weight' => $element['#weight'],
  );

  // If a default value is not exactly on a radio value, round up to the next one
  if ($element['#default_value'] > $this_value && $element['#default_value'] <= $next_value) {
    $element['vote']['#default_value'] = $next_value;
  }

  // Set a class for the display of label text on hover.
  $label_class = $element['#labels_enable'] ? ' fivestar-labels-hover' : '';
  $element['#id'] = 'edit-vote-' . $fivestar_id;
  $element['#prefix'] = '<div class="fivestar-form-item ' . (isset($element['#attributes']['class']) ? $element['#attributes']['class'] : '') . $label_class . '">';
  $element['#suffix'] = '</div>';

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