You are here

public function AccessSchemeEntityController::save in Access Control Kit 7

Saves an access scheme to the database.

Parameters

object $scheme: A scheme object with the following properties:

  • sid: (optional) The unique ID for the scheme being saved. If $scheme->sid is empty or omitted, a new scheme will be inserted.
  • machine_name: The unique, machine-readable name of the scheme.
  • name: The human-readable name of the scheme.
  • type: The type of object or value that defines the scheme's realms.
  • description: A description of the access scheme.
  • realm_field_name: (optional) The name of the realm field.
  • settings: (optional) An array of additional scheme settings.
  • original: (optional) The original scheme object before any changes were applied. When omitted, the unchanged scheme object is loaded from the database and stored in the property.
  • handlers: (optional) An array of attached handler objects. If set, the scheme's handlers will be updated to match; setting array() will detach all handlers. If not set, the scheme's handlers are left unchanged.

Return value

int Status constant indicating whether the scheme was inserted (SAVED_NEW) or updated (SAVED_UPDATED). When inserting a new scheme, $scheme->sid will contain the ID of the newly created scheme.

File

./access_scheme_entity_controller.inc, line 111
Contains the access scheme entity controller.

Class

AccessSchemeEntityController
Provides the entity controller for access schemes.

Code

public function save($scheme) {
  $transaction = db_transaction();
  try {

    // Prevent leading and trailing spaces in scheme names.
    if (!empty($scheme->name)) {
      $scheme->name = trim($scheme->name);
    }

    // Make sure the settings property is valid.
    if (!isset($scheme->settings) || !is_array($scheme->settings)) {
      $scheme->settings = array();
    }

    // Load the stored scheme, if any.
    if (!empty($scheme->sid) && !isset($scheme->original)) {
      $scheme->original = entity_load_unchanged('access_scheme', $scheme->sid);
    }

    // The machine name and type are not editable after creation.
    if (!empty($scheme->original)) {
      $scheme->machine_name = $scheme->original->machine_name;
      $scheme->type = $scheme->original->type;
    }
    module_invoke_all('access_scheme_presave', $scheme);
    module_invoke_all('entity_presave', $scheme, 'access_scheme');
    if (empty($scheme->sid)) {
      $op = 'insert';
      $status = drupal_write_record('access_scheme', $scheme);
      access_scheme_static_reset();
      field_attach_create_bundle('access_grant', $scheme->machine_name);
    }
    else {
      $op = 'update';
      $status = drupal_write_record('access_scheme', $scheme, 'sid');
      access_scheme_static_reset(array(
        $scheme->sid,
      ));
    }

    // Update attached handlers.
    if (isset($scheme->handlers)) {

      // Delete any handlers that were detached from the scheme.
      if (!empty($scheme->original)) {
        foreach (array_keys($scheme->original->handlers) as $object_type) {
          if (empty($scheme->handlers[$object_type])) {
            db_delete('access_handler')
              ->condition('scheme', $scheme->machine_name)
              ->condition('object_type', $object_type)
              ->execute();
          }
        }
      }

      // Save current handlers.
      $handler_info = access_handler_info();
      foreach ($scheme->handlers as $object_type => $handler) {
        if (!empty($handler)) {
          $key = array(
            'scheme' => $scheme->machine_name,
            'object_type' => $object_type,
          );
          $class = get_class($handler);
          $fields = array(
            'handler' => $class,
            'module' => $handler_info[$class]['module'],
            'settings' => serialize($handler
              ->getSettings()),
          );
          db_merge('access_handler')
            ->key($key)
            ->fields($fields)
            ->execute();
        }
      }
    }
    module_invoke_all('access_scheme_' . $op, $scheme);
    module_invoke_all('entity_' . $op, $scheme, 'access_scheme');
    unset($scheme->original);
    cache_clear_all();
    return $status;
  } catch (Exception $e) {
    $transaction
      ->rollback();
    watchdog_exception('access', $e);
    throw $e;
  }
}