You are here

function availability_calendars_node_edit_form in Availability Calendars 6

Same name and namespace in other branches
  1. 5 availability_calendars.module \availability_calendars_node_edit_form()

Creates the node edit form.

Return value

array

1 string reference to 'availability_calendars_node_edit_form'
availability_calendars_node_edit in ./availability_calendars.module
Callback function to produce the node edit form.

File

./availability_calendars.module, line 228
Availability Calendars Module.

Code

function availability_calendars_node_edit_form() {
  $nid = check_plain(arg(1));
  $settings = availability_calendar_getsettings('node', $nid);
  $year = check_plain(arg(2));
  $month = check_plain(arg(3));
  $form['nid'] = array(
    '#type' => 'hidden',
    '#default_value' => $nid,
  );
  $form['year'] = array(
    '#type' => 'hidden',
    '#default_value' => $year,
  );
  $form['month'] = array(
    '#type' => 'hidden',
    '#default_value' => $month,
  );
  $month_meta = availability_calendars_month_meta($year, $month, $settings);

  // find all entries in database for this month ($availability, $notes) and pre-populate
  $notes_result = db_query('SELECT week, note FROM {availability_calendars_week} WHERE nid = %d AND year = %d and month = %d', $nid, $year, $month);
  while ($note = db_fetch_array($notes_result)) {
    $notes[$note['week']] = $note['note'];
  }
  $status_result = db_query('SELECT day, status FROM {availability_calendars_day} WHERE nid = %d AND year = %d AND month = %d', $nid, $year, $month);
  while ($status = db_fetch_array($status_result)) {
    $day_status[$status['day']] = $status['status'];
  }
  $day = 1;
  for ($week = 1; $week <= $month_meta['weeksinmonth']; $week++) {
    $form['week-' . $week] = array(
      '#type' => 'fieldset',
      '#title' => t('Week @week', array(
        '@week' => $week,
      )),
    );
    $form['week-' . $week]['note-' . $week] = array(
      '#type' => 'textfield',
      '#title' => t('Note'),
      '#default_value' => $notes[$week],
      '#description' => t('This will be displayed beside the week in the calendar. It could include, for example, a weekly price.'),
    );
    if ($week == 1) {
      $daysinweekremaining = 7 - $month_meta['firstday'];
    }
    else {
      $daysinweekremaining = 7;
    }
    while ($daysinweekremaining > 0 && $day <= $month_meta['daysinmonth']) {
      $form['week-' . $week]['day-' . $day] = array(
        '#type' => 'select',
        '#title' => format_date(mktime(12, 0, 0, $month, $day, $year), 'custom', 'l j F'),
        '#options' => availability_calendars_options(),
        '#default_value' => $day_status[$day] === NULL ? $settings->defaultstatus : $day_status[$day],
      );
      $day++;
      $daysinweekremaining--;
    }
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}