You are here

campaignmonitor_user.module in Campaign Monitor 8

Same filename and directory in other branches
  1. 7 modules/campaignmonitor_user/campaignmonitor_user.module

Adds a tab to the profile page that enables users to select which newsletters they want to subscribe to.

File

modules/campaignmonitor_user/campaignmonitor_user.module
View source
<?php

/**
 * @file
 * Adds a tab to the profile page that enables users to select which newsletters
 *   they want to subscribe to.
 */

/**
 * Helper function to get a user's subscriptions.
 *
 * @param $email
 * @param $format
 *   Return value depends on format requested
 *
 * @return array
 */
function campaignmonitor_user_get_user_subscriptions($email, $format = 'checkboxes') {
  $lists = campaignmonitor_get_lists();

  // Build options for the form selector.
  $options = [];
  $default = [];
  foreach ($lists as $list_id => $list) {

    // Check if the list is selected to be shown.
    $list_options = campaignmonitor_get_list_settings($list_id);
    if (campaignmonitor_is_list_enabled($list_id)) {

      // Check if the user is subscribed to the current list.
      $default[$list_id] = 0;
      if (campaignmonitor_is_subscribed($list_id, $email)) {
        $default[$list_id] = $list_id;
        $options[$list_id] = $list['name'];
      }
    }
  }
  switch ($format) {
    case 'checkboxes':
      return $default;
    default:
      return $options;
  }
}

/**
 * Access callback for the user newsletters page.
 */

/**
 * Function campaignmonitor_user_access($account) {
 * global $user;
 * if ($user->uid && $user->uid == $account->uid && user_access('access campaign monitor user')) {
 * return TRUE;
 * }
 * return FALSE;
 * }.
 */
function campaignmonitor_user_form_campaignmonitor_admin_settings_list_edit_alterx(&$form, &$form_state, $form_id) {

  // Find form key to index the form array and load defaults.
  $form_key = 'campaignmonitor_list_' . $form['listId']['#value'];
  $defaults = variable_get($form_key, []);

  // Add option to enable this form on the user page.
  $form[$form_key]['display']['user'] = [
    '#type' => 'checkbox',
    '#title' => t('Display list on user page'),
    '#description' => t('Enable this list on the user page and allow subscription.'),
    '#default_value' => isset($defaults['display']['user']) ? $defaults['display']['user'] : 0,
  ];
}

/**
 * Implements hook_link_alter().
 *
 * @param $variables
 */
function campaignmonitor_user_link_alter(&$variables) {
  $config = \Drupal::config('campaignmonitor_user.settings');

  // Change link text.
  if (isset($variables['route_name']) && $variables['route_name'] == 'campaignmonitor.user.subscriptions') {
    $variables['text'] = $config
      ->get('subscription_heading');
  }
}

/**
 * Implements hook_theme().
 */
function campaignmonitor_user_theme($existing, $type, $theme, $path) {
  return [
    'campaignmonitor_user_profile' => [
      'variables' => [
        'content' => NULL,
      ],
      'template' => 'campaignmonitor-user-profile',
    ],
  ];
}

/**
 * Implements hook_user_update().
 *
 * Makes sure that user e-mail addresses are synchronized with Campaign Monitor
 * when user accounts are updated, if this option has been enabledin the
 * administration interface.
 */
function campaignmonitor_user_updatex(&$edit, $account, $category) {

  // In $edit['mail'] we have the entered e-mail address, and in
  // $account->original->mail the original one before editing the account. If
  // they are equal, we do nothing.
  if (isset($edit['mail']) && $edit['mail'] != $account->original->mail && $category == 'account') {

    // Get Campaign Monitor settings and check if the e-mail address should be
    // synchronized.
    $settings = variable_get('campaignmonitor_general', []);
    if (isset($settings['synchronize']) && $settings['synchronize']) {
      $cm = CampaignMonitor::getConnector();
      $lists_info = $cm
        ->getLists();
      $lists_to_update = [];
      foreach ($lists_info as $list_id => $list) {

        // Check if the list is selected to be shown.
        $list_options = variable_get('campaignmonitor_list_' . $list_id, []);
        if (campaignmonitor_is_list_enabled($list_id) && isset($list_options['display']['user']) && $list_options['display']['user']) {

          // Check if the user is subscribed to the current list, but not yet
          // subscribed with the new e-mail.
          if ($cm
            ->isSubscribed($list_id, $account->original->mail) && !$cm
            ->isSubscribed($list_id, $edit['mail'])) {
            if ($cm
              ->updateSubscriberEmail($list_id, $account->original->mail, $edit['mail'])) {
              drupal_set_message(t('Your e-mail adress has been updated for the "@list" list.', [
                '@list' => $list['name'],
              ]), 'status');
            }
            else {
              drupal_set_message(t('Your e-mail adress has not been updated for the "@list" list.', [
                '@list' => $list['name'],
              ]), 'error');
            }
          }
        }
      }
    }
  }
}

Functions

Namesort descending Description
campaignmonitor_user_form_campaignmonitor_admin_settings_list_edit_alterx Function campaignmonitor_user_access($account) { global $user; if ($user->uid && $user->uid == $account->uid && user_access('access campaign monitor user')) { return TRUE; } return FALSE; }.
campaignmonitor_user_get_user_subscriptions Helper function to get a user's subscriptions.
campaignmonitor_user_link_alter Implements hook_link_alter().
campaignmonitor_user_theme Implements hook_theme().
campaignmonitor_user_updatex Implements hook_user_update().