You are here

class CampaignMonitorListsController in Campaign Monitor 8.2

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

Campaign Monitor Lists controller.

Hierarchy

Expanded class hierarchy of CampaignMonitorListsController

File

src/Controller/CampaignMonitorListsController.php, line 17

Namespace

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

  /**
   * The campaignmonitor manager.
   *
   * @var \Drupal\campaignmonitor\CampaignMonitorManager
   */
  protected $campaignMonitorManager;

  /**
   * Drupal\Core\Config\ConfigFactoryInterface definition.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * TaxonomyViewsIntegratorManager constructor.
   *
   * @param \Drupal\campaignmonitor\CampaignMonitorManager $campaignmonitor_manager
   *   The campaign monitor manager service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(CampaignMonitorManager $campaignmonitor_manager, ConfigFactoryInterface $config_factory) {
    $this->campaignMonitorManager = $campaignmonitor_manager;
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $campaignmonitor_manager = $container
      ->get('campaignmonitor.manager');
    return new static($campaignmonitor_manager, $container
      ->get('config.factory'));
  }

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

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

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

        // Add disable operation.
        $link = Link::fromTextAndUrl($this
          ->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', ['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,
            ['attributes' => ['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,
    ];
    return $content;
  }

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

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

  /**
   * Enables the toggling of list.
   */
  private function listToggleEnable($list_id) {

    // Get local list information and update enabled state.
    $list_options = $this->campaignMonitorManager
      ->getListSettings($list_id);
    $enable = 0;
    if (isset($list_options['status']['enabled'])) {
      $enable = $list_options['status']['enabled'] == 1 ? 0 : 1;
    }
    $list_options['status']['enabled'] = $enable;
    $list_config = $this->configFactory
      ->getEditable('campaignmonitor.settings.list');
    $list_key = $this->campaignMonitorManager
      ->listKey($list_id);
    $list_config
      ->set($list_key, $list_options)
      ->save();

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

  /**
   * Callback to clear config cache.
   */
  public function clearListCache() {
    $this->campaignMonitorManager
      ->clearCache();
    $this
      ->messenger()
      ->addStatus('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::$campaignMonitorManager protected property The campaignmonitor manager.
CampaignMonitorListsController::$configFactory protected property Drupal\Core\Config\ConfigFactoryInterface definition. Overrides ControllerBase::$configFactory
CampaignMonitorListsController::clearListCache public function Callback to clear config cache.
CampaignMonitorListsController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
CampaignMonitorListsController::listDisable public function Disable the list.
CampaignMonitorListsController::listEnable public function Enable the list.
CampaignMonitorListsController::listToggleEnable private function Enables the toggling of list.
CampaignMonitorListsController::overview public function
CampaignMonitorListsController::verifyAccess public function Verify that a token is set for CSRF protection.
CampaignMonitorListsController::__construct public function TaxonomyViewsIntegratorManager constructor.
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::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.