You are here

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

Administrative page callbacks for the zopim module.

File

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

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

/**
 * Implementation of hook_admin_settings() for configuring the module
 */
function zopim_admin_settings_form(&$form_state) {
  $settings = zopim_get_settings();
  $account_details = zopim_get_account_details();

  // Add Farbtastic color picker and ctools dependent js.
  $form['js']['#after_build'] = array(
    'zopim_admin_settings_form_load_js',
  );

  // General Settings
  $form['widget'] = array(
    '#type' => 'fieldset',
    '#title' => t('General'),
    '#collapsible' => FALSE,
    '#tree' => TRUE,
  );
  $form['widget']['language'] = array(
    '#type' => 'select',
    '#title' => t('Language'),
    '#options' => _zopim_get_languages(),
    '#default_value' => $settings['widget']['language'],
  );
  $form['widget']['position'] = array(
    '#type' => 'select',
    '#title' => t('Position'),
    '#options' => array(
      'br' => t('Bottom Right'),
      'bl' => t('Bottom Left'),
    ),
    '#default_value' => $settings['widget']['position'],
  );
  $form['widget']['use_user_info'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use Logged in Username / Email'),
    '#description' => t('This will use the visitors Username and Email address if they are logged into this site.'),
    '#default_value' => $settings['widget']['use_user_info'],
  );
  $form['widget']['hide_when_offline'] = array(
    '#type' => 'checkbox',
    '#title' => t('Hide chat bar when offline'),
    '#description' => t('This prevents visitors from sending you offline messages.'),
    '#default_value' => $settings['widget']['hide_when_offline'],
  );

  // Theming
  $form['theming'] = array(
    '#type' => 'fieldset',
    '#title' => t('Color and Theme'),
    '#collapsible' => FALSE,
    '#tree' => TRUE,
  );
  $form['theming']['color'] = array(
    '#type' => 'textfield',
    '#title' => t('Color'),
    '#description' => $account_details->color_customization_enabled !== 1 ? t('Sorry, your plan does not allow for color customization. Please upgrade to enjoy choice of color!') : t('Sets the color for the chat bubble.'),
    '#default_value' => $settings['theming']['color'],
    '#suffix' => '<div id="colorpicker"></div>',
    '#size' => 10,
    '#disabled' => $account_details->color_customization_enabled !== 1,
  );
  $form['theming']['theme'] = array(
    '#type' => 'select',
    '#title' => t('Select a Theme'),
    '#description' => $account_details->widget_customization_enabled !== 1 ? t('Sorry, your plan does not allow for theme customization. Please upgrade to enjoy choice of themes!') : t('View the chat panel for changes.'),
    '#options' => zopim_get_themes(),
    '#default_value' => $settings['theming']['theme'],
    '#disabled' => $account_details->widget_customization_enabled !== 1,
  );

  // Help Bubble
  $form['help_bubble'] = array(
    '#type' => 'fieldset',
    '#title' => t('Help Bubble'),
    '#collapsible' => FALSE,
    '#tree' => TRUE,
  );
  $form['help_bubble']['display'] = array(
    '#type' => 'select',
    '#title' => t('Display Help Bubble'),
    '#options' => array(
      'default' => t('Let user decide'),
      'show' => t('Always show'),
      'hide' => t('Always hide'),
    ),
    '#default_value' => $settings['help_bubble']['display'],
  );
  $form['help_bubble']['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Help Bubble Title'),
    '#description' => t('Entice your visitors with this friendly chat bubble title!'),
    '#process' => array(
      'ctools_dependent_process',
    ),
    '#dependency' => array(
      'edit-help-bubble-display' => array(
        'default',
        'show',
      ),
    ),
    '#default_value' => $settings['help_bubble']['title'],
  );
  $form['help_bubble']['message'] = array(
    '#type' => 'textfield',
    '#title' => t('Help Bubble Message'),
    '#description' => t('Welcome your visitors and encourage them to talk with you.'),
    '#process' => array(
      'ctools_dependent_process',
    ),
    '#dependency' => array(
      'edit-help-bubble-display' => array(
        'default',
        'show',
      ),
    ),
    '#default_value' => $settings['help_bubble']['message'],
  );

  // Greeting Messages
  $form['greeting_messages'] = array(
    '#type' => 'fieldset',
    '#title' => t('Greeting Message'),
    '#collapsible' => FALSE,
    '#tree' => TRUE,
  );
  $form['greeting_messages']['enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use Greetings'),
    '#description' => t('This prevents visitors from sending you offline messages.'),
    '#default_value' => $settings['greeting_messages']['enabled'],
  );
  $form['greeting_messages']['chatbar'] = array(
    '#type' => 'fieldset',
    '#title' => t('Chatbar Messages'),
    '#collapsible' => FALSE,
    '#process' => array(
      'ctools_dependent_process',
    ),
    '#dependency' => array(
      'edit-use-greetings' => array(
        '1',
      ),
    ),
    '#input' => TRUE,
    '#id' => 'zopim-greetings-chatbar',
    '#prefix' => '<div id="zopim-greetings-chatbar-wrapper">',
    '#suffix' => '</div>',
  );
  $form['greeting_messages']['chatbar']['online'] = array(
    '#type' => 'textfield',
    '#title' => t('Online'),
    '#maxlength' => 26,
    '#description' => t('Maximum length of 26 characters.'),
    '#default_value' => $settings['greeting_messages']['chatbar']['online'],
  );
  $form['greeting_messages']['chatbar']['away'] = array(
    '#type' => 'textfield',
    '#title' => t('Away'),
    '#maxlength' => 26,
    '#description' => t('Maximum length of 26 characters.'),
    '#default_value' => $settings['greeting_messages']['chatbar']['away'],
  );
  $form['greeting_messages']['chatbar']['offline'] = array(
    '#type' => 'textfield',
    '#title' => t('Offline'),
    '#maxlength' => 26,
    '#description' => t('Maximum length of 26 characters.'),
    '#default_value' => $settings['greeting_messages']['chatbar']['offline'],
  );
  $form['greeting_messages']['panel'] = array(
    '#type' => 'fieldset',
    '#title' => t('Panel Messages'),
    '#collapsible' => FALSE,
    '#process' => array(
      'ctools_dependent_process',
    ),
    '#dependency' => array(
      'edit-use-greetings' => array(
        '1',
      ),
    ),
    '#input' => TRUE,
    '#id' => 'zopim-greetings-panel',
    '#prefix' => '<div id="zopim-greetings-panel-wrapper">',
    '#suffix' => '</div>',
  );
  $form['greeting_messages']['panel']['online'] = array(
    '#type' => 'textfield',
    '#title' => t('Online'),
    '#maxlength' => 26,
    '#description' => t('Maximum length of 140 characters.'),
    '#default_value' => $settings['greeting_messages']['panel']['online'],
  );
  $form['greeting_messages']['panel']['away'] = array(
    '#type' => 'textfield',
    '#title' => t('Away'),
    '#maxlength' => 26,
    '#description' => t('Maximum length of 140 characters.'),
    '#default_value' => $settings['greeting_messages']['panel']['away'],
  );
  $form['greeting_messages']['panel']['offline'] = array(
    '#type' => 'textfield',
    '#title' => t('Offline'),
    '#maxlength' => 26,
    '#description' => t('Maximum length of 140 characters.'),
    '#default_value' => $settings['greeting_messages']['panel']['offline'],
  );
  $form['#submit'][] = 'zopim_set_settings';
  return system_settings_form($form);
}
function zopim_admin_settings_form_load_js($element, &$form_state) {
  if (!$form_state['form']['form']['theming']['color']['#disabled']) {
    drupal_add_css('misc/farbtastic/farbtastic.css');
    drupal_add_js('misc/farbtastic/farbtastic.js');
    drupal_add_js(drupal_get_path('module', 'zopim') . '/includes/zopim-customize.js');
  }
  ctools_include('dependent');
  return $element;
}
function zopim_admin_settings_form_validate($form, &$form_state) {
  $font_color = $form_state['values']['theming']['color'];
  if (!empty($font_color) && !zopim_is_hex_code($font_color)) {
    form_set_error('theming][color', t('Font Color should be a valid hexidecimal code.'));
  }
}
function zopim_is_hex_code($string) {
  return preg_match('/^#([a-f0-9]{3}){1,2}$/iD', $string) > 0;
}
function _zopim_get_languages() {
  $languages = array(
    '--' => t('Auto-Detect'),
    'ar' => t('Arabic'),
    'bg' => t('Bulgarian'),
    'cs' => t('Czech'),
    'da' => t('Danish'),
    'de' => t('German'),
    'es' => t('Spanish; Castilian'),
    'fa' => t('Persian'),
    'fo' => t('Faroese'),
    'fr' => t('French'),
    'he' => t('Hebrew'),
    'hr' => t('Croatian'),
    'id' => t('Indonesian'),
    'it' => t('Italian'),
    'ja' => t('Japanese'),
    'ko' => t('Korean'),
    'ms' => t('Malay'),
    'nb' => t('Norwegian Bokmal'),
    'nl' => t('Dutch; Flemish'),
    'pl' => t('Polish'),
    'pt' => t('Portuguese'),
    'ru' => t('Russian'),
    'sk' => t('Slovak'),
    'sl' => t('Slovenian'),
    'sv' => t('Swedish'),
    'th' => t('Thai'),
    'tr' => t('Turkish'),
    'ur' => t('Urdu'),
    'vi' => t('Vietnamese'),
    'zh_CN' => t('Chinese (China)'),
  );
  return $languages;
}
function zopim_get_themes() {
  $settings = zopim_get_settings();
  $ssl = $ssl ? $ssl : $settings['general']['use_ssl'];
  $headers = array(
    'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
  );
  $url = $ssl === 1 ? ZOPIM_THEMES_LIST : str_replace("https", "http", ZOPIM_THEMES_LIST);
  $request = drupal_http_request($url, $headers);
  $themes = explode("\n", $request->data);
  $keyed_themes = array();
  foreach ($themes as $id) {
    if (!empty($id)) {
      $keyed_themes[$id] = $id;
    }
  }
  ksort($keyed_themes);
  return $keyed_themes;
}