You are here

asset_emfield.module in Asset 6

Same filename and directory in other branches
  1. 5.2 contrib/asset_emfield/asset_emfield.module

Asset Embedded Media Module

Integration with emfield for using 3rd-party embedded media as assets.

File

contrib/asset_emfield/asset_emfield.module
View source
<?php

/**
 * @file 
 * Asset Embedded Media Module
 *
 * Integration with emfield for using 3rd-party embedded media as assets.
 */

/**
 * Implementation of hook_asset_type().
 */
function asset_emfield_asset_type($op = 'info', &$asset = NULL) {
  switch ($op) {
    case 'info':
      return array(
        'emfield_video' => array(
          'name' => t('Embedded Video'),
          'module' => 'asset_emfield',
          'description' => t('Create a new video asset using embed code from a 3rd-party site.'),
        ),
      );
      break;
    case 'form':
      return asset_emfield_invoke('form', $asset);
    case 'validate':
      asset_emfield_invoke('validate', $asset);
      break;
    case 'insert':
      asset_emfield_invoke('submit', $asset);
      $field = $asset->emfield[0];
      db_query('INSERT INTO {asset_emfield} (aid, embed, value, provider, data) VALUES (%d, "%s", "%s", "%s", "%s")', $asset->aid, $field['embed'], $field['value'], $field['provider'], serialize($field['data']));
      break;
    case 'update':
      asset_emfield_invoke('submit', $asset);
      $field = $asset->emfield[0];
      db_query('UPDATE {asset_emfield} SET embed="%s", value="%s", provider="%s", data="%s" WHERE aid=%d', $field['embed'], $field['value'], $field['provider'], serialize($field['data']), $asset->aid);
      break;
    case 'load':
      $result = db_query('SELECT * FROM {asset_emfield} WHERE aid=%d', $asset->aid);
      while ($row = db_fetch_array($result)) {
        $asset->emfield[0] = $row;
      }
      $return = asset_emfield_invoke('load', $asset);
      return array(
        'emfield' => $return['emfield'],
      );
    case 'view':
      switch ($asset->type) {
        case 'emfield_video':
          return asset_render($asset, 'video_video');
      }
      break;
    case 'img':
    case 'icon':
      if ($asset->type == 'emfield_video') {
        $item = $asset->emfield[0];
        if ($item['value'] && $item['provider']) {

          // if we have emthumb installed, then give it a chance to override our thumbnail
          $thumbnail_url = module_invoke('emthumb', 'thumbnail_url', $item);

          // if we don't have a custom thumbnail, then see if the provider gives us one
          $thumbnail_url = $thumbnail_url ? $thumbnail_url : module_invoke('emfield', 'include_invoke', 'video_cck', $item['provider'], 'thumbnail', NULL, $item, NULL, NULL, 64, 64);
          return $thumbnail_url;
        }
      }
      break;
  }
}

/**
 * Implementation of hook_asset_formatter().
 */
function asset_emfield_asset_formatter($op = 'info', $asset = null, $attr = array()) {
  $module_map = array(
    'emfield_video' => 'video_cck',
  );
  $module = $module_map[$asset->type];
  switch ($op) {
    case 'info':
      foreach ($module_map as $module) {
        $cck_formats = module_invoke($module, 'field_formatter_info');
        foreach ($cck_formats as $key => $cck_format) {
          if ($key != 'default') {
            $formats[$key] = array(
              'name' => $cck_format['label'],
              'description' => $cck_format['description'],
              'module' => 'asset_emfield',
            );
          }
        }
      }
      return $formats;
    case 'load':
      if ($asset->type == 'emfield_video') {
        $formats = array();
        $cck_formats = module_invoke($module, 'field_formatter_info');
        foreach ($cck_formats as $key => $cck_format) {
          if ($key != 'default') {
            $formats[] = $key;
          }
        }
        return $formats;
      }
      break;
    case 'render':
      return asset_emfield_invoke('render', $asset, $attr['format']);
    case 'form':

      //      $function = 'asset_embed_'. $asset->embed['provider'] .'_'. $op;
      //      if(function_exists($function)){
      //        return call_user_func($function, $asset, $attr);
      //      }
      break;
  }
}

/**
 * helper function to bridge hook_asset_type and emfield cck hooks
 */
function asset_emfield_invoke($op, &$asset, $a3 = NULL) {

  // TODO: use widget settings to provide a dynamic widget object
  $field = array(
    'field_name' => 'emfield',
    'type' => $map[$asset->type],
    'required' => 1,
    'multiple' => 0,
    'db_storage' => 1,
    // ????
    'widget' => array(
      'default_value' => array(
        0 => array(
          'embed' => '',
          'value' => '',
        ),
      ),
      'providers' => array(),
      'type' => 'video_cck_textfields',
      'label' => t('Embedded Media'),
      'description' => '',
    ),
  );
  $module_map = array(
    'emfield_video' => 'video_cck',
  );
  $module = $module_map[$asset->type];
  $node = new stdClass();

  // emfield_emfield_field_formatter checks for a node type, and if not will load
  // the field widget settings.  the type doesn't really matter though.
  $node->type = '__asset';
  if ($op == 'form') {
    return emfield_emfield_widget($op, $node, $field, $asset->emfield, $module);
  }
  elseif ($op == 'render') {
    return module_invoke('emfield', 'emfield_field_formatter', $field, $asset->emfield[0], $a3, $node, $module);
  }
  else {
    return emfield_emfield_field($op, $node, $field, $asset->emfield, 0, 0, $module);
  }
}
function asset_emfield_asset_search($op = 'info', $type = NULL, $a3 = NULL) {

  // load all of our provider includes inside the asset_emfield module folder
  asset_emfield_system_list('asset_emfield');
  $module = 'video_cck';
  switch ($op) {
    case 'info':
      $providers = emfield_system_list($module);
      $searches = array();
      foreach ($providers as $provider) {
        $info = emfield_include_invoke($module, $provider->name, 'asset_search', 'info');
        if ($info && is_array($info)) {
          $searches = $searches + $info;
        }
      }
      return $searches;
    case 'url':
      $url = emfield_include_invoke($module, $type['emfield_provider'], 'asset_search', 'url', $type, $a3);
      return $url;
    case 'feed item':
      $asset = emfield_include_invoke($module, $type['emfield_provider'], 'asset_search', 'feed item', $type, $a3);
      return $asset;
  }
}

/**
 * copied-and-modified version of emfield_system_list, to load our own provider includes
 */
function asset_emfield_system_list($module, $provider = NULL, $load = TRUE) {
  static $list = array();
  $files = drupal_system_listing("{$provider}\\.inc", drupal_get_path('module', $module) . "/providers", 'name', 0);
  ksort($files);
  if ($load) {
    foreach ($files as $file) {
      if ($file && !isset($list[$file->name])) {
        include_once './' . $file->filename;
        $list[$file->name] = $file->name;
      }
    }
  }
  return $files;
}

Functions

Namesort descending Description
asset_emfield_asset_formatter Implementation of hook_asset_formatter().
asset_emfield_asset_search
asset_emfield_asset_type Implementation of hook_asset_type().
asset_emfield_invoke helper function to bridge hook_asset_type and emfield cck hooks
asset_emfield_system_list copied-and-modified version of emfield_system_list, to load our own provider includes