You are here

function key_save_config in Key 7.2

Same name and namespace in other branches
  1. 7 key.module \key_save_config()

Save a key configuration.

Parameters

array $fields: The fields of the configuration to save.

bool $messages: TRUE if messages should be displayed.

2 calls to key_save_config()
key_config_features_rebuild in includes/key_config.features.inc
Implements hook_features_rebuild().
key_config_form_submit in includes/key.admin.inc
Form submission handler for key_config_form().

File

./key.module, line 371
Provides the ability to manage keys, which can be used by other modules.

Code

function key_save_config($fields, $messages = TRUE) {

  // Load the key provider.
  $provider = key_get_provider($fields['provider']);

  // 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;
  }

  // Return the saved configuration.
  return $key_config;
}