<?php
// $Id: forms.inc frans@vdmi.nl $

/**
 * @file
 * functions for creating and managing forms in the forms module.
 */

function forms_overview() {

  $field_ui = module_exists('field_ui');
  $header = array(t('Name'), t('Machine name'),  t('Description'), array('data' => t('Operations'), 'colspan' => $field_ui ? '5' : '3'));
  $rows = array();
  $result = db_query("SELECT * FROM {forms}");
  while ($forms = $result->fetchObject()) {

    $forms_url_str = str_replace('_', '-', $forms->machine_name);
    $row = array();

    $row[] = check_plain($forms->name);
    $row[] = check_plain($forms->machine_name);
    $row[] = check_plain(nl2br($forms->description));

    // Set the view column.
    $row[] = array('data' => l(t('view'), 'forms/' . $forms_url_str));

    // Set the edit column.
    $row[] = array('data' => l(t('edit'), 'admin/structure/forms/manage/' . $forms_url_str));

    if ($field_ui) {
      // Manage fields.
      $row[] = array('data' => l(t('manage fields'), 'admin/structure/forms/manage/' . $forms_url_str . '/fields'));

      // Display fields.
      $row[] = array('data' => l(t('manage display'), 'admin/structure/forms/manage/' . $forms_url_str . '/display'));
    }

    // Set the delete column.
    $row[] = array('data' => l(t('delete'), 'admin/structure/forms/manage/' . $forms_url_str . '/delete'));


    $rows[] = $row;

  }

  $build['forms_table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('No forms available. <a href="@link">Add form</a>.', array('@link' => url('admin/structure/forms/add'))),
  );

  return $build;
}

function forms_page_title($forms) {
  return t('Form') . ' ' . $forms->name;
}

function forms_form($form, &$form_state, $forms = NULL) {

  if (!isset($forms->machine_name)) {
    $forms = forms_set_defaults();
  }

  $form['name'] = array(
    '#title' => t('Name'),
    '#type' => 'textfield',
    '#default_value' => $forms->name,
    '#description' => t('The human-readable name of this form. It is recommended that this name begin with a capital letter and contain only letters, numbers, and spaces. This name must be unique.'),
    '#required' => TRUE,
    '#size' => 30,
  );

  $form['machine_name'] = array(
    '#type' => 'machine_name',
    '#default_value' => $forms->machine_name,
    '#maxlength' => 32,
    '#machine_name' => array(
      'exists' => 'forms_load',
    ),
    '#description' => t('A unique machine-readable name for this content type. It must only contain lowercase letters, numbers, and underscores. This name will be used for constructing the URL of the forms, in which underscores will be converted into hyphens.'),
  );
  $form['old_machine_name'] = array(
    '#type' => 'value',
    '#value' => $forms->machine_name,
  );
  $form['orig_machine_name'] = array(
    '#type' => 'value',
    '#value' => isset($forms->orig_machine_name) ? $forms->orig_machine_name : '',
  );

  $form['description'] = array(
    '#title' => t('Description'),
    '#type' => 'textarea',
    '#default_value' => $forms->description,
    '#description' => t('Describe this form.'),
  );

  $form['status'] = array(
    '#type' => 'fieldset',
    '#title' => t('Status settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['status']['status'] = array(
    '#type' => 'checkbox',
    '#title' => t('Publish'),
    '#default_value' => $forms->status,
    '#description' => t('This form is published (visible to non-administrators).'),
  );
  $form['status']['submissions'] = array(
    '#type' => 'checkbox',
    '#title' => t('New submissions'),
    '#default_value' => $forms->submissions,
    '#description' => t('This form accepts new submissions.'),
  );

  $form['display'] = array(
    '#type' => 'fieldset',
    '#title' => t('Display settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['display']['submit_label'] = array(
    '#title' => t('Submit label'),
    '#type' => 'textfield',
    '#default_value' => $forms->submit_label,
    '#maxlength' => 32,
    '#description' => t('The translateable label on the submit button.'),
    '#required' => TRUE,
  );

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save form'),
    '#weight' => 40,
  );
  if (isset($forms->machine_name) && !empty($forms->machine_name)) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete form'),
      '#weight' => 45,
    );
  }
  return $form;
}

function forms_form_validate($form, &$form_state) {

  if (in_array(trim($form_state['values']['machine_name']), array('0', 'theme'))) {
    form_set_error('machine_name', t("Invalid machine-readable name. Enter a name other than %invalid.", array('%invalid' => trim($form_state['values']['machine_name']))));
  }

}

function forms_form_submit($form, &$form_state) {

  $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';

  $forms = forms_set_defaults();
  $forms->machine_name = trim($form_state['values']['machine_name']);
  $forms->orig_machine_name = trim($form_state['values']['orig_machine_name']);
  $forms->old_machine_name = isset($form_state['values']['old_machine_name']) ? $form_state['values']['old_machine_name'] : $forms->machine_name;
  $forms->name = trim($form_state['values']['name']);
  $forms->description = $form_state['values']['description'];
  $forms->status = $form_state['values']['status'];
  $forms->submissions = $form_state['values']['submissions'];
  $forms->submit_label = $form_state['values']['submit_label'];

  if ($op == t('Delete form')) {
    $form_state['redirect'] = 'admin/structure/forms/manage/' . str_replace('_', '-', $forms->old_machine_name) . '/delete';
    return;
  }

  switch (forms_save($forms)) {
    case SAVED_UPDATED:
      drupal_set_message(t('The form %name has been updated.', array('%name' => $forms->name)));
      break;
    case SAVED_NEW:
      drupal_set_message(t('The form %name has been added.', array('%name' => $forms->name)));
      watchdog('forms', 'Added form %name.', array('%name' => $forms->name), WATCHDOG_NOTICE, l(t('view'), 'admin/structure/forms/manage/' . str_replace('_', '-', $forms->machine_name)));
      break;
  }

  $form_state['redirect'] = 'admin/structure/forms/manage/' . str_replace('_', '-', $forms->machine_name);
  return;

}
function forms_delete_confirm($form, &$form_state, $forms) {
  $form['machine_name'] = array('#type' => 'value', '#value' => $forms->machine_name);
  $form['name'] = array('#type' => 'value', '#value' => $forms->name);

  $message = t('Are you sure you want to delete the form %machine_name?', array('%machine_name' => $forms->machine_name));
  $caption = '';

  $num_submissions = db_query("SELECT COUNT(fsid) FROM {forms_submission} WHERE machine_name = :machine_name", array(':machine_name' => $forms->machine_name))->fetchField();
  if ($num_submissions) {
    $caption .= '<p>' . format_plural($num_submissions, '%machine_name has 1 submission. If you remove this form, you will not be able to edit the %machine_name submission and it may not display correctly.', '%machine_name hase @count submissions. If you remove %machine_name, you will not be able to edit the %machine_name submissions and it may not display correctly.', array('%machine_name' => $forms->machine_name)) . '</p>';
    $form['submissions'] = array(
      '#type' => 'checkbox',
      '#title' => t('Delete submissions'),
      '#default_value' => 0,
      '#description' => t('Delete the submissions also.'),
    );
  }

  $caption .= '<p>' . t('This action cannot be undone.') . '</p>';

  return confirm_form($form, $message, 'admin/structure/forms', $caption, t('Delete'));
}

function forms_delete_confirm_submit($form, &$form_state) {
  forms_delete($form_state['values']['machine_name']);

  $t_args = array('%name' => $form_state['values']['name']);
  drupal_set_message(t('The form %name has been deleted.', $t_args));
  watchdog('forms', 'Deleted form %name.', $t_args, WATCHDOG_NOTICE);

  if (isset($form_state['values']['submissions']) && $form_state['values']['submissions']) {
    // doe iets met een queue, dit kan in potentie waanzinnig veel tijd kosten.
  }

  menu_rebuild();

  $form_state['redirect'] = 'admin/structure/forms';
  return;
}
