Skip to content
Snippets Groups Projects
Commit af3b10c2 authored by Fran Garcia-Linares's avatar Fran Garcia-Linares Committed by Neil Drumm
Browse files

Issue #3516901: Bulk import of event volunteers, speakers via CSV and drush

parent fa78028c
No related branches found
No related tags found
1 merge request!334Command to bulk update event volunteers via CSV
......@@ -26,6 +26,13 @@ function drupalorg_drush_command() {
'drupalorg-org-credit' => [
'description' => 'Update organization credit totals.',
],
'drupalorg-event-volunteers' => [
'description' => 'Update speakers, volunteers fields from an event in bulk.',
'arguments' => [
'event_nid' => 'Event node ID.',
'csv_file' => 'CSV file containing the list.',
],
],
'drupalorg-integrity' => [
'description' => 'Integrity check for a non-ideal world.',
],
......@@ -181,6 +188,76 @@ function drupalorg_drush_command() {
];
}
/**
* Bulk add volunteers and speakers to an event.
*/
function drush_drupalorg_event_volunteers($event_nid, $csv_file) {
// Load the event node.
if (!($event = node_load($event_nid))) {
drush_set_error(dt('Could not load the event.'));
return;
}
if ($event->type !== 'event') {
drush_set_error(dt('Node ID is not an event.'));
return;
}
// Read CSV.
// Format - 2 columns: [username|mail|user],[coc|organizer|speaker|volunteer].
if (!file_exists($csv_file)) {
drush_set_error(dt('Could not find the file.'));
return;
}
$map_field = [
'coc' => 'field_code_of_conduct_contact',
'organizer' => 'field_organizers',
'speaker' => 'field_event_speakers',
'volunteer' => 'field_event_volunteers',
];
$csv = array_map('str_getcsv', file($csv_file));
if (!empty($csv)) {
$unmatched_users = [];
foreach ($csv as $line) {
if (is_array($line) && count($line) === 2) {
if (is_numeric($line[0])) {
$loading_function = 'user_load';
}
elseif (strpos($line[0], '@') !== FALSE) {
$loading_function = 'user_load_by_mail';
}
else {
$loading_function = 'user_load_by_name';
}
$user = $loading_function($line[0]);
$volunteer_field = isset($map_field[$line[1]]) ? $map_field[$line[1]] : 'field_event_volunteers';
if (!empty($user)) {
drush_log(dt('Adding user @uid as @type.', [
'@uid' => $user->uid,
'@type' => $volunteer_field,
]));
$event->{$volunteer_field}[LANGUAGE_NONE][] = [
'target_id' => $user->uid,
];
}
else {
$unmatched_users[] = $line[0];
}
}
}
drush_log(dt('Saving event.'));
node_save($event);
if (!empty($unmatched_users)) {
drush_log(dt('@total user(s) could not be matched: @users.', [
'@total' => count($unmatched_users),
'@users' => implode(', ', $unmatched_users),
]), LogLevel::WARNING);
}
}
}
function drush_drupalorg_rebuild_stats() {
drupalorg_get_activity(TRUE);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment