You are here

campaignmonitor_local.module in Campaign Monitor 8

Campaignmonitor Local core functions.

File

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

/**
 * @file
 * Campaignmonitor Local core functions.
 */

/*
 * Get all the user subscriptions from Campaign Monitor
 */

/**
 * Queue all active users ready for processing.
 */
function campaignmonitor_local_queue_subscriptions() {
  \Drupal::database()
    ->delete('queue')
    ->condition('name', 'campaignmonitor_local_subscriptions')
    ->execute();
  $users = campaignmonitor_local_get_active_users();
  foreach ($users as $uid => $mail) {
    campaignmonitor_local_queue($uid, $mail);
  }
}

/**
 *
 */
function campaignmonitor_local_queue($uid, $mail) {
  $data['uid'] = $uid;
  $data['mail'] = $mail;
  $queue = \Drupal::queue('campaignmonitor_local_subscriptions');
  $queue
    ->createQueue();
  $queue
    ->createItem($data);
}

/**
 * Get all active users.
 */
function campaignmonitor_local_get_active_users() {
  $result = db_select('users_field_data', 'ufd')
    ->condition('ufd.status', 1)
    ->fields('ufd', [
    'mail',
    'uid',
  ])
    ->orderBy('uid', 'ASC')
    ->execute()
    ->fetchAll();
  $users = [];
  foreach ($result as $record) {
    $users[$record->uid] = $record->mail;
  }
  return $users;
}

/**
 * Insert the data into the database.
 */
function campaignmonitor_local_insert_user_data($uid, $subs) {
  foreach ($subs as $list_id => $value) {
    if ($value > 0) {
      campaignmonitor_local_dbmerge($uid, $list_id);
    }
  }
}

/**
 * Insert the list data into the database.
 */
function campaignmonitor_local_insert_list_data() {
  $lists = campaignmonitor_get_lists();
  foreach ($lists as $list_id => $values) {
    db_merge('campaignmonitor_local_lists')
      ->keys([
      'list_id' => $list_id,
    ])
      ->fields([
      'name' => $values['name'],
      'updated' => REQUEST_TIME,
    ])
      ->execute();
  }
}

/**
 * Implements hook_campaignmonitor_subscribe().
 */
function campaignmonitor_local_campaignmonitor_subscribe($list_id, $email) {

  // Add local record.
  $uid = campaignmonitor_get_uid_from_email($email);
  campaignmonitor_local_dbmerge($uid, $list_id);
}

/**
 * Implements hook_campaignmonitor_unsubscribe().
 */
function campaignmonitor_local_campaignmonitor_unsubscribe($list_id, $email) {

  // Remove local record.
  $uid = campaignmonitor_get_uid_from_email($email);
  \Drupal::database()
    ->delete('campaignmonitor_local_subscriptions')
    ->condition('uid', $uid)
    ->condition('list_id', $list_id)
    ->execute();
}

/**
 * @param $uid
 * @param $list_id
 */
function campaignmonitor_local_dbmerge($uid, $list_id) {
  \Drupal::database()
    ->merge('campaignmonitor_local_subscriptions')
    ->keys([
    'uid' => $uid,
    'list_id' => $list_id,
  ])
    ->fields([
    'updated' => REQUEST_TIME,
  ])
    ->execute();
}

Functions

Namesort descending Description
campaignmonitor_local_campaignmonitor_subscribe Implements hook_campaignmonitor_subscribe().
campaignmonitor_local_campaignmonitor_unsubscribe Implements hook_campaignmonitor_unsubscribe().
campaignmonitor_local_dbmerge
campaignmonitor_local_get_active_users Get all active users.
campaignmonitor_local_insert_list_data Insert the list data into the database.
campaignmonitor_local_insert_user_data Insert the data into the database.
campaignmonitor_local_queue
campaignmonitor_local_queue_subscriptions Queue all active users ready for processing.