<?php

function migrate_ui_help($path, $arg) {
  switch ($path) {
    case 'admin/content/migrate':
      return t('The current status of each migration group defined in this system. Click on a group name for details on its configuration.');
  }
}

/**
 * Implementation of hook_menu().
 */
function migrate_ui_menu() {
  $items = array();

  $items['admin/content/migrate'] = array(
    'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM,
    'title' => 'Migrate',
    'description' => 'Manage importing of data into your Drupal site',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('migrate_ui_migrate_dashboard'),
    'access arguments' => array(MIGRATE_ACCESS_BASIC),
    'access callback' => 'user_access',
    'file' => 'migrate_ui.pages.inc',
  );
  $items['admin/content/migrate/groups'] = array(
    'title' => 'Dashboard',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );

  $items['admin/content/migrate/configure'] = array(
    'title' => 'Configuration',
    'type' => MENU_LOCAL_TASK,
    'description' => 'Configure migration settings',
    'page callback' => 'migrate_ui_configure',
    'access arguments' => array(MIGRATE_ACCESS_ADVANCED),
    'file' => 'migrate_ui.pages.inc',
    'weight' => 100,
  );

  // Add tabs for each implemented migration wizard.
  $wizards = migrate_ui_wizards();
  foreach ($wizards as $wizard_class => $wizard) {
    $items["admin/content/migrate/new/$wizard_class"] = array(
      'type' => MENU_LOCAL_TASK,
      'title' => 'Import from @source_title',
      'title arguments' => array('@source_title' => $wizard->getSourceName()),
      'page callback' => 'drupal_get_form',
      'page arguments' => array('migrate_ui_wizard', $wizard_class),
      'access arguments' => array(MIGRATE_ACCESS_BASIC),
      'file' => 'migrate_ui.wizard.inc',
    );
  }

  $items['admin/content/migrate/groups/%'] =
    array(
      'title callback' => 'migrate_ui_migrate_group_title',
      'title arguments' => array(4),
      'type' => MENU_NORMAL_ITEM,
      'page callback' => 'drupal_get_form',
      'page arguments' => array('migrate_ui_migrate_group', 4),
      'access arguments' => array(MIGRATE_ACCESS_BASIC),
      'file' => 'migrate_ui.pages.inc',
    );

  $items['admin/content/migrate/groups/%/%'] =
    array(
      'title callback' => 'migrate_ui_migrate_migration_title',
      'title arguments' => array(5),
      'type' => MENU_NORMAL_ITEM,
      'page callback' => 'drupal_get_form',
      'page arguments' => array('migrate_migration_info', 4, 5),
      'access arguments' => array(MIGRATE_ACCESS_BASIC),
      'file' => 'migrate_ui.pages.inc',
    );

  $items['admin/content/migrate/groups/%/%/view'] =
    array(
      'title' => 'View',
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => -10,
    );

  $items['admin/content/migrate/groups/%/%/edit'] =
    array(
      'type' => MENU_LOCAL_TASK,
      'title' => 'Edit',
      'description' => 'Edit migration mappings',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('migrate_ui_edit_mappings', 4, 5),
      'access arguments' => array(MIGRATE_ACCESS_ADVANCED),
      'file' => 'migrate_ui.pages.inc',
    );

  $items['admin/content/migrate/groups/%/%/messages'] =
    array(
      'type' => MENU_LOCAL_TASK,
      'title' => 'Messages',
      'description' => 'View messages from a migration',
      'page callback' => 'migrate_ui_messages',
      'page arguments' => array(4, 5),
      'access arguments' => array(MIGRATE_ACCESS_ADVANCED),
      'file' => 'migrate_ui.pages.inc',
    );

  return $items;
}

/**
 * Title callback for the migrate group view page.
 */
function migrate_ui_migrate_group_title($group_name) {
  $group = MigrateGroup::getInstance($group_name);
  return $group->getTitle();
}

/**
 * Title callback for the migration view page.
 */
function migrate_ui_migrate_migration_title($migration_name) {
  return $migration_name;
}

/**
* Implements hook_theme()
*
* @return array
*/
function migrate_ui_theme() {
  return array(
    'migrate_ui_field_mapping_form' => array(
      'arguments' => array('field_mappings' => NULL),
      'render element' => 'field_mappings',
      'file' => '/migrate_ui.pages.inc',
    ),
    'migrate_ui_field_mapping_dependencies' => array(
      'arguments' => array('dependencies' => NULL),
      'render element' => 'dependencies',
      'file' => '/migrate_ui.pages.inc',
    ),
  );
}

/**
 * Implementation of hook_mail().
 *
 * @param $key
 * @param $message
 * @param $params
 */
function migrate_ui_mail($key, &$message, $params) {
  $options['language'] = $message['language'];
  user_mail_tokens($variables, array(), $options);
  $langcode = $message['language']->language;
  $subject = variable_get('migrate_drush_mail_subject', '');
  $message['subject'] = t($subject, array(), array('langcode' => $langcode));
  $body = variable_get('migrate_drush_mail_body', '');
  $body .= "\n" . $params['output'];
  $message['body'][] = t($body, array(), array('langcode' => $langcode));
}

/**
 * Get info on all modules supporting a migration wizard.
 *
 * @return array
 *  key: machine_name for a particular wizard implementation. Used in the menu
 *    link.
 *  value: Wizard configuration array containing:
 *    source_title -
 */
function migrate_ui_wizards() {
  $module_apis = migrate_get_module_apis();
  $wizards = array();
  foreach ($module_apis as $info) {
    if (isset($info['wizard classes']) && is_array($info['wizard classes'])) {
      foreach ($info['wizard classes'] as $wizard_class) {
        $wizard_class = strtolower($wizard_class);
        $wizards[$wizard_class] = new $wizard_class;
      }
    }
  }
  return $wizards;
}
