You are here

function fivestar_comment_form_alter in Fivestar 6.2

Same name and namespace in other branches
  1. 5 fivestar_comment.module \fivestar_comment_form_alter()
  2. 6 fivestar_comment.module \fivestar_comment_form_alter()

Form alter specification for comments.

File

./fivestar_comment.module, line 21

Code

function fivestar_comment_form_alter(&$form, &$form_state, $form_id) {

  // Comment settings.
  if ($form_id == 'fivestar_node_type_tag_form') {
    $tag = $form_state['fivestar_tag'];
    $type_name = $form_state['fivestar_node_type'];
    $suffix = fivestar_get_suffix($type_name, $tag);
    $form['comment'] = array(
      '#type' => 'fieldset',
      '#title' => t('Comment widget'),
      '#description' => t('Enabling Fivestar for comments will display a rating widget when a user posts a comment. The rating of the comment will affect its parent content.'),
      '#weight' => 1,
    );
    $form['comment']['fivestar_comment'] = array(
      '#type' => 'radios',
      '#title' => t('Fivestar comment settings'),
      '#options' => array(
        FIVESTAR_COMMENT_DISABLED => t('Disabled'),
        FIVESTAR_COMMENT_OPTIONAL => t('Optional rating'),
        FIVESTAR_COMMENT_REQUIRED => t('Required rating'),
      ),
      '#default_value' => variable_get('fivestar_comment_' . $suffix, FIVESTAR_COMMENT_DISABLED),
    );
    $form['comment']['fivestar_comment_preview'] = array(
      '#type' => 'item',
      '#title' => t('Comment widget preview'),
      '#value' => theme('fivestar_preview', 'compact', 'none', $form['fivestar_stars']['#default_value'], $form['comment']['fivestar_comment']['#default_value'] == 1 ? 1 : 0),
    );
    if (!$form['fivestar']['#default_value'] || !$form['comment']['fivestar_comment']['#default_value']) {
      $form['comment']['fivestar_comment_preview']['#value'] = theme('fivestar_preview_wrapper', '', 'comment');
    }
    else {
      $form['comment']['fivestar_comment_preview']['#value'] = theme('fivestar_preview_wrapper', $form['comment']['fivestar_comment_preview']['#value'], 'comment');
    }
  }

  // Comment form. Do not allow ratings inside of threads.
  if ($form_id == 'comment_form' && empty($form['pid']['#value']) && user_access('rate content')) {
    $node = node_load($form['nid']['#value']);

    // Splice in the fivestar right before the body.
    $new_form = array();
    foreach ($form as $key => $element) {
      if ($key == 'comment_filter') {
        foreach (fivestar_get_tags() as $tag) {
          if ($form['cid']['#value']) {
            $current_rating = fivestar_comment_load($form['cid']['#value'], $form['nid']['#value']);
            $default_value = $current_rating[$tag]['value'];
          }
          else {
            $votes = fivestar_get_votes('node', $form['nid']['#value'], $tag);
            $default_value = isset($votes['user']['value']) ? $votes['user']['value'] : 0;
          }
          if (fivestar_validate_target('node', $node->nid, $tag)) {
            $suffix = fivestar_get_suffix($node->type, $tag);
            if (variable_get('fivestar_comment_' . $suffix, FIVESTAR_COMMENT_DISABLED)) {
              $new_form['fivestar_rating']['fivestar_rating_tag_' . $tag] = array(
                '#type' => 'fivestar',
                '#title' => t($tag),
                '#stars' => variable_get('fivestar_stars_' . $node->type, 5),
                '#allow_clear' => variable_get('fivestar_comment_' . $suffix, FIVESTAR_COMMENT_DISABLED) == FIVESTAR_COMMENT_OPTIONAL ? 1 : 0,
                '#content_id' => $node->nid,
                '#required' => variable_get('fivestar_comment_' . $suffix, FIVESTAR_COMMENT_DISABLED) == FIVESTAR_COMMENT_REQUIRED ? 1 : 0,
                '#default_value' => $default_value,
                '#labels' => variable_get('fivestar_labels_' . $suffix, array()),
              );
            }
          }
        }
      }
      $new_form[$key] = $element;
    }
    if ($new_form['fivestar_rating']) {
      $form = $new_form;
    }
  }
}