You are here

function mailchimp_user in Mailchimp 6

Same name and namespace in other branches
  1. 5.2 mailchimp.module \mailchimp_user()
  2. 5 mailchimp.module \mailchimp_user()
  3. 6.2 mailchimp.module \mailchimp_user()

File

./mailchimp.module, line 13
Mailchimp module.

Code

function mailchimp_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case "delete":
      if ($q = _mailchimp_get_api_object()) {
        foreach ((array) $q
          ->lists() as $list) {
          $ret = $ret || _mailchimp_unsubscribe_user($list, $account->mail, TRUE, $q);
          if (!$ret) {
            watchdog('mailchimp', $q->errorMessage, array(), WATCHDOG_ERROR);
          }
        }
        if (!$ret) {
          watchdog('mailchimp', $q->errorMessage, array(), WATCHDOG_ERROR);
        }
      }
      break;
    case "form":
      if (variable_get('mailchimp_user_edit', TRUE) && $category == 'account' && ($q = _mailchimp_get_api_object())) {
        $form = array();
        $list_form = array();
        $lists = _mailchimp_get_available_lists($account, $q);
        foreach ($lists as $list) {
          if (variable_get('mailchimp_list_' . $list['id'] . '_listtype', '') !== 'required') {
            $list_form['chimpmail_list_' . $list['id']] = array(
              '#type' => 'checkbox',
              '#title' => $list["name"],
              '#default_value' => _mailchimp_is_subscribed($list['id'], $account->mail, $q),
              '#description' => variable_get('mailchimp_list_' . $list['id'] . '_description', ''),
            );
          }
        }
        if ($list_form) {
          $form['chimpmail_lists'] = array(
            '#type' => 'fieldset',
            '#title' => t('Newsletter subscriptions'),
            '#weight' => 5,
            '#collapsible' => TRUE,
          );
          $form['chimpmail_lists'] = array_merge($form['chimpmail_lists'], $list_form);
        }
        return $form;
      }
      break;
    case "insert":
    case "update":
      if (!empty($edit) && ($q = _mailchimp_get_api_object())) {
        $lists = _mailchimp_get_available_lists($account, $q);
        foreach ($lists as $list) {
          if (variable_get('mailchimp_list_' . $list['id'] . '_listtype', '') === 'required') {
            if (!variable_get('mailchimp_cron', FALSE)) {
              if ($account->mail !== $edit['mail']) {
                $ret = $q
                  ->listUnsubscribe($list['id'], $account->mail, FALSE, FALSE, FALSE);
              }
              $userinfo = _mailchimp_load_user_list_mergevars($account->uid, $list['id'], $q
                ->listMergeVars($list['id']));
              $userinfo['EMAIL'] = $edit['mail'];
              $ret = _mailchimp_subscribe_user($list, $userinfo, FALSE, $q);
            }
          }
          else {
            $is_subscribed = _mailchimp_is_subscribed($list['id'], $account->mail, $q);
            $ret = TRUE;
            if (variable_get('mailchimp_user_edit', TRUE) && $op == "update" && $category == 'account' || variable_get('mailchimp_user_register', TRUE) && $op == "insert") {

              // subscribe the user if they are not previously subscribed and want to be
              if (!$is_subscribed && @$edit['chimpmail_list_' . $list['id']]) {
                $merge_vars = _mailchimp_load_user_list_mergevars($account->uid, $list['id'], $q
                  ->listMergeVars($list['id']));
                $merge_vars['EMAIL'] = $edit['mail'];
                $ret = _mailchimp_subscribe_user($list, $merge_vars, TRUE, $q);
              }
              else {
                if ($is_subscribed && !@$edit['chimpmail_list_' . $list['id']]) {
                  $ret = _mailchimp_unsubscribe_user($list, $account->mail, TRUE, $q);
                }
                else {
                  if ($is_subscribed && $account->mail !== $edit['mail']) {
                    $merge_vars = _mailchimp_load_user_list_mergevars($account->uid, $list['id'], $q
                      ->listMergeVars($list['id']));
                    $merge_vars['EMAIL'] = $edit['mail'];
                    $ret = _mailchimp_update_user($list, $account->mail, $merge_vars, $q);
                  }
                }
              }
            }
            else {
              if ($is_subscribed) {
                $merge_vars = _mailchimp_load_user_list_mergevars($account->uid, $list['id'], $q
                  ->listMergeVars($list['id']));
                $ret = _mailchimp_update_user($list, $account->mail, $merge_vars, $q);
              }
            }
            if (!$ret) {
              watchdog('mailchimp', $q->errorMessage, array(), WATCHDOG_ERROR);
            }
          }
        }
      }
      break;
  }
}