function key_save_config in Key 7
Same name and namespace in other branches
- 7.2 key.module \key_save_config()
Save a key configuration.
Parameters
array $fields: The fields of the configuration to save.
array $key: The key to save, if desired and allowed for the chosen key provider.
array $overwrite: FALSE if a new configuration should be created, instead of overwriting an existing one with the same name.
bool $messages: TRUE if messages should be displayed.
3 calls to key_save_config()
- key_config_features_rebuild in includes/key_config.features.inc 
- Implements hook_features_rebuild().
- key_install in ./key.install 
- Implements hook_install().
- key_ui_key_config_form_submit in modules/key_ui/ includes/ key_ui.admin.inc 
- Form submission handler for key_ui_key_config_form().
File
- ./key.module, line 377 
- Provides the ability to manage keys, which can be used by other modules.
Code
function key_save_config($fields, $key = FALSE, $overwrite = TRUE, $messages = TRUE) {
  // Load the key provider.
  $provider = key_get_provider($fields['provider']);
  // If the configuration should not be overwritten, create a new one
  // and make sure that certain fields are unique.
  if (!$overwrite) {
    $configs = key_get_configs();
    // Make sure the configuration name is unique.
    $counter = 2;
    $base_name = $fields['name'];
    while (key_get_config($fields['name'])) {
      $fields['name'] = "{$base_name}_{$counter}";
      $counter++;
    }
    // Make sure the configuration label is unique.
    $counter = 2;
    $base_label = $fields['label'];
    $config_labels = array();
    foreach ($configs as $index => $config) {
      $config_labels[] = $config['label'];
    }
    while (in_array($fields['label'], $config_labels)) {
      $fields['label'] = "{$base_label} {$counter}";
      $counter++;
    }
    // If the key provider is variable and the key is defined,
    // make sure the variable name is unique.
    if ($fields['provider'] == 'variable' && isset($key)) {
      $counter = 2;
      $base_variable_name = $fields['provider_settings']['variable_name'];
      $config_variable_names = array();
      foreach ($configs as $index => $config) {
        if ($config['provider'] == 'variable') {
          $config_variable_names[] = $config['provider_settings']['variable_name'];
        }
      }
      while (in_array($fields['provider_settings']['variable_name'], $config_variable_names)) {
        $fields['provider_settings']['variable_name'] = "{$base_variable_name}_{$counter}";
        $counter++;
      }
    }
    foreach ($configs as $index => $config) {
      // Change the configuration label to be unique.
      while (key_get_config($fields['name'])) {
        $fields['name'] = "{$base_name}_{$counter}";
        $counter++;
      }
    }
  }
  // Serialize any field that is an array.
  foreach ($fields as $index => $field) {
    if (is_array($field)) {
      $fields[$index] = serialize($field);
    }
  }
  // If the created field is empty, set it to the request time.
  if (empty($fields['created'])) {
    $fields['created'] = REQUEST_TIME;
  }
  // If the changed field is empty, set it to the request time.
  if (empty($fields['changed'])) {
    $fields['changed'] = REQUEST_TIME;
  }
  // Save the configuration.
  $merge_status = db_merge('key_config')
    ->key(array(
    'name' => $fields['name'],
  ))
    ->fields($fields)
    ->execute();
  // Display message and log to watchdog.
  if ($messages) {
    $t_args = array(
      '%label' => $fields['label'],
    );
    switch ($merge_status) {
      case MergeQuery::STATUS_INSERT:
        drupal_set_message(t('The key %label has been added.', $t_args));
        watchdog('key', 'Added key %label.', $t_args, WATCHDOG_NOTICE, l(t('view'), KEY_MENU_PATH . '/list'));
        break;
      case MergeQuery::STATUS_UPDATE:
        drupal_set_message(t('The key %label has been updated.', $t_args));
        watchdog('key', 'Updated key %label.', $t_args, WATCHDOG_NOTICE, l(t('view'), KEY_MENU_PATH . '/list'));
        break;
    }
  }
  // Load the configuration to make sure it was saved.
  $key_config = key_get_config($fields['name'], TRUE);
  if (empty($key_config)) {
    return NULL;
  }
  // Set the key if the key provider supports it and a key was defined.
  if (isset($key) && ($key_set_callback = ctools_plugin_get_function($provider, 'key set callback'))) {
    call_user_func($key_set_callback, $key_config['provider_settings'], $key);
  }
  // Return the saved configuration.
  return $key_config;
}