You are here

function rate_widget_form_submit in Rate 7

Same name and namespace in other branches
  1. 6.2 rate.admin.inc \rate_widget_form_submit()

Form submit handler.

File

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

Code

function rate_widget_form_submit($form, &$form_state) {
  if ($form_state['triggering_element']['#value'] == t('Add another option')) {
    $form_state['rebuild'] = TRUE;
    return $form_state['values'];
  }
  $widget = new stdClass();
  $widget->name = $form_state['values']['name'];
  $widget->tag = filter_xss($form_state['values']['tag']);
  $widget->title = filter_xss($form_state['values']['title']);
  $widget->node_types = array();
  $widget->comment_types = array();
  $widget->options = array();
  $widget->template = $form_state['values']['widget_template'];
  $widget->node_display = $form_state['values']['node_display'];
  $widget->teaser_display = (bool) $form_state['values']['teaser_display'];
  $widget->comment_display = $form_state['values']['comment_display'];
  $widget->node_display_mode = $form_state['values']['node_display_mode'];
  $widget->teaser_display_mode = $form_state['values']['teaser_display_mode'];
  $widget->comment_display_mode = $form_state['values']['comment_display_mode'];
  $widget->roles = $form_state['values']['roles'];
  $widget->allow_voting_by_author = $form_state['values']['allow_voting_by_author'];
  $widget->noperm_behaviour = $form_state['values']['noperm_behaviour'];
  $widget->displayed = $form_state['values']['displayed'];
  $widget->displayed_just_voted = $form_state['values']['displayed_just_voted'];
  $widget->description = $form_state['values']['description'];
  $widget->description_in_compact = (bool) $form_state['values']['description_in_compact'];
  $widget->delete_vote_on_second_click = $form_state['values']['delete_vote_on_second_click'];
  if (module_exists('translation')) {
    $widget->use_source_translation = !empty($form_state['values']['use_source_translation']);
  }
  else {
    $widget->use_source_translation = TRUE;
  }
  if ($widget->template != 'custom') {

    // Take over the values from the template.
    $template = _rate_get_template($widget->template);
    if (!$template->customizable) {
      $widget->options = $template->options;
    }
    $widget->value_type = $template->value_type;
    !isset($template->theme) or $widget->theme = $template->theme;
    !isset($template->css) or $widget->css = $template->css;
    !isset($template->js) or $widget->js = $template->js;
    $widget->translate = $template->translate;
    if (isset($form_state['values']['translate'])) {
      $widget->translate = (bool) $form_state['values']['translate'];
    }
  }
  else {
    $widget->value_type = $form_state['values']['value_type'];
    $widget->translate = (bool) $form_state['values']['translate'];
  }
  foreach ($form_state['values'] as $name => $value) {
    if (preg_match('/^value([0-9]+)$/', $name, $match) && preg_match('/^\\-?[0-9]+$/', $value)) {
      if (!empty($form_state['values']['delete' . $match[1]])) {

        // The delete option is checked.
        continue;
      }
      $widget->options[] = array(
        $value,
        filter_xss($form_state['values']['label' . $match[1]]),
      );
    }
    if (preg_match('/^node_type_(.+)$/', $name, $match) && $value) {
      $widget->node_types[] = $match[1];
    }
    if (preg_match('/^comment_type_(.+)$/', $name, $match) && $value) {
      $widget->comment_types[] = $match[1];
    }
  }

  // Let other modules alter the rate widget.
  $hook = 'rate_widget_' . (empty($form['#widget_id']) ? 'insert' : 'update');
  module_invoke_all($hook, $widget, $form_state['values']);

  // Save the widget.
  $widgets = variable_get(RATE_VAR_WIDGETS, array());
  if (!empty($form['#widget_id'])) {
    $id = (int) $form['#widget_id'];
  }
  else {
    $id = 1;
    while (isset($widgets[$id])) {
      ++$id;
    }
  }
  $widgets[$id] = $widget;
  variable_set(RATE_VAR_WIDGETS, $widgets);
  drupal_set_message(t('The widget has been saved.'));
  $form_state['redirect'] = RATE_PATH_ADMIN_PAGE;
}