You are here

function scald_update_atom in Scald: Media Management made easy 6

Update a Scald Atom

Passing just a SID will trigger hook_scald_update_atom() without updating any core Atom object values.

Parameters

$atom: An Atom object which has a registered SID OR a SID

$values: An optional array keyed with member names and values. For recognized Atom members, the Atom is updated directly. The array is passed to the Providers as well.

Return value

The fully-updated Atom object. FALSE upon failure.

3 calls to scald_update_atom()
mee_field in mee/mee.module
Implementation of hook_field().
scald_composite_nodeapi in scald_composite/scald_composite.module
Implementation of hook_nodeapi().
scald_example_content_type_node_update in scald_example_content_type/scald_example_content_type.module
Implements hook_node_update.

File

./scald.module, line 1061

Code

function scald_update_atom($atom, $values = NULL) {

  // Argument validation
  if (is_object($atom)) {
    $sid = $atom->sid;
  }
  else {
    $sid = $atom;
  }
  if ($my_atom = scald_is_registered($sid) === FALSE) {
    return FALSE;
  }
  if (!is_object($atom)) {
    $atom = $my_atom;
  }

  // If using the $values array
  if (is_array($values) && !empty($values)) {
    foreach ($values as $key => $value) {
      switch ($key) {

        // Recognized Atom object memebers are updated and removed from $values
        case 'provider':
        case 'type':
        case 'base_id':
        case 'publisher':
        case 'actions':
        case 'title':
        case 'authors':
        case 'relationships':
          $atom->{$key} = $value;
          unset($values[$key]);
          break;

        // Recognized Atom object members which cannot reasonably be updated
        //  either by Scald Core or by one of the Providers are simply removed.
        case 'sid':
        case 'fetched':
          unset($values[$key]);
          break;
        default:
          break;
      }
    }
  }

  // Handle a common error in the $values array.
  if (is_array($atom->relationships) && !isset($atom->relationships['forward'])) {
    $atom->relationships = array(
      'forward' => $atom->relationships,
      'reverse' => array(),
    );
  }
  $scald_config = variable_get('scald_config', 0);

  // Run the Provider Hooks
  scald_invoke($scald_config->types[$atom->type]['provider'], 'scald_update_atom', $atom, $values, 'type');
  scald_invoke($atom->provider, 'scald_update_atom', $atom, $values, 'atom');
  foreach ($scald_config->relationships as $relationship) {
    scald_invoke($relationship['provider'], 'scald_update_atom', $atom, $values, 'relationship');
  }
  foreach ($scald_config->contexts as $context => $details) {
    if (!empty($details['type_format'][$atom->type]['transcoder'])) {
      $transcoder = $details['type_format'][$atom->type]['transcoder'];
      scald_invoke($scald_config->transcoders[$transcoder]['provider'], 'scald_update_atom', $atom, $values, 'transcoder');
    }
  }

  // @@@TODO: Enforce defaults/valid values (as in scald_register_atom())
  // Update the basic data in the Scald Atom Registry
  if (!db_query("\n        UPDATE\n          {scald_atoms}\n        SET\n          provider = '%s',\n          type = '%s',\n          base_id = '%s',\n          publisher = %d,\n      \t  actions = %d,\n          title = '%s'\n        WHERE\n          sid = %d\n      ", $atom->provider, $atom->type, $atom->base_id, $atom->publisher, $atom->actions, $atom->title, $atom->sid)) {
    return FALSE;
  }

  // @@@TODO: Figure out a good way to *not* brute-force this
  db_query("DELETE FROM {scald_atom_authors} WHERE sid = %d", $atom->sid);
  foreach ($atom->authors as $weight => $author_id) {
    db_query("\n        INSERT INTO\n          {scald_atom_authors}\n        SET\n          sid = %d,\n          aid = %d,\n          weight = %d\n      ", $atom->sid, $author_id, $weight);
  }

  // @@@TODO: Figure out a good way to *not* brute-force this
  db_query("DELETE FROM {scald_atom_relationships} WHERE sid_left = %d", $atom->sid);

  // @@@TODO: Figure out how to invalidate any 'reverse' Relationships *and* re-calculate them effectively.
  foreach ($atom->relationships['forward'] as $relationship => $atoms) {
    foreach ($atoms as $sid_right) {
      db_query("\n          INSERT INTO\n            {scald_atom_relationships}\n          SET\n            sid_left = %d,\n            relationship = '%s',\n            sid_right = %d\n        ", $atom->sid, $relationship, $sid_right);
    }
  }

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

  // Clear the memory caches
  return scald_fetch($atom->sid, TRUE);
}