You are here

class BackupController in Backup and Migrate 5.0.x

Same name and namespace in other branches
  1. 8.4 src/Controller/BackupController.php \Drupal\backup_migrate\Controller\BackupController

@package Drupal\backup_migrate\Controller

Hierarchy

Expanded class hierarchy of BackupController

File

src/Controller/BackupController.php, line 17

Namespace

Drupal\backup_migrate\Controller
View source
class BackupController extends ControllerBase {

  /**
   * @var \Drupal\backup_migrate\Core\Destination\DestinationInterface
   */
  public $destination;

  /**
   *
   */
  public function listAll() {
    $storage = \Drupal::entityTypeManager()
      ->getStorage('backup_migrate_destination');
    $out = [];
    foreach ($storage
      ->getQuery()
      ->execute() as $key) {
      $entity = $storage
        ->load($key);
      $destination = $entity
        ->getObject();
      $label = $destination
        ->confGet('name');
      $out[$key] = [
        'title' => [
          '#markup' => '<h2>' . $this
            ->t('Most recent backups in %dest', [
            '%dest' => $label,
          ]) . '</h2>',
        ],
        'list' => $this::listDestinationBackups($destination, $key, 5),
      ];

      // Add the more link.
      if ($entity
        ->access('backups') && $entity
        ->hasLinkTemplate('backups')) {
        $out[$key]['link'] = $entity
          ->toLink($this
          ->t('View all backups in %dest', [
          '%dest' => $label,
        ]), 'backups')
          ->toRenderable();
      }
    }
    return $out;
  }

  /**
   * Get the title for the listing page of a destination entity.
   *
   * @param \Drupal\backup_migrate\Entity\Destination $backup_migrate_destination
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
   */
  public function listDestinationEntityBackupsTitle(Destination $backup_migrate_destination) {
    return $this
      ->t('Backups in @destination_name', [
      '@destination_name' => $backup_migrate_destination
        ->label(),
    ]);
  }

  /**
   * List the backups in the given destination.
   *
   * @param \Drupal\backup_migrate\Entity\Destination $backup_migrate_destination
   *
   * @return mixed
   */
  public function listDestinationEntityBackups(Destination $backup_migrate_destination) {
    $destination = $backup_migrate_destination
      ->getObject();
    return $this
      ->listDestinationBackups($destination, $backup_migrate_destination
      ->id());
  }

  /**
   * List the backups in the given destination.
   *
   * @param \Drupal\backup_migrate\Core\Destination\ListableDestinationInterface $destination
   * @param $backup_migrate_destination_id
   * @param int $count
   *
   * @return mixed
   */
  public function listDestinationBackups(ListableDestinationInterface $destination, $backup_migrate_destination_id, $count = NULL) {

    // Get a sorted list of files.
    $rows = [];
    $header = [
      [
        'data' => $this
          ->t('Name'),
        'class' => [
          RESPONSIVE_PRIORITY_MEDIUM,
        ],
        'field' => 'name',
      ],
      [
        'data' => $this
          ->t('Date'),
        'class' => [
          RESPONSIVE_PRIORITY_MEDIUM,
        ],
        'field' => 'datestamp',
        'sort' => 'asc',
      ],
      [
        'data' => $this
          ->t('Size'),
        'class' => [
          RESPONSIVE_PRIORITY_MEDIUM,
        ],
        'field' => 'filesize',
        'sort' => 'desc',
      ],
      [
        'data' => $this
          ->t('Operations'),
        'class' => [
          RESPONSIVE_PRIORITY_LOW,
        ],
      ],
    ];
    $order = TableSort::getOrder($header, \Drupal::request());
    $sort = TableSort::getSort($header, \Drupal::request());
    $php_sort = $sort == 'desc' ? SORT_DESC : SORT_ASC;
    $backups = $destination
      ->queryFiles([], $order['sql'], $php_sort, $count);
    foreach ($backups as $backup_id => $backup) {
      $rows[] = [
        'data' => [
          // Cells.
          $backup
            ->getFullName(),
          \Drupal::service('date.formatter')
            ->format($backup
            ->getMeta('datestamp')),
          format_size($backup
            ->getMeta('filesize')),
          [
            'data' => [
              '#type' => 'operations',
              '#links' => [
                'restore' => [
                  'title' => $this
                    ->t('Restore'),
                  'url' => Url::fromRoute('entity.backup_migrate_destination.backup_restore', [
                    'backup_migrate_destination' => $backup_migrate_destination_id,
                    'backup_id' => $backup_id,
                  ]),
                ],
                'download' => [
                  'title' => $this
                    ->t('Download'),
                  'url' => Url::fromRoute('entity.backup_migrate_destination.backup_download', [
                    'backup_migrate_destination' => $backup_migrate_destination_id,
                    'backup_id' => $backup_id,
                  ]),
                ],
                'delete' => [
                  'title' => $this
                    ->t('Delete'),
                  'url' => Url::fromRoute('entity.backup_migrate_destination.backup_delete', [
                    'backup_migrate_destination' => $backup_migrate_destination_id,
                    'backup_id' => $backup_id,
                  ]),
                ],
              ],
            ],
          ],
        ],
      ];
    }
    $build['backups_table'] = [
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#empty' => $this
        ->t('There are no backups in this destination.'),
    ];
    return $build;
  }

  /**
   * Download a backup via the browser.
   *
   * @param \Drupal\backup_migrate\Entity\Destination $backup_migrate_destination
   * @param $backup_id
   */
  public function download(Destination $backup_migrate_destination, $backup_id) {
    $destination = $backup_migrate_destination
      ->getObject();
    $file = $destination
      ->getFile($backup_id);
    $file = $destination
      ->loadFileForReading($file);
    $browser = new DrupalBrowserDownloadDestination();
    $browser
      ->saveFile($file);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BackupController::$destination public property
BackupController::download public function Download a backup via the browser.
BackupController::listAll public function
BackupController::listDestinationBackups public function List the backups in the given destination.
BackupController::listDestinationEntityBackups public function List the backups in the given destination.
BackupController::listDestinationEntityBackupsTitle public function Get the title for the listing page of a destination entity.
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 46
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.