You are here

function rate_widget_form in Rate 6.2

Same name and namespace in other branches
  1. 7 rate.admin.inc \rate_widget_form()

Form for adding and editing widgets.

1 string reference to 'rate_widget_form'
rate_menu in ./rate.module
Implements hook_menu().

File

./rate.admin.inc, line 115
Rating admin

Code

function rate_widget_form(&$form_state, $id = NULL) {
  $form = array();

  // Workaround core issue #591696 in AHAH forms.
  // See http://drupal.org/node/591696#comment-3369036 for details.
  if ($id) {
    $form['#action'] = url(str_replace('%', $id, RATE_PATH_ADMIN_PAGE_EDIT));
  }
  else {
    $form['#action'] = url(RATE_PATH_ADMIN_PAGE_ADD);
  }
  if ($id) {
    $widgets = variable_get(RATE_VAR_WIDGETS, array());
    if (!isset($widgets[$id])) {
      drupal_not_found();
      module_invoke_all('exit') & exit;
    }
    $widget = $widgets[$id];
  }
  else {
    $widget = new stdClass();
    $widget->name = '';
    $widget->tag = 'vote';
    $widget->title = '';
    $widget->node_types = array();
    $widget->comment_types = array();
    $widget->value_type = 'percent';
    $widget->options = array();
    $widget->template = 'custom';
    $widget->node_display = RATE_DISPLAY_BELOW_CONTENT;
    $widget->teaser_display = FALSE;
    $widget->node_display_mode = RATE_FULL;
    $widget->teaser_display_mode = RATE_FULL;
    $widget->comment_display_mode = RATE_FULL;
    $widget->comment_display = RATE_DISPLAY_BELOW_CONTENT;
    $widget->roles = array();
    $widget->allow_voting_by_author = TRUE;
    $widget->noperm_behaviour = RATE_NOPERM_REDIRECT_WITH_MESSAGE;
    $widget->displayed = RATE_AVERAGE;
    $widget->displayed_just_voted = RATE_USER;
  }

  // Determine the template from GET (which is only available before the form
  // was post) or from post values (only available after the form was posted).
  if (isset($form_state['post']['widget_template'])) {
    $widget->template = $form_state['post']['widget_template'];
  }
  elseif (isset($_GET['template'])) {

    // The template can be overridden (also used for changing templates).
    $widget->template = $_GET['template'];
  }

  // Check widget for missing values (legacy).
  _rate_check_widget($widget);
  if ($widget->template != 'custom') {
    if (!($template = _rate_get_template($widget->template))) {
      $form['error'] = array(
        '#value' => t('You cannot edit this widget because it was built using the template %template which does not exists anymore.', array(
          '%template' => $widget->template,
        )),
      );
      return $form;
    }
    $widget->customizable = isset($template->customizable) ? $template->customizable : TRUE;
    if ($widget->customizable && !$id) {
      $widget->options = $template->options;
      $widget->value_type = $template->value_type;
    }
  }
  if ($id) {
    $form['#widget_id'] = $id;
  }

  // The GET value for the template is not available anymore after posting,
  // so we need to add the template as a hidden value.
  $form['widget_template'] = array(
    '#type' => 'hidden',
    '#value' => $widget->template,
  );
  $form['#widget_customizable'] = $widget->customizable;
  if ($id) {
    $link = l(t('change the widget type'), str_replace('%', $id, RATE_PATH_ADMIN_PAGE_TEMPLATE));
    if ($widget->template == 'custom') {
      $text = t('This is a custom widget. You may !change.', array(
        '!change' => $link,
      ));
    }
    else {
      $template = _rate_get_template($widget->template);
      $text = t('This is a %type widget. You may !change.', array(
        '%type' => $template->template_title,
        '!change' => $link,
      ));
    }
    $text = filter_xss($text);
    $form['template'] = array(
      '#type' => 'fieldset',
      '#title' => t('Widget type'),
    );
    $form['template']['switch'] = array(
      '#value' => $text,
    );
  }
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => $widget->title,
    '#required' => TRUE,
  );

  /**
   * @todo Implement http://drupal.org/node/302054#comment-1110505
   */
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Machine readable name'),
    '#default_value' => $widget->name,
    '#required' => TRUE,
  );
  $form['tag'] = array(
    '#type' => 'textfield',
    '#title' => t('Tag'),
    '#default_value' => $widget->tag,
    '#description' => t('Tag used for VotingAPI. Widgets with the same tag share their voting results.'),
    '#required' => TRUE,
  );
  if ($widget->customizable) {
    $options = array(
      'percent' => t('Percentage'),
      'points' => t('Points'),
      'option' => t('Options'),
    );
    $form['value_type'] = array(
      '#type' => 'radios',
      '#title' => t('Value type'),
      '#options' => $options,
      '#default_value' => $widget->value_type,
      '#required' => TRUE,
    );
    $form['options'] = array(
      '#type' => 'fieldset',
      '#title' => t('Options'),
      '#description' => t('Define the available voting buttons.'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['options']['options'] = array(
      '#theme' => 'rate_admin_options',
      '#prefix' => "<div id=\"rate-options\">",
      '#suffix' => '</div>',
    );
    if ($form_state['submitted']) {
      $option_count = (int) $form_state['values']['option_count'];
      if ($form_state['clicked_button']['#id'] == 'edit-add-another') {
        ++$option_count;
      }
    }
    else {
      $option_count = max(2, count($widget->options));
    }
    $id = 0;
    $c = $option_count;
    for ($i = 0; $i < $c; $i++) {
      if ($form_state['submitted'] && !empty($form_state['values']['delete' . $i])) {
        --$option_count;
        continue;
      }
      $form['options']['options']['option' . $id] = array();
      if (isset($form_state['values']['value' . $i])) {
        $default_value = $form_state['values']['value' . $i];
        $default_label = $form_state['values']['label' . $i];
      }
      elseif (isset($widget->options[$i]) && !$form_state['submitted']) {
        $default_value = $widget->options[$i][0];
        $default_label = $widget->options[$i][1];
      }
      else {
        $default_value = '';
        $default_label = '';
      }
      $form['options']['options']['option' . $id]['value' . $id] = array(
        '#type' => 'textfield',
        '#title' => t('Value'),
        '#default_value' => $default_value,
        '#size' => 16,
      );
      $form['options']['options']['option' . $id]['label' . $id] = array(
        '#type' => 'textfield',
        '#title' => t('Label'),
        '#default_value' => $default_label,
        '#size' => 40,
      );
      $form['options']['options']['option' . $id]['delete' . $id] = array(
        '#type' => 'checkbox',
        '#title' => t('Delete'),
        '#default_value' => FALSE,
      );
      ++$id;
    }
    $form['options']['option_count'] = array(
      '#type' => 'hidden',
      '#value' => $option_count,
    );
    $form['options']['add_another'] = array(
      '#type' => 'submit',
      '#value' => t('Add another option'),
      '#ahah' => array(
        'path' => RATE_PATH_ADMIN_AHAH,
        'wrapper' => 'rate-options',
        'method' => 'replace',
        'effect' => 'none',
      ),
    );
    $form['translate'] = array(
      '#type' => 'checkbox',
      '#title' => t('Translate options'),
      '#default_value' => $widget->translate,
    );
  }
  $form['types'] = array(
    '#type' => 'fieldset',
    '#title' => t('Node types'),
    '#description' => t('Select the node types on which to enable this widget.'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#theme' => 'rate_admin_types',
  );
  $sql = 'SELECT type, name FROM {node_type} ORDER BY name ASC';
  $res = db_query($sql);
  while ($rec = db_fetch_array($res)) {
    $form['types'][$rec['type']] = array(
      '#type' => 'fieldset',
      '#title' => $rec['name'],
    );
    $form['types'][$rec['type']]['node_type_' . $rec['type']] = array(
      '#type' => 'checkbox',
      '#title' => $rec['name'],
      '#default_value' => in_array($rec['type'], $widget->node_types),
    );
    $form['types'][$rec['type']]['comment_type_' . $rec['type']] = array(
      '#type' => 'checkbox',
      '#title' => $rec['name'],
      '#default_value' => in_array($rec['type'], $widget->comment_types),
    );
  }
  $form['display'] = array(
    '#type' => 'fieldset',
    '#title' => t('Display settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $display_options = array(
    RATE_DISPLAY_DISABLE => t('Do not add automatically'),
    RATE_DISPLAY_ABOVE_CONTENT => t('Above the content'),
    RATE_DISPLAY_BELOW_CONTENT => t('Below the content'),
    RATE_DISPLAY_LINKS => t('Within the Links'),
  );
  $form['display']['node_display'] = array(
    '#type' => 'radios',
    '#title' => t('Node display'),
    '#options' => $display_options,
    '#default_value' => $widget->node_display,
  );
  $form['display']['teaser_display'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display in teaser'),
    '#default_value' => $widget->teaser_display,
  );
  $display_mode_options = array(
    RATE_FULL => t('Full widget'),
    RATE_DISABLED => t('Display only'),
    RATE_COMPACT_DISABLED => t('Display only, compact'),
    RATE_COMPACT => t('Compact'),
  );
  $form['display']['node_display_mode'] = array(
    '#type' => 'select',
    '#title' => t('Appearance in full node'),
    '#options' => $display_mode_options,
    '#default_value' => $widget->node_display_mode,
  );
  $form['display']['teaser_display_mode'] = array(
    '#type' => 'select',
    '#title' => t('Appearance in teaser'),
    '#options' => $display_mode_options,
    '#default_value' => $widget->teaser_display_mode,
  );
  $form['display']['comment_display'] = array(
    '#type' => 'radios',
    '#title' => t('Comment display'),
    '#options' => $display_options,
    '#default_value' => $widget->comment_display,
  );
  $form['display']['comment_display_mode'] = array(
    '#type' => 'select',
    '#title' => t('Appearance in comments'),
    '#options' => $display_mode_options,
    '#default_value' => $widget->comment_display_mode,
  );
  $form['interaction'] = array(
    '#type' => 'fieldset',
    '#title' => t('Interaction'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('Note that these settings do not apply for rate widgets inside views. Widgets in views will display the average voting when a relationship to the voting results is used and the users vote in case of a relationship to the votes.'),
  );
  $options = array(
    RATE_AVERAGE => t('Average rating'),
    RATE_USER => t('Users vote if available, empty otherwise'),
    RATE_USER_OR_AVERAGE => t('Users vote if available, average otherwise'),
  );
  $form['interaction']['displayed'] = array(
    '#type' => 'radios',
    '#title' => t('Which rating should be displayed?'),
    '#options' => $options,
    '#default_value' => $widget->displayed,
  );
  $options = array(
    RATE_AVERAGE => t('Average rating'),
    RATE_USER => t('Users vote'),
  );
  $form['interaction']['displayed_just_voted'] = array(
    '#type' => 'radios',
    '#title' => t('Which rating should be displayed when the user just voted?'),
    '#options' => $options,
    '#default_value' => $widget->displayed_just_voted,
  );
  $form['permissions'] = array(
    '#type' => 'fieldset',
    '#title' => t('Permissions'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $sql = 'SELECT rid, name FROM {role} ORDER BY name ASC';
  $res = db_query($sql);
  $options = array();
  while ($rec = db_fetch_array($res)) {
    $options[$rec['rid']] = $rec['name'];
  }
  $form['permissions']['roles'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Roles'),
    '#options' => $options,
    '#default_value' => $widget->roles,
    '#description' => t('Allow voting only for the selected role(s). If you select no roles, all users are allowed to vote.'),
  );
  $form['permissions']['allow_voting_by_author'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow author to rate his / her own content'),
    '#default_value' => $widget->allow_voting_by_author,
    '#description' => t('Will change the state to disabled. Always true for anonymous users.'),
  );
  $options = array(
    RATE_NOPERM_REDIRECT_WITH_MESSAGE => t('Redirect to login and show message'),
    RATE_NOPERM_REDIRECT_WITHOUT_MESSAGE => t('Redirect to login but do not show a message'),
    RATE_NOPERM_SHOW_DISABLED_WIDGET => t('Show a disabled widget (with non clickable buttons)'),
    RATE_NOPERM_HIDE_WIDGET => t('Hide widget'),
  );
  $form['permissions']['noperm_behaviour'] = array(
    '#type' => 'radios',
    '#title' => t('Behaviour when user has no permission to vote'),
    '#options' => $options,
    '#default_value' => $widget->noperm_behaviour,
    '#description' => t('Choose an action what will happen if a user clicks on the widget but doesn\'t have the permission to vote.'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#weight' => 50,
  );
  return $form;
}