<?php

/**
 * Implementation of hook_help().
 */
function date_tools_help($section, $arg) {
  switch ($section) {
    case 'admin/content/date/tools':
      return '<p>'. t('Tools for creating and importing dates and calendars.') .'</p>';

    case 'admin/content/date/tools/change':
      return '<p>'. t('Change a date field from one type to another. Very experimental, use at your own risk!') .'</p>';

    case 'admin/content/date/tools/date_wizard':
      $disabled_modules = date_tools_wizard_disabled_modules(array('popup', 'repeat', 'linked'));
      if (!empty($disabled_modules)) {
        drupal_set_message(t('The following modules are required for the wizard to work:') .'<ul><li>'. implode('</li><li>', $disabled_modules) .'</li></ul>', 'error');
      }
      $output =        
      t('Fill out the following form to auto-create a date content type, with a datetime field and matching pre-configured calendar. A calendar and upcoming events block will be created, an ical feed will be added to the calendar, and the mini calendar, calendar legend, and upcoming events blocks will be added to the sidebar of the default theme. Nodes created from this new content type will include a link to the calendar, and the calendar will have a link to the \'add new date\' form. If the Signup module is enabled, Signups will also be enabled for this field. You can also add new date fields to an existing content type by entering the existing content type name instead of creating a new one.') .
      '</p><p>'.
      t('Only a limited set of options are displayed here to make this easy to set up. Once the date has been created you will be able to make other changes to the date settings and add other fields to your new content type on the Manage fields screen, and make changes to the calendar on the Views edit page.') .
      '</p>';
      return $output;
  }
}

function date_tools_perm() {
  return array('administer date tools');
}

function date_tools_menu() {

  $items = array();
  $items['admin/content/date/tools'] = array(
    'title'    => 'Date Tools',
    'description' => 'Tools to import and auto-create dates and calendars.',
    'access arguments'   => array('administer date tools'),
    'page callback' => 'date_tools_page',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/content/date/tools/about'] = array(
    'title' => 'About',
    'description' => 'Tools to import and auto-create dates and calendars.',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -5,
    'priority' => 1,
    'page callback' => 'date_tools_page',
    'access arguments' => array('administer date tools'),
    );
  $items['admin/content/date/tools/date_wizard'] = array(
    'title' => 'Date wizard',
    'description' => 'Easy creation of date content types and calendars.',
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
    'priority' => 1,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('date_tools_wizard_form'),
    'access arguments' => array('administer date tools'),
    'file' => 'date_tools.wizard.inc',
    'path' => drupal_get_path('module', 'date_tools'),
    );
  $items['admin/content/date/tools/import'] = array(
    'title'    => 'Import',
    'access arguments'   => array('administer date tools'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('date_tools_copy_import_event_form'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
    'file' => 'date_tools.event.inc',
    'path' => drupal_get_path('module', 'date_tools'),
  );
  $items['admin/content/date/tools/import/event'] = array(
    'title'    => 'Event import',
    'access arguments'   => array('administer date tools'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('date_tools_copy_import_event_form'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 3,
    'file' => 'date_tools.event.inc',
    'path' => drupal_get_path('module', 'date_tools'),
  );
  $items['date/tools/remove'] = array(
    'title'    => 'Remove calendar',
    'access arguments'   => array('administer date tools'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('date_tools_remove_form', 3),
    'type' => MENU_CALLBACK,
  );
  $items['admin/content/date/tools/change'] = array(
    'title'    => 'Change',
    'access arguments'   => array('administer date tools'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('date_tools_change_type_form'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 3,
    'file' => 'date_tools.change_type.inc',
    'path' => drupal_get_path('module', 'date_tools'),
  );
  return $items;
}

/**
 *  Main Date Tools page
 */
function date_tools_page() {
  $choices = array();
  $choices[] = t('Dates and calendars can be complicated to set up. The !date_wizard makes it easy to create a simple date content type and related calendar.', array('!date_wizard' => l(t('Date wizard'), 'admin/content/date/tools/date_wizard')));
  $content = '<ul><li>'. implode('</li><li>', $choices) .'</li></ul>';

  if (module_exists('calendar')) {
    $calendar_options = variable_get('calendar_default_view_options', array());
    $calendars = array();
    $node_types = node_get_types();
    foreach ($calendar_options as $key => $option) {
      $type_name = str_replace('calendar_', '', $key);
      if (array_key_exists($type_name, $node_types)) {
        $type = $node_types[$type_name];
        $calendars[] = array(
          l($type->name, 'admin/content/node-type/'. $type_name .'/fields'), 
          l($key, 'admin/build/views/edit/'. $key), 
          t('The calendar %view is a default calendar created for the content type %type_name.', array('%view' => $key, '%type_name' => $type->name)),
          l(t('remove !view', array('!view' => $key)), 'date/tools/remove/' . $key),
        );
      }
      else {
        // Do some cleanup while we're here if we have default calendars
        // for non-existent content types.
        calendar_remove($type_name);
      }
    }
    if (!empty($calendars)) {
      $headers = array(t('Content Type'), t('Calendar'), t('Description'), t('Operations'));
      $content .= theme('table', $headers, $calendars);
    }
  }

  return $content;
}

/**
 * Menu callback; present a form for removing a field from a content type.
 */
function date_tools_remove_form(&$form_state, $view_name) {
  $form = array();
  $form['view_name'] = array(
    '#type' => 'value',
    '#value' => $view_name,
  );
  $output = confirm_form($form,
    t('Are you sure you want to remove the view %view?', array('%view' => $view_name)),
    'admin/content/date/tools',
    t('This action cannot be undone.'),
    t('Remove'), t('Cancel')
  );

  return $output;
}

/**
 * Remove a field from a content type.
 */
function date_tools_remove_form_submit($form, &$form_state) {
  $form_values = $form_state['values'];
  $view_name = $form_values['view_name'];

  if ($view_name && $form_values['confirm']) {
    calendar_remove($view_name);
    drupal_set_message(t('Removed calendar %calendar.', array(
      '%calendar' => $view_name)));
  }
  $form_state['redirect'] = 'admin/content/date/tools';
}
