You are here

function makemeeting_update_poll_submit in Make Meeting Scheduler 6

Same name and namespace in other branches
  1. 7 makemeeting.module \makemeeting_update_poll_submit()

makemeeting_update_poll_submit()

Return value

void

File

./makemeeting.module, line 912
Make Meeting module

Code

function makemeeting_update_poll_submit($form, &$form_state) {

  // poll head
  $node_id = db_result(db_query("SELECT nid FROM {makemeeting_poll_heads} WHERE admin_url = '%s'", $form_state['values']['poll_admin_url']));
  $node = node_load($node_id);

  // save node options
  $node->title = $form_state['values']['title'];
  $node->body = $form_state['values']['body'];
  node_save($node);

  // save poll options
  db_query("UPDATE {makemeeting_poll_heads} SET anonym_name = '%s', anonym_email = '%s', email_notification = %d, multiple_allowed = %d, secure = %d, maybe_option = %d WHERE nid = %d", $form_state['values']['anonym']['user_name'], $form_state['values']['anonym']['user_email'], $form_state['values']['email_notification'], $form_state['values']['poll_options']['multiple_allowed'], $form_state['values']['poll_options']['secure'], $form_state['values']['poll_options']['maybe_option'], $node_id);

  // days and options
  // collect the ids of the days which are already in the db
  $days_ids_result = db_query("SELECT answer_id FROM {makemeeting_poll_rows} WHERE nid = %d", $node->nid);
  $days_ids = array();
  while ($row = db_fetch_array($days_ids_result)) {
    $days_ids[] = $row['answer_id'];
  }

  // insert and update days and options datas
  $i = 0;
  foreach ($form_state['values']['calendar'] as $day => $options_array) {

    // get the first id in the db
    $current_answer_id = $days_ids[$i++];

    // if we have an option in this position, we updating it
    if ($current_answer_id > 0) {
      db_query("UPDATE {makemeeting_poll_rows} SET answer_text = '%s' WHERE answer_id = %d", $day, $current_answer_id);
      $answer_id = $current_answer_id;
    }
    else {

      // else we insert a new row with the new day
      db_query("INSERT INTO {makemeeting_poll_rows} (nid, answer_text, type) VALUES (%d, '%s', %d)", $node->nid, $day, 1);
      $answer_id = db_result(db_query("SELECT MAX(answer_id) FROM {makemeeting_poll_rows}"));
    }

    // at here we have in $answer_id the acutal day, so we updating the day's option
    // the method is the same
    // collecting the options ids for the day
    $options_ids_result = db_query("SELECT alter_id FROM {makemeeting_poll_alters} WHERE answer_id = %d", $answer_id);
    $options_ids = array();
    while ($row = db_fetch_array($options_ids_result)) {
      $options_ids[] = $row['alter_id'];
    }

    // drive through the options array, and update the old or insert the new one
    $j = 0;
    foreach ($options_array as $option) {
      $current_alter_id = $options_ids[$j++];
      if ($current_alter_id > 0) {
        db_query("UPDATE {makemeeting_poll_alters} SET alter_text = '%s' WHERE alter_id = %d", $option, $current_alter_id);
      }
      else {
        db_query("INSERT INTO {makemeeting_poll_alters} (answer_id, alter_text) VALUES (%d, '%s')", $answer_id, $option);
      }
    }

    // if there is some unused id in $options_ids, that's mean we deleted them
    while ($j < sizeof($options_ids)) {
      db_query("DELETE FROM {makemeeting_poll_alters} WHERE alter_id = %d", $options_ids[$j]);
      $j++;
    }
  }

  // if there is some unused id in $days_ids, that's mean we deleted them
  while ($i < sizeof($days_ids)) {
    db_query("DELETE FROM {makemeeting_poll_rows} WHERE answer_id = %d", $days_ids[$i]);
    $i++;
  }
  drupal_set_message(t("Saved."));
  drupal_goto('makemeeting/' . $form_state['values']['poll_admin_url']);
}