You are here

file_aliases.module in File Aliases 5

Same filename and directory in other branches
  1. 6 file_aliases.module
  2. 7 file_aliases.module

File

file_aliases.module
View source
<?php

/**
 * Implementation of hook_menu().
 */
function file_aliases_menu($may_cache) {
  if (!$may_cache) {
    $items[] = array(
      'path' => 'filefield_paths/alias/' . arg(2),
      'callback' => 'file_aliases_load_fid',
      'callback arguments' => array(
        arg(2),
      ),
      'access' => TRUE,
      'type' => MENU_CALLBACK,
    );
  }
  return $items;
}

/**
 * Implementation of hook_filefield_paths_field_settings().
 */
function file_aliases_filefield_paths_field_settings() {
  return array(
    'file_alias' => array(
      'title' => 'File alias',
      'sql' => 'filealias',
      'data' => array(
        'display' => 'file_alias_display',
      ),
      'form' => array(
        'file_alias' => array(
          '#type' => 'textfield',
          '#title' => t('File alias'),
          '#default_value' => '',
        ),
        'file_alias_display' => array(
          '#type' => 'checkbox',
          '#title' => t('Display alias'),
          '#description' => t('If checked, the file alias will be displayed instead of the file') . '.',
          '#default_value' => 0,
          '#weight' => 3,
        ),
      ),
    ),
  );
}

/**
 * Implementation of hook_filefield_paths_process_file().
 */
function file_aliases_filefield_paths_process_file($new, $file, $settings, $node, $update) {
  if ($new) {
    $file['filealias'] = filefield_paths_process_string($settings['filealias']['value'], 'node', $node, $settings['filealias']);
    $file['filealias'] = filefield_paths_process_string($file['filealias'], 'field', array(
      0 => $file['field'],
    ), $settings['filealias']);
    path_set_alias('filefield_paths/alias/' . $file['field']['fid'], $file['filealias']);
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function file_aliases_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'view':
      if (($ffp = filefield_paths_get_fields($node, $op)) == FALSE) {
        break;
      }
      foreach ($ffp['#files'] as &$file) {

        // Convert Upload.module file to Array
        if ($file['module'] == 'upload') {
          $file['field'] = (array) $file['field'];
        }
        if ($ffp['#settings'][$file['name']]['filealias']['display'] == TRUE) {
          $filefield_paths_alias = 'filefield_paths/alias/' . $file['field']['fid'];
          if (($alias = drupal_get_path_alias($filefield_paths_alias)) != $filefield_paths_alias) {
            $file['name'] = $file['name'] == 'upload' ? 'files' : $file['name'];
            if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
              $file['field']['filepath'] = 'system/' . $file['field']['filepath'];
            }
            $node->content[$file['name']]['#value'] = str_replace($file['field']['filepath'], $alias, $node->content[$file['name']]['#value']);
            $node->content[$file['name']]['#value'] = str_replace($file['field']['description'], array_pop(explode('/', $alias)), $node->content[$file['name']]['#value']);
          }
        }

        // Convert Upload.module file back to Object
        if ($file['module'] == 'upload') {
          $file['field'] = (object) $file['field'];
        }
      }
      break;
  }
}
function file_aliases_load_fid($fid) {
  if (request_uri() == base_path() . 'filefield_paths/alias/' . $fid) {
    drupal_not_found();
    exit;
  }
  $result = db_fetch_object(db_query("SELECT filemime, filepath FROM {files} WHERE fid = %d", $fid));
  if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
    file_download($result->filepath);
  }
  else {
    header('Content-Type: ' . $result->filemime);

    // Fix for IE/PDF download issue
    // @see http://drupal.org/node/411910
    header('Cache-Control: public');
    readfile($result->filepath);
  }
}

Functions

Namesort descending Description
file_aliases_filefield_paths_field_settings Implementation of hook_filefield_paths_field_settings().
file_aliases_filefield_paths_process_file Implementation of hook_filefield_paths_process_file().
file_aliases_load_fid
file_aliases_menu Implementation of hook_menu().
file_aliases_nodeapi Implementation of hook_nodeapi().