<?php

/**
 * @file
 * The Social Event Enrolments Export module.
 */

use Drupal\Core\StreamWrapper\StreamWrapperManager;

/**
 * Implements hook_file_download().
 */
function social_event_enrolments_export_file_download($uri) {
  $scheme = StreamWrapperManager::getScheme($uri);
  $target = StreamWrapperManager::getTarget($uri);
  $access = \Drupal::currentUser()->hasPermission('administer users');

  if ($scheme === 'private' && preg_match('/^csv\/export-enrollments-([a-f0-9]{16})\.csv$/i', $target) && $access) {
    return [
      'Content-disposition' => 'attachment; filename="' . basename($target) . '"',
    ];
  }
}

/**
 * Implements hook_social_event_action_ACTION_ID_finish().
 */
function social_event_enrolments_export_social_event_managers_action_social_event_enrolments_export_enrollments_action_finish($success) {
  if ($success) {
    return [
      'singular' => '1 selected enrollee has been exported successfully',
      'plural' => '@count selected enrollees have been exported successfully',
    ];
  }

  return [
    'singular' => '1 selected enrollee has not been exported successfully',
    'plural' => '@count selected enrollees have not been exported successfully',
  ];
}
