[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/filefield_sources/sources/ -> imce.inc (source)

   1  <?php
   2  // $Id: imce.inc,v 1.6 2010/08/09 05:24:10 quicksketch Exp $
   3  
   4  /**
   5   * @file
   6   * A FileField extension to allow referencing of files from IMCE.
   7   *
   8   * The "hooks" in this file are not true hooks, they're called individually
   9   * from the main filefield_sources.module in the corresponding hook by the
  10   * same name. Any of these hooks could be broken out into a separate module.
  11   */
  12  
  13  /**
  14   * Implementation of hook_filefield_source_info().
  15   */
  16  function filefield_source_imce_info() {
  17    $source = array();
  18  
  19    if (!module_exists('imce') || !imce_access()) {
  20      return $source;
  21    }
  22  
  23    $source['imce'] = array(
  24      'name' => t('IMCE file browser'),
  25      'label' => t('File browser'),
  26      'description' => t('Select a file to use from a file browser.'),
  27      'process' => 'filefield_source_imce_process',
  28      'value' => 'filefield_source_imce_value',
  29      'weight' => -1,
  30    );
  31    return $source;
  32  }
  33  
  34  /**
  35   * Implementation of hook_menu().
  36   */
  37  function filefield_source_imce_menu() {
  38    $items = array();
  39    $items['filefield/imce/%/%'] = array(
  40      'page callback' => 'filefield_source_imce_page',
  41      'page arguments' => array(2, 3),
  42      'access callback' => 'filefield_edit_access',
  43      'access arguments' => array(2, 3),
  44      'file' => 'sources/imce.inc',
  45      'type' => MENU_CALLBACK,
  46    );
  47    return $items;
  48  }
  49  
  50  /**
  51   * Implementation of hook_theme().
  52   */
  53  function filefield_source_imce_theme() {
  54    return array(
  55      'filefield_source_imce_element' => array(
  56        'arguments' => array('element' => NULL),
  57        'file' => 'sources/imce.inc',
  58      ),
  59   );
  60  }
  61  
  62  /**
  63   * Implementation of hook_filefield_source_settings().
  64   */
  65  function filefield_source_imce_settings($op, $field) {
  66    $return = array();
  67  
  68    // Add settings to the FileField widget form.
  69  
  70    return $return;
  71  
  72  }
  73  
  74  /**
  75   * A #process callback to extend the filefield_widget element type.
  76   */
  77  function filefield_source_imce_process($element, $edit, &$form_state, $form) {
  78    $field = content_fields($element['#field_name'], $element['#type_name']);
  79  
  80    $element['filefield_imce'] = array(
  81      '#weight' => 100.5,
  82      '#access' => empty($element['fid']['#value']),
  83      '#theme' => 'filefield_source_imce_element',
  84      '#description' => filefield_sources_element_validation_help($element['#upload_validators']),
  85    );
  86  
  87    $filepath_id = $element['#id'] . '-imce-path';
  88    $display_id = $element['#id'] . '-imce-display';
  89    $select_id = $element['#id'] . '-imce-select';
  90    $element['filefield_imce']['file_path'] = array(
  91      '#type' => 'hidden',
  92      '#value' => '',
  93      '#id' => $filepath_id,
  94      '#attributes' => array(
  95        'onchange' => "if (!jQuery('#$select_id').attr('disabled')) { jQuery('#$select_id').mousedown().disable(); jQuery('#$display_id').html(this.value); }",
  96      ),
  97    );
  98  
  99    $imce_function = 'window.open(\'' . url('filefield/imce/' . $element['#type_name'] . '/' . $element['#field_name'], array('query' => 'app=' . rawurlencode($field['widget']['label']) . '|url@' . $filepath_id)) . '\', \'\', \'width=760,height=560,resizable=1\'); return false;';
 100    $element['filefield_imce']['display_path'] = array(
 101      '#type' => 'markup',
 102      '#value' => '<span id="' . $display_id . '" class="filefield-sources-imce-display">' . t('No file selected') . '</span> (<a class="filefield-sources-imce-browse" href="#" onclick="' . $imce_function . '">' . t('browse') . '</a>)',
 103    );
 104  
 105    $element['filefield_imce']['select'] = array(
 106      '#type' => 'submit',
 107      '#value' => t('Select'),
 108      '#submit' => array('node_form_submit_build_node'),
 109      '#name' => $element['#name'] . '[filefield_imce][button]',
 110      '#id' => $select_id,
 111      '#attributes' => array('style' => 'display: none;'),
 112      '#ahah' => array(
 113         'path' => 'filefield/ahah/'. $element['#type_name'] .'/'. $element['#field_name'] .'/'. $element['#delta'],
 114         'wrapper' => $element['#id'] .'-ahah-wrapper',
 115         'method' => 'replace',
 116         'effect' => 'fade',
 117      ),
 118    );
 119  
 120    return $element;
 121  }
 122  
 123  /**
 124   * A #filefield_value_callback function.
 125   */
 126  function filefield_source_imce_value($element, &$item) {
 127    if (isset($item['filefield_imce']['file_path']) && $item['filefield_imce']['file_path'] != '') {
 128      if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC) {
 129        $file_path = preg_replace('/^' . preg_quote(base_path(), '/') . '/', '', $item['filefield_imce']['file_path']);
 130      }
 131      else {
 132        $file_path = preg_replace('/^' . preg_quote(url('system/files'), '/') . '/', file_directory_path(), $item['filefield_imce']['file_path']);
 133      }
 134  
 135      // Resolve the file path to an FID.
 136      if ($fid = db_result(db_query("SELECT fid FROM {files} WHERE filepath = '%s'", rawurldecode($file_path)))) {
 137        $file = field_file_load($fid);
 138        if (filefield_sources_element_validate($element, (object) $file)) {
 139          $item = array_merge($item, $file);
 140        }
 141      }
 142      else {
 143        form_error($element, t('The selected file could not be used because the file does not exist in the database.'));
 144      }
 145      // No matter what happens, clear the value from the file path field.
 146      $item['filefield_imce']['file_path'] = '';
 147    }
 148  }
 149  
 150  /**
 151   * Theme the output of the autocomplete field.
 152   */
 153  function theme_filefield_source_imce_element($element) {
 154    $output = theme('markup', $element['display_path']) . theme('hidden', $element['file_path']) . theme('submit', $element['select']);
 155    return '<div class="filefield-source filefield-source-imce clear-block">' . theme('form_element', $element, $output) . '</div>';
 156  }
 157  
 158  /**
 159   * Outputs the IMCE browser for FileField.
 160   */
 161  function filefield_source_imce_page($content_type, $field_name) {
 162    global $conf;
 163  
 164    // Check access.
 165    if (!module_exists('imce') || !imce_access() || !content_fields($field_name, $content_type)) {
 166      return drupal_access_denied();
 167    }
 168    // Set custom directory scan.
 169    $conf['imce_custom_scan'] = 'filefield_source_imce_custom_scan';
 170  
 171    // Disable absolute URLs.
 172    $conf['imce_settings_absurls'] = 0;
 173  
 174    // Set custom post-process to disable undesired features.
 175    if (empty($conf['imce_custom_process'])) {
 176      $conf['imce_custom_process'] = array();
 177    }
 178    $conf['imce_custom_process'] += array('filefield_source_imce_custom_process' => 1);
 179  
 180    // IMCE 6.x-2.x.
 181    if (module_hook('imce', 'file_references')) {
 182      module_load_include('inc', 'imce', 'inc/imce.page');
 183      return imce();
 184    }
 185    // IMCE 6.x-1.x.
 186    else {
 187      module_load_include('inc', 'imce', 'inc/page');
 188      return imce_page();
 189    }
 190  }
 191  
 192  /**
 193   * Scan directory and return file list, subdirectories, and total size.
 194   */
 195  function filefield_source_imce_custom_scan($dirname) {
 196    // Get a list of files in the database for this directory.
 197    $sql_dir_name = $dirname == '.' ? file_directory_path() : file_directory_path() .'/'. $dirname;
 198  
 199    $result = db_query("SELECT filename FROM {files} WHERE filepath LIKE '%s' AND filepath NOT LIKE '%s'", $sql_dir_name .'/%', $sql_dir_name .'/%/%');
 200    $db_files = array();
 201    while ($row = db_fetch_object($result)) {
 202      $db_files[$row->filename] = $row->filename;
 203    }
 204  
 205    // Get the default IMCE directory scan, then filter down to database files.
 206    $directory = imce_scan_directory($dirname);
 207    foreach ($directory['files'] as $filename => $file) {
 208      if (!isset($db_files[$filename])) {
 209        unset($directory['files'][$filename]);
 210        $directory['dirsize'] -= $file['size'];
 211      }
 212    }
 213  
 214    return $directory;
 215  }
 216  
 217  /**
 218   * Post process IMCE profile.
 219   */
 220  function filefield_source_imce_custom_process(&$imce) {
 221    // Disable file operations.
 222    foreach ($imce['perm'] as $name => $val) {
 223      if ($name != 'browse' && $name != 'subnav') {
 224        $imce['perm'][$name] = 0;
 225      }
 226    }
 227  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7