You are here

public function BrightcoveSubscriptionController::listSubscriptions in Brightcove Video Connect 8

Same name and namespace in other branches
  1. 8.2 src/Controller/BrightcoveSubscriptionController.php \Drupal\brightcove\Controller\BrightcoveSubscriptionController::listSubscriptions()
  2. 3.x src/Controller/BrightcoveSubscriptionController.php \Drupal\brightcove\Controller\BrightcoveSubscriptionController::listSubscriptions()

Lists available Brightcove Subscriptions.

Throws

\Drupal\brightcove\Entity\Exception\BrightcoveSubscriptionException

1 string reference to 'BrightcoveSubscriptionController::listSubscriptions'
brightcove.routing.yml in ./brightcove.routing.yml
brightcove.routing.yml

File

src/Controller/BrightcoveSubscriptionController.php, line 120

Class

BrightcoveSubscriptionController
Provides controller for subscription related callbacks.

Namespace

Drupal\brightcove\Controller

Code

public function listSubscriptions() {

  // Set headers.
  $header = [
    'endpoint' => $this
      ->t('Endpoint'),
    'api_client' => $this
      ->t('API Client'),
    'events' => $this
      ->t('Events'),
    'operations' => $this
      ->t('Operations'),
  ];

  // Get Subscriptions.
  $brightcove_subscriptions = BrightcoveSubscription::loadMultiple();

  // Whether a warning has benn shown about the missing subscriptions on
  // Brightcove or not.
  $warning_set = FALSE;

  // Assemble subscription list.
  $rows = [];
  foreach ($brightcove_subscriptions as $key => $brightcove_subscription) {
    $api_client = $brightcove_subscription
      ->getApiClient();
    $rows[$key] = [
      'endpoint' => $brightcove_subscription
        ->getEndpoint() . ($brightcove_subscription
        ->isDefault() ? " ({$this->t('default')})" : ''),
      'api_client' => !empty($api_client) ? $this->linkGenerator
        ->generate($api_client
        ->label(), Url::fromRoute('entity.brightcove_api_client.edit_form', [
        'brightcove_api_client' => $api_client
          ->id(),
      ])) : '',
      'events' => implode(', ', array_filter($brightcove_subscription
        ->getEvents(), function ($value) {
        return !empty($value);
      })),
    ];

    // Default subscriptions can be enabled or disabled only.
    if ((bool) $brightcove_subscription
      ->isDefault()) {
      $enable_link = Url::fromRoute('entity.brightcove_subscription.enable', [
        'id' => $brightcove_subscription
          ->getId(),
      ]);
      $disable_link = Url::fromRoute('entity.brightcove_subscription.disable', [
        'id' => $brightcove_subscription
          ->getId(),
      ]);
      $rows[$key]['operations'] = [
        'data' => [
          '#type' => 'operations',
          '#links' => [
            'change_status' => [
              'title' => $brightcove_subscription
                ->isActive() ? $this
                ->t('Disable') : $this
                ->t('Enable'),
              'url' => $brightcove_subscription
                ->isActive() ? $disable_link : $enable_link,
            ],
          ],
        ],
      ];
    }
    else {
      $subscriptions = BrightcoveSubscription::listFromBrightcove($api_client);
      $subscription_found = FALSE;
      foreach ($subscriptions as $subscription) {
        if ($brightcove_subscription
          ->getEndpoint() == $subscription
          ->getEndpoint()) {
          $subscription_found = TRUE;

          // If the endpoint exist but their ID is different, fix it.
          if ($brightcove_subscription
            ->getBcSid() != ($id = $subscription
            ->getId())) {
            $brightcove_subscription
              ->setBcSid($id);
            $brightcove_subscription
              ->save();
          }
          break;
        }
      }
      if (!$warning_set && !$subscription_found) {
        drupal_set_message($this
          ->t('There are subscriptions which are not available on Brightcove.<br>You can either <strong>create</strong> them on Brightcove or <strong>delete</strong> them if no longer needed.'), 'warning');
        $warning_set = TRUE;
      }

      // Add create link if the subscription is missing from Brightcove.
      $create_link = [];
      if (!$subscription_found) {
        $create_link = [
          'create' => [
            'title' => $this
              ->t('Create'),
            'url' => Url::fromRoute('entity.brightcove_subscription.create', [
              'id' => $brightcove_subscription
                ->getId(),
            ]),
          ],
        ];
      }
      $rows[$key]['operations'] = [
        'data' => [
          '#type' => 'operations',
          '#links' => $create_link + [
            'delete' => [
              'title' => $this
                ->t('Delete'),
              'weight' => 10,
              'url' => Url::fromRoute('entity.brightcove_subscription.delete_form', [
                'id' => $brightcove_subscription
                  ->getId(),
              ]),
            ],
          ],
        ],
      ];
    }
  }

  // Check default subscriptions for each api client.
  $api_clients_without_default_subscription = [];

  /** @var \Drupal\brightcove\Entity\BrightcoveAPIClient $api_client */
  foreach (BrightcoveAPIClient::loadMultiple() as $api_client) {
    if (BrightcoveSubscription::loadDefault($api_client) == NULL) {
      $api_clients_without_default_subscription[] = $api_client
        ->getLabel();
    }
  }
  if (!empty($api_clients_without_default_subscription)) {
    drupal_set_message($this
      ->t('There are missing default subscription(s) for the following API Client(s): %api_clients<br><a href="@link">Create missing subscription(s)</a>.', [
      '%api_clients' => implode(', ', $api_clients_without_default_subscription),
      '@link' => Url::fromRoute('entity.brightcove_subscription.create_defaults')
        ->toString(),
    ]), 'warning');
  }
  $page['subscriptions'] = [
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => $this
      ->t('There are no subscriptions yet.'),
  ];
  return $page;
}