You are here

public function MailchimpCampaignController::overview 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::overview()
1 string reference to 'MailchimpCampaignController::overview'
mailchimp_campaign.routing.yml in modules/mailchimp_campaign/mailchimp_campaign.routing.yml
modules/mailchimp_campaign/mailchimp_campaign.routing.yml

File

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

Class

MailchimpCampaignController
Mailchimp Campaign controller.

Namespace

Drupal\mailchimp_campaign\Controller

Code

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;
}