You are here

zopim-account.admin.inc in Zopim Live Chat 6.2

Administrative page callbacks for the zopim module.

File

includes/zopim-account.admin.inc
View source
<?php

/**
 * @file
 * Administrative page callbacks for the zopim module.
 */

/**
 * Implementation of hook_admin_settings().
 */
function zopim_admin_settings_form(&$form_state) {
  $settings = zopim_get_settings();
  $account_details = zopim_get_account_details();

  // Add ctools dependent js.
  $form['js']['#after_build'] = array(
    'zopim_admin_settings_form_load_js',
  );
  if ($account_details->error == 'Invalid Salt') {
    drupal_set_message(t('We could not verify your Zopim account.  You may need to reset your Zopim credentials below.'), 'error');
  }
  if (is_object($account_details)) {
    if ($account_details->package_id == "trial") {
      $account_details->package_id = t('Free Lite Package + 14 Days Full-features');
    }
    else {
      $account_details->package_id = t('%s Package', array(
        '%s' => ucfirst($account_details->package_id),
      ));
    }
  }
  $form['general'] = array(
    '#type' => 'fieldset',
    '#title' => t('General Settings'),
    '#collapsible' => FALSE,
    '#tree' => TRUE,
  );

  // array of translatable tokens used in agreement text.
  $agreement_tokens = array(
    '%s' => 'Zopim',
    '!tos' => l(t('Terms of Service'), 'http://www.zopim.com/termsnconditions'),
    '!pol' => l(t('Privacy Policy'), 'http://www.zopim.com/privacypolicy'),
  );
  $form['general']['agreement'] = array(
    '#title' => $settings['general']['agreement'] === 1 ? t("You have already accepted %s's !tos and !pol", $agreement_tokens) : t("I agree to %s's !tos and !pol", $agreement_tokens),
    '#type' => 'checkbox',
    '#required' => TRUE,
    '#description' => t("You must agree to Zopim's terms in order to use this module."),
    '#disabled' => $settings['general']['agreement'] === 1,
    '#default_value' => $settings['general']['agreement'],
  );
  $form['general']['use_ssl'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use SSL Connection for Account Authorization'),
    '#description' => t('Uncheck this if you are unable to login or register.'),
    '#default_value' => $settings['general']['use_ssl'],
  );
  $form['account_setup'] = array(
    '#type' => 'fieldset',
    '#title' => !$settings['general']['username'] && !$settings['general']['password'] ? t('Account Setup') : t('Account Settings'),
    '#collapsible' => FALSE,
    '#tree' => TRUE,
  );

  // If no username/password set then allow setting up new account.
  if (!$settings['general']['username'] && !$settings['general']['password']) {
    $form['account_setup']['account'] = array(
      '#type' => 'radios',
      '#title' => t('Select whether you have an existing account or not'),
      '#required' => TRUE,
      '#options' => array(
        0 => t('Give me a new account - %s', array(
          '%s' => 'absolutely free!',
        )),
        1 => t('I already have a %s account', array(
          '%s' => 'Zopim',
        )),
      ),
      '#default_value' => $settings['general']['account'],
    );
    $form['account_setup']['existing_account'] = array(
      '#type' => 'fieldset',
      '#title' => t('Existing Account Settings'),
      '#collapsible' => FALSE,
      '#process' => array(
        'ctools_dependent_process',
      ),
      '#dependency' => array(
        'radio:account_setup[account]' => array(
          '1',
        ),
      ),
      '#input' => TRUE,
      '#id' => 'existing-account',
      '#prefix' => '<div id="existing-account-wrapper">',
      '#suffix' => '</div>',
      '#tree' => TRUE,
    );
    $form['account_setup']['existing_account']['username'] = array(
      '#type' => 'textfield',
      '#title' => t('Zopim Username (E-mail)'),
      '#size' => 32,
    );
    $form['account_setup']['existing_account']['password'] = array(
      '#type' => 'password',
      '#title' => t('Zopim Password'),
      '#size' => 32,
    );
    $form['account_setup']['new_account'] = array(
      '#type' => 'fieldset',
      '#title' => t('Create a New Account'),
      '#collapsible' => FALSE,
      '#process' => array(
        'ctools_dependent_process',
      ),
      '#dependency' => array(
        'radio:account_setup[account]' => array(
          '0',
        ),
      ),
      '#input' => TRUE,
      '#id' => 'new-account',
      '#prefix' => '<div id="new-account-wrapper">',
      '#suffix' => '</div>',
    );
    $form['account_setup']['new_account']['first_name'] = array(
      '#type' => 'textfield',
      '#title' => t('First Name'),
      '#size' => 32,
    );
    $form['account_setup']['new_account']['last_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Last Name'),
      '#size' => 32,
    );
    $form['account_setup']['new_account']['email'] = array(
      '#type' => 'textfield',
      '#title' => t('E-mail'),
      '#size' => 32,
    );

    // Had to http, instead of https for the scripts here, the https won't load
    // due to invalid certificate from recaptcha.
    $recaptcha = <<<EOC
<script type="text/javascript" src="http://api-secure.recaptcha.net/challenge?k=6Lfr8AQAAAAAAC7MpRXM2hgLfyss_KKjvcJ_JFIk"></script>
<noscript>
  <iframe src="http://api-secure.recaptcha.net/noscript?k=6Lfr8AQAAAAAAC7MpRXM2hgLfyss_KKjvcJ_JFIk" height="300" width="500" frameborder="0"></iframe><br />
  <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
  <input type="hidden" name="recaptcha_response_field" value="manual_challenge">
</noscript>
EOC;
    $form['account_setup']['new_account']['verification'] = array(
      '#title' => t('Verification'),
      '#type' => 'markup',
      '#value' => $recaptcha,
    );
  }
  else {
    $form['account_setup']['username'] = array(
      '#value' => '<div class="form-item form-item-labeled"><label>' . t('Zopim Username (E-mail)') . '</label><div>' . $settings['general']['username'] . '</div></div>',
    );
    $form['account_setup']['package'] = array(
      '#value' => '<div class="form-item form-item-labeled"><label>' . t('Package') . '</label><div><i>' . $account_details->package_id . '</i></div></div>',
    );
    $form['account_setup']['existing_account']['reset'] = array(
      '#type' => 'checkbox',
      '#title' => t('Reset Zopim Credentials'),
      '#description' => t('This will reset your Zopim Username and Password that are stored in Drupal, allowing you to reinput them.'),
      '#default_value' => 0,
    );
  }
  $form['#submit'][] = 'zopim_set_settings';
  return system_settings_form($form);
}
function zopim_admin_settings_form_load_js($element) {
  ctools_include('dependent');
  return $element;
}
function zopim_admin_settings_form_validate($form, &$form_state) {
  $settings = zopim_get_settings();
  switch ($form_state['values']['account_setup']['account']) {
    case '1':

      // setting credentials for an existing account.
      $data = array(
        'email' => $form_state['values']['account_setup']['existing_account']['username'],
        'password' => $form_state['values']['account_setup']['existing_account']['password'],
      );
      $data_response = zopim_post_request(ZOPIM_LOGIN_URL, $data, $form_state['values']['general']['use_ssl']);
      if (isset($data_response->error)) {
        form_set_error('account_setup][existing_account][username', 'Zopim Username and/or Password are incorrect.');
        form_set_error('account_setup][existing_account][password', ' ');
      }
      else {
        $form_state['values']['general']['salt'] = $data_response->salt;
        $form_state['values']['general']['account'] = 1;
        $form_state['values']['general']['username'] = $form_state['values']['account_setup']['existing_account']['username'];
        $form_state['values']['general']['password'] = md5($form_state['values']['account_setup']['existing_account']['password']);
        $account_details = zopim_get_account_details($data_response->salt);
        $form_state['values']['general']['account_key'] = $account_details->account_key;
        $form_state['values']['general']['customization']['color'] = $account_details->color_customization_enabled;
        $form_state['values']['general']['customization']['widget'] = $account_details->widget_customization_enabled;
      }
      break;
    case '0':

      // creating new account.
      $data = array(
        'email' => $form_state['values']['account_setup']['new_account']['email'],
        'first_name' => $form_state['values']['account_setup']['new_account']['first_name'],
        'last_name' => $form_state['values']['account_setup']['new_account']['last_name'],
        'display_name' => $form_state['values']['account_setup']['new_account']['first_name'] . ' ' . $form_state['values']['account_setup']['new_account']['last_name'],
        'eref' => '',
        'source' => "drupal",
        'recaptcha_challenge_field' => $_POST["recaptcha_challenge_field"],
        'recaptcha_response_field' => $_POST["recaptcha_response_field"],
      );
      $data_response = zopim_post_request(ZOPIM_SIGNUP_URL, $data, $form_state['values']['general']['use_ssl']);
      if ($data_response->account_key) {
        $account_key = $data_response->account_key;
        $form_state['values']['general']['account_key'] = $account_key;
        $form_state['values']['general']['account'] = 1;
        drupal_set_message(t('Thank you for signing up. Please check your E-mail for your password then enter your credentials here to complete the process.'), 'warning');
      }
      else {
        form_set_error('account_setup][new_account][first_name', t('Error during activation: ') . $data_response->error . '. Please try again.');
        form_set_error('account_setup][new_account][last_name', ' ');
        form_set_error('account_setup][new_account][email', ' ');
      }
      break;
    default:

      // reseting credentials.
      $form_state['values']['general']['account'] = 0;
      $form_state['values']['general']['account_key'] = FALSE;
      $form_state['values']['general']['agreement'] = 0;
      $form_state['values']['general']['username'] = FALSE;
      $form_state['values']['general']['password'] = FALSE;
      $form_state['values']['general']['salt'] = FALSE;
      drupal_set_message(t('Your credentials have been deleted.  You must add credentials for another account or create a new account to continue using Zopim.'), 'warning');
      break;
  }

  // remove account_setup and all child elements so as to not set them in the variable.
  unset($form_state['values']['account_setup']);
}

Functions