updated user_import

This commit is contained in:
Bachir Soussi Chiadmi 2016-04-19 16:29:53 +02:00
parent 3a55dcea49
commit fb0666538c
5 changed files with 321 additions and 153 deletions

View File

@ -84,6 +84,37 @@ function field_user_import_default_field_processor($user_fields, $field_name, $v
return $field; return $field;
} }
function field_user_import_taxonomy_field_processor($user_fields, $field_name, $values) {
$field = $user_fields->$field_name;
for ($i = 0; $i < count($values); $i++) {
if (empty($values[$i])) {
continue; // Do not save empty fields
}
// Get taxonomy term ID before saving if term already exists
$field_info = field_info_field($field_name);
$vocabulary = $field_info['settings']['allowed_values'][0]['vocabulary'];
$tid = taxonomy_get_term_by_name($values[$i], $vocabulary);
if (empty($tid)) {
// Create a new taxonomy term
$field[LANGUAGE_NONE][$i]['tid'] = 'autocreate';
$field[LANGUAGE_NONE][$i]['vid'] = taxonomy_vocabulary_machine_name_load($vocabulary)->vid;
$field[LANGUAGE_NONE][$i]['name'] = $values[$i];
$field[LANGUAGE_NONE][$i]['description'] = '';
$field[LANGUAGE_NONE][$i]['format'] = 'plain_text';
}
else {
$field[LANGUAGE_NONE][$i]['tid'] = array_shift($tid)->tid;
}
}
return $field;
}
function field_user_import_supported_fields($output = 'all') { function field_user_import_supported_fields($output = 'all') {
static $supported = array(); static $supported = array();
@ -142,7 +173,7 @@ function field_user_import_supported_fields($output = 'all') {
$supported['taxonomy_term_reference'] = array( $supported['taxonomy_term_reference'] = array(
'validate' => 'field_user_import_default_field_validator', 'validate' => 'field_user_import_default_field_validator',
'save' => 'field_user_import_default_field_processor', 'save' => 'field_user_import_taxonomy_field_processor',
); );
/** Unsupported for the moment **/ /** Unsupported for the moment **/

View File

@ -99,35 +99,6 @@ function _user_import_send_email($account, $password, $profile, $subject, $body,
return; return;
} }
/**
* Implementation of hook_mail().
*/
function user_import_mail($key, &$message, $params) {
switch ($key) {
case 'welcome':
$message['subject'] = (empty($params['subject'])) ? _user_mail_text('register_admin_created_subject', $message['language'], $params) : strtr($params['subject'], $params);
$body = (empty($params['body'])) ? _user_mail_text('register_admin_created_body', $message['language'], $params) : strtr($params['body'], $params);
if ($params['email_format'] == 1) {
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8';
$body_head = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />';
if (!empty($params['css'])) $body_head .= '<style type="text/css">' . check_plain($params['css']) . '</style>';
$message['body'][] = $body_head . '</head><body>' . $body . '</body></html>';
}
else {
$message['body'][] = $body;
}
break;
}
}
function _user_import_edit_settings_fields(&$form, $import, $collapsed) { function _user_import_edit_settings_fields(&$form, $import, $collapsed) {
$form['optional'] = array( $form['optional'] = array(

View File

@ -11,9 +11,9 @@ files[] = user_import.test
; Information added by Drupal.org packaging script on 2015-04-01 ; Information added by Drupal.org packaging script on 2016-03-06
version = "7.x-2.3" version = "7.x-3.2"
core = "7.x" core = "7.x"
project = "user_import" project = "user_import"
datestamp = "1427908388" datestamp = "1457261641"

View File

@ -146,6 +146,34 @@ function user_import_schema() {
return $schema; return $schema;
} }
/**
* Implementation of hook_install().
*/
function user_import_install() {
// Add a new mail system for HTML emails.
$mail_system = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
$mail_system['user_import'] = 'UserImportMailSystem';
variable_set('mail_system', $mail_system);
}
/**
* Implementation of hook_uninstall().
*/
function user_import_uninstall() {
variable_del('user_import_settings');
variable_del('user_import_max');
variable_del('user_import_line_max');
variable_del('user_export_checked_usernames');
variable_del('user_import_profile_date_format');
//
$mail_system = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
unset($mail_system['user_import']);
variable_set('mail_system', $mail_system);
}
function user_import_update_1() { function user_import_update_1() {
$ret = array(); $ret = array();
_system_update_utf8(array('user_import', 'user_import_errors')); _system_update_utf8(array('user_import', 'user_import_errors'));
@ -249,15 +277,13 @@ function user_import_update_7200(&$sandbox) {
db_add_field('user_import', 'auto_import_directory', $field); db_add_field('user_import', 'auto_import_directory', $field);
} }
/** /**
* Implementation of hook_uninstall(). * Add a new mail system for HTML emails.
*/ */
function user_import_uninstall() { function user_import_update_7201() {
variable_del('user_import_settings'); $ret = array();
variable_del('user_import_max'); $mail_system = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
variable_del('user_import_line_max'); $mail_system['user_import'] = 'UserImportMailSystem';
variable_del('user_export_checked_usernames'); variable_set('mail_system', $mail_system);
variable_del('user_import_profile_date_format'); return $ret;
} }

View File

@ -313,10 +313,17 @@ function user_import_limited_errors($import_id = NULL, $template_id = NULL) {
$result = $query->execute(); $result = $query->execute();
foreach ($result as $line) { foreach ($result as $line) {
$file_lines[] = array('data' => unserialize($line->data), 'errors' => unserialize($line->errors)); $file_lines[] = array(
'data' => unserialize($line->data),
'errors' => unserialize($line->errors)
);
} }
$output .= theme('user_import_errors_display', array('import' => $import, 'file_lines' => $file_lines, 'total' => $total)); $output .= theme('user_import_errors_display', array(
'import' => $import,
'file_lines' => $file_lines,
'total' => $total
));
} }
$output .= l(t('Return'), "admin/people/user_import"); $output .= l(t('Return'), "admin/people/user_import");
@ -344,13 +351,22 @@ function theme_user_import_list() {
$output = ''; $output = '';
$imports = _user_import_settings_select(); $imports = _user_import_settings_select();
if (!$imports) return ' '; if (!$imports) {
return ' ';
}
foreach ($imports as $import) { foreach ($imports as $import) {
// header labels // header labels
$import_label = ($import['setting'] == 'tested' || $import['setting'] == 'test') ? t('importable') : t('imported'); $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')); $header = array(
t('file'),
t('started'),
t('processed'),
$import_label,
t('errors'),
t('status')
);
// info row // info row
$errors = db_query('SELECT COUNT(import_id) FROM {user_import_errors} WHERE import_id = :import_id', array(':import_id' => $import['import_id']))->fetchField(); $errors = db_query('SELECT COUNT(import_id) FROM {user_import_errors} WHERE import_id = :import_id', array(':import_id' => $import['import_id']))->fetchField();
@ -373,8 +389,12 @@ function theme_user_import_list() {
$import_link = l(t('Import'), 'admin/people/user_import/import/' . $import['import_id']); $import_link = l(t('Import'), 'admin/people/user_import/import/' . $import['import_id']);
$output .= $delete_link; $output .= $delete_link;
if ($import['setting'] == 'tested' || $import['setting'] == 'test') $output .= ' | ' . $import_link; if ($import['setting'] == 'tested' || $import['setting'] == 'test') {
if ($import['setting'] == 'test' || $import['setting'] == 'import') $output .= ' | ' . $continue_link; $output .= ' | ' . $import_link;
}
if ($import['setting'] == 'test' || $import['setting'] == 'import') {
$output .= ' | ' . $continue_link;
}
} }
return $output; return $output;
@ -385,7 +405,12 @@ function theme_user_import_edit($variables) {
$output = ''; $output = '';
$rows = array(); $rows = array();
$form = $variables['form']; $form = $variables['form'];
$header = array(t('CSV column'), t('Drupal fields'), t('Username'), t('Abbreviate')); $header = array(
t('CSV column'),
t('Drupal fields'),
t('Username'),
t('Abbreviate')
);
foreach (element_children($form['field_match']) as $key) { foreach (element_children($form['field_match']) as $key) {
@ -397,7 +422,10 @@ function theme_user_import_edit($variables) {
); );
} }
$form['field_match']['#value'] = theme('table', array('header' => $header, 'rows' => $rows)); $form['field_match']['#value'] = theme('table', array(
'header' => $header,
'rows' => $rows
));
if (isset($form['remove'])) { if (isset($form['remove'])) {
$output .= drupal_render($form['remove']); $output .= drupal_render($form['remove']);
@ -443,7 +471,10 @@ function theme_user_import_errors_display($settings) {
if (!empty($column_info['username'])) { if (!empty($column_info['username'])) {
$header[$column_info['username']] = t('Name %sort', array('%sort' => $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"); $row[$column_info['username']] = array(
"data" => $field_data[0],
"align" => "left"
);
} }
if ($column_info['field_id'] == 'email') { if ($column_info['field_id'] == 'email') {
@ -472,10 +503,12 @@ function theme_user_import_errors_display($settings) {
"header" => $header, "header" => $header,
"rows" => $rows, "rows" => $rows,
"attributes" => array(), "attributes" => array(),
"sticky" => FALSE, // Table header will be sticky "sticky" => FALSE,
// Table header will be sticky
"caption" => '', "caption" => '',
"colgroups" => array(), "colgroups" => array(),
"empty" => t("There are no errors.") // The message to be displayed if table is empty "empty" => t("There are no errors.")
// The message to be displayed if table is empty
) )
) . theme("pager"); ) . theme("pager");
@ -500,7 +533,8 @@ function theme_user_import_username_errors($errors) {
function _user_import_settings_save($settings) { function _user_import_settings_save($settings) {
// Database field defaults. // Database field defaults.
$database_fields = array('import_id' => NULL, $database_fields = array(
'import_id' => NULL,
'name' => '', 'name' => '',
'auto_import_directory' => '', 'auto_import_directory' => '',
'filename' => '', 'filename' => '',
@ -517,7 +551,17 @@ function _user_import_settings_save($settings) {
); );
// Form elements we never want to save in the options column. // 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(
'form_id',
'form_token',
'form_build_id',
'cancel',
'import',
'submit',
'op',
0,
'return_path'
);
$form_variables = array_flip($form_variables); $form_variables = array_flip($form_variables);
// Remove settings we don't need in the options column. // Remove settings we don't need in the options column.
@ -571,6 +615,7 @@ function _user_import_settings_save($settings) {
$auto_import_directory = ''; $auto_import_directory = '';
} }
$file = new ArrayObject();
$file->filename = isset($settings['filename']) ? $settings['filename'] : ''; $file->filename = isset($settings['filename']) ? $settings['filename'] : '';
$file->oldfilename = isset($settings['oldfilename']) ? $settings['oldfilename'] : ''; $file->oldfilename = isset($settings['oldfilename']) ? $settings['oldfilename'] : '';
$file->filepath = isset($settings['filepath']) ? $settings['filepath'] : ''; $file->filepath = isset($settings['filepath']) ? $settings['filepath'] : '';
@ -633,16 +678,22 @@ function user_import_import_set($name = '', $file = '', $pointer = 0, $processed
function _user_import_settings_select($import_id = NULL, $template = FALSE) { function _user_import_settings_select($import_id = NULL, $template = FALSE) {
$import = array(); $import = array();
if (!empty($import_id) && !is_numeric($import_id)) return; if (!empty($import_id) && !is_numeric($import_id)) {
return;
}
if (!empty($import_id)) { if (!empty($import_id)) {
$sql = 'SELECT * FROM {user_import} WHERE import_id = :import_id'; $sql = 'SELECT * FROM {user_import} WHERE import_id = :import_id';
if ($template) $sql .= " AND setting = 'template'"; if ($template) {
$sql .= " AND setting = 'template'";
}
$import = (array) db_query_range($sql, 0, 1, array(':import_id' => $import_id))->fetchObject(); $import = (array) db_query_range($sql, 0, 1, array(':import_id' => $import_id))->fetchObject();
if (empty($import)) return FALSE; if (empty($import)) {
return FALSE;
}
$import['field_match'] = unserialize($import['field_match']); $import['field_match'] = unserialize($import['field_match']);
$import['roles'] = unserialize($import['roles']); $import['roles'] = unserialize($import['roles']);
@ -794,15 +845,19 @@ function _user_import_file_deletion($filepath, $filename, $old_filename, $ftp, $
$file = new stdClass(); $file = new stdClass();
$file->uri = $filepath; $file->uri = $filepath;
$file->filename = $filename; $file->filename = $filename;
$file->fid = db_query("SELECT fid FROM {file_managed} WHERE uri = :filepath", array(':filepath' => $filepath))->fetchField(); $file->fid = db_query("SELECT fid FROM {file_managed} WHERE uri = :filepath", array(':filepath' => $filepath))->fetchField();;
;
$removed = file_delete($file); $removed = file_delete($file);
} }
if (!$message) return; if (!$message) {
return;
}
if (empty($removed)) { if (empty($removed)) {
drupal_set_message(t("File error: file '%old_filename' (%filename) could not be deleted.", array('%old_filename' => $oldfilename, '%filename' => $filename)), 'error'); drupal_set_message(t("File error: file '%old_filename' (%filename) could not be deleted.", array(
'%old_filename' => $oldfilename,
'%filename' => $filename
)), 'error');
} }
else { else {
drupal_set_message(t("File '%old_filename' was deleted.", array('%old_filename' => $old_filename))); drupal_set_message(t("File '%old_filename' was deleted.", array('%old_filename' => $old_filename)));
@ -811,6 +866,91 @@ function _user_import_file_deletion($filepath, $filename, $old_filename, $ftp, $
return; return;
} }
/**
* Implementation of hook_mail().
*/
function user_import_mail($key, &$message, $params) {
switch ($key) {
case 'welcome':
$message['subject'] = (empty($params['subject'])) ? _user_mail_text('register_admin_created_subject', $message['language'], $params) : strtr($params['subject'], $params);
$body = (empty($params['body'])) ? _user_mail_text('register_admin_created_body', $message['language'], $params) : strtr($params['body'], $params);
if ($params['email_format'] == 1) {
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8';
$body_head = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />';
if (!empty($params['css'])) {
$body_head .= '<style type="text/css">' . check_plain($params['css']) . '</style>';
}
$message['body'][] = $body_head . '</head><body>' . $body . '</body></html>';
}
else {
$message['body'][] = $body;
}
break;
}
}
/**
* Modify the drupal mail system to send HTML emails.
*
* See http://drupal.org/node/900794.
*/
class UserImportMailSystem implements MailSystemInterface {
/**
* Concatenate and wrap the e-mail body.
*
* @param $message
* A message array, as described in hook_mail_alter().
*
* @return
* The formatted $message.
*/
public function format(array $message) {
$message['body'] = implode("\n\n", $message['body']);
return $message;
}
/**
* Send an e-mail message, using Drupal variables and default settings.
*
* @see <a href="http://php.net/manual/en/function.mail.php
* " title="http://php.net/manual/en/function.mail.php
* " rel="nofollow">http://php.net/manual/en/function.mail.php
* </a> * @see drupal_mail()
*
* @param $message
* A message array, as described in hook_mail_alter().
* @return
* TRUE if the mail was successfully accepted, otherwise FALSE.
*/
public function mail(array $message) {
$mimeheaders = array();
foreach ($message['headers'] as $name => $value) {
$mimeheaders[] = $name . ': ' . mime_header_encode($value);
}
$line_endings = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
return mail(
$message['to'],
mime_header_encode($message['subject']),
// Note: e-mail uses CRLF for line-endings. PHP's API requires LF
// on Unix and CRLF on Windows. Drupal automatically guesses the
// line-ending format appropriate for your system. If you need to
// override this, adjust $conf['mail_line_endings'] in settings.php.
preg_replace('@\r?\n@', $line_endings, $message['body']),
// For headers, PHP's API suggests that we use CRLF normally,
// but some MTAs incorrectly replace LF with CRLF. See #234403.
join("\n", $mimeheaders)
);
}
}