Have you ever needed to export users into a csv file from Drupal? Working in corporate environments, I've had to do this many a time, and it's usually a PITA. However, the new Views Bulk Operations (or VBO) just made that job much, much easier.
VBO is an awesome style plugin for Views 2 that can turn pretty much any view into an administrative interface for bulk operations by allowing you to apply system actions and hook_[user/node/comment]_operations() to whatever your view displays. You can read more about it on the project page; I'm just here to tell you about how I used it to make a common task a lot easier.
First of all, the code:
<?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);
}
?>
hook_user_operations() simply announces to Drupal (and therefor VBO) that there is a batch operation that can be utilized and points it to the function 'user_export_csv'. By default, the ids that are selected and the view you are using are what are passed into this function. In our case, we're using a view (@see vbo-pr0n) that uses the 'users' table as it's base, so an array of user ids selected in our view become our first parameter, $users. If this was a node view, an array of node ids would be passed instead. The second parameter, $view, is the full view object.

Our user_export_csv function then uses the $view object to create the headers for our csv file, and then appends our $users to the view's query. This query is then run and the results formatted for our csv file. Then upon confirmation of our submission, you're presented with a download of our csv file, popuplated with the data from the view of only the user's you select.

Using the view to build our query also allows you to select what fields you want to export by simply editing the view's fields. it also Allows us to be able to use exposed filters to narrow down your selection criteria.

So if you need to build robust administrative interfaces, VBO is your module. Have fun!
Quick links:
Comments
Very cool
Great usage of VBO. Thanks a lot!
A lighweight option not
A lighweight option not involving views is Profile CSV
Very nice, where you put this
Very nice,
where you put this function ?
Thanks.
I put it in a custom module
I put it in a custom module called user_export, but you can put it in one of your own custom modules.
I put the function in a
I put the function in a module (and activate it) but when I try to use it, it does nothing! (I get your views).
Thanks for your help.
Please read here
Please read here (http://drupal.org/node/231276) for the proper way to create a Drupal 6 module.
Please also keep in mind that this is not the full code needed for a module, but just outlines the idea of how to use hook_node_operations() in conjunction with VBO.
looks like there is a little
looks like there is a little thing to add :
$result = db_query($sql,$view->build_info['query_args']);
instead of
$result = db_query($sql);
Thanks for this post!