You are here

function constant_contact_user in Constant Contact 6.2

Same name and namespace in other branches
  1. 6.3 constant_contact.module \constant_contact_user()

This method adds extra functionality to the user methods Create, Update, Delete

File

./constant_contact.module, line 228

Code

function constant_contact_user($op, &$edit, &$account, $category = NULL) {
  global $user;
  $action_type = 'contact';
  if (isset($user->uid) && $user->uid) {
    $uid = $user->uid;
    $allowed = array(
      'Administrator',
      'Editor',
    );
    foreach ($user->roles as $role) {
      if (in_array($role, $allowed)) {
        $action_type = 'customer';
      }
    }
  }
  $show_on_register = variable_get('constant_contact_show_on_register', CONSTANT_CONTACT_SHOW_ON_REGISTER);
  $show_lists_selection = variable_get('constant_contact_show_list_selection', CONSTANT_CONTACT_SHOW_LIST_SELECTION);
  $lists = variable_get('constant_contact_lists', '');
  $selected_lists = array();
  if (!$show_on_register) {
    return;
  }
  $valid_ops = array(
    'form',
    'delete',
    'after_update',
    'insert',
  );
  if (in_array($op, $valid_ops)) {
    $cc = constant_contact_create_object();
    if (!is_object($cc)) {
      drupal_set_message(t("Failed to create constant contact object, please check your account details are correctly entered on the settings page"), 'error');
      return;
    }
  }
  if ($op == 'form') {
    $selected_lists = array();
    if (!$show_on_register) {
      return;
    }
    $cc = constant_contact_create_object();
    if (!is_object($cc)) {
      drupal_set_message(t("Failed to create constant contact object, please check your account details are correctly entered on the settings page"), 'error');
      return;
    }

    // get contact and selected lists
    $contact = $cc
      ->query_contacts($account->mail);
    $contact = $cc
      ->get_contact($contact['id']);
    if ($contact['lists']) {
      $selected_lists = $contact['lists'];
    }
    $form['constant_contact'] = array(
      '#type' => 'fieldset',
      '#title' => t('Newsletter'),
      '#collapsible' => TRUE,
      '#collapsed' => true,
      '#tree' => false,
      '#description' => t('Manage your newsletter subscription'),
    );
    $form['constant_contact']['cc_newsletter'] = array(
      '#type' => 'checkbox',
      '#title' => variable_get('constant_contact_signup_title', CONSTANT_CONTACT_SIGNUP_TITLE),
      '#description' => variable_get('constant_contact_signup_description', CONSTANT_CONTACT_SIGNUP_DESCRIPTION),
      '#weight' => 10,
    );
    $form['constant_contact']['cc_newsletter']['#default_value'] = $account->cc_newsletter;
    if ($show_lists_selection) {
      $exclude_lists = variable_get('constant_contact_lists', '');
      $lists = $cc
        ->get_lists();
      if ($lists) {
        foreach ($lists as $k => $v) {
          $lists[$k] = $v['id'];
        }
      }
      if (!is_array($exclude_lists)) {
        $exclude_lists = array();
      }
      $options = array();
      foreach ($lists as $list_id) {
        if (!in_array($list_id, $exclude_lists)) {
          $list = $cc
            ->get_list($list_id);
          $options[$list['id']] = $list['Name'];
        }
      }
      if (count($options) > 0) {
        $form['constant_contact']['cc_newsletter_lists'] = array(
          '#type' => 'select',
          '#description' => variable_get('constant_contact_signup_lists_description', CONSTANT_CONTACT_SIGNUP_LISTS_DESCRIPTION),
          '#options' => $options,
          '#multiple' => true,
          '#size' => count($options),
          '#default_value' => $selected_lists,
          '#weight' => 11,
        );
      }
    }
    return $form;
  }
  elseif ($op == 'insert') {
    if (isset($edit['cc_newsletter']) && $edit['cc_newsletter']) {
      $lists = variable_get('constant_contact_lists', '');
      $fields = variable_get('constant_contact_custom_fields', CONSTANT_CONTACT_CUSTOM_FIELDS);
      if ($show_lists_selection && is_array($lists)) {
        $lists = $edit['cc_newsletter_lists'];
      }
      elseif (!$show_lists_selection) {
        $_lists = $cc
          ->get_lists();
        if ($_lists) {
          foreach ($_lists as $k => $v) {
            $_lists[$k] = $v['id'];
          }
        }
        $newlists = array();
        foreach ($_lists as $list_id) {
          if (in_array($list_id, $lists)) {
            $list = $cc
              ->get_list($list_id);
            $newlists[$list['id']] = $list['Name'];
          }
        }
        $lists = $newlists;
      }
      if ($lists) {

        // parse custom fields
        $fields_bits = explode(',', $fields);
        $fields = array();
        if ($fields_bits) {
          foreach ($fields_bits as $bit) {
            $field_bit = explode(':', trim($bit));
            if (is_array($field_bit) && isset($field_bit[0], $field_bit[1])) {
              $cc_fieldname = $field_bit[0];
              $fieldname = $field_bit[1];

              // if the fieldname has been posted add to our custom CC fields array
              if (isset($_POST[$fieldname])) {
                $fields[$cc_fieldname] = $_POST[$fieldname];
              }
            }
          }
        }

        // find contact
        $contact = $cc
          ->query_contacts($edit['mail']);
        $cc
          ->set_action_type($action_type);
        if ($contact) {
          $contact = $cc
            ->get_contact($contact['id']);

          // merge contact lists user is already subscribed to
          if ($lists && $contact['lists']) {
            foreach ($contact['lists'] as $list_id) {
              if (!isset($lists[$list_id])) {
                $list = $cc
                  ->get_list($list_id);
                $lists[$list_id] = $list['Name'];
              }
            }
          }
          $status = $cc
            ->update_contact($contact['id'], $edit['mail'], array_keys($lists), $fields);
        }
        else {
          $status = $cc
            ->create_contact($edit['mail'], array_keys($lists), $fields);
        }
        if (!$status) {
          drupal_set_message(t("Sorry we encountered an error: {$cc->last_error}"), 'error');
        }
      }
    }
  }
  elseif ($op == 'delete') {
    if ($account->cc_newsletter) {

      // find contact
      $contact = $cc
        ->query_contacts($account->mail);

      /* important, this tells CC that the contact made this action */
      $cc
        ->set_action_type($action_type);
      if ($contact) {
        $cc
          ->delete_contact($contact['id']);
      }
    }
  }
  elseif ($op == 'after_update') {
    $fields = variable_get('constant_contact_custom_fields', CONSTANT_CONTACT_CUSTOM_FIELDS);
    if ($show_lists_selection) {
      $lists = $edit['cc_newsletter_lists'];
    }
    else {
      $_lists = $cc
        ->get_lists();
      if ($_lists) {
        foreach ($_lists as $k => $v) {
          $_lists[$k] = $v['id'];
        }
      }
      $newlists = array();
      foreach ($_lists as $list_id) {
        if (in_array($list_id, $lists)) {
          $list = $cc
            ->get_list($list_id);
          $newlists[$list['id']] = $list['Name'];
        }
      }
      $lists = $newlists;
    }

    // parse custom fields
    $fields_bits = explode(',', $fields);
    $fields = array();
    if ($fields_bits) {
      foreach ($fields_bits as $bit) {
        $field_bit = explode(':', trim($bit));
        if (is_array($field_bit) && isset($field_bit[0], $field_bit[1])) {
          $cc_fieldname = $field_bit[0];
          $fieldname = $field_bit[1];

          // if the fieldname has been posted add to our custom CC fields array
          if (isset($edit[$fieldname])) {
            $fields[$cc_fieldname] = $edit[$fieldname];
          }
        }
      }
    }

    // find contact
    $contact = $cc
      ->query_contacts($edit['mail']);
    $cc
      ->set_action_type('contact');
    $status = true;
    if ($contact) {
      $contact = $cc
        ->get_contact($contact['id']);

      // merge contact lists user is already subscribed to
      if ($lists && $contact['lists']) {
        foreach ($contact['lists'] as $list_id) {
          if (!isset($lists[$list_id])) {
            $list = $cc
              ->get_list($list_id);
            $lists[$list_id] = $list['Name'];
          }
        }
      }
      if (isset($edit['cc_newsletter']) && $edit['cc_newsletter']) {
        $status = $cc
          ->update_contact($contact['id'], $edit['mail'], array_keys($lists), $fields);
      }
      else {
        $status = $cc
          ->update_contact($contact['id'], $edit['mail'], array(), $fields);
      }
    }
    elseif (isset($edit['cc_newsletter']) && $edit['cc_newsletter']) {
      $status = $cc
        ->create_contact($edit['mail'], array_keys($lists), $fields);
    }
  }
}