You are here

class CampaignMonitorListsController in Campaign Monitor 8

Same name and namespace in other branches
  1. 8.2 src/Controller/CampaignMonitorListsController.php \Drupal\campaignmonitor\Controller\CampaignMonitorListsController

Campaign Monitor Lists controller.

Hierarchy

Expanded class hierarchy of CampaignMonitorListsController

File

src/Controller/CampaignMonitorListsController.php, line 13

Namespace

Drupal\campaignmonitor\Controller
View source
class CampaignMonitorListsController extends ControllerBase {

  /**
   * {@inheritdoc}
   */
  public function overview() {
    $content = [];
    $lists_admin_url = Url::fromUri('https://waxeye.createsend.com/subscribers/', [
      'attributes' => [
        'target' => '_blank',
      ],
    ]);
    $lists_empty_message = t('You don\'t have any lists configured in your
      Campaign Monitor account, (or you haven\'t configured your API key correctly on
      the Global Settings tab). Head over to @link and create some lists, then
      come back here and click "Refresh lists from Campaign Monitor"', [
      '@link' => Link::fromTextAndUrl(t('Campaign Monitor'), $lists_admin_url)
        ->toString(),
    ]);
    $content['lists_table'] = [
      '#type' => 'table',
      '#header' => [
        t('Name'),
        t('List ID'),
        t('Subscribed'),
        t('Operations'),
      ],
      '#empty' => $lists_empty_message,
    ];
    $cm_lists = campaignmonitor_get_lists();

    // $total_webhook_events = count(campaignmonitor_default_webhook_events());
    foreach ($cm_lists as $key => $cm_list) {
      $details = campaignmonitor_get_list_details($key);
      $stats = campaignmonitor_get_list_stats($key);
      $edit_link = Link::fromTextAndUrl(t('Edit'), Url::fromUri('internal:/admin/config/services/campaignmonitor/list/' . $key . '/edit'))
        ->toString();
      $delete_link = Link::fromTextAndUrl(t('Delete'), Url::fromUri('internal:/admin/config/services/campaignmonitor/list/' . $key . '/delete'))
        ->toString();
      $operations = [
        'Edit' => $edit_link,
        'Delete' => $delete_link,
      ];
      $list_options = campaignmonitor_get_list_settings($key);
      if (isset($list_options['status']['enabled']) && !$list_options['status']['enabled']) {

        // Add enable operation.
        $class = 'campaignmonitor-list-disabled';
        $link = Link::fromTextAndUrl(t('Enable'), Url::fromUri('internal:/admin/config/services/campaignmonitor/list/' . $key . '/enable'))
          ->toString();
        $operations['enable'] = $link;
      }
      else {

        // Add disable operation.
        $class = 'campaignmonitor-list-enabled';
        $link = Link::fromTextAndUrl(t('Disable'), Url::fromUri('internal:/admin/config/services/campaignmonitor/list/' . $key . '/disable'))
          ->toString();
        $operations['disable'] = $link;
      }

      // $enabled_webhook_events = count(campaignmonitor_enabled_webhook_events($cm_list->id));
      //      $webhook_url = Url::fromRoute('campaignmonitor.webhook', array('list_id' => $cm_list->id));
      //      $webhook_link = Link::fromTextAndUrl('update', $webhook_url);
      //
      //      $webhook_status = $enabled_webhook_events . ' of ' . $total_webhook_events . ' enabled (' .  $webhook_link->toString() . ')';.
      // $list_url = Url::fromUri('https://admin.campaignmonitor.com/lists/dashboard/overview?id=' . $cm_list->id, array('attributes' => array('target' => '_blank')));.
      $content['lists_table'][$key]['name'] = [
        '#markup' => $cm_list['name'],
      ];
      $content['lists_table'][$key]['id'] = [
        '#markup' => $key,
      ];
      $content['lists_table'][$key]['stats'] = [
        '#markup' => $stats['TotalActiveSubscribers'] . ' / ' . $stats['TotalUnsubscribes'],
      ];
      $content['lists_table'][$key]['operations'] = [
        '#markup' => implode(' ', $operations),
      ];
    }
    $refresh_url = Url::fromRoute('campaignmonitor.refresh_lists', [
      'destination' => 'admin/config/services/campaignmonitor/lists',
    ]);
    $content['refresh'] = [
      '#type' => 'container',
    ];
    $content['refresh']['refresh_link'] = [
      '#title' => 'Refresh lists from CampaignMonitor',
      '#type' => 'link',
      '#url' => $refresh_url,
    ];
    $create_url = Url::fromRoute('campaignmonitor.list_create_form', [
      'destination' => 'admin/config/services/campaignmonitor/lists',
    ]);
    $content['create'] = [
      '#type' => 'container',
    ];
    $content['create']['create_link'] = [
      '#title' => 'Create a new list',
      '#type' => 'link',
      '#url' => $create_url,
    ];
    return $content;
  }

  /**
   *
   */
  public function listEnable($list_id) {
    $this
      ->verifyAccess();
    $this
      ->listToggleEnable($list_id);
    drupal_set_message('list enabled');
    return new RedirectResponse('/admin/config/services/campaignmonitor/lists');
  }

  /**
   *
   */
  public function listDisable($list_id) {
    $this
      ->verifyAccess();
    $this
      ->listToggleEnable($list_id);
    drupal_set_message('list disabled');
    return new RedirectResponse('/admin/config/services/campaignmonitor/lists');
  }

  /**
   *
   */
  private function listToggleEnable($list_id) {

    // Get local list information and update enabled state.
    $list_options = campaignmonitor_get_list_settings($list_id);
    $enable = 0;
    if (isset($list_options['status']['enabled'])) {
      $enable = $list_options['status']['enabled'] == 1 ? 0 : 1;
    }
    $list_options['status']['enabled'] = $enable;
    campaignmonitor_set_list_settings($list_id, $list_options);

    // Clear blocks cache.
    //    _block_rehash();
  }

  /**
   * Callback to clear config cache.
   */
  public function clearListCache() {
    $caches = [
      'cache.config',
      'cache.data',
    ];
    campaignmonitor_clear_cache($caches);
    drupal_set_message('Campaign Monitor caches cleared');
    return new RedirectResponse('/admin/config/services/campaignmonitor/lists');
  }

  /**
   * Verify that a token is set for CSRF protection.
   */
  public function verifyAccess() {

    // If (!isset($_GET['token']) || !drupal_valid_token($_GET['token'])) {
    //      drupal_not_found();
    //      module_invoke_all('exit');
    //      exit();
    //    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CampaignMonitorListsController::clearListCache public function Callback to clear config cache.
CampaignMonitorListsController::listDisable public function
CampaignMonitorListsController::listEnable public function
CampaignMonitorListsController::listToggleEnable private function
CampaignMonitorListsController::overview public function
CampaignMonitorListsController::verifyAccess public function Verify that a token is set for CSRF protection.
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::$entityManager protected property The entity manager.
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 40
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
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. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator 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. 29
MessengerTrait::messenger public function Gets the messenger. 29
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. 1
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.