You are here

campaignmonitor_ics.module in Campaign Monitor 8

.

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

File

modules/campaignmonitor_ics/campaignmonitor_ics.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_ics_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_ics_access($account) {
 * global $user;
 * if ($user->uid && $user->uid == $account->uid && user_access('access campaign monitor user')) {
 * return TRUE;
 * }
 * return FALSE;
 * }.
 */
function campaignmonitor_ics_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_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_ics_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_ics_form_campaignmonitor_admin_settings_list_edit_alterx Function campaignmonitor_ics_access($account) { global $user; if ($user->uid && $user->uid == $account->uid && user_access('access campaign monitor user')) { return TRUE; } return FALSE; }.
campaignmonitor_ics_get_user_subscriptions Helper function to get a user's subscriptions.
campaignmonitor_ics_updatex Implements hook_user_update().