You are here

campaignmonitor_registration_hold.module in Campaign Monitor 8

Campaign Monitor hold functions .

File

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

/**
 * @file
 * Campaign Monitor hold functions
 * .*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Component\Utility\SafeMarkup;

/**
 *
 * @param $list_id
 * @param $mail
 */
function campaignmonitor_registration_hold_campaignmonitor_registration_submit($list_id, $mail) {

  // Save.
  $uid = campaignmonitor_get_uid_from_email($mail);
  campaignmonitor_registration_hold_dbmerge($uid, $list_id);

  // Prevent immediate creation of subscription.
  return [
    'subscribe' => 0,
  ];
}

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

/**
 * Implements hook_ENTITY_TYPE_update().
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 */
function campaignmonitor_registration_hold_user_update(EntityInterface $entity) {

  // When the user is updated and is activated check for a hold record.
  // Todo provide a check to see if previous status was 0.
  if ($entity
    ->get('status')
    ->getValue()[0]['value'] == 1) {
    if ($lists = campaignmonitor_registration_hold_user_records($entity
      ->id())) {
      $mail = $entity
        ->get('mail')
        ->getValue()[0]['value'];
      foreach ($lists as $list_id) {
        $settings = campaignmonitor_get_list_settings($list_id);
        $name = $entity
          ->get('name')
          ->getValue()[0]['value'];
        if (isset($settings['display']['name_field']) && is_string($settings['display']['name_field']) && $settings['display']['name_field'] != '') {
          $field_name = $entity
            ->get($settings['display']['name_field'])
            ->getValue();
          if (isset($field_name[0]['value']) && $field_name[0]['value'] != '') {
            $name = $field_name[0]['value'];
          }
        }

        // Try to subscribe to the list.
        try {
          campaignmonitor_subscribe($list_id, SafeMarkup::checkPlain($mail), SafeMarkup::checkPlain($name));

          // Todo send out an alert?
          campaignmonitor_registration_hold_delete_record($entity
            ->id(), $list_id);
        } catch (Exception $e) {
          \Drupal::logger('campaignmonitor_registration_hold')
            ->error('An error occurred subscribing {email} to list {list}.
          "{message}"', [
            'email' => $mail,
            'list' => $list_id,
            'message' => $e
              ->getMessage(),
          ]);
        }
      }
    }
  }
}

/**
 * Get registration hold records if they exist.
 *
 * @param $uid
 *
 * @return mixed
 */
function campaignmonitor_registration_hold_user_records($uid) {
  $result = \Drupal::database()
    ->select('campaignmonitor_registration_hold', 'crh')
    ->condition('crh.uid', $uid)
    ->fields('crh', [
    'list_id',
  ])
    ->execute()
    ->fetchAll();
  $lists = [];
  foreach ($result as $list) {
    $lists[] = $list->list_id;
  }
  return $lists;
}

/**
 * Delete a registration hold record.
 *
 * @param $uid
 * @param $list_id
 */
function campaignmonitor_registration_hold_delete_record($uid, $list_id) {
  return \Drupal::database()
    ->delete('campaignmonitor_registration_hold')
    ->condition('uid', $uid)
    ->condition('list_id', $list_id)
    ->execute();
}