<?php
// $Id: user_relationship_migrate.module,v 1.3.4.4 2007/12/21 09:46:51 sprsquish Exp $

/**
 * Drupal Module: User Relationship Migrate
 *
 * @author: 
 *    Jeff Smick <sprsquish [at] gmail [dot] com>
 *    JB Christy <JBChristy [at] pacbell. [dot] net>
 * @file
 *    Migrate buddylist relationships to user relationships
 */

$db_type = $GLOBALS['db_type'];
$db_type = strpos($db_type, 'mysql') !== FALSE ? 'mysql' : $db_type;
$file = "user_relationship_migrate.{$db_type}";
include_once($file);

/**
 * hook_menu()
 */
function user_relationship_migrate_menu($may_cache) {
  $items = array();

  if ($may_cache) {
    // configuration form (select relationship type)
    $items[] = array(
      'path'      => 'admin/user/relationships/migrate',
      'title'     => t('Migrate from buddylist'),
      'callback'  => 'user_relationship_migrate_page',
      'access'    => user_access('administer user relationships'),
      'type'      => MENU_LOCAL_TASK,
      'weight'    => 4,
    );

    $items[] = array(
      'path'      => 'admin/user/relationships/migrate/error',
      'title'     => t('Migrating from buddylist error'),
      'callback'  => 'user_relationship_migrate_error',
      'access'    => user_access('administer user relationships'),
      'type'      => MENU_CALLBACK,
    );
  }

  return $items;
}

function user_relationship_migrate_page() {
  if ($last_run = variable_get('user_relationship_migrate_last_run', NULL)) {
    $last_run = strftime('%D %T', $last_run);
    drupal_set_message(t("You ran this migration %date", array('%date' => $last_run)));
  }

  return drupal_get_form('user_relationship_migrate_form');
}

/**
 * Migrate relationship form
 *
 * This function just provides the form elements. theme_user_relationship_migrate_form()
 * provides (most of) the supporting text/descriptions.
 */
function user_relationship_migrate_form() {
  $relationships = user_relationships_types_load();
  foreach ($relationships as $relationship) {
    $relationships[$relationship->rtid] = $relationship->name;
  }

  $count = db_result(db_query("SELECT COUNT(*) FROM {buddylist}"));
  $form['migrate'] = array(
    '#type' => 'fieldset',
    '#description' => t("%count buddylist records await your import.", array('%count' => $count))
  );

  $form['migrate']['migration_skip_main'] = array(
    '#title'          => t("Don't do the main migration"),
    '#type'           => 'checkbox',
    '#default_value'  => (bool)variable_get('user_relationship_migrate_last_run', FALSE),
    '#description'    => t("Check this if you've already done the main migration and don't want to re-run it.")
  );
  $form['migrate']['migration_rtid'] = array(
    '#title'    => t('Relationship type for migrated relationships'),
    '#type'     => 'select',
    '#options'  => $relationships,
    '#required' => TRUE
  );
  
  $count = db_result(db_query("SELECT COUNT(*) FROM {buddylist_pending_requests}"));
  $form['migrate']['migrate_pending'] = array(
    '#type'              => 'checkbox',
    '#title'             => t('Migrate %count pending requests', array('%count' => $count)),
    '#default_value'     => TRUE,
  );
  $form['migrate']['migrate_email'] = array(
    '#type'              => 'checkbox',
    '#title'             => t("Migrate users' email settings"),
    '#default_value'     => TRUE,
  );
  $form['migrate']['submit'] = array(
    '#type'              => 'submit',
    '#value'             => t('Migrate!'),
  );

  return $form;
}


/**
 * Validate migrate relationship form submission.
 */
function user_relationship_migrate_form_validate($form_id, &$form_values) {
  if (!empty($form_values['migration_rtid'])) {
    if (!user_relationships_type_load($form_values['migration_rtid'])) {
      form_set_error('migration_rtid', t("You must enter the name of an existing relationship type."));
    }
  }
}


/**
 * Process migrate relationship form submission.
 */
function user_relationship_migrate_form_submit($form_id, &$form_values) {
  variable_del('user_relationship_migrate_last_run');

  if (!$form_values['migration_skip_main']) {
    $start = time();
    if (_user_relationship_migrate_run($form_values['migration_rtid'])) {
      drupal_set_message(t('Buddylist migration successfully ran.'));
      drupal_set_message(t('Buddies Migration took @amount', array('@amount' => gmdate("H:i:s", time() - $start))));
    }
    else {
      drupal_set_message(t('There was an error migrating the buddylist.'), 'error');
    }
  }

  if ($form_values['migrate_pending']) {
    $start = time();

    if (user_relationship_migrate_pending()) {
      drupal_set_message(t('Pending relationships migration successfully ran.'));
      drupal_set_message(t('Pending Buddies Migration took @amount', array('@amount' => gmdate("H:i:s", time() - $start))));
    }
    else {
      drupal_set_message(t('There was a problem migrating pending relationships'), 'error');
    }
  }

  if ($form_values['migrate_email']) {
    $start = time();

    if (user_relationship_migrate_email_preferences()) {
      drupal_set_message(t('Email settings successfully updated.'));
      drupal_set_message(t('Email Preferences Migration took @amount', array('@amount' => gmdate("H:i:s", time() - $start))));
    }
    else {
      drupal_set_message(t('There was an error migrating email preferences.'), 'error');
    }
  }

  variable_set('user_relationship_migrate_last_run', time());

  return 'admin/user/relationships/migrate';
}

