You are here

function scald_register_atom in Scald: Media Management made easy 6

Register a new Scald Atom with Scald Core.

Parameters

$values: An associative array with keys which correspond to Scald Atom Object members 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

The Scald ID upon successful registration FALSE upon failure

5 calls to scald_register_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_dailymotion_register in scald_dailymotion/scald_dailymotion.module
Creates an atom based on a DailyMotion video id or an object containing the video informations..
scald_example_content_type_node_insert in scald_example_content_type/scald_example_content_type.module
Implements hook_node_insert.
scald_example_content_type_node_update in scald_example_content_type/scald_example_content_type.module
Implements hook_node_update.

File

./scald.module, line 843

Code

function scald_register_atom($values) {

  // Argument validation
  if (!is_array($values) || empty($values)) {
    return FALSE;
  }

  // Begin building the Atom object
  $atom = new stdClass();
  foreach ($values as $key => $value) {
    switch ($key) {

      // Recognized Atom object memebers are assigned 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 assigned
      //  either by Scald Core or by one of the Providers are simply removed.
      case 'sid':
      case 'fetched':
        unset($values[$key]);
        break;
      default:
        break;
    }
  }

  // First pass Atom object validation
  $scald_config = variable_get('scald_config', 0);

  // Verify type & provider
  if (empty($scald_config->types[$atom->type]) || !in_array($atom->provider, $scald_config->types[$atom->type]['atom_providers'])) {
    return FALSE;
  }

  // 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->authors)) {
    $atom->authors = array();
  }
  if (!isset($atom->relationships)) {
    $atom->relationships = array(
      'forward' => array(),
      'reverse' => array(),
    );
  }

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

  // Hand the new 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.
  scald_invoke($atom->provider, 'scald_register_atom', $atom, $values, '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)) {
    return FALSE;
  }
  if (empty($scald_config->types[$atom->type]) || !in_array($atom->provider, $scald_config->types[$atom->type]['atom_providers'])) {
    return FALSE;
  }

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

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

  // Put the basic data in the Scald Atom Registry
  if (!db_query("\n        INSERT INTO\n          {scald_atoms}\n        SET\n          provider = '%s',\n          type = '%s',\n          base_id = '%s',\n          publisher = %d,\n          actions = %d,\n          title = '%s'\n      ", $atom->provider, $atom->type, $atom->base_id, $atom->publisher, $atom->actions, $atom->title)) {
    return FALSE;
  }
  $atom->sid = db_last_insert_id('scald_atoms', 'sid');

  // 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(),
    );
  }

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

  // Relationships
  foreach ($scald_config->relationships as $relationship) {
    scald_invoke($relationship['provider'], 'scald_register_atom', $atom, $values, 'relationship');
  }

  // Only worry about the "forward" Relationships.  "reverse" Relationships are
  //  populated in scald_fetch() and are updated when the other Atom is updated
  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);
    }
  }

  // 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).
  foreach ($scald_config->contexts as $context => $details) {
    if (isset($details['type_format'][$atom->type])) {
      $transcoder = $details['type_format'][$atom->type]['transcoder'];
      $values['@ccontext'] = $context;
      scald_invoke($scald_config->transcoders[$transcoder]['provider'], 'scald_register_atom', $atom, $values, 'transcoder');
    }
  }
  return $atom->sid;
}