<?php

/**
 * @file
 * Import or update users with data from a comma separated file (csv).
 */

// Update options for existing users
define ('UPDATE_NONE', 0);
define ('UPDATE_REPLACE', 1);
define ('UPDATE_ADD', 2);

/**
 * - - - - - - - -  HOOKS - - - - - - - - 
 */

/**
 * Implementation of hook_theme().
 */
function user_import_theme() {
  return array(
    'user_import_list' => array(
      'arguments' => array(),
    ),
    'user_import_edit' => array(
      'arguments' => array('form' => NULL),
    ),
    'user_import_errors_display' => array(
      'arguments' => array('settings' => NULL, 'data' => NULL, 'total' => NULL),
    ),
    'user_import_username_errors' => array(
      'arguments' => array('errors' => NULL),
    ), 
  );
}

/**
 * Implementation of hook_help().
 */
function user_import_help($path, $arg) {
  switch ($path) {
    case 'admin/user/user_import':
      return t("Import or update users from a comma separated file (csv). Click 'Import' to start a new import.");
  }
}

/**
 * Implementation of hook_perm().
 */
function user_import_perm() {
  return array('import users');
}

/**
 * Implementation of hook_menu().
 */
function user_import_menu() {
  $items['admin/user/user_import'] = array(
      'title' => 'User Imports',
      'description' => 'Import or update users from a comma separated file (csv).',
      'page callback' => 'user_import_list',
      'access arguments' => array('import users'),
      'file' => 'user_import.admin.inc',
      );
  $items['admin/user/user_import/list'] = array( 
      'title' => 'List Imports', 
      'access arguments' => array('import users'),
      'weight' => -10,
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'file' => 'user_import.admin.inc',
      );
  $items['admin/user/user_import/add'] = array( 
    'title' => 'Import',
    'page callback' => 'user_import_preferences',
    'access arguments' => array('import users'),
    'weight' => -5,
    'type' => MENU_LOCAL_TASK,
    'file' => 'user_import.admin.inc',
  );
  $items['admin/user/user_import/continue'] = array( 
    'title' => 'Continue',
    'page callback' => 'user_import_continue',
    'access arguments' => array('import users'),
    'type' => MENU_CALLBACK,
    'file' => 'user_import.admin.inc',
  );
  $items['admin/user/user_import/import'] = array( 
    'title' => 'Import',
    'page callback' => 'user_import_import',
    'access arguments' => array('import users'),
    'type' => MENU_CALLBACK,
    'file' => 'user_import.admin.inc',
  );
  $items['admin/user/user_import/delete'] = array(
    'title' => 'Delete Import',
    'page callback' => 'user_import_delete',
    'access arguments' => array('import users'),
    'type' => MENU_CALLBACK,
    'file' => 'user_import.admin.inc',
  );
  $items['admin/user/user_import/configure'] = array(
    'title' => 'Configure',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('user_import_configure_form'),
    'access arguments' => array('import users'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'user_import.admin.inc',
  );
  $items['user_import/delete'] = array( 
    'title' => 'Remove Info',
    'page callback' => 'user_import_limited_delete',
    'type' => MENU_CALLBACK,
    'access arguments' => array('limited user import'),
  );
  $items['user_import/errors'] = array(
    'title' => 'Import Errors',
    'page callback' => 'user_import_limited_errors',
    'type' => MENU_CALLBACK,
    'access arguments' => array('limited user import'),
  ); 

  return $items;
}

/**
 * Implementation of hook_cron().
 */
function user_import_cron() {
  module_load_include('inc', 'user_import', 'user_import.import');
	$imports = _user_import_settings_select();

	if (!$imports) return;

	foreach ($imports as $import) {
		if ($import['setting'] == 'test' || $import['setting'] == 'import') { 
			_user_import_process($import);
		}
	}
   
	return;
}

// - - - - - - - -  FORMS - - - - - - - - 

/**
 * Saves options on content type configuration form 
 * @todo check if this is cruft
 * @todo check $form['type']
 */
function user_import_content_type_submit($form, &$form_state) {
  // user import template for Organic Groups content type
  $templates = variable_get('user_import_og_template', array());
  $templates[$form['type']] = $form_state['values']['user_import_og'];
  variable_set('user_import_og_template', $templates);
}

// - - - - - - - -  PAGES - - - - - - - - 

function user_import_limited_errors($import_id = NULL, $template_id = NULL) {

  if (empty($import_id) || !is_numeric($import_id)) drupal_goto('user_import/' . $template_id);
 
  $pager_id = 1;
  $max = 25;
  $import = _user_import_settings_select($import_id);
  
  $total = db_result(db_query("SELECT count(data) FROM {user_import_errors} WHERE import_id = %d", $import['import_id']));
  
  if (empty($total))  {
  
    $output .= theme('There were no import errors');
  
  } else {
  
    $results = pager_query("SELECT * FROM {user_import_errors} WHERE import_id = %d", $max, $pager_id, NULL, array($import['import_id']));
    
    while ($line = db_fetch_array($results)) {

      $line['data'] = unserialize($line['data']);
      $line['errors'] = unserialize($line['errors']);
      $file_lines[] = $line;
    }
 
    $output .= theme('user_import_errors_display', $import, $file_lines, $total);
    $output .= theme('pager', NULL, $max, $pager_id); 
  }
  
  $output .= l(t('Return'), "user_import/$template_id/$import_id");
 
  return $output;
}

function user_import_limited_delete($import_id = NULL, $template_id = NULL) { 
  user_import_delete($import_id, "user_import/$template_id");
}

// - - - - - - - -  THEMES - - - - - - - - 

function theme_user_import_list() {
  $output = '';
  $imports = _user_import_settings_select();
  
  if (!$imports) return ' ';

  foreach ($imports as $import) {

      // header labels
      $import_label = ($import['setting'] == 'tested' || $import['setting'] == 'test') ? t('importable') : t('imported');
      $header = array(t('file'), t('started'), t('processed'), $import_label, t('errors'), t('status'));
                  
      // info row
      $errors = db_result(db_query("SELECT COUNT(import_id) FROM {user_import_errors} WHERE import_id = %d", $import['import_id']));         
      $errors_link = ($errors == 0) ? '0': l($errors, 'admin/user/user_import/errors/' . $import['import_id']);           
      
      $rows[0] = array(
          check_plain($import['oldfilename']),
          format_date($import['started'], 'small'),
          array("data" => $import['processed'], "align" => 'center'),
          array("data" => $import['valid'], "align" => 'center'),
          array("data" => $errors_link, "align" => 'center'),
          $import['setting'],
      );
      
      $output .= theme('table', $header, $rows);
      
      // action buttons
      $settings_link = l(t('Settings'), 'admin/user/user_import/add/' . $import['import_id']);
      $delete_link = l(t('Delete'), 'admin/user/user_import/delete/' . $import['import_id']);
      $continue_link = l(t('Continue Processing'), 'admin/user/user_import/continue/' . $import['import_id']);
      $import_link = l(t('Import'), 'admin/user/user_import/import/' . $import['import_id']);
      
      $output .= $settings_link  . ' | ';   
      $output .= $delete_link;   
      if ($import['setting'] == 'tested' || $import['setting'] == 'test') $output .= ' | ' . $import_link;
      if ($import['setting'] == 'test' || $import['setting']  == 'import') $output .= ' | ' . $continue_link;
  } 
  
  return $output;
}

function theme_user_import_edit($form) {
 
  $header = array(t('CSV column'), t('Drupal fields'), t('Username'), t('Abbreviate'));

  foreach (element_children($form['field_match']) as $key) {

	  $rows[] = array(
	      drupal_render($form['field_match'][$key]['csv']),
	      drupal_render($form['field_match'][$key]['field_match']),
	      drupal_render($form['field_match'][$key]['username']),
	      drupal_render($form['field_match'][$key]['abbreviate']),
	  );
  }

  $form['field_match']['#value'] = theme('table', $header, $rows);

  $output = drupal_render($form['remove']);
  $output .= drupal_render($form['options']);
  $output .= drupal_render($form['field_match']);
  $output .= drupal_render($form);
  return $output;
}

function theme_user_import_errors_display($settings, $data, $total) {
    $output = '';
    $error_count = 0;
    $field_match = _user_import_unconcatenate_field_match($settings['field_match']);
    $header[0] = t('Email Address');

    foreach ($data as $data_row) {

        $row = NULL;
        
        foreach ($data_row['data'] as $type => $fields) {
   
          if (!empty($fields)) {
          
            foreach ($fields as $field_id => $field_data) {
  
              foreach ($field_match as $column_info) {
              
                if ($column_info['type'] ==  $type && $column_info['field_id'] == $field_id) {
                  
                  if (!empty($column_info['username'])) { 
                    $header[$column_info['username']] = t('Name %sort', array('%sort' => $column_info['username']));
                    $row[$column_info['username']] = array("data" => $field_data[0], "align" => "left");
                  }
                  
                  if ($column_info['field_id'] == 'email') { 
                    $row[0] = array("data" => $field_data[0], "align" => "left");
                  }
                }
              }
            
            } 
          }  
        }
  
        ksort($row);
        $row[] = implode('<br />', $data_row['errors']);
        $rows[] = $row;
    }
    
    $output .= '<p>' . t('<strong>CSV File:</strong> %file', array('%file' => $settings['oldfilename'])) . '<br />';
    $output .= t('<strong>Errors:</strong> !total', array('!total' => $total)) . '</p>';

    $header['errors'] = t('Errors');
    $output .= theme('table', $header, $rows);    
    return $output;
}

function theme_user_import_username_errors($errors) {
    
    if (empty($errors)) {
        $output = '<p><strong>' . t('All usernames are OK.') . '</strong></p>';
    } else {
        $header = array(t('User ID'), t('Email'), t('Username'), t('Error'));
        $output = theme('table', $header, $errors);
    }
    
    return $output;
}

// - - - - - - - -  MISC - - - - - - - - 

function _user_import_settings_save($settings) {
  // Database field defaults.
  $database_fields = array('import_id' => NULL, 
													 'name' => '', 
													 'filename' => '', 
													 'oldfilename' => '', 
													 'filepath' => '', 
													 'started' => 0, 
													 'pointer' => 0, 
													 'processed' => 0, 
													 'valid' => 0, 
													 'field_match' => array(), 
													 'roles' => '', 
													 'options' => array(), 
													 'setting' => '',
													);												

 	// Form elements we never want to save in the options column.
  $form_variables = array('form_id', 'form_token', 'form_build_id', 'cancel', 'import', 'submit', 'op', 0, 'return_path');
  $form_variables = array_flip($form_variables);

  // Remove settings we don't need in the options column.
	$options = array_diff_key($settings, $database_fields);
  $options = array_diff_key($options, $form_variables);

  // Set defaults for options.
	foreach ($options as $key => $value) {
		$settings['options'][$key] = isset($settings[$key]) ? $settings[$key] : '';
	}

  // Set defaults for fields. 
	foreach ($database_fields as $key => $value) {
		$settings[$key] = isset($settings[$key]) ? $settings[$key] : $value;
	}
   
   // Update settings for existing import
   if (isset($settings['import_id'])) {
   
       db_query("UPDATE {user_import} 
           SET name = '%s', filename = '%s', oldfilename = '%s', filepath = '%s', pointer = %d, processed = %d, valid= %d, field_match = '%s', roles = '%s', options = '%s', setting = '%s' 
           WHERE import_id = %d
           ", trim($settings['name']), $settings['filename'], $settings['oldfilename'], $settings['filepath'], $settings['pointer'], $settings['processed'], $settings['valid'], serialize($settings['field_match']), serialize($settings['roles']), serialize($settings['options']), $settings['setting'], $settings['import_id']);
       
   // Save settings for new import
   } 
   else {

       db_query("INSERT INTO {user_import} 
           (name, filename, oldfilename, filepath, started, pointer, processed, valid, field_match, roles, options, setting) 
           VALUES ('%s', '%s', '%s', '%s', %d, %d, %d, %d, '%s', '%s', '%s', '%s')
           ", trim($settings['name']), $settings['filename'], $settings['oldfilename'], $settings['filepath'], time(), $settings['pointer'], $settings['processed'], $settings['valid'], serialize($settings['field_match']), serialize($settings['roles']), serialize($settings['options']), $settings['setting']);

       switch ($GLOBALS['db_type']) {
         case 'mysql':
         case 'mysqli':
           $settings['import_id'] = db_result(db_query("SELECT LAST_INSERT_ID()"));
           break;
         case 'pgsql':
           $settings['import_id'] = db_result(db_query("SELECT currval('{user_import_import_id_seq}')"));
           break;
       }
   }
   
   return $settings;
}

/**
 *  Return either a single import setting, or all template, or all non-template settings.
 */
function _user_import_settings_select($import_id = NULL, $template = FALSE) {

  $import = array();

  if (!empty($import_id) && !is_numeric($import_id)) return;

  if (!empty($import_id)) {
	  $sql = 'SELECT * FROM {user_import} WHERE import_id = %d';
	  if ($template) $sql .=  " AND setting = 'template'";
	  $import = db_fetch_array(db_query_range($sql, $import_id, 0, 1));

	  if (empty($import)) return FALSE;

	  $import['field_match'] = unserialize($import['field_match']);
	  $import['roles'] = unserialize($import['roles']);
	  $import['options'] = unserialize($import['options']);
	
		foreach ($import['options'] as $key => $value) {
			$import[$key] = $value;
		}
  } 
  else {
    
      $query = ($template) ? "SELECT * FROM {user_import} WHERE setting = 'template'" : "SELECT * FROM {user_import} WHERE setting != 'template' ORDER BY started DESC";
      $results = db_query($query);

      while ($row = db_fetch_array($results)) {
          $row['field_match'] = unserialize($row['field_match']);
          $row['roles'] = unserialize($row['roles']);
          $row['options'] = unserialize($row['options']);

					foreach ($row['options'] as $key => $value) {
						$row[$key] = $value;
					}

          $import[] = $row;
      }
  }

  return $import;      
}

function _user_import_settings_deletion($import_id) {

    db_query("DELETE FROM {user_import} WHERE import_id = %d", $import_id);
    db_query("DELETE FROM {user_import_errors} WHERE import_id = %d", $import_id);
    return;
}

// Used by user_import_og.module
function user_import_profile_load($user) {

  $result = db_query('SELECT f.name, f.type, f.fid, v.value FROM {profile_fields} f INNER JOIN {profile_values} v ON f.fid = v.fid WHERE uid = %d', $user->uid);

  while ($field = db_fetch_object($result)) {
  
    if (empty($profile[$field->fid])) {
      
      $profile[$field->fid] = _profile_field_serialize($field->type) ? unserialize($field->value) : $field->value;
    }
    
  }
  
  return $profile;
}

function _user_import_unconcatenate_field_match($settings) {

  $settings_updated = array();

  foreach ($settings as $column_id => $values) {
    
    if (!empty($values['field_match']) || !empty($values['username'])) {
      // If we have a username but no field_match, set a special type.
      // This allows us to skip saving the field but still use it in
      // concatenating a username value.
      if (empty($values['field_match'])) {
        $values['type'] = 'username_part';
        $values['field_id'] = 'username_part_'. $column_id;
      }
      else {
        $key_parts = explode('-', $values['field_match']);
        $values['type'] = array_shift($key_parts);
        $values['field_id'] = implode('-', $key_parts);
      }
      unset($values['field_match']);
      $settings_updated[$column_id] = $values;
    }
  }
  
  return $settings_updated;
}

/**
 * Loads the hooks for the supported modules.
 */
function user_import_load_supported() {
  static $loaded = FALSE;

  if (!$loaded) {
    $path = drupal_get_path('module', 'user_import') . '/supported';
    $files = drupal_system_listing('.*\.inc$', $path, 'name', 0);

    foreach ($files as $module_name => $file) {

      if (module_exists($module_name)) {
        include_once($file->filename);
      }
    }

    $loaded = TRUE;
  }
}   

/**
* Implementation of hook_simpletest().
*/
function user_import_simpletest() {
  $module_name = 'user_import';
  $dir = drupal_get_path('module', $module_name). '/tests';
  $tests = file_scan_directory($dir, '\.test$');
  return array_keys($tests);
}





