You are here

class MailchimpCampaignController in Mailchimp 8

Same name and namespace in other branches
  1. 2.x modules/mailchimp_campaign/src/Controller/MailchimpCampaignController.php \Drupal\mailchimp_campaign\Controller\MailchimpCampaignController

Mailchimp Campaign controller.

Hierarchy

Expanded class hierarchy of MailchimpCampaignController

File

modules/mailchimp_campaign/src/Controller/MailchimpCampaignController.php, line 22

Namespace

Drupal\mailchimp_campaign\Controller
View source
class MailchimpCampaignController extends ControllerBase {

  /**
   * The current request.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * The entity query.
   *
   * @var \Drupal\Core\Entity\Query\QueryFactory
   */
  protected $entityQuery;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * Entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * A logger instance.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * Initializes a MailchimpCampaignController.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity type manager.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   */
  public function __construct(Request $request, DateFormatterInterface $date_formatter, EntityTypeManagerInterface $entityTypeManager, MessengerInterface $messenger, LoggerInterface $logger) {
    $this->request = $request;
    $this->dateFormatter = $date_formatter;
    $this->entityTypeManager = $entityTypeManager;
    $this->messenger = $messenger;
    $this->logger = $logger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('request_stack')
      ->getCurrentRequest(), $container
      ->get('date.formatter'), $container
      ->get('entity_type.manager'), $container
      ->get('messenger'), $container
      ->get('logger.factory')
      ->get('mailchimp_campaign'));
  }

  /**
   * {@inheritdoc}
   */
  public function overview() {
    $content = [];
    $content['campaigns_table'] = [
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Title'),
        $this
          ->t('Subject'),
        $this
          ->t('Status'),
        $this
          ->t('Mailchimp Audience'),
        $this
          ->t('Mailchimp Template'),
        $this
          ->t('Created'),
        $this
          ->t('Actions'),
      ],
      '#empty' => '',
    ];
    try {
      $campaigns = mailchimp_campaign_load_multiple();
      $templates = mailchimp_campaign_list_templates();
    } catch (MailchimpAPIException $e) {
      $this
        ->messenger()
        ->addError($this
        ->t('Unable to fetch Mailchimp campaign data, caught API exception: @error', [
        '@error' => $e
          ->getMessage(),
      ]));
      return $content;
    }

    /* @var $campaign \Drupal\mailchimp_campaign\Entity\MailchimpCampaign */
    foreach ($campaigns as $campaign) {
      if (!$campaign
        ->isInitialized()) {
        continue;
      }

      // Ensure the associated list/audience still exists.
      if (!isset($campaign->list) || !$campaign->list) {
        continue;
      }
      $campaign_id = $campaign
        ->getMcCampaignId();
      $archive_url = Url::fromUri($campaign->mc_data->archive_url);
      $campaign_url = Url::fromRoute('entity.mailchimp_campaign.view', [
        'mailchimp_campaign' => $campaign_id,
      ]);
      $list_url = Url::fromUri('https://admin.mailchimp.com/lists/dashboard/overview?id=' . $campaign->list->id, [
        'attributes' => [
          'target' => '_blank',
        ],
      ]);
      $send_url = Url::fromRoute('entity.mailchimp_campaign.send', [
        'mailchimp_campaign' => $campaign_id,
      ]);
      if ($campaign->mc_data->status === "save") {
        $send_link = Link::fromTextAndUrl($this
          ->t("Send"), $send_url)
          ->toString();
      }
      elseif ($campaign->mc_data->status === "sending") {
        $campaigns = mailchimp_campaign_load_multiple([
          $campaign_id,
        ], TRUE);
        $campaign = $campaigns[$campaign_id];
        $send_link = $this
          ->t("Sent");
      }
      else {
        $send_link = $this
          ->t("Sent");
      }
      $actions = [
        Link::fromTextAndUrl('View Archive', $archive_url)
          ->toString(),
        Link::fromTextAndUrl('View', $campaign_url)
          ->toString(),
        $send_link,
      ];
      $content['campaigns_table'][$campaign_id]['title'] = [
        '#markup' => Link::fromTextAndUrl($campaign->mc_data->settings->title, $campaign_url)
          ->toString(),
      ];
      $content['campaigns_table'][$campaign_id]['subject'] = [
        '#markup' => $campaign->mc_data->settings->subject_line,
      ];
      $content['campaigns_table'][$campaign_id]['status'] = [
        '#markup' => $campaign->mc_data->status,
      ];
      $content['campaigns_table'][$campaign_id]['list'] = [
        '#markup' => Link::fromTextAndUrl($campaign->list->name, $list_url)
          ->toString(),
      ];
      if (empty($campaign->mc_data->settings->template_id)) {
        $content['campaigns_table'][$campaign_id]['template'] = [
          '#markup' => "-- none --",
        ];
      }
      else {
        $template_url = Url::fromUri('https://admin.mailchimp.com/templates/edit?id=' . $campaign->mc_data->settings->template_id, [
          'attributes' => [
            'target' => '_blank',
          ],
        ]);
        $category = FALSE;

        // Templates are grouped into categories, so we go hunting for our
        // template ID in each category.
        $template_category = [];
        foreach ($templates as $category_name => $template_category) {
          if (isset($template_category[$campaign->mc_data->settings->template_id])) {
            $category = $category_name;
            break;
          }
        }
        if ($category) {
          $content['campaigns_table'][$campaign_id]['template'] = [
            '#markup' => Link::fromTextAndUrl($template_category[$campaign->mc_data->settings->template_id]->name, $template_url)
              ->toString(),
          ];
        }
        else {
          $content['campaigns_table'][$campaign_id]['template'] = [
            '#markup' => $this
              ->t('-- template %template_url not found --', [
              '%template_url' => Link::fromTextAndUrl($campaign->mc_data->settings->template_id, $template_url)
                ->toString(),
            ]),
          ];
        }
      }
      $content['campaigns_table'][$campaign_id]['created'] = [
        '#markup' => $this->dateFormatter
          ->format(strtotime($campaign->mc_data->create_time), 'custom', 'F j, Y - g:ia'),
      ];
      $content['campaigns_table'][$campaign_id]['actions'] = [
        '#markup' => implode(' | ', $actions),
      ];
    }
    $mailchimp_campaigns_url = Url::fromUri('https://admin.mailchimp.com/campaigns', [
      'attributes' => [
        'target' => '_blank',
      ],
    ]);
    $content['mailchimp_list_link'] = [
      '#title' => $this
        ->t('Go to Mailchimp Campaigns'),
      '#type' => 'link',
      '#url' => $mailchimp_campaigns_url,
    ];
    return $content;
  }

  /**
   * View a Mailchimp campaign.
   *
   * @param \Drupal\mailchimp_campaign\Entity\MailchimpCampaign $mailchimp_campaign
   *   The Mailchimp campaign to view.
   *
   * @return array
   *   Renderable array of page content.
   */
  public function view(MailchimpCampaign $mailchimp_campaign) {
    $view_builder = $this->entityTypeManager
      ->getViewBuilder('mailchimp_campaign');
    $content = $view_builder
      ->view($mailchimp_campaign);
    return $content;
  }

  /**
   * View a Mailchimp campaign stats.
   *
   * @param \Drupal\mailchimp_campaign\Entity\MailchimpCampaign $mailchimp_campaign
   *   The Mailchimp campaign to view stats for.
   *
   * @return array
   *   Renderable array of page content.
   */
  public function stats(MailchimpCampaign $mailchimp_campaign) {
    $content = [];

    /* @var \Mailchimp\MailchimpReports $mc_reports */
    $mc_reports = mailchimp_get_api_object('MailchimpReports');
    try {
      if (!$mc_reports) {
        throw new MailchimpAPIException('Cannot get campaign stats without Mailchimp API. Check API key has been entered.');
      }
      $response = $mc_reports
        ->getCampaignSummary($mailchimp_campaign
        ->getMcCampaignId());
    } catch (\Exception $e) {
      $this->messenger
        ->addError($e
        ->getMessage());
      $this->logger
        ->error('An error occurred getting report data from Mailchimp: {message}', [
        'message' => $e
          ->getMessage(),
      ]);
    }
    if (!empty($response)) {

      // Attach stats JS.
      $content['#attached']['library'][] = 'mailchimp_campaign/google-jsapi';
      $content['#attached']['library'][] = 'mailchimp_campaign/campaign-stats';

      // Time series chart data.
      $content['#attached']['drupalSettings']['mailchimp_campaign'] = [
        'stats' => [],
      ];
      foreach ($response->timeseries as $series) {
        $content['#attached']['drupalSettings']['mailchimp_campaign']['stats'][] = [
          'timestamp' => $series->timestamp,
          'emails_sent' => isset($series->emails_sent) ? $series->emails_sent : 0,
          'unique_opens' => $series->unique_opens,
          'recipients_click' => isset($series->recipients_click) ? $series->recipients_click : 0,
        ];
      }
      $content['charts'] = [
        '#prefix' => '<h2>' . $this
          ->t('Hourly stats for the first 24 hours of the campaign') . '</h2>',
        '#markup' => '<div id="mailchimp-campaign-chart"></div>',
      ];
      $content['metrics_table'] = [
        '#type' => 'table',
        '#header' => [
          $this
            ->t('Key'),
          $this
            ->t('Value'),
        ],
        '#empty' => '',
        '#prefix' => '<h2>' . $this
          ->t('Other campaign metrics') . '</h2>',
      ];
      $stat_groups = [
        'bounces',
        'forwards',
        'opens',
        'clicks',
        'facebook_likes',
        'list_stats',
      ];
      foreach ($stat_groups as $group) {
        $content['metrics_table'][] = [
          'key' => [
            '#markup' => '<strong>' . ucfirst(str_replace('_', ' ', $group)) . '</strong>',
          ],
          'value' => [
            '#markup' => '',
          ],
        ];
        foreach ($response->{$group} as $key => $value) {
          if ($key == "last_open" && !empty($value)) {
            $value = $this->dateFormatter
              ->format(strtotime($value), 'custom', 'F j, Y - g:ia');
          }
          $content['metrics_table'][] = [
            'key' => [
              '#markup' => $key,
            ],
            'value' => [
              '#markup' => $value,
            ],
          ];
        }
      }
    }
    else {
      $content['unavailable'] = [
        '#markup' => $this
          ->t('The campaign stats are unavailable at this time.'),
      ];
    }
    return $content;
  }

  /**
   * Callback for entity title autocomplete field.
   *
   * @param string $entity_type
   *   The entity type to search by title.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   A JSON response containing matched entity data.
   */
  public function entityAutocomplete($entity_type) {
    $q = $this->request
      ->get('q');
    $query = $this->entityTypeManager
      ->getStorage($entity_type)
      ->getQuery()
      ->condition('title', $q, 'CONTAINS')
      ->range(0, 10);
    $entity_ids = $query
      ->execute();
    $entities = [];
    if (!empty($entity_ids)) {
      $entities_data = $this->entityTypeManager
        ->getStorage($entity_type)
        ->loadMultiple($entity_ids);
      if (!empty($entities_data)) {

        /* @var $entity \Drupal\Core\Entity\EntityInterface */
        foreach ($entities_data as $id => $entity) {
          $title = $entity
            ->getTypedData()
            ->getString('title');
          $entities[] = [
            'value' => $title . ' [' . $id . ']',
            'label' => Html::escape($title),
          ];
        }
      }
    }
    return new JsonResponse($entities);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::$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.
MailchimpCampaignController::$dateFormatter protected property The date formatter service.
MailchimpCampaignController::$entityQuery protected property The entity query.
MailchimpCampaignController::$entityTypeManager protected property Entity type manager. Overrides ControllerBase::$entityTypeManager
MailchimpCampaignController::$logger protected property A logger instance.
MailchimpCampaignController::$messenger protected property The messenger service. Overrides MessengerTrait::$messenger
MailchimpCampaignController::$request protected property The current request.
MailchimpCampaignController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
MailchimpCampaignController::entityAutocomplete public function Callback for entity title autocomplete field.
MailchimpCampaignController::overview public function
MailchimpCampaignController::stats public function View a Mailchimp campaign stats.
MailchimpCampaignController::view public function View a Mailchimp campaign.
MailchimpCampaignController::__construct public function Initializes a MailchimpCampaignController.
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.