You are here

class ArchiveDownloader in Content Synchronizer 3.x

Same name and namespace in other branches
  1. 8.2 src/Service/ArchiveDownloader.php \Drupal\content_synchronizer\Service\ArchiveDownloader
  2. 8 src/Service/ArchiveDownloader.php \Drupal\content_synchronizer\Service\ArchiveDownloader

Class ArchiveDownloader.

@package Drupal\content_synchronizer\Service

Hierarchy

Expanded class hierarchy of ArchiveDownloader

5 files declare their use of ArchiveDownloader
ArchiveDownloaderController.php in src/Controller/ArchiveDownloaderController.php
content_synchronizer.module in ./content_synchronizer.module
Hooks definitions for content_synchronizer module.
ExportConfirmForm.php in src/Form/ExportConfirmForm.php
LaunchExportForm.php in src/Form/LaunchExportForm.php
QuickExportController.php in src/Controller/QuickExportController.php
1 string reference to 'ArchiveDownloader'
content_synchronizer.services.yml in ./content_synchronizer.services.yml
content_synchronizer.services.yml
1 service uses ArchiveDownloader
content_synchronizer.archive_downloader in ./content_synchronizer.services.yml
\Drupal\content_synchronizer\Service\ArchiveDownloader

File

src/Service/ArchiveDownloader.php, line 16

Namespace

Drupal\content_synchronizer\Service
View source
class ArchiveDownloader {
  const SERVICE_NAME = 'content_synchronizer.archive_downloader';
  const ARCHIVE_PARAMS = 'cs_archive';

  /**
   * Retourne le singleton.
   *
   * @return static
   *   Le singleton.
   */
  public static function me() {
    return \Drupal::service(static::SERVICE_NAME);
  }

  /**
   * Current User.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * ArchiveDownloader constructor.
   *
   * @param \Drupal\Core\Session\AccountProxyInterface $currentUser
   *   The current user.
   */
  public function __construct(AccountProxyInterface $currentUser) {
    $this->currentUser = $currentUser;
  }

  /**
   * Donwload archive by adding js library.
   *
   * @param array $vars
   *   Preprocess data.
   */
  public function donwloadArchive(array &$vars) {
    if ($this
      ->canDownload()) {
      $vars['#attached']['library'][] = 'content_synchronizer/download_archive';
      $vars['#attached']['drupalSettings']['content_synchronizer']['download_archive_path'] = Url::fromRoute('content_synchronizer.download_archive')
        ->getInternalPath();
    }
  }

  /**
   * Return true if the current page is admin.
   *
   * @see https://drupal.stackexchange.com/questions/219370/how-to-test-if-current-page-is-an-admin-page
   *
   * @return bool
   *   True if can download.
   */
  public function canDownload() {
    return User::load($this->currentUser
      ->id())
      ->hasPermission('add export entity entities');
  }

  /**
   * Redirect to the page with download.
   *
   * @param string $redirectUrl
   *   The url.
   * @param string $archiveUri
   *   The archiev url.
   */
  public function redirectWithArchivePath($redirectUrl, $archiveUri) {
    $path = str_replace(ExportEntityWriter::getGeneratorDir(), '', $archiveUri);
    $redirectUrl .= "#" . static::ARCHIVE_PARAMS . '=' . urlencode($path);
    $redirect = new RedirectResponse($redirectUrl);
    $redirect
      ->send();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ArchiveDownloader::$currentUser protected property Current User.
ArchiveDownloader::ARCHIVE_PARAMS constant
ArchiveDownloader::canDownload public function Return true if the current page is admin.
ArchiveDownloader::donwloadArchive public function Donwload archive by adding js library.
ArchiveDownloader::me public static function Retourne le singleton.
ArchiveDownloader::redirectWithArchivePath public function Redirect to the page with download.
ArchiveDownloader::SERVICE_NAME constant
ArchiveDownloader::__construct public function ArchiveDownloader constructor.