You are here

function makemeeting_update_poll_submit in Make Meeting Scheduler 7

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

makemeeting_update_poll_submit()

Return value

void

File

./makemeeting.module, line 1008
Make Meeting module

Code

function makemeeting_update_poll_submit($form, &$form_state) {

  // poll head
  $node_id = db_query("SELECT nid FROM {makemeeting_poll_heads} WHERE admin_url = :admin_url", array(
    ':admin_url' => $form_state['values']['poll_admin_url'],
  ))
    ->fetchField();
  $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_update('makemeeting_poll_heads')
    ->fields(array(
    'anonym_name' => $form_state['values']['anonym']['user_name'],
    'anonym_email' => $form_state['values']['anonym']['user_email'],
    'email_notification' => $form_state['values']['email_notification'],
    'multiple_allowed' => $form_state['values']['poll_options']['multiple_allowed'],
    'secure' => $form_state['values']['poll_options']['secure'],
    'maybe_option' => $form_state['values']['poll_options']['maybe_option'],
  ))
    ->condition('nid', $node_id)
    ->execute();

  // 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 = :nid", array(
    ':nid' => $node->nid,
  ));
  $days_ids = array();
  foreach ($days_ids_result as $row) {
    $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_update('makemeeting_poll_rows')
        ->fields(array(
        'answer_text' => $day,
      ))
        ->condition('answer_id', $current_answer_id)
        ->execute();
      $answer_id = $current_answer_id;
    }
    else {

      // else we insert a new row with the new day
      $answer_id = db_insert('makemeeting_poll_rows')
        ->fields(array(
        'nid',
        'answer_text',
        'type',
      ), array(
        $node->nid,
        $day,
        1,
      ))
        ->execute();
    }

    // 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 = :answer_id", array(
      ':answer_id' => $answer_id,
    ));
    $options_ids = array();
    foreach ($options_ids_result as $row) {
      $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 = :alter_id", array(
        ':alter_id' => $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 = :answer_id", array(
      ':answer_id' => $days_ids[$i],
    ));
    $i++;
  }
  drupal_set_message(t("Saved."));
  drupal_goto('makemeeting/' . $form_state['values']['poll_admin_url']);
}