Voici une petit module pour importer une liste
d’utilisateurs WEB
1)
Installation du module
Créer un nouveau module appelé ImportWebusers
Copiez le code dans le champ "code source php"
Enregistrez.
Le nouveau module ImportWebusers s’affiche dans le sous menu
2)
Fichier d’importation
Créer un fichier texte avec une ligne par utilisateur sur le modèle (ne pas changer l’ordre des champ !):
Fullname;username;password;email
Fullname = ex :Jean Dupont
Username = ex : jdupont
Password = mot de passe non crypté (le module cryptera en MD5 le mot de passe)
Pour importer votre fichier, afficher le module ImportWebusers
Sélectionnez votre fichier texte formaté
Cliquez sur "Importer"
Voilou
C’est une version développée rapidement et testée avec un petit nombre de ligne dans le fichier.
Je vais l’optimiser un peu avant de le mettre sur le répository
Enfin j’espère que ça pourra aider hemhel
Code du module :
/**
*
* Module:ImportWebusers
* V 0.1
* Author: laurent Carbonnel
* Date: 31/08/2006
* For: MODx 0.9.1
*/
global $tb_prefix;
$action = (isset ($_POST['action'])) ? $_POST['action'] : '';
$_lang['module_title'] = 'ImportWebusers';
$_lang['Box_title'] = 'Importer des utilisateurs web';
$_lang['file_imported'] = 'Imortation effectuée';
$_lang['cancel'] = 'Fermer';
$basePath = $modx->config['base_path'];
$siteURL = $modx->config['site_url'];
$tb_prefix = $modx->db->config['table_prefix'];
//-- get theme
$theme = $modx->db->select('setting_value', $tb_prefix . 'system_settings', 'setting_name=\'manager_theme\'', '');
$theme = $modx->db->getRow($theme);
$theme = ($theme['setting_value'] <> '') ? '/' . $theme['setting_value'] : '';
//-- setup initial vars
$table_webusers = $modx->getFullTableName('web_users');
$table_webusers_attributes = $modx->getFullTableName('web_user_attributes');
$output = '';
$errorMsg = '';
$msg = '';
if ($action == 'insert') {
//::::::::::::::::::::::::::::: fichier images :::::::::::::::::::::::::::::://
if ($_FILES["csvfile"]['name'] !=""){
$real_filename = $_FILES["csvfile"]['name'];
// copie du fichier ----------------------------------------------------------------//
$chemin= $basePath."assets/";
$file=$_FILES["csvfile"]['tmp_name'];
copy($file, "$chemin$real_filename");
unlink($file);
// copie du fichier ----------------------------------------------------------------//
}
if (file_exists($basePath."assets/$real_filename")){ // si la suppression de la 1ere ligne c bien passée
$fp = fopen($basePath."assets/$real_filename","r"); //overture fichier en lecture
while($data=fgetcsv($fp,900000,";")){ //recupération du contenu ligne par ligne
$fullname = addslashes($data[0]);
$username = addslashes($data[1]);
$password = md5($data[2]);
$email = addslashes($data[3]);
//$date=date("Y-m-d"); // A SUPPRIMER SI BESOIN
$fields1 = array ('username' => $modx->db->escape($username), 'password' => $modx->db->escape($password));
$modx->db->insert($fields1, $table_webusers);
$newId = $modx->db->getInsertId();
$fields2 = array ('internalkey' => $newId, 'fullname' => $modx->db->escape($fullname),'email' => $modx->db->escape($email));
$modx->db->insert($fields2, $table_webusers_attributes);
}
unlink($basePath."assets/$real_filename");
}
//::::::::::::::::::::::::::::: fichier images :::::::::::::::::::::::::::::://
}
//--- HEAD
$output .= '
<html>
<head>
<link rel="stylesheet" type="text/css" href="media/style' . $theme . '/style.css" />
<link rel="stylesheet" type="text/css" href="media/style' . $theme . '/coolButtons2.css" />
<style type="text/css">
input.button {
display: none;
}
ul.sortableList {
padding-left: 20px;
margin: 0px;
width: 300px;
font-family: Arial, sans-serif;
}
ul.sortableList li {
font-weight: bold;
cursor: move;
color: white;
padding: 2px 2px;
margin: 2px 0px;
border: 1px solid #000000;
background-image: url(media/style/MODx/images/grid_hdr.gif);
background-repeat: repeat-x;
}
#bttn .bttnheight {
height: 25px !important;
padding: 0px;
padding-top: 6px;
float: left;
vertical-align: middle !important;
}
#bttn a{
cursor: default !important;
font: icon !important;
color: black !important;
border: 0px !important;
padding: 5px 5px 7px 5px!important;
white-space: nowrap !important;
vertical-align: middle !important;
background: transparent !important;
}
#bttn a:hover {
border: 1px solid darkgreen !important;
padding:4px 4px 6px 4px !important;
background-image:url("media/style/MODx/images/button_dn.gif") !important;
text-decoration: none;
}
#bttn a img {
vertical-align: middle !important;
}
</style>
</head>
';
$output .= '
<body>
<div class="subTitle" id="bttn">
<span class="right"><img src="media/style' . $theme . '/images/_tx_.gif" width="1" height="5"><br />' . $_lang['module_title'] . '</span>
<div class="bttnheight"><a id="Button5" onclick="document.location.href=\'index.php?a=106\';">
<img src="media/images/icons/cancel.gif"> ' . $_lang['cancel'] . '</a>
</div>
<div class="stay"> </div>
</div>
';
//--- HEAD END
switch($action){
case 'insert': //######### Insertion ######################################################################################
$output .= '<div class="sectionHeader"><img src=\'media/images/misc/dot.gif\' alt="." /> ' . $_lang['Box_title'] . '</div>
<div class="sectionBody">';
$output .='<table width="540" border="0" cellspacing="0" cellpadding="0">';
$output .= '<tr><td align="center">'.$_lang['file_imported'].'</td></tr>';
$output .= '</table></div>';
break;
default: //######### LISTE ######################################################################################
//$qryLGgallery = $modx->db->select('*', $table, '', 'name ASC');
$output .= '<div class="sectionHeader"><img src=\'media/images/misc/dot.gif\' alt="." /> ' . $_lang['Box_title'] . '</div>
<div class="sectionBody">';
$output .='<table width="540" border="0" cellspacing="0" cellpadding="0">';
$output .='<form name="InsertWebusers" enctype="multipart/form-data" method="post" action="">
<tr>
<td width="124">Fichier CSV </td>
<td width="240">
<input type="file" name="csvfile">
</td>
<td width="136" align="center">
<input type="submit" name="Submit" value="Importer">
</td>
</tr><input name="action" type="hidden" id="action" value="insert">
</form>';
$output .= '</table></div>';
break;
}
//-- send output
$output .= '</body></html>';
return $output;