You are here

function mobile_tools_device_group_form_submit in Mobile Tools 7.3

Handles form submission for a new device group

File

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

Code

function mobile_tools_device_group_form_submit($form, &$form_state) {

  // Try to load an existing device group.
  if (!($device_group = ctools_export_crud_load('mobile_tools_device_group', $form_state['values']['dgid']))) {

    // If none was found, create a new one.
    $device_group = ctools_export_crud_new('mobile_tools_device_group');
  }

  // Reset these every time.
  $device_group->detection_settings = array();
  $device_group->dgid = $form_state['values']['dgid'];
  $device_group->title = $form_state['values']['title'];
  $device_group->purl_modifier = $form_state['values']['purl_modifier'];
  $device_group->description = $form_state['values']['description'];
  $device_group->weight = intval($form_state['values']['weight']);

  // Only save a value if a detection method was selected
  if (isset($form_state['values']['detector'])) {
    if ($form_state['values']['detector'] != 'none') {
      $device_group->detector = $form_state['values']['detector'];

      // Grab the detection methods' settings fields and add them to our serialized
      // field for db storage.
      $device_group->detection_settings[$device_group->detector]['activation callback'] = $form[$device_group->detector]['#activation callback'];
      if (isset($form[$device_group->detector]['#fields'])) {
        foreach ($form[$device_group->detector]['#fields'] as $field) {
          $device_group->detection_settings[$device_group->detector][$field] = $form_state['values'][$field];
        }
      }
    }
    else {

      // Clear out the detection settings
      $device_group->detector = '';
      $device_group->detection_settings = '';
    }
  }

  // Parse the httpheaders field into an array to be stored
  $headers = array();
  foreach (explode("\r\n", $form_state['values']['httpheaders']) as $header) {
    if (!empty($header)) {

      // Split the line only by non-escaped colon
      $split_header = preg_split('/(?<!\\\\):/', $header);
      if (isset($split_header[1])) {
        $headers[$split_header[0]] = $split_header[1];
      }
      else {
        form_set_error($form_state['values']['httpheaders'], t('Please enter a valid HTTP Headers'));
      }
    }
  }
  $device_group->httpheaders = $headers;

  // Set the default value for the theme if ThemeKey isn't present
  $theme = MOBILE_TOOLS_FLAG_DEFAULT_SITE_THEME;
  if (!empty($form_state['values']['theme']) && $form_state['values']['theme'] != MOBILE_TOOLS_FLAG_DEFAULT_SITE_THEME) {
    $theme = $form_state['values']['theme'];
  }
  $device_group->theme = $theme;
  mobile_tools_device_group_save($device_group);
  drupal_set_message(t("Device group %title has been saved.", array(
    '%title' => check_plain($form_state['values']['title']),
  )));
  $form_state['redirect'] = 'admin/config/system/mobile-tools';
}