You are here

function mobile_tools_device_group_form in Mobile Tools 7.3

Form to add a new device group

2 string references to 'mobile_tools_device_group_form'
MobileToolsUserRolesTestCase::testAdminUserAccess in ./mobile_tools.test
Tests that the admin user can access the Mobile Tools pages.
mobile_tools_menu in ./mobile_tools.module
Implements hook_menu().

File

./mobile_tools.admin.inc, line 133
Adminstrative pages for Mobile Tools

Code

function mobile_tools_device_group_form($form, &$form_state, $device_group = NULL) {

  // If a device group is specified, load it
  if (is_object($device_group)) {

    // Do nothing
  }
  else {
    $device_group = _mobile_tools_empty_device_group();
  }

  // Load the list of theme options
  $themes = list_themes();
  $theme_options = array(
    MOBILE_TOOLS_FLAG_DEFAULT_SITE_THEME => t('Default site theme'),
  );
  foreach ($themes as $theme) {

    // Ensure the theme is enabled
    if ($theme->status == TRUE) {
      $theme_options[$theme->name] = $theme->name;
    }
  }
  $form['title'] = array(
    '#title' => t('Device Group Name'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#description' => t('Human-readable name for this device group'),
    '#default_value' => isset($form_state['values']['title']) ? $form_state['values']['title'] : $device_group->title,
  );
  $form['dgid'] = array(
    '#title' => t('Machine Name'),
    '#type' => 'machine_name',
    '#required' => TRUE,
    '#maxlength' => 32,
    '#description' => t('Machine-readable name for this device group'),
    '#disabled' => empty($device_group->dgid) ? FALSE : TRUE,
    '#machine_name' => array(
      'exists' => 'mobile_tools_device_group_exists',
      'source' => array(
        'title',
      ),
    ),
    '#default_value' => isset($form_state['values']['dgid']) ? $form_state['values']['dgid'] : $device_group->dgid,
  );
  $form['purl_modifier'] = array(
    '#type' => 'textfield',
    '#title' => t('URL Modifier'),
    '#description' => t('Enter the modifer to be used in your URL (ex: mobile)'),
    '#required' => TRUE,
    '#default_value' => isset($form_state['values']['purl_modifier']) ? $form_state['values']['purl_modifier'] : $device_group->purl_modifier,
  );

  // Check if themekey is available
  if (module_exists('themekey')) {
    $form['theme'] = array(
      '#type' => 'select',
      '#title' => t('Theme'),
      '#description' => t('Select a theme to activate for this device group'),
      '#options' => $theme_options,
      '#default_value' => isset($form_state['values']['title']) ? $form_state['values']['theme'] : $device_group->theme,
    );
  }
  else {
    $form['theme_notice'] = array(
      '#title' => t('Theme'),
      '#markup' => '<p>' . t('To enable theme switching you must first enable the !themekey module.', array(
        '!themekey' => l(t('ThemeKey'), 'http://drupal.org/project/themekey'),
      )) . '</p>',
    );
  }
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#description' => t('Enter the device group description here'),
    '#default_value' => isset($form_state['values']['description']) ? $form_state['values']['description'] : $device_group->description,
  );
  $options = array();
  for ($x = -50; $x < 51; $x++) {
    $options[$x] = $x;
  }
  $detectors = _mobile_tools_get_device_detectors();
  if (!empty($detectors)) {
    $detection_options['none'] = t('Disabled');

    // Set the detection options
    foreach ($detectors as $module => $detector) {
      $detection_options[$module] = $detector['name'];
    }

    // Set the default value for detection
    // Check for a previous value in the form_state
    if (isset($form_state['values']['detector'])) {
      $detection_default = $form_state['values']['detector'];
    }
    elseif (isset($device_group->detector)) {
      $detection_default = $device_group->detector;
    }
    else {
      $detection_default = 'none';
    }
    $form['detector'] = array(
      '#type' => 'select',
      '#title' => t('Device detection'),
      '#description' => t('Enable device detection'),
      '#options' => $detection_options,
      '#default_value' => $detection_default,
    );
    foreach ($detectors as $detector => $detector_form) {
      if (isset($detector_form['form callback'])) {
        $form[$detector]['#type'] = 'fieldset';
        $form[$detector]['#title'] = $detectors[$detector]['name'];
        $form[$detector]['#states'] = array(
          'visible' => array(
            ':input[value="' . $detector . '"]' => array(
              'checked' => TRUE,
            ),
          ),
        );
        $detector_form_settings = array();
        if (isset($device_group->detection_settings[$detector])) {
          $detector_form_settings = $device_group->detection_settings[$detector];
        }
        $form_addition = call_user_func($detector_form['form callback'], $detector_form_settings);

        // Remember the additional field names so we can use them later.
        $form[$detector]['#fields'] = array_keys($form_addition);
        $form[$detector]['#activation callback'] = isset($detector_form['activation callback']) ? $detector_form['activation callback'] : NULL;
        $form[$detector] += $form_addition;
      }
    }
  }
  else {
    $form['detection_notice'] = array(
      '#title' => t('Device detection'),
      '#markup' => '<p>' . t('To enable device dection you must first enable a module that supports Mobile Tools device detection, such as !browscap and Mobile Tools Browscap modules.', array(
        '!browscap' => l(t('Browscap'), 'http://drupal.org/project/browscap'),
      )) . '</p>',
    );
  }
  $form['weight'] = array(
    '#type' => 'select',
    '#title' => t('Weight'),
    '#description' => t('Adjust the weight of the device group. Lower weights give higher priority to the device group.'),
    '#options' => $options,
    '#default_value' => isset($form_state['values']['weight']) ? $form_state['values']['weight'] : $device_group->weight,
  );

  // If a device group was loaded and it has an httpheaders field, parse it
  // into a string for the form's textarea.
  $headers = "";
  if (is_object($device_group) && isset($device_group->httpheaders) && !empty($device_group->httpheaders)) {
    foreach ($device_group->httpheaders as $header => $value) {
      $headers .= $header . ":" . $value . "\n";
    }
  }
  $form['httpheaders'] = array(
    '#type' => 'textarea',
    '#title' => t('HTTP Headers'),
    '#description' => t('A set of HTTP headers to include when this device group is active.'),
    '#cols' => 60,
    '#rows' => 5,
    '#default_value' => isset($form_state['values']['httpheaders']) ? $form_state['values']['httpheaders'] : $headers,
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save device group'),
  );
  $form['actions']['cancel'] = array(
    '#markup' => l(t('Cancel'), 'admin/config/system/mobile-tools'),
  );
  return $form;
}