You are here

mobile_switch.admin.inc in Mobile Switch 6

Administrative page callbacks for the Mobile Switch module.

File

includes/mobile_switch.admin.inc
View source
<?php

/**
 * @file
 * Administrative page callbacks for the Mobile Switch module.
 */

/**
 * Form constructor for the Basic settings form.
 *
 * @ingroup forms
 */
function mobile_switch_settings_form() {
  if (variable_get('browscap_version', 0) == 0) {
    return mobile_switch_browscap_message();
  }
  $module_path = drupal_get_path('module', 'mobile_switch');
  drupal_add_js($module_path . '/js/mobile_switch.admin.js', 'module', 'footer');
  $form = array();
  $active_themes = array_merge(array(
    'none' => t('Do not use'),
  ), mobile_switch_get_themes());
  $form['global_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Theme settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => -5,
  );
  $form['global_settings']['mobile_switch_mobile_theme'] = array(
    '#type' => 'select',
    '#title' => t('Mobile theme'),
    '#description' => t('Which theme should be used as default theme when a mobile device detected.'),
    '#options' => $active_themes,
    '#default_value' => variable_get('mobile_switch_mobile_theme', 'none'),
  );
  $form['global_settings']['mobile_switch_admin_usage'] = array(
    '#type' => 'select',
    '#title' => t('Administration usage'),
    '#description' => t('Use the mobile theme on administration pages when a mobile device detected.'),
    '#options' => array(
      FALSE => t('No'),
      TRUE => t('Yes'),
    ),
    '#default_value' => variable_get('mobile_switch_admin_usage', 0),
  );
  $form['#submit'][] = 'mobile_switch_settings_form_submit';
  return system_settings_form($form);
}

/**
 * Form constructor for the Advanced settings form.
 *
 * @ingroup forms
 */
function mobile_switch_advanced_settings_form() {
  if (variable_get('browscap_version', 0) == 0) {
    return mobile_switch_browscap_message();
  }
  if (variable_get('mobile_switch_mobile_theme', 'none') === 'none') {
    return mobile_switch_theme_message();
  }
  $module_path = drupal_get_path('module', 'mobile_switch');
  drupal_add_js($module_path . '/js/mobile_switch.admin.js', 'module', 'footer');
  $form['preventing'] = array(
    '#type' => 'fieldset',
    '#title' => t('Preventing'),
    '#description' => t('Prevention of mobile devices. This allows to bypass mobile devices the automatic switching to the mobile theme.'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => -5,
  );
  $form['preventing']['mobile_switch_prevent_devices'] = array(
    '#type' => 'select',
    '#title' => t('Use preventing'),
    '#options' => array(
      FALSE => t('No'),
      TRUE => t('Yes'),
    ),
    '#default_value' => variable_get('mobile_switch_prevent_devices', 0),
  );
  $description['mobile_switch_prevent_devices_strings'] = t("Configure user agent string parts. Use letters, single white spaces or underscores - no other characters! Do not use the '*' wildcard character! Enter one string per line. Example string parts: <pre>htc_flyer\niPad\nSony Tablet S\nXOOM</pre> The string detection is case insensitive.");
  $description['mobile_switch_prevent_devices_strings'] .= ' ' . t('Get a user agent string: In the !development-settings under <em>Developer modus</em> -> <em>Advanced developer modus settings</em> use the <em>Display user agent</em> option.', array(
    '!development-settings' => l(t('Development settings'), 'admin/settings/mobile-switch/development'),
  ));
  $form['preventing']['mobile_switch_prevent_devices_strings'] = array(
    '#type' => 'textarea',
    '#title' => t('Mobile devices identification for preventing'),
    '#description' => $description['mobile_switch_prevent_devices_strings'],
    '#default_value' => variable_get('mobile_switch_prevent_devices_strings', ''),
    '#required' => FALSE,
    '#element_validate' => array(
      '_mobile_switch_prevent_devices_strings_validate',
    ),
  );
  $form['#submit'][] = 'mobile_switch_settings_form_submit';
  return system_settings_form($form);
}

/**
 * Form constructor for the Development settings form.
 *
 * @ingroup forms
 */
function mobile_switch_development_settings_form() {
  if (variable_get('browscap_version', 0) == 0) {
    return mobile_switch_browscap_message();
  }
  if (variable_get('mobile_switch_mobile_theme', 'none') === 'none') {
    return mobile_switch_theme_message();
  }
  $module_path = drupal_get_path('module', 'mobile_switch');
  drupal_add_js($module_path . '/js/mobile_switch.admin.js', 'module', 'footer');
  $form['development'] = array(
    '#type' => 'fieldset',
    '#title' => t('Development'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => -5,
    '#prefix' => '<div class="development-settings">' . t('Mobile Switch can help to develop a mobile site.') . '</div>',
  );
  $form['development']['mobile_switch_developer'] = array(
    '#type' => 'select',
    '#title' => t('Developer modus'),
    '#description' => t('Extends the browser detection for desktop emulators such as <em>Opera mobile</em>, <em>Fennec</em> or more other. Don\'t use it on production sites.'),
    '#options' => array(
      FALSE => t('No'),
      TRUE => t('Yes'),
    ),
    '#default_value' => variable_get('mobile_switch_developer', 0),
  );
  $advanced_collapsed = FALSE;
  if (variable_get('mobile_switch_display_useragent', 0) || variable_get('mobile_switch_display_browscapinfo', 0)) {
    $advanced_collapsed = TRUE;
  }
  $form['development']['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced developer modus settings'),
    '#collapsible' => TRUE,
    '#collapsed' => $advanced_collapsed ? FALSE : TRUE,
    '#prefix' => '<div id="edit-mobile-switch-advanced-developer">',
    '#suffix' => '</div>',
  );
  $form['development']['advanced']['mobile_switch_emulator_strings'] = array(
    '#type' => 'textarea',
    '#title' => t('Mobile emulator identification'),
    '#description' => t("Configure user agent string parts. Use letters, single white spaces or underscores - no other characters! Do not use the '*' wildcard character! Enter one string per line. As example the default string parts: <pre>Fennec\nAndroid\nTablet\nMobi</pre> The string detection is case insensitive."),
    '#default_value' => variable_get('mobile_switch_emulator_strings', "Fennec\nAndroid\nTablet\nMobi"),
    '#required' => TRUE,
    '#element_validate' => array(
      '_mobile_switch_emulator_strings_validate',
    ),
  );
  $form['development']['advanced']['mobile_switch_display_useragent'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display user agent'),
    '#description' => t('Use this function to display a message that contains a user agent string. Do not forget to turn off this option. For the display is required: the permissions %administer-site-configuration or %administer-themes.', array(
      '%administer-site-configuration' => t('Administer site configuration'),
      '%administer-themes' => t('Administer themes'),
    )),
    '#default_value' => variable_get('mobile_switch_display_useragent', 0),
  );
  $form['development']['advanced']['mobile_switch_display_browscapinfo'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display browscap informations'),
    '#description' => t('Use this function to display detailed browscap informations. Do not forget to turn off this option. For the display is required: the permissions %administer-site-configuration or %administer-themes.', array(
      '%administer-site-configuration' => t('Administer site configuration'),
      '%administer-themes' => t('Administer themes'),
    )),
    '#default_value' => variable_get('mobile_switch_display_browscapinfo', 0),
  );
  $form['development']['mobile_switch_deskbrowser'] = array(
    '#type' => 'select',
    '#title' => t('Desktop browser'),
    '#description' => t('Use the %mobile-theme with desktop browsers. Do not forget to turn off this option on production sites.', array(
      '%mobile-theme' => t('Mobile theme'),
    )),
    '#options' => array(
      FALSE => t('No'),
      TRUE => t('Yes'),
    ),
    '#default_value' => variable_get('mobile_switch_deskbrowser', 0),
  );
  $form['#submit'][] = 'mobile_switch_settings_form_submit';
  return system_settings_form($form);
}

/**
 * Helper function to display a message.
 *
 * The message are displayed on the Advanced and Development form if no
 * mobile theme enabled.
 *
 * @see mobile_switch_advanced_settings_form()
 * @see mobile_switch_development_settings_form()
 */
function mobile_switch_theme_message() {
  $message = t('Please configure the !basic-settings and enable a mobile theme.', array(
    '!basic-settings' => l(t('Basic settings'), 'admin/settings/mobile-switch'),
  ));
  $enable['mobile_switch_theme_message'] = array(
    '#value' => $message,
  );
  return $enable;
}

/**
 * Helper function to display a message.
 *
 * The message are displayed on the administration pages if browscap not
 * configured.
 *
 * @see mobile_switch_settings_form()
 * @see mobile_switch_advanced_settings_form()
 * @see mobile_switch_development_settings_form()
 */
function mobile_switch_browscap_message() {
  $message = t('Please administer the !administer-browscap module. It have not yet been fetched browser data.', array(
    '!administer-browscap' => l('Browscap', 'admin/settings/browscap'),
  ));
  $configure['mobile_switch_browscap_message'] = array(
    '#value' => $message,
  );
  return $configure;
}

/**
 * Form submission handler for multiple mobile switch setting forms.
 *
 * @see mobile_switch_settings_form()
 * @see mobile_switch_advanced_settings_form()
 * @see mobile_switch_development_settings_form()
 */
function mobile_switch_settings_form_submit($form, &$form_state) {

  // It exist problems if used a "mobile jQuery" based theme as mobile theme.
  // This problems cannot be solved here.
  drupal_flush_all_caches();
  drupal_set_message(t('Caches cleared.'), 'status');
}

/**
 * Render API callback: Validates the user agent string parts.
 *
 * Ensures that only letters and no blank lines has been entered.
 *
 * This function is assigned as an #element_validate callback in
 * mobile_switch_settings_form().
 */
function _mobile_switch_prevent_devices_strings_validate($element, &$form_state) {
  if (preg_match("/[^a-z][^\na-z]|[a-z][^\na-z]\$|.{0}\n\$/i", $element['#value'])) {
    form_error($element, t('%title: Use only the described characters and no blank lines.', array(
      '%title' => t($element['#title']),
    )));
  }
}

/**
 * Render API callback: Validates the user agent string parts.
 *
 * Ensures that only letters and no blank lines has been entered.
 *
 * This function is assigned as an #element_validate callback in
 * mobile_switch_settings_form().
 */
function _mobile_switch_emulator_strings_validate($element, &$form_state) {
  if (preg_match("/[^a-z][^\na-z]|[a-z][^\na-z]\$|.{0}\n\$/i", $element['#value'])) {
    form_error($element, t('%title: Use only the described characters and no blank lines.', array(
      '%title' => t($element['#title']),
    )));
  }
}

Functions

Namesort descending Description
mobile_switch_advanced_settings_form Form constructor for the Advanced settings form.
mobile_switch_browscap_message Helper function to display a message.
mobile_switch_development_settings_form Form constructor for the Development settings form.
mobile_switch_settings_form Form constructor for the Basic settings form.
mobile_switch_settings_form_submit Form submission handler for multiple mobile switch setting forms.
mobile_switch_theme_message Helper function to display a message.
_mobile_switch_emulator_strings_validate Render API callback: Validates the user agent string parts.
_mobile_switch_prevent_devices_strings_validate Render API callback: Validates the user agent string parts.