You are here

function fivestar_custom_widget in Fivestar 6

Same name and namespace in other branches
  1. 5 fivestar.module \fivestar_custom_widget()
  2. 6.2 fivestar.module \fivestar_custom_widget()
  3. 7.2 fivestar.module \fivestar_custom_widget()
1 call to fivestar_custom_widget()
fivestar_form in ./fivestar.module
Create the fivestar form for the current item. Note that this is not an implementation of hook_form(). We should probably change the function to reflect that.
2 string references to 'fivestar_custom_widget'
fivestar_views_widget_handler in ./fivestar.module
Generic VotingAPI Views formatter for displaying rating widget.
theme_fivestar_preview in ./fivestar.module

File

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

Code

function fivestar_custom_widget(&$form_state, $values, $settings) {
  $form = array(
    '#attributes' => array(
      'class' => 'fivestar-widget',
    ),
    '#redirect' => FALSE,
    '#theme' => 'fivestar_widget',
  );
  $form['#submit'][] = 'fivestar_form_submit';
  if (isset($settings['content_type'])) {
    $form['content_type'] = array(
      '#type' => 'hidden',
      '#value' => $settings['content_type'],
    );
  }
  if (isset($settings['content_id'])) {
    $form['content_id'] = array(
      '#type' => 'hidden',
      '#value' => $settings['content_id'],
    );
  }
  $settings += array(
    'tag' => 'vote',
  );
  $form['vote'] = array(
    '#type' => 'fivestar',
    '#stars' => $settings['stars'],
    '#vote_count' => $values['count'],
    '#vote_average' => $values['average'],
    '#auto_submit' => isset($settings['autosubmit']) ? $settings['autosubmit'] : TRUE,
    '#auto_submit_path' => !isset($settings['autosubmit']) || $settings['autosubmit'] ? 'fivestar/vote/' . $settings['content_type'] . '/' . $settings['content_id'] . '/' . $settings['tag'] : NULL,
    '#allow_clear' => $settings['allow_clear'],
    '#content_id' => isset($settings['content_id']) ? $settings['content_id'] : NULL,
    '#required' => isset($settings['required']) ? $settings['required'] : FALSE,
    '#feedback_enable' => isset($settings['feedback_enable']) ? $settings['feedback_enable'] : TRUE,
    '#labels_enable' => isset($settings['labels_enable']) ? $settings['labels_enable'] : TRUE,
    '#labels' => isset($settings['labels']) ? $settings['labels'] : NULL,
    '#tag' => $settings['tag'],
  );
  $form['destination'] = array(
    '#type' => 'hidden',
    '#value' => $_GET['q'],
  );
  $form['fivestar_submit'] = array(
    '#type' => 'submit',
    '#value' => t('Rate'),
    '#attributes' => array(
      'class' => 'fivestar-submit',
    ),
  );
  $form['vote']['#attributes']['class'] = isset($form['vote']['#attributes']['class']) ? $form['vote']['#attributes']['class'] : '';
  $settings['feedback_enable'] = isset($settings['feedback_enable']) ? $settings['feedback_enable'] : TRUE;
  switch ($settings['text']) {
    case 'user':
      $form['vote']['#description'] = theme('fivestar_summary', $values['user'], NULL, $settings['style'] == 'dual' ? NULL : $values['count'], $settings['stars'], $settings['feedback_enable']);
      $form['vote']['#attributes']['class'] .= ' fivestar-user-text';
      break;
    case 'average':
      $form['vote']['#description'] = $settings['style'] == 'dual' ? NULL : theme('fivestar_summary', NULL, $values['average'], $values['count'], $settings['stars'], $settings['feedback_enable']);
      $form['vote']['#attributes']['class'] .= ' fivestar-average-text';
      break;
    case 'smart':
      $form['vote']['#description'] = $settings['style'] == 'dual' && !$values['user'] ? NULL : theme('fivestar_summary', $values['user'], $values['user'] ? NULL : $values['average'], $settings['style'] == 'dual' ? NULL : $values['count'], $settings['stars'], $settings['feedback_enable']);
      $form['vote']['#attributes']['class'] .= ' fivestar-smart-text ' . ($values['user'] ? 'fivestar-user-text' : 'fivestar-average-text');
      break;
    case 'dual':
      $form['vote']['#description'] = theme('fivestar_summary', $values['user'], $settings['style'] == 'dual' ? NULL : $values['average'], $settings['style'] == 'dual' ? NULL : $values['count'], $settings['stars'], $settings['feedback_enable']);
      $form['vote']['#attributes']['class'] .= ' fivestar-combo-text';
      break;
  }
  switch ($settings['style']) {
    case 'average':
      $form['vote']['#title'] = t('Average');
      $form['vote']['#default_value'] = $values['average'];
      $form['vote']['#attributes']['class'] .= ' fivestar-average-stars';
      break;
    case 'user':
      $form['vote']['#title'] = t('Your rating');
      $form['vote']['#default_value'] = $values['user'];
      $form['vote']['#attributes']['class'] .= ' fivestar-user-stars';
      break;
    case 'smart':
      $form['vote']['#title'] = $values['user'] ? t('Your rating') : t('Average');
      $form['vote']['#default_value'] = $values['user'] ? $values['user'] : $values['average'];
      $form['vote']['#attributes']['class'] .= ' fivestar-smart-stars ' . ($values['user'] ? 'fivestar-user-stars' : 'fivestar-average-stars');
      break;
    case 'dual':
      $form['vote']['#title'] = t('Your rating');
      $form['vote']['#default_value'] = $values['user'];
      $form['vote']['#attributes']['class'] .= ' fivestar-combo-stars';
      $form['#attributes']['class'] .= ' fivestar-combo-stars';
      $static_average = theme('fivestar_static', $values['average'], $settings['stars'], $settings['tag']);
      if ($settings['text'] == 'none' && !$settings['labels_enable'] && !$settings['feedback_enable']) {
        $static_description = NULL;
      }
      elseif ($settings['text'] != 'none') {
        $static_description = theme('fivestar_summary', NULL, $settings['text'] == 'user' ? NULL : (isset($values['average']) ? $values['average'] : 0), isset($values['count']) ? $values['count'] : 0, $settings['stars'], FALSE);
      }
      else {
        $static_description = ' ';
      }
      $form['average'] = array(
        '#type' => 'markup',
        '#value' => theme('fivestar_static_element', $static_average, $settings['title'] !== FALSE ? t('Average') : NULL, $static_description),
        '#weight' => -1,
      );
      break;
  }

  // Set an over-ridding title if passed in.
  // An empty title won't change the default, a string will set a new title,
  // and title === FALSE will unset the title entirely.
  if (isset($settings['title'])) {
    if ($settings['title'] !== FALSE) {
      $form['vote']['#title'] = $settings['title'];
    }
    else {
      unset($form['vote']['#title']);
      unset($form['average']['#title']);
    }
  }
  return $form;
}