diff --git a/drupalorg/drupalorg.drush.inc b/drupalorg/drupalorg.drush.inc index dd8cf0e3baaee4bbe20377927e93262d32c48fce..6fe6024a36999e6690df31e4c0c0aab18fcf7e10 100644 --- a/drupalorg/drupalorg.drush.inc +++ b/drupalorg/drupalorg.drush.inc @@ -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); }