<?php

/**
 * @file
 * The Drupal Reset module.
 *
 * Resets the current site by doing one or both of deleting the site files
 * directory, and deleting all database tables and redirecting to install.php.
 */
require_once (dirname(__FILE__) . '/drupal_reset.lib.inc');

/**
 * Implements hook_menu().
 */
function drupal_reset_menu() {
  $items['admin/config/development/drupal_reset'] = array(
    'title' => 'Drupal Reset',
    'description' => 'Resets the current site by doing one or both of deleting the site files directory, and deleting all database tables and redirecting to install.php.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('drupal_reset_admin_settings'),
    'access arguments' => array('reset drupal'),
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}

/**
 * Implements hook_permission().
 */
function drupal_reset_permission() {
  return array(
    'reset drupal' => array(
      'title' => t('Drupal Reset'),
      'description' => t('Resets the current site by doing one or both of deleting the site files directory, and deleting all database tables and redirecting to install.php.'),
      'restrict access' => TRUE,
    ),
  );
}

/**
 * Implements hook_form().
 */
function drupal_reset_admin_settings() {
  if (_drupal_reset_is_supported_database_configuration()) {
    $form['drupal_reset_agree'] = array(
      '#type' => 'radios',
      '#title' => t('Select what to reset'),
      '#prefix' => '<strong>Select this option to delete the files directory and/or the database tables for the current site, resetting the site so that Drupal is ready to be reinstalled from scratch.</strong>',
      '#options' => array(
        'reset_all' => t('Delete all database tables and files'),
        'reset_database' => t('Delete only database tables'),
        'reset_files' => t('Delete only files')
      ),
    );

    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Reset this site'),
    );

    $form['#validate'][] = 'drupal_reset_admin_settings_validate';
    $form['#submit'][] = 'drupal_reset_admin_settings_submit';
  }
  else {
    $form['drupal_reset_message'] = array(
      '#markup' => '<p>Your database configuration is not supported by Drupal Reset. There must be one database (no master/slave) and the table prefix must be set to a string (not an array); use the empty string if you do not want a prefix. See your settings.php file.</p>',
    );
  }

  return $form;
}

/**
 * Validation function for admin settings.
 */
function drupal_reset_admin_settings_validate($form, &$form_state) {
  if (!$form_state['values']['drupal_reset_agree']) {
    form_set_error('drupal_reset_agree', t('You must select something to reset before you submit the form.'));
  }
}

/**
 * Submit handler for admin form.
 */
function drupal_reset_admin_settings_submit($form, &$form_state) {
  $reset_what = $form_state['values']['drupal_reset_agree'];

  if ($reset_what === 'reset_all' || $reset_what === 'reset_files') {
    // Delete the files.
    _drupal_reset_delete_files();
    drupal_set_message(t('All files deleted.'));
  }

  if ($reset_what === 'reset_all' || $reset_what === 'reset_database') {
    // Drop the database
    _drupal_reset_drop_database();

    // Redirect to install page.
    drupal_goto('install.php');
  }

}
