You are here

asset.module in Asset 6

Same filename and directory in other branches
  1. 5.2 asset.module
  2. 5 asset.module
  3. 7 asset.module

File

asset.module
View source
<?php

define('ASSET_PRIVATE', 0);
define('ASSET_PUBLIC', 1);
$__asset_path = drupal_get_path('module', 'asset');
require_once $__asset_path . '/inc/asset.routines.inc';
require_once $__asset_path . '/inc/asset.api.inc';
require_once $__asset_path . '/inc/asset.node.inc';
_asset_include();
function asset_init() {
  module_invoke_all('asset_formatter', 'init');
  drupal_add_css(drupal_get_path('module', 'asset') . '/misc/asset.css');
}

/**
 * Implementation of hook_menu()
 */
function asset_menu() {
  $items = array();
  $w = 0;

  // administer menu items
  $items['admin/settings/asset'] = array(
    'title' => t('Asset'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'asset_admin_settings',
    ),
    'access arguments' => array(
      'administer assets',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'inc/asset.admin.inc',
  );
  $items['admin/settings/asset/settings'] = array(
    'title' => t('Settings'),
    'access arguments' => array(
      'administer assets',
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => $w++,
    'file' => 'inc/asset.admin.inc',
  );
  $items['admin/settings/asset/defaults'] = array(
    'title' => t('Formatter Defaults'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'asset_admin_formatter_defaults',
    ),
    'access arguments' => array(
      'administer assets',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => $w++,
    'file' => 'inc/asset.admin.inc',
  );
  foreach (module_implements('asset_settings') as $module) {
    $items['admin/settings/asset/' . $module] = array(
      'title' => $module,
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        $module . '_asset_settings',
      ),
      'access arguments' => array(
        'administer assets',
      ),
      'type' => MENU_LOCAL_TASK,
      'weight' => $w++,
      'file' => 'inc/asset.admin.inc',
    );
  }

  // commin menu items
  $items['asset/widget/js'] = array(
    'page callback' => 'asset_widget_js',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['asset/wizard'] = array(
    'title' => t('Asset Wizard'),
    'page callback' => 'asset_wizard',
    'access callback' => call_user_func(create_function('', "return (user_access('create assets') || user_access('administer assets'));")),
    'type' => MENU_CALLBACK,
  );
  $items['asset/js/preview'] = array(
    'page callback' => 'asset_js_preview',
    'access callback' => call_user_func(create_function('', "return (user_access('create assets') || user_access('administer assets'));")),
    'type' => MENU_CALLBACK,
  );
  $items['asset/img'] = array(
    'page callback' => 'asset_img_preview',
    'access arguments' => array(
      'administer assets',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function asset_perm() {
  $perms = array(
    'create assets',
    'administer assets',
  );
  foreach (module_implements('asset_type') as $module) {
    foreach (module_invoke($module, 'asset_type', 'info') as $delta => $type) {
      $perms[] = 'create ' . $delta . ' assets';
    }
  }
  return $perms;
}

/**
 * Implementation of hook_theme().
 */
function asset_theme() {
  return array(
    'asset' => array(
      'arguments' => array(
        'content' => NULL,
        'attributes' => NULL,
      ),
      'file' => 'inc/asset.themes.inc',
    ),
    'asset_wizard_theme_form' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'inc/asset.themes.inc',
    ),
    'asset_admin_formatter_defaults' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'inc/asset.themes.inc',
    ),
    'asset_render_default' => array(
      'arguments' => array(
        'asset' => NULL,
      ),
      'file' => 'inc/asset.themes.inc',
    ),
    'asset_textarea_link' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'inc/asset.themes.inc',
    ),
    'asset_popup' => array(
      'arguments' => array(
        'content' => NULL,
      ),
      'file' => 'inc/asset_wizard.themes.inc',
    ),
    'asset_wizard_form' => array(
      'arguments' => array(
        'form' => NULL,
        'main_class' => NULL,
      ),
      'file' => 'inc/asset_wizard.themes.inc',
    ),
    'asset_wizard_selection_form' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'inc/asset_wizard.themes.inc',
    ),
    'asset_image_button' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'inc/asset_wizard.themes.inc',
    ),
    'asset_upload_permissions' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'inc/asset_wizard.themes.inc',
    ),
    'asset_formatter_default' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'inc/asset.themes.inc',
    ),
  );
}

/**
 * Integrate asset with lightbox
 */
function asset_lightbox($assets, $preview = FALSE, $teaser = FALSE) {
  if (!$preview && file_exists(path_to_theme() . "/lightbox/lightbox.js")) {

    // Do not include Lightbox integration in preview mode: it breaks the jQuery fieldsets
    drupal_add_js(path_to_theme() . "/lightbox/prototype.js");
    drupal_add_js(path_to_theme() . "/lightbox/scriptaculous.js?load=effects");
    drupal_add_js(path_to_theme() . "/lightbox/lightbox.js");
    drupal_add_css(path_to_theme() . "/lightbox/lightbox.css");
  }
  $output = "";
  $is_image = array(
    'jpg',
    'jpeg',
    'png',
    'gif',
    'tif',
    'tiff',
    'bmp',
  );
  $is_audio = array(
    'mp3',
    'wma',
    'wav',
    'ogg',
    'm4a',
    'aac',
  );
  $count = 1;
  $total = 3;
  $imgpresent = 1;
  $imgcount = 0;
  $imgdisplay = 0;
  $firstimg = "";
  foreach ($assets as $asset) {
    $a = asset_load(array(
      'aid' => $asset['aid'],
    ));
    if (in_array($a->extension, $is_image)) {

      // If it's an image and imagecache configuration has been done, display the imagecached version
      $imagecache = asset_get_default_formatter('local', $a->extension, $teaser);
      if (function_exists('theme_imagecache') && substr($imagecache, 0, 11) == "imagecache:") {
        list($imagecache, $preset) = split(":", $imagecache);
        $img = theme('imagecache', $preset, $a->filepath, $asset['caption'], $asset['caption']);
      }
      else {
        $img = theme('image', $a->filepath, $asset['caption'], $asset['caption']);
      }
      $link = '<a href="' . file_create_url($a->filepath) . '" rel="lightbox[gallery]" title="' . str_replace("\"", "'", $asset['caption']);
      $link .= $asset['copyright'] ? '<br/>&#169; ' . str_replace("\"", "'", $asset['copyright']) . '"' : '"';

      // We only want to display $total thumbnails, but we need the html for the lightbox gallery
      if ($count > $total) {
        $link .= " style='display:none'";
      }
      else {
        $imgdisplay++;
      }
      if (empty($firstimg)) {
        $firstimg = $link . ">";
      }
      $link .= ">" . $img . "</a>";
      $output .= $link;
      if ($count <= $total) {
        $output .= $asset['caption'] ? "<p>" . $asset['caption'] . "</p>" : "";
        $output .= $asset['copyright'] ? "<p class='attribution'>&#169; " . $asset['copyright'] . "</p>" : "";
        $output .= "<br/>";
      }
      $imgcount++;
    }
    else {

      // Not an image so automatically asset_preview() it
      $output .= asset_preview($asset['aid']);
      $output .= $asset['caption'] ? "<p>" . $asset['caption'] . "</p>" : "";
      $output .= $asset['copyright'] ? "<p class='attribution'>&#169; " . $asset['copyright'] . "</p>" : "";
      if (in_array($a->extension, $is_audio)) {
        $output .= '<p><a href="' . file_create_url($a->filepath) . '">' . t("Download this audio file") . '</a></p>';
      }
    }
    $count++;
  }
  if (!empty($imgcount) && $imgcount > $imgdisplay) {
    $firstimg = str_replace("style='display:none'", "", $firstimg);
    $output .= t('<br/><div class="view-all-images">' . $firstimg . '%mycount </a></div>', array(
      '%mycount' => format_plural($imgcount, 'View the image', 'View all @count images'),
    ));
  }
  return $output;
}

/**
 * Return all macros as an array.
 */
function asset_get_macros($text) {
  $m = array();
  preg_match_all('/ \\[ ( [^\\[\\]]+ )* \\] /x', $text, $matches);
  $tag_match = (array) array_unique($matches[1]);

  // Don't process duplicates.
  foreach ($tag_match as $macro) {
    $current_macro = '[' . $macro . ']';
    $param = array_map('trim', explode('|', $macro));
    $func_name = array_shift($param);

    // The first macro param is assumed to be the function name.

    //$num_params = count($param); // Get the number of parameters
    if ($func_name == 'asset') {
      $vars = array();
      foreach ($param as $p) {
        $pos = strpos($p, '=');
        $varname = substr($p, 0, $pos);
        $varvalue = substr($p, $pos + 1);
        $vars[$varname] = $varvalue;
      }

      // the full unaltered filter string is the key for the array of filter attributes
      $m[$current_macro] = $vars;
    }
  }
  return $m;
}

/**
 * build html from atrributes array.
 *
 * @param $attr
 *   Array of attributes parsed from filter macro.
 */
function asset_render_macro($attr = array()) {
  $asset = asset_load($attr['aid']);
  $asset->title = $attr['title'] ? $attr['title'] : $asset->title;
  if ($attr['formatter']) {
    $content = module_invoke($attr['formatter'], 'asset_formatter', 'render', $asset, $attr);
    return theme('asset', $content, $attr);
  }
  else {
    return theme('asset_render_default', $asset);
  }
}

/**
 * Build a macro from the specified attributes.
 */
function asset_build_macro($attr = array()) {
  $macro = '[asset';
  if (!empty($attr)) {
    foreach ($attr as $k => $v) {
      $macro .= '|' . $k . '=' . $v;
    }
  }
  $macro .= ']';
  return $macro;
}

/**
 * Utility function to retrieve a list of all available formatters.
 *
 * @return array
 *     returns an array keyed by filetype(extension) with elements as arrays of format information.
 */
function asset_get_formatters($reset = false) {
  static $formatters;
  if (empty($formatters) || $reset) {
    $cache = cache_get('asset_formatters');
    if ($reset || empty($cache) || empty($cache->data)) {
      $formatters = array();
      foreach (module_implements('asset_formatter') as $module) {
        $list = module_invoke($module, 'asset_formatter', 'info');
        foreach ((array) $list as $key => $data) {
          $data['module'] = $module;
          $data['format'] = $key;
          foreach ($data['types'] as $type => $exts) {
            foreach ($exts as $ext) {
              $formatters[$type][$ext][] = $data;
            }
          }
        }
      }
      cache_set('asset_formatters', $formatters);
    }
    else {
      $formatters = $cache->data;
    }
  }
  return $formatters;
}

/**
 * Menu callback to load the asset popup window.
 */
function asset_wizard() {

  // all wizard related functionality first comes through this function so load the wizard inc
  require_once drupal_get_path('module', 'asset') . '/inc/asset_wizard.inc';
  $theme = variable_get('asset_wizard_theme', drupal_get_path('module', 'asset') . '/misc/themes/default/asset_wizard.css');
  drupal_add_css($theme);
  switch (arg(2)) {
    case 'textarea':
      drupal_add_js(drupal_get_path('module', 'asset') . '/misc/asset_textarea.js');
      break;
    case 'textfield':
      drupal_add_js(drupal_get_path('module', 'asset') . '/misc/asset_textfield.js');
      break;
    case 'tinymce':
      if (module_exists('tinytinymce')) {
        $path = drupal_get_path('module', 'tinytinymce');
        drupal_add_js($path . '/tinymce/jscripts/tiny_mce/tiny_mce.js');
        drupal_add_js($path . '/tinymce/jscripts/tiny_mce/tiny_mce_popup.js');
      }
      else {
        drupal_add_js(drupal_get_path('module', 'asset') . '/misc/tinymce/jscripts/tiny_mce/tiny_mce_popup.js');
      }
      drupal_add_js(drupal_get_path('module', 'asset') . '/misc/tinymce/asset_tinymce.js');
      break;
  }
  drupal_add_js(drupal_get_path('module', 'asset') . '/misc/asset.js');
  drupal_add_css(drupal_get_path('module', 'asset') . '/misc/asset_wizard.css');
  $output = drupal_get_form('asset_wizard_form');
  ob_end_clean();
  print theme('asset_popup', $output);
  exit;
}

/**
 * The #process callback function for the textareas
 */
function asset_textarea($element) {
  if (_asset_page_match()) {
    $output = theme('asset_textarea_link', $element);
    $element['#suffix'] .= $output;
    if (module_exists('tinymce')) {
      drupal_add_js(drupal_get_path('module', 'asset') . '/misc/tinymce/asset_tinymce_helper.js');
    }
  }
  return $element;
}

/**
 * Should we show the insert access link, determined by admin setting
 * Borrowed from tinymce.module
 *
 * @return
 *   TRUE if can render, FALSE if not allowed.
 */
function _asset_page_match() {
  $page_match = FALSE;
  if (!user_access('create assets') && !user_access('administer assets')) {
    return $page_match;
  }
  $access_option = variable_get('asset_access_option', 1);
  $access_pages = variable_get('asset_access_pages', "node/add/*\nnode/*/edit");
  if ($access_pages) {

    // If the PHP option wasn't selected
    if ($access_option < 2) {
      $path = drupal_get_path_alias($_GET['q']);
      $regexp = '/^(' . preg_replace(array(
        '/(\\r\\n?|\\n)/',
        '/\\\\\\*/',
        '/(^|\\|)\\\\<front\\\\>($|\\|)/',
      ), array(
        '|',
        '.*',
        '\\1' . preg_quote(variable_get('site_frontpage', 'node'), '/') . '\\2',
      ), preg_quote($access_pages, '/')) . ')$/';
      $page_match = !($access_option xor preg_match($regexp, $path));
    }
    else {
      $page_match = drupal_eval($access_pages);
    }
  }
  else {

    // No pages were specified to block so show on all
    $page_match = TRUE;
  }
  return $page_match;
}

/**
 * Menu Callback from javascript to print an assets preview
 */
function asset_js_preview($aid, $show_details = true, $return = false) {
  if (!isset($aid)) {
    exit;
  }
  $asset = asset_load($aid);
  $formatters = asset_get_formatters();
  if (isset($formatters[$asset->type][$asset->extension][0])) {
    $format = $formatters[$asset->type][$asset->extension][0];
  }
  elseif (isset($formatters[$asset->type]['*'][0])) {
    $format = $formatters[$asset->type]['*'][0];
  }
  else {
    $format = $formatters['*']['*'][0];
  }
  $output .= asset_preview($asset->aid);
  if ($show_details) {
    $details[t('File Size')] = format_size($asset->filesize);
    if (!empty($asset->author)) {
      $details[t('Author')] = $asset->author;
    }
    $details = array_merge($details, (array) module_invoke($format['module'], 'asset_formatter', 'details', $asset, $format));
    foreach ($details as $label => $value) {
      $output .= '<div><strong>' . check_plain($label) . '</strong>: ' . check_plain($value) . '</div>';
    }
  }
  if ($return) {
    return $output;
  }
  print $output;
  exit;
}

/**
 * Build a preview of an asset based on module and format options.  If no module
 * and format info is given then the default teaser formatter is used.
 */
function asset_preview($aid, $module = NULL, $format = NULL) {
  $asset = asset_load($aid);
  if (empty($module) || empty($format)) {
    $formatter = asset_get_default_formatter($asset->type, $asset->extension, TRUE);
    list($module, $format) = explode(':', $formatter);
  }
  $output = module_invoke($module, 'asset_formatter', 'preview', $asset, array(
    'format' => $format,
  ));
  return $output;
}

/**
 * Callback function to output an image for preview of an asset.
 */
function asset_img_preview($aid, $module = NULL, $format = NULL) {
  $asset = asset_load($aid);
  if (!$module || !$format) {
    $formatter = asset_get_default_formatter($asset->type, $asset->extension, TRUE);
    list($module, $format) = explode(':', $formatter);
  }
  $src = module_invoke($module, 'asset_formatter', 'img', $asset, array(
    'format' => $format,
  ));

  // We're not useing drupal_goto because filenames will have the language code prepended
  // return drupal_goto($src);
  $src = substr($src, 0, 5) != "http:" ? base_path() . $src : $src;
  return header('location: ' . $src);
}

Functions

Namesort descending Description
asset_build_macro Build a macro from the specified attributes.
asset_get_formatters Utility function to retrieve a list of all available formatters.
asset_get_macros Return all macros as an array.
asset_img_preview Callback function to output an image for preview of an asset.
asset_init
asset_js_preview Menu Callback from javascript to print an assets preview
asset_lightbox Integrate asset with lightbox
asset_menu Implementation of hook_menu()
asset_perm Implementation of hook_perm().
asset_preview Build a preview of an asset based on module and format options. If no module and format info is given then the default teaser formatter is used.
asset_render_macro build html from atrributes array.
asset_textarea The #process callback function for the textareas
asset_theme Implementation of hook_theme().
asset_wizard Menu callback to load the asset popup window.
_asset_page_match Should we show the insert access link, determined by admin setting Borrowed from tinymce.module

Constants