You are here

public static function ScaldAtomController::save in Scald: Media Management made easy 7

Save changes to a Scald Atom, or create a new one.

Parameters

ScaldAtom $atom: At minimum, 'type', 'provider', and 'base_id' (which uniquely identifies a given Atom) are required. Additional included values which are keyed by recognized Scald Atom Object members will be used for those members and any additional values will be passed along to the Providers.

Return value

mixed The saved atom upon successful save. NULL upon failure.

2 calls to ScaldAtomController::save()
ScaldAtom::save in includes/ScaldAtom.inc
scald_atom_save in ./scald.module
Save changes to a Scald Atom, or create a new one.

File

includes/ScaldAtomController.inc, line 208
This file contains the Scald Atom controller.

Class

ScaldAtomController
Controller class for Scald Atoms.

Code

public static function save($atom) {

  // First pass Atom object validation.
  $types = scald_types();

  // Verify type.
  if (empty($atom->type) || empty($atom->provider) || empty($types[$atom->type])) {
    return NULL;
  }

  // Ensure the Atom Object has all the required members.
  if (!isset($atom->publisher)) {
    $atom->publisher = NULL;
  }
  if (!isset($atom->actions)) {
    $atom->actions = NULL;
  }
  if (!isset($atom->title)) {
    $atom->title = '';
  }
  if (!isset($atom->data)) {
    $atom->data = array();
  }
  if (!isset($atom->created)) {
    $atom->created = REQUEST_TIME;
  }
  if (!isset($atom->changed)) {
    $atom->changed = REQUEST_TIME;
  }
  $op = empty($atom->sid) ? 'insert' : 'update';
  if ($op == 'update') {
    $hook = 'scald_update_atom';
    $atom->original = entity_load_unchanged('scald_atom', $atom->sid);

    // Nobody updated the changed date, so we do it.
    if ($atom->original->changed === $atom->changed) {
      $atom->changed = REQUEST_TIME;
    }
  }
  else {
    $hook = 'scald_register_atom';
  }

  // The Type Provider can implement some other defaults at this point, but
  // the Atom Provider may override them.
  module_invoke($types[$atom->type]->provider, $hook, $atom, 'type');

  // Hand the Atom off to the Atom Provider to do additional processing and
  // population.
  // NOTE: Providers explicitly have access to change the Atom's basic members
  // to allow for hypothetical "dispatch Providers" which would determine the
  // appropriate Provider and/or characteristics of an Atom upon registration.
  module_invoke($atom->provider, $hook, $atom, 'atom');

  // Another round of member validation is necessary due to the potential for
  // the Providers to modify them.  By design!
  if (empty($atom->type) || empty($atom->provider) || empty($types[$atom->type])) {
    return NULL;
  }

  // Only supply defaults for the Actions bitstring if the Provider did
  // nothing. Otherwise assume that the bitstring is intentional.
  if (is_null($atom->actions)) {
    $defaults = scald_atom_defaults($atom->type);
    $atom->actions = $defaults->actions;
  }

  // Do "poor-man's" UID validation.
  if (empty($atom->publisher) || !is_numeric($atom->publisher) || !($atom->publisher > 0)) {
    global $user;
    $atom->publisher = $user->uid;
  }

  // Let Field API have a pass at our atom too.
  field_attach_presave('scald_atom', $atom);

  // Inform all modules before writing the atom in the database.
  module_invoke_all('scald_atom_presave', $atom);
  module_invoke_all('entity_presave', $atom, 'scald_atom');

  // Put the basic data in the Scald Atom Registry.
  if ($op == 'update') {
    $written = drupal_write_record('scald_atoms', $atom, array(
      'sid',
    ));
  }
  else {
    $written = drupal_write_record('scald_atoms', $atom);
  }
  if (!$written) {
    return NULL;
  }
  $function = 'field_attach_' . $op;
  $function('scald_atom', $atom);

  // Notify all modules of our new atom.
  module_invoke_all('scald_atom_' . $op, $atom);
  module_invoke_all('entity_' . $op, $atom, 'scald_atom');

  // Transcoding.
  // Only fire hook_register_atom() for Transcoder Providers that might be
  // responsible for transcoding this Atom (based on the currently-configured
  // Context and Transcoder settings).
  $contexts = scald_contexts();
  $transcoders = scald_transcoders();
  foreach ($contexts as $context => $details) {
    if (isset($details['type_format'][$atom->type])) {
      $transcoder = $details['type_format'][$atom->type]['transcoder'];
      $values['@ccontext'] = $context;
      module_invoke($transcoders[$transcoder]['provider'], $hook, $atom, 'transcoder');
    }
  }

  // Clear the render cache.
  cache_clear_all($atom->sid . ':', 'cache_scald', TRUE);

  // Clear the static caches.
  entity_get_controller('scald_atom')
    ->resetCache(array(
    $atom->sid,
  ));
  return $atom;
}