View Single Post
  #1  
Old 03-11-2007, 01:05 PM
 
speedworx speedworx is offline
 

Member
  
Join Date: Jan 2006
Posts: 10
 

Default Export Users 4.0.x Solution

I've poured through the forums here and have seen many requests for a method to export user information from within the administration interface. As I need this too, I developed a solution that is working for me in 4.0.17 - 4.0.19. There are a few steps but they're relatively simple to follow.

Step 1 - Add Language Variable (Administration > Languages > English >)
Variable: lbl_export_users
Value: Export Users

Step 2 - Create New Directories
your_xcart_dir/modules/Export_Users
your_xcart_dir/skin1/modules/Export_Users

Step 3 - Create Admin Wrapper File (your_xcart_dir/admin/export_users.php)
Complete file content:
Code:
<?php # # $Id: export_users.php,v 1.0.0.0 2007/03/10 07:41:48 tewald Exp $ # define('USE_TRUSTED_POST_VARIABLES',1); $trusted_post_variables = array("mode"); require "./auth.php"; require $xcart_dir."/include/security.php"; $location[] = array(func_get_langvar_by_name("lbl_export_users"), ""); include $xcart_dir."/modules/Export_Users/export_users.php"; $smarty->assign("main","export_users_admin"); # Assign the current location line $smarty->assign("location", $location); @include $xcart_dir."/modules/gold_display.php"; func_display("admin/home.tpl",$smarty); ?>

Step 4 - Create Admin Processing File (your_xcart_dir/modules/Export_Users/export_users.php)
Complete file content:
Code:
<?php # # $Id: export_users.php,v 1.22.2.11 2007/03/10 14:58:23 tewald Exp $ # if ( !defined('XCART_SESSION_START') ) { header("Location: ../"); die("Access denied"); } if ($REQUEST_METHOD == "POST") { $users_data = func_query("SELECT CONCAT(firstname, \" \", lastname) AS fullname, company, b_address, b_city, b_state, b_zipcode, b_country, email, phone, fax FROM xcart_customers WHERE usertype LIKE 'C' ORDER BY fullname ASC"); if ($users_data) { $delimiter="\t"; if (!strncmp($export_fmt,"csv",3)) { header("Content-Type: application/csv"); header("Content-Disposition: attachment; filename=\"users_data.csv\""); switch($export_fmt) { case "csv_semi": $delimiter=";"; break; case "csv_comma": $delimiter=","; break; default: $delimiter="\t"; break; } } elseif (!strncmp($export_fmt,"xls",3)) { header("Content-Type: application/x-msdownload"); header("Content-Disposition: attachment; filename=\"users_data.xls\""); } else { header("Content-type: text/plain"); header("Content-disposition: attachment; filename=users_data.csv"); } $smarty->assign("delimiter", $delimiter); $_tmp_smarty_debug = $smarty->debugging; $smarty->debugging = false; $data .= "Full Name" . $delimiter; $data .= "Company" . $delimiter; $data .= "Address" . $delimiter; $data .= "City" . $delimiter; $data .= "State" . $delimiter; $data .= "Zip" . $delimiter; $data .= "Country" . $delimiter; $data .= "Email" . $delimiter; $data .= "Phone" . $delimiter; $data .= "Fax" . $delimiter; for ($row = 0; $row < sizeof($users_data); $row++) { $line = ''; while (list($key, $value) = each($users_data[$row])) { if ((!isset($value)) OR ($value == "")) { $value = $delimiter; } else { $value = str_replace('"', '""', $value); $value = str_replace("\r","",$value); $value = str_replace("\r\n","",$value); $value = trim($value); $value = '"' . $value . '"' . $delimiter; } $line .= $value; } $data .= trim($line)."\n"; } $data = str_replace("\r","",$data); $data = str_replace("\r\n","",$data); if ($data == "") { $data = "\n(0) Records Found!\n"; } $smarty->assign("data", $data); func_display("modules/Export_Users/users_export.tpl",$smarty); flush(); } $smarty->debugging = $_tmp_smarty_debug; exit; func_header_location($HTTP_REFERER); } ?>

Step 4 - Create Skin File 1 of 2 (your_xcart_dir/skin1/modules/Export_Users/export_users_admin.tpl)
Complete file content:
Code:
{* $Id: export_users_admin.tpl,v 1.40.2.5 2007/03/10 12:56:57 tewald Exp $ *} {include file="page_title.tpl" title=$lng.lbl_export_users} {capture name=dialog} <TABLE border="0" cellpadding="1" cellspacing="5"> <FORM name="export_users" action="export_users.php" method="POST"> <INPUT type="hidden" name="mode" value="export"> <TR> <TD width="20%" class="FormButton" nowrap>{$lng.lbl_export_file_format}:</TD> <TD width="10">&nbsp;</TD> <TD width="80%"> <SELECT name="export_fmt"> <OPTION value="xls_tab"{if $search_prefilled.export_fmt eq "xls_tab"} selected{/if}>XLS</OPTION> <OPTION value="csv_tab"{if $search_prefilled.export_fmt eq "csv_tab"} selected{/if}>CSV {$lng.lbl_with_tab_delimiter}</OPTION> <OPTION value="csv_semi"{if $search_prefilled.export_fmt eq "csv_semi"} selected{/if}>CSV {$lng.lbl_with_semicolon_delimiter}</OPTION> <OPTION value="csv_comma"{if $search_prefilled.export_fmt eq "csv_comma"} selected{/if}>CSV {$lng.lbl_with_comma_delimiter}</OPTION> </SELECT> <INPUT type="button" value="{$lng.lbl_export_all}" onclick="document.export_users.mode.value='export_all'; document.export_users.submit();"> </TD> </TR> </FORM> </TABLE> {/capture} {include file="dialog.tpl" title=$lng.lbl_export_users content=$smarty.capture.dialog extra="width=100%"}

Step 5 - Create Skin File 2 of 2 (your_xcart_dir/skin1/modules/users_export.tpl)
Complete file content:
Code:
{* $Id: users_export.tpl,v 1.10.2.1 2007/03/10 06:20:27 tewald Exp $ *} {$data}

Step 6 - Add Export Users Link to Admin Menu
Modify your_xcart_dir/skin1/admin/menu.tpl:
Add:
Code:
<A href="{$catalogs.admin}/export_users.php" class="VertMenuItems">{$lng.lbl_export_users}</A><BR>
After:
Code:
<A href="{$catalogs.admin}/users.php" class="VertMenuItems">{$lng.lbl_users}</A><BR>

Step 7 - Add Export Interface
Modify your_xcart_dir/skin1/single/home.tpl:
Add:
Code:
{* START EXPORT USERS ADMIN *} {elseif $main eq "export_users_admin"} {include file="modules/Export_Users/export_users_admin.tpl"} {* END EXPORT USERS ADMIN *}
After:
Code:
{elseif $main eq "general_info"} {include file="admin/main/general.tpl"}

Notes:
1. This only exports the usertype of Customer (not Admins or Providers). If you want to change this, just modify the SQL statement in your_xcart_dir/modules/Export_Users/export_users.php. For example, you can remove the "WHERE usertype LIKE 'C'" to get all users.
2. If you want to add/remove exported fields, modify the SQL statement in your_xcart_dir/modules/Export_Users/export_users.php AND add/remove lines in the same file from the section containing code like:
$data .= "Company" . $delimiter;
Just remember if you add a field, add a line and vice versa for deleting. The last line though must have the newline character ( i.e. $data .= "Fax" . $delimiter . "\n"; ).

Well, that's it for my first posted mod. I hope it helps as I've received great assistance from this forum as well.
__________________
XCart4.0.19
Reply With Quote