You are here

asset.routines.inc in Asset 6

File

inc/asset.routines.inc
View source
<?php

function _asset_include() {

  // load the views hooks if the views module is enabled
  if (module_exists('views')) {
    require_once drupal_get_path('module', 'asset') . '/inc/asset_views.inc';
  }

  // Load all our module 'on behalfs'.
  $files = drupal_system_listing('asset_.*\\.inc$', drupal_get_path('module', 'asset') . '/inc/modules', 'name', 0);
  foreach ($files as $file) {

    // The filename format is very specific. It must be asset_MODULENAME.inc
    $module = substr_replace($file->name, '', 0, 6);
    if (module_exists($module)) {
      require_once $file->filename;
    }
  }
}

/**
 * Utility function to return an array of available formatters for a file suitable for FAPI #options
 *
 * @param $filename
 *    Filename to retrieve options for.  Will also work with just an extension.
 */
function asset_formatter_options($type = '*', $filename = null) {
  $pos = strrpos($filename, '.');
  if ($pos === false) {

    // allow for case where extension is passed instead of filename
    $ext = $filename;
  }
  else {
    $ext = substr($filename, $pos + 1);
  }
  $formatters = asset_get_formatters();
  $options = array();
  if (is_array($formatters[$type][$ext])) {
    foreach ($formatters[$type][$ext] as $formatter) {
      $options[$formatter['module'] . ':' . $formatter['format']] = $formatter['name'];
    }
  }
  if (is_array($formatters[$type]['*'])) {
    foreach ($formatters[$type]['*'] as $formatter) {
      $options[$formatter['module'] . ':' . $formatter['format']] = $formatter['name'];
    }
  }
  if (is_array($formatters['*']['*'])) {
    foreach ($formatters['*']['*'] as $formatter) {
      $options[$formatter['module'] . ':' . $formatter['format']] = $formatter['name'];
    }
  }
  return $options;
}

/**
 * Get the default formatter for a given type and extension
 */
function asset_get_default_formatter($type, $ext, $teaser = FALSE) {
  $prefix = 'asset_default_formatter_' . ($teaser ? 'teaser_' : '');
  $vars = array(
    $prefix . $type . '_' . $ext,
    $prefix . $type . '_*',
    $prefix . '*_*',
  );
  foreach ($vars as $var) {
    if ($formatter = variable_get($var, false)) {
      break;
    }
  }

  // if var is not set, then cascade through possible formatters to find a suitable one
  if (!$formatter) {
    $formatters = asset_get_formatters();
    if (isset($formatters[$type][$ext][0])) {
      $format = $formatters[$type][$ext][0];
    }
    elseif (isset($formatters[$type]['*'][0])) {
      $format = $formatters[$type]['*'][0];
    }
    else {
      $format = $formatters['*']['*'][0];
    }
    $formatter = $format['module'] . ':' . $format['format'];
  }
  return $formatter;
}

/**
 * Modified version of file_transfer()
 *
 * Only output image formats and do not require src to be in file_directory_path
 */
function asset_img_output($src) {
  ob_end_clean();
  $mimes = array(
    'jpg' => 'image/jpeg',
    'gif' => 'image/gif',
    'png' => 'image/x-png',
  );
  $info = pathinfo($src);
  if (!$mimes[$info['extension']]) {
    return drupal_not_found();
  }
  header('Content-type: ' . $mimes[$info['extension']]);
  if ($fd = fopen($src, 'rb')) {
    while (!feof($fd)) {
      print fread($fd, 1024);
    }
    fclose($fd);
  }
  else {
    drupal_not_found();
  }
  exit;
}

/**
 * Wrapper for file_check_directory that also checks/adds a matching asset
 */
function asset_check_directory(&$directory, $mode = 0, $form_item = NULL, $form_values = array()) {
  $return = file_check_directory(file_create_path($directory), $mode, $form_item);
  if ($return && $mode && !empty($form_values)) {
    $asset = asset_load(array(
      'dirname' => $form_values['parent'],
      'filename' => $form_values['title'],
    ));
    if (!$asset) {
      $asset = new stdClass();
      $asset->filepath = $directory;
      $asset->filesize = 0;
      $asset->type = 'directory';
      asset_save($asset, $form_values);
    }
  }
  return $return;
}

/**
 * Helper function to build a query string from an array
 */
function asset_build_query($data) {
  foreach ($data as $k => $v) {
    $items[] = "{$k}={$v}";
  }
  return join('&', $items);
}

/**
 * wrapper around pathinfo() that strips file_directory_path from path and .
 * from dirname
 */
function asset_pathinfo($path) {
  $path = trim(str_replace(file_directory_path(), '', $path), '/');
  $info = pathinfo($path);
  if ($info['dirname'] == '.') {
    $info['dirname'] = '';
  }
  return $info;
}

Functions

Namesort descending Description
asset_build_query Helper function to build a query string from an array
asset_check_directory Wrapper for file_check_directory that also checks/adds a matching asset
asset_formatter_options Utility function to return an array of available formatters for a file suitable for FAPI #options
asset_get_default_formatter Get the default formatter for a given type and extension
asset_img_output Modified version of file_transfer()
asset_pathinfo wrapper around pathinfo() that strips file_directory_path from path and . from dirname
_asset_include