You are here

function _scald_register_provider in Scald: Media Management made easy 6

Register a Scald Provider with Scald Core.

NOTE: This function does *not* automatically update the list of registered Scald Providers maintained by Scald Core.

Parameters

$provider: The name of the Scald Provider being registered. Should be the name of a Drupal module (though technically, a prefix for which all the appropriate Scald hooks were implemented would probably work).

$definitions: A structured array with data specifying what the Scald Provider is providing. Expected to match spec for the return value of hook_scald_provider().

$additional: A boolean indicating whether $definitions should be evaluated as a full Provider specification or as a re-run looking for additional things to register. If FALSE, duplicate registrations result in function failure. If TRUE, duplicate registration attempts are ignored. Note that even when $additional is TRUE, the function can fail if db queries fail or a bad slug is provided. Setting $additional to TRUE only removes the failures which are due to duplication. Duplicate registrations won't be inserted into the DB, either, they just won't cause an error.

Return value

FALSE if one of the registrations of a Scald Provider failed. TRUE if all of the registrations succeeded.

3 calls to _scald_register_provider()
scald_admin_provider_reregister in ./scald.admin.inc
Forces a Scald Provider to re-register with Scald Core, potentially previously-unknown Provisions.
scald_image_update_6001 in scald_image/scald_image.install
Implements hook_update_N(). Converts our imagecache based transcoders to use the presetname instead of the presetid as a key. This is more robust to change such as putting presets in code.
scald_update_providers in ./scald.module
Determine if Scald Providers have been enabled or disabled and update the Scald Registries as necessary.

File

./scald.module, line 58

Code

function _scald_register_provider($provider = '', $definitions = array(), $additional = FALSE) {

  // Argument validation
  if (empty($provider) || empty($definitions) || !is_array($definitions)) {
    return FALSE;
  }
  $scald_config = variable_get('scald_config', 0);
  foreach ($definitions as $provider_type => $provisions) {
    foreach ($provisions as $slug => $details) {

      // Check for slug validity
      if (strlen($slug) > SCALD_SLUG_MAX_LENGTH || !preg_match(SCALD_SLUG_MATCH_PATTERN, $slug)) {
        return FALSE;
      }
      switch ($provider_type) {
        case 'types':

          // Only register the Scald Unified Type if it isn't already in the
          //  Scald Unified Types Registry
          if (empty($scald_config->types[$slug])) {
            $success = db_query("\n                INSERT INTO\n                  {scald_types}\n                SET\n                  type        = '%s',\n                  provider    = '%s',\n                  title       = '%s',\n                  description = '%s'\n              ", $slug, $provider, $details['title'], $details['description']);
            if (!$success) {
              return FALSE;
            }

            // Set the per-Type defaults for Atom objects
            $scald_atom_defaults = variable_get('scald_atom_defaults', 0);
            $scald_atom_defaults->thumbnail_source[$slug] = $details['thumbnail_source'];
            $scald_atom_defaults->description[$slug] = $details['description_default'];
            variable_set('scald_atom_defaults', $scald_atom_defaults);
          }
          else {
            if (!$additional) {
              return FALSE;
            }
          }
          break;

        // end 'types'
        case 'atoms':

          // Fail if the Scald Unified Type is not registered
          if (empty($scald_config->types[$slug])) {
            return FALSE;
          }
          foreach ($details as $base_description) {

            // The Primary Key constraint on {scald_atom_providers} will prevent
            //  the duplicate registration of the exact same facet of an Atom
            //  Provider twice.
            $success = db_query("\n                INSERT" . ($additional ? " IGNORE " : " ") . "INTO\n                  {scald_atom_providers}\n                SET\n                  type             = '%s',\n                  provider         = '%s',\n                  base_description = '%s'\n              ", $slug, $provider, $base_description);
            if (!$success) {

              // @@@TODO: Make this handle both query failures and key constraint violations (the former should actually return FALSE regardless, the latter on if !$additional).
              if (!$additional) {
                return FALSE;
              }
            }
          }
          break;

        // end 'atoms'
        case 'contexts':

          // Skip registration if this Scald Context is already registered and
          //  fail if it is not registered to this Scald Provider.
          if (!empty($scald_config->contexts[$slug])) {
            if ($scald_config->contexts[$slug]['provider'] != $provider) {
              if (!$additional) {
                return FALSE;
              }
              else {
                continue;
              }
            }
            else {
              continue;
            }
          }
          $success = db_query("\n              INSERT INTO\n                {scald_contexts}\n              SET\n                context         = '%s',\n                provider        = '%s',\n                title           = '%s',\n                description     = '%s',\n                render_language = '%s',\n                parseable       = %d\n            ", $slug, $provider, $details['title'], $details['description'], $details['render_language'], $details['parseable']);
          if (!$success) {
            return FALSE;
          }

          // Ignore inefficiency of mutiple queries for clarity and because
          //  this registration code runs very infrequently.
          foreach ($details['formats'] as $type => $formats) {
            if (!is_array($formats)) {
              $formats = array(
                $formats,
              );
            }
            foreach ($formats as $format) {
              $success = $success && db_query("\n                  INSERT INTO\n                    {scald_context_type_formats}\n                  SET\n                    context     = '%s',\n                    type        = '%s',\n                    file_format = '%s'\n                ", $slug, $type, $format);
            }
          }
          if (!$success) {

            // @@@TODO: Handle a failure here more gracefully.  Just returning false results in a db with only *some* (or none) of the Scald Context's type/format mappings specified but no way to re-register the Scald Context Provider because re-running the registration routine will indicate that the provider is already registered.
            return FALSE;
          }
          break;

        // end 'contexts'
        case 'actions':

          // Skip registration if this Action is already registered to this
          //  Provider; fail if it's already registered to another Provider.
          if (!empty($scald_config->actions[$slug])) {
            if ($scald_config->actions[$slug]['provider'] != $provider) {
              if (!$additional) {
                return FALSE;
              }
              else {
                continue;
              }
            }
            else {
              continue;
            }
          }

          // Since the Scald Actions bitstring is limited to 31 bits and the
          //  high bit is reserved by Scald, only 30 actions can be registered.
          //  See scald.constants.inc or docs/scald_provider_api.txt for
          //  additional details.
          $max_power = db_result(db_query("SELECT MAX(power) FROM {scald_actions}"));
          if ($max_power >= SCALD_ACTIONS_MAX_POWER) {
            return FALSE;
          }
          if (!db_query("\n      \t      INSERT INTO\n\t              {scald_actions}\n      \t      SET\n\t              action      = '%s',\n      \t        provider    = '%s',\n\t              title       = '%s',\n      \t        description = '%s',\n                power        = %d\n      \t    ", $slug, $provider, $details['title'], $details['description'], (int) $max_power + 1)) {
            return FALSE;
          }
          break;

        // end 'actions'
        case 'transcoders':

          // Skip registration if this Transcoder is already registered to this
          //  Provider; fail if it's already registered to another Provider.
          if (!empty($scald_config->transcoders[$slug])) {
            if ($scald_config->transcoders[$slug]['provider'] != $provider) {
              if (!$additional) {
                return FALSE;
              }
              else {
                continue;
              }
            }
            else {
              continue;
            }
          }
          if (!db_query("\n              INSERT INTO\n                {scald_transcoders}\n              SET\n                transcoder  = '%s',\n                provider    = '%s',\n                title       = '%s',\n                description = '%s'\n            ", $slug, $provider, $details['title'], $details['description'])) {
            return FALSE;
          }
          $success = TRUE;
          foreach ($details['formats'] as $type => $format) {
            $success = $success && db_query("\n                INSERT INTO\n                  {scald_transcoder_formats}\n                SET\n                  transcoder  = '%s',\n                  type        = '%s',\n                  file_format = '%s'\n              ", $slug, $type, $format);
          }
          if (!$success) {

            // @@@TODO: Handle a failure here more gracefully.  Just returning false results in a db with only *some* (or none) of the Transcoder's type/format mappings specified but no way to re-register the Scald Transcoder Provider because re-running the registration routine will indicate that the provider is already registered.
            drupal_set_message('Failed to register some of the transcoder formats', 'error');
            return FALSE;
          }
          break;

        // end 'transcoders'
        case 'relationships':

          // Skip registration if this Relationship is already registered to
          //  this Provider; fail if it's already registered to another
          //  Provider.
          if (!empty($scald_config->relationships[$slug])) {
            if ($scald_config->relationships[$slug]['provider'] != $provider) {
              if (!$additional) {
                return FALSE;
              }
              else {
                continue;
              }
            }
            else {
              continue;
            }
          }
          if (!db_query("\n              INSERT INTO\n                {scald_relationships}\n              SET\n                relationship  = '%s',\n                provider      = '%s',\n                title         = '%s',\n                title_reverse = '%s',\n                description   = '%s'\n            ", $slug, $provider, $details['title'], $details['title_reverse'], $details['description'])) {
            return FALSE;
          }
          break;

        // end 'relationships'
        default:

          // The registration type slug was not recognized.  Fail.
          return FALSE;
          break;
      }

      // end switch $provider_type
    }

    // end foreach $definitions
  }

  // end foreach $provisions
  return TRUE;
}