Skip to content

Instantly share code, notes, and snippets.

@sirkitree
Created February 23, 2011 22:09
Show Gist options
  • Save sirkitree/841305 to your computer and use it in GitHub Desktop.
Save sirkitree/841305 to your computer and use it in GitHub Desktop.
hook_user_operations *csv*
<?php
/**
* Implementation of hook_node_operations().
*/
function user_export_user_operations() {
return array(
'export' => array(
'label' => t('Export to CSV'),
'callback' => 'user_export_csv',
),
);
}
/**
* Returns CSV file to download.
* @param array $accounts
*/
function user_export_csv($users, $view) {
$header = 'uid, '. implode(', ', array_keys($view->field));
$sql = $view->build_info['query'] .' AND users.uid IN ('. implode(',', $users) .')';
$result = db_query($sql);
while ($account = db_fetch_array($result)) {
$accounts[$account['uid']] = implode('~ ', $account);
// remove any commas that could mess up the CSV file
$accounts[$account['uid']] = str_replace(',', '', $accounts[$account['uid']]);
// re-add in comma delimiters
$accounts[$account['uid']] = str_replace('~', ',', $accounts[$account['uid']]);
}
$file = $header ."\n";
foreach ($accounts as $row) {
$file .= $row ."\n";
}
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="users.csv"');
die(print $file);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment