You are here

elfinder_ffsel.module in elFinder file manager 6

File

modules/elfinder_ffsel/elfinder_ffsel.module
View source
<?php

// $Id: elfinder_ffsel.module,v 1.1.2.3 2010/11/26 11:31:52 ph0enix Exp $
// Main hooks to tell FielField Sources about our source
function elfinder_ffsel_filefield_sources_info() {
  $params = array();
  return elfinder_ffsel_hooks('info', $params);
}
function elfinder_ffsel_hooks($method, &$params) {
  $return = array();
  $source = 'elfinder';
  $function = 'filefield_source_' . $source . '_' . $method;
  if (function_exists($function)) {
    $result = call_user_func_array($function, $params);
    if (isset($result) && is_array($result)) {
      $return = array_merge_recursive($return, $result);
    }
    else {
      if (isset($result)) {
        $return[] = $result;
      }
    }
  }
  return $return;
}

// Source specific functions

/**
 * Implementation of hook_filefield_source_info().
*/
function filefield_source_elfinder_info() {
  $source = array();
  if (!module_exists('elfinder')) {
    return $source;
  }
  $source['elfinder'] = array(
    'name' => t('elFinder'),
    'label' => t('elFinder File browser'),
    'description' => t('Select a file to use from a file browser.'),
    'process' => 'filefield_source_elfinder_process',
    'value' => 'filefield_source_elfinder_value',
    'weight' => -1,
  );
  return $source;
}

/**
 * Implementation of hook_menu().
 */
function filefield_source_elfinder_menu() {
  $items = array();
  $items['filefield/elfinder/%/%'] = array(
    'page callback' => 'filefield_source_elfinder_page',
    'page arguments' => array(
      2,
      3,
    ),
    'access callback' => 'filefield_edit_access',
    'access arguments' => array(
      2,
      3,
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 */
function elfinder_ffsel_theme($existing, $type, $theme, $path) {
  return array(
    'filefield_source_elfinder_element' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
  );
}

/**
 * Implementation of hook_filefield_source_settings().
 */
function filefield_source_elfinder_settings($op, $field) {
  $return = array();

  // Add settings to the FileField widget form.
  return $return;
}

/**
 * A #process callback to extend the filefield_widget element type.
 */
function filefield_source_elfinder_process($element, $edit, &$form_state, $form) {
  $field = content_fields($element['#field_name'], $element['#type_name']);
  $element['filefield_elfinder'] = array(
    '#weight' => 100.5,
    '#access' => empty($element['fid']['#value']),
    '#theme' => 'filefield_source_elfinder_element',
    '#filefield_source' => TRUE,
    // Required for proper theming.
    '#description' => filefield_sources_element_validation_help($element['#upload_validators']),
  );
  $filepath_id = $element['#id'] . '-elfinder-path';
  $display_id = $element['#id'] . '-elfinder-display';
  $select_id = $element['#id'] . '-elfinder-select';
  $element['filefield_elfinder']['file_path'] = array(
    '#type' => 'hidden',
    '#value' => '',
    '#id' => $filepath_id,
    '#attributes' => array(
      'onchange' => "if (!jQuery('#{$select_id}').attr('disabled')) { jQuery('#{$select_id}').mousedown(); jQuery('#{$display_id}').html(this.value); }",
    ),
  );
  $elfinder_function = 'window.open(\'' . url('elfinder', array(
    'query' => array(
      'app' => 'ffs',
      'field_name' => $element['#field_name'],
      'filepath_id' => $filepath_id,
      'select_id' => $select_id,
    ),
  )) . '\', \'\', \'width=760,height=560,resizable=1\'); return false;';
  $element['filefield_elfinder']['display_path'] = array(
    '#type' => 'markup',
    '#value' => '<span id="' . $display_id . '" class="filefield-sources-elfinder-display">' . t('No file selected') . '</span> (<a class="filefield-sources-elfinder-browse" href="#" onClick="' . $elfinder_function . '">' . t('browse') . '</a>)',
  );
  $element['filefield_elfinder']['select'] = array(
    '#type' => 'submit',
    '#value' => t('Select'),
    '#submit' => array(
      'node_form_submit_build_node',
    ),
    '#name' => $element['#name'] . '[filefield_elfinder][button]',
    '#id' => $select_id,
    '#attributes' => array(
      'style' => 'display: none;',
    ),
    '#ahah' => array(
      'path' => 'filefield/ahah/' . $element['#type_name'] . '/' . $element['#field_name'] . '/' . $element['#delta'],
      'wrapper' => $element['#id'] . '-ahah-wrapper',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
  return $element;
}

/**
 * A #filefield_value_callback function.
 */
function filefield_source_elfinder_value($element, &$item) {
  $field = content_fields($element['#field_name'], $element['#type_name']);
  $validators = $element['#upload_validators'];
  if (isset($item['filefield_elfinder']['file_path']) && $item['filefield_elfinder']['file_path'] != '') {
    if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC) {
      $file_path = preg_replace('/^' . preg_quote(base_path(), '/') . '/', '', $item['filefield_elfinder']['file_path']);
    }
    else {
      $file_path = preg_replace('/^' . preg_quote(url('system/files'), '/') . '/', file_directory_path(), $item['filefield_elfinder']['file_path']);
    }

    // Resolve the file path to an FID.
    if ($fid = db_result(db_query("SELECT fid FROM {files} WHERE filepath = '%s'", rawurldecode($file_path)))) {
      if ($file = field_file_load($fid)) {

        // Run all the normal validations, minus file size restrictions.
        if (isset($element['#upload_validators']['filefield_validate_size'])) {
          unset($element['#upload_validators']['filefield_validate_size']);
        }
        if (filefield_sources_element_validate($element, (object) $file)) {
          $item = array_merge($item, $file);
        }
      }
      else {
        form_error($element, t('The referenced file could not be used'));
      }
    }
    else {

      // We create a new file from original that is referenced to the node.
      // NOTE: New file is saved at the fielfield path, set in field settings for content type
      if ($file = field_file_save_file(rawurldecode($file_path), $validators, filefield_widget_file_path($field))) {
        $item = array_merge($item, $file);
        drupal_set_message(t('File created'));
      }

      // Note: we do not remove original file - so it can be used in other places.
    }

    // No matter what happens, clear the value from the file path field.
    $item['filefield_elfinder']['file_path'] = '';
  }
}

/**
 * Theme the output of the autocomplete field.
 */
function theme_filefield_source_elfinder_element($element) {
  $output = theme('markup', $element['display_path']) . theme('hidden', $element['file_path']) . theme('submit', $element['select']);
  return '<div class="filefield-source filefield-source-elfinder clear-block">' . theme('form_element', $element, $output) . '</div>';
}

/** 
 * Implementation of hook_elfinder_js_settings
 */
function elfinder_ffsel_elfinder_js_settings($settings) {

  /* for filefield_sources support */
  if ($settings['editorApp'] == 'ffs') {
    $settings['connectorUrl'] = url('elfinder/connector/relative');
    if (isset($_GET['field_name'])) {
      if (preg_match("/^[a-zA-z0-9_-]+\$/", $_GET['field_name'])) {
        $settings['field_name'] = $_GET['field_name'];
      }
    }
    if (isset($_GET['filepath_id'])) {
      if (preg_match("/^[a-zA-z0-9_-]+\$/", $_GET['filepath_id'])) {
        $settings['filepath_id'] = $_GET['filepath_id'];
      }
    }
    drupal_add_js(drupal_get_path('module', 'elfinder_ffsel') . '/ffs.callback.js');
    $settings['editorCallback'] = 'elfinder_ffs_callback';
  }
  return $settings;
}

Functions

Namesort descending Description
elfinder_ffsel_elfinder_js_settings Implementation of hook_elfinder_js_settings
elfinder_ffsel_filefield_sources_info
elfinder_ffsel_hooks
elfinder_ffsel_theme Implementation of hook_theme().
filefield_source_elfinder_info Implementation of hook_filefield_source_info().
filefield_source_elfinder_menu Implementation of hook_menu().
filefield_source_elfinder_process A #process callback to extend the filefield_widget element type.
filefield_source_elfinder_settings Implementation of hook_filefield_source_settings().
filefield_source_elfinder_value A #filefield_value_callback function.
theme_filefield_source_elfinder_element Theme the output of the autocomplete field.