You are here

function mailchimp_admin_settings in Mailchimp 5.2

Same name and namespace in other branches
  1. 5 mailchimp.module \mailchimp_admin_settings()
  2. 6.2 mailchimp.admin.inc \mailchimp_admin_settings()
  3. 6 mailchimp.module \mailchimp_admin_settings()
  4. 7.5 includes/mailchimp.admin.inc \mailchimp_admin_settings()
  5. 7 mailchimp.admin.inc \mailchimp_admin_settings()
  6. 7.2 includes/mailchimp.admin.inc \mailchimp_admin_settings()
  7. 7.3 includes/mailchimp.admin.inc \mailchimp_admin_settings()
  8. 7.4 includes/mailchimp.admin.inc \mailchimp_admin_settings()
1 string reference to 'mailchimp_admin_settings'
mailchimp_menu in ./mailchimp.module
Implementation of hook_menu.

File

./mailchimp.module, line 266

Code

function mailchimp_admin_settings() {
  $form['mailchimp_account_info'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => 'MailChimp Account Information',
  );

  // only show the list selection forms if account info provided
  $username = variable_get('mailchimp_username', FALSE);
  $password = variable_get('mailchimp_password', FALSE);
  if ($username && $password) {
    $form['mailchimp_account_info']['mailchimp_setup_account'] = array(
      '#value' => t('Current configured account is for: @username.', array(
        '@username' => $username,
      )),
    );
    $form['mailchimp_account_info']['reset'] = array(
      '#value' => l('Click here to login as a different user.', 'mailchimp/reset'),
    );
    $q = new MCAPI($username, $password);
    if (!$q->errorCode) {
      $lists = $q
        ->lists();
      if (!empty($lists)) {
        $form['mailchimp_lists'] = array(
          '#type' => 'fieldset',
          '#collapsible' => FALSE,
          '#title' => 'MailChimp Subscription Lists',
        );
        foreach ($lists as $list) {
          $form['mailchimp_lists']['mailchimp_list_' . $list['id']] = array(
            '#type' => 'fieldset',
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
            '#title' => $list['name'],
          );

          // Add a row of checkboxes for role enabling.
          $form['mailchimp_lists']['mailchimp_list_' . $list['id']]['mailchimp_list_' . $list['id'] . '_roles'] = array(
            '#type' => 'fieldset',
            '#title' => t('Roles'),
            '#description' => t('Choose which roles may subscribe to this list. Select the anonymous user role to have the list appear on the !subscribe page. Select the authenticated user role to have the list appear on the !signup page. For all other roles, the list will appear on the settings page for users with that role.', array(
              '!subscribe' => l(t('Newsletter Subscription'), 'mailchimp/subscribe'),
              '!signup' => l(t('User Registration'), 'user/register'),
            )),
            '#tree' => FALSE,
          );
          foreach (user_roles() as $rid => $name) {
            $form['mailchimp_lists']['mailchimp_list_' . $list['id']]['mailchimp_list_' . $list['id'] . '_roles']['mailchimp_list_' . $list['id'] . '_role_' . $rid] = array(
              '#type' => 'checkbox',
              '#title' => $name,
              '#default_value' => variable_get('mailchimp_list_' . $list['id'] . '_role_' . $rid, 0),
            );
          }
          $form['mailchimp_lists']['mailchimp_list_' . $list['id']]['mailchimp_list_' . $list['id'] . '_description'] = array(
            '#type' => 'textarea',
            '#title' => t('List Description'),
            '#default_value' => variable_get('mailchimp_list_' . $list['id'] . '_description', ''),
            '#description' => t('This description will be shown to the user on the list signup and user account settings pages'),
          );
          $form['mailchimp_lists']['mailchimp_list_' . $list['id']]['mailchimp_list_' . $list['id'] . '_listtype'] = array(
            '#type' => 'select',
            '#title' => t('Subscription Method'),
            '#options' => array(
              'optin' => "Opt-in",
              'optin' => "Opt-in",
              'optout' => 'Opt-out',
              'required' => 'Required',
            ),
            '#default_value' => variable_get('mailchimp_list_' . $list['id'] . '_listtype', 'optin'),
            '#description' => t('<strong>Opt-in:</strong> Users must sign up to recieve messages.<br/><strong>Opt-out: </strong> Users are automatically signed up but may unsubscribe.<br/><strong>Required: </strong> Users will remain on the list as long as they have an account and cannot unsubscribe.'),
          );
          $form['mailchimp_lists']['mailchimp_list_' . $list['id']]['mailchimp_list_' . $list['id'] . '_doublein'] = array(
            '#type' => 'checkbox',
            '#title' => t('Require subscribers to Double Opt-in'),
            '#default_value' => variable_get('mailchimp_list_' . $list['id'] . '_doublein', 0),
            '#description' => t('New subscribers will be sent a link with an email they must follow to confirm their subscription.'),
          );
          $mergevars = $q
            ->listMergeVars($list['id']);
          if ($mergevars) {

            // Merge var fieldset
            $form['mailchimp_lists']['mailchimp_list_' . $list['id']]['mailchimp_list_' . $list['id'] . '_mergevars'] = array(
              '#type' => 'fieldset',
              '#title' => t('Merge Variables'),
              '#description' => t('Select Drupal user variables to send to Mailchimp as Merge Variables. Available Drupal variables are any Profile, Bio module or Token variables for the given user. For more information on Merge Variables, see the !doc', array(
                '!doc' => l(t('Mailchimp Documentation'), 'http://server.iad. liveperson.net/hc/s-31286565/cmd/kbresource/kb-8214439208090042855/view_question!PAGETYPE?sq=merge%2bvariables&sf=101113&sg=0&st=188569&documentid=143258&action=view'),
              )),
              '#tree' => FALSE,
            );

            //@TODO: handle interest categories coming from MC
            $mergeoptions = mailchimp_get_merge_keys();
            foreach ($mergevars as $mergevar) {
              if ($mergevar['tag'] !== 'EMAIL') {
                $form['mailchimp_lists']['mailchimp_list_' . $list['id']]['mailchimp_list_' . $list['id'] . '_mergevars']['mailchimp_list_' . $list['id'] . '_merge_' . $mergevar['tag']] = array(
                  '#type' => 'select',
                  '#title' => $mergevar['name'],
                  '#options' => $mergeoptions,
                  '#default_value' => variable_get('mailchimp_list_' . $list['id'] . '_merge_' . $mergevar['tag'], ''),
                );
              }
            }
          }
        }
        $form['mailchimp_messages'] = array(
          '#type' => 'fieldset',
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
          '#title' => 'Messaging',
        );
        $form['mailchimp_messages']['mailchimp_subscription_success_message'] = array(
          '#type' => 'textarea',
          '#title' => t('Subscription Success Message'),
          '#default_value' => variable_get('mailchimp_subscription_success_message', SUBSCRIPTION_SUCCESS),
        );
        $form['mailchimp_messages']['mailchimp_subscription_failure_message'] = array(
          '#type' => 'textarea',
          '#title' => t('Subscription Failure Message'),
          '#default_value' => variable_get('mailchimp_subscription_failure_message', SUBSCRIPTION_FAILURE),
        );
        $form['mailchimp_messages']['mailchimp_unsubscription_success_message'] = array(
          '#type' => 'textarea',
          '#title' => t('Unsubscription Success Message'),
          '#default_value' => variable_get('mailchimp_unsubscription_success_message', UNSUBSCRIPTION_SUCCESS),
        );
        $form['mailchimp_messages']['mailchimp_unsubscription_failure_message'] = array(
          '#type' => 'textarea',
          '#title' => t('Unsubscription Failure Message'),
          '#default_value' => variable_get('mailchimp_unsubscription_failure_message', UNSUBSCRIPTION_FAILURE),
        );
        $form['mailchimp_user_register'] = array(
          '#type' => 'checkbox',
          '#title' => t('Show Subscription Options on User Registration Page'),
          '#default_value' => variable_get('mailchimp_registration', TRUE),
        );
        $form['mailchimp_user_edit'] = array(
          '#type' => 'checkbox',
          '#title' => t('Show Subscription Options on User Edit Screen'),
          '#default_value' => variable_get('mailchimp_user_edit', TRUE),
        );
        $form['mailchimp_cron'] = array(
          '#type' => 'checkbox',
          '#title' => t('Sync Required Lists During Cron'),
          '#default_value' => variable_get('mailchimp_cron', FALSE),
          '#description' => t('If this is set, users will be subscribed to the required list during cron runs. Otherwise subscription will take place when a user is added/edited.'),
        );
      }
      else {
        drupal_set_message(t('You do not have any valid MailChimp mailing lists.'));
      }
    }
    else {
      if (FALSE && $q->errorCode === 'INVALID_LOGIN') {
        drupal_set_message(t('Could not login to mailchimp. Please check your username and password.'), "error");
      }
      else {
        if ($q->errorMessage) {
          drupal_set_message(t('Could not retrieve info for mailchimp. The following error was returned: %error.', array(
            '%error' => $q->errorMessage,
          )), "error");
        }
        else {
          drupal_set_message(t('Could not retrieve info for mailchimp for an unknown reason. Please try again later'), "error");
        }
      }
    }
  }
  else {
    $form['mailchimp_account_info']['mailchimp_username'] = array(
      '#type' => 'textfield',
      '#title' => t('Mailchimp username'),
      '#required' => TRUE,
      '#default_value' => variable_get('mailchimp_username', ''),
      '#description' => t('The username used to login to your MailChimp account.'),
    );
    $form['mailchimp_account_info']['mailchimp_password'] = array(
      '#type' => 'password',
      '#title' => t('Mailchimp password'),
      '#required' => TRUE,
      '#default_value' => variable_get('mailchimp_password', ''),
      '#description' => t('The password used to login to your MailChimp account.'),
    );
  }
  return system_settings_form($form);
}