[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

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

   1  <?php
   2  // $Id: attach.inc,v 1.2 2010/08/09 15:06:50 quicksketch Exp $
   3  
   4  /**
   5   * @file
   6   * A FileField extension to allow use of files within a server directory.
   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_attach_info() {
  17    $source = array();
  18    $source['attach'] = array(
  19      'name' => t('File attach from server directory'),
  20      'label' => t('File attach'),
  21      'description' => t('Select a file from a directory on the server.'),
  22      'process' => 'filefield_source_attach_process',
  23      'value' => 'filefield_source_attach_value',
  24      'weight' => 3,
  25    );
  26    return $source;
  27  }
  28  
  29  /**
  30   * Implementation of hook_theme().
  31   */
  32  function filefield_source_attach_theme() {
  33    return array(
  34      'filefield_source_attach_element' => array(
  35        'arguments' => array('element' => NULL),
  36        'file' => 'sources/attach.inc',
  37      ),
  38    );
  39  }
  40  
  41  /**
  42   * Implementation of hook_filefield_source_settings().
  43   */
  44  function filefield_source_attach_settings($op, $field) {
  45    $return = array();
  46  
  47    if ($op == 'form') {
  48      $return['source_attach'] = array(
  49        '#title' => t('File attach settings'),
  50        '#type' => 'fieldset',
  51        '#collapsible' => TRUE,
  52        '#collapsed' => TRUE,
  53        '#description' => t('File attach allows for selecting a file from a directory on the server, commonly used in combination with FTP.') . ' <strong>' . t('This file source will ignore file size checking when used.') . '</strong>',
  54        '#element_validate' => array('_filefield_source_attach_file_path_validate'),
  55        '#weight' => 3,
  56      );
  57      $return['source_attach']['filefield_source_attach_path'] = array(
  58        '#type' => 'textfield',
  59        '#title' => t('File attach path'),
  60        '#default_value' => empty($field['filefield_source_attach_path']) ? 'file_attach' : $field['filefield_source_attach_path'],
  61        '#size' => 60,
  62        '#maxlength' => 128,
  63        '#description' => t('The directory within the <em>File attach location</em> that will contain attachable files.'),
  64      );
  65      $return['source_attach']['filefield_source_attach_absolute'] = array(
  66        '#type' => 'radios', 
  67        '#title' => t('File attach location'),
  68        '#options' => array(
  69          0 => t('Within the files directory'),
  70          1 => t('Absolute server path'),
  71        ),
  72        '#default_value' => isset($field['filefield_source_attach_absolute']) ? $field['filefield_source_attach_absolute'] : 0,
  73        '#description' => t('The <em>File attach path</em> may be with the files directory (%file_directory) or from the root of your server. If an absolute path is used and it does not start with a "/" your path will be relative to your site directory: %realpath.', array('%file_directory' => file_directory_path(), '%realpath' => realpath('./'))),
  74      );
  75      $return['source_attach']['filefield_source_attach_mode'] = array(
  76        '#type' => 'radios',
  77        '#title' => t('Attach method'),
  78        '#options' => array(
  79          'move' => t('Move the file directly to the final location'),
  80          'copy' => t('Leave a copy of the file in the attach directory'),
  81        ),
  82        '#default_value' => isset($field['filefield_source_attach_mode']) ? $field['filefield_source_attach_mode'] : 'move',
  83      );
  84      $return['source_attach']['tokens'] = array(
  85        '#type' => 'markup',
  86        '#value' => theme('token_help', 'user'),
  87      );
  88    }
  89    elseif ($op == 'save') {
  90      $return[] = 'filefield_source_attach_path';
  91      $return[] = 'filefield_source_attach_absolute';
  92      $return[] = 'filefield_source_attach_mode';
  93    }
  94  
  95    return $return;
  96  }
  97  
  98  function _filefield_source_attach_file_path_validate($element, &$form_state) {
  99    // Strip slashes from the end of the file path.
 100    $filepath = rtrim($element['filefield_source_attach_path']['#value'], '\\/');
 101    form_set_value($element['filefield_source_attach_path'], $filepath, $form_state);
 102  
 103    $filepath = _filefield_source_attach_directory($form_state['values']);
 104  
 105    // Check that the directory exists and is writable.
 106    if (!field_file_check_directory($filepath, FILE_CREATE_DIRECTORY)) {
 107      form_error($element['filefield_source_attach_path'], t('Specified file attach path must exist or be writable.'));
 108    }
 109  }
 110  
 111  /**
 112   * A #process callback to extend the filefield_widget element type.
 113   */
 114  function filefield_source_attach_process($element, $edit, &$form_state, $form) {
 115    $field = content_fields($element['#field_name'], $element['#type_name']);
 116  
 117    $element['filefield_attach'] = array(
 118      '#theme' => 'filefield_source_attach_element',
 119      '#weight' => 100.5,
 120      '#access' => empty($element['fid']['#value']),
 121    );
 122  
 123    $path = _filefield_source_attach_directory($field['widget']);
 124    $options = _filefield_source_attach_options($path);
 125  
 126    $description = t('This method may be used to attach files that exceed the file size limit. Files may be attached from the %directory directory on the server, usually uploaded through FTP.', array('%directory' => realpath($path)));
 127  
 128    // Error messages.
 129    if ($options === FALSE || empty($field['widget']['filefield_source_attach_path'])) {
 130      $attach_message = t('A file attach directory could not be located.');
 131      $attach_description = t('Please check your settings for the %field field.',  array('%field' => $field['widget']['label']));
 132    }
 133    elseif (!count($options)) {
 134      $attach_message = t('There currently no files to attach.');
 135      $attach_description = $description;
 136    }
 137  
 138    if (isset($attach_message)) {
 139      $element['filefield_attach']['attach_message'] = array(
 140        '#value' => $attach_message,
 141      );
 142      $element['filefield_attach']['#description'] = $attach_description;
 143    }
 144    else {
 145      $validators = $element['#upload_validators'];
 146      if (isset($validators['filefield_validate_size'])) {
 147        unset($validators['filefield_validate_size']);
 148      }
 149      $description .= '<br />' . filefield_sources_element_validation_help($validators);
 150      $element['filefield_attach']['filename'] = array(
 151        '#type' => 'select',
 152        '#options' => $options,
 153      );
 154      $element['filefield_attach']['#description'] = $description;
 155    }
 156  
 157    $element['filefield_attach']['attach'] = array(
 158      '#type' => 'submit',
 159      '#value' => $attach_message ? t('Refresh') : t('Attach'),
 160      '#submit' => array('node_form_submit_build_node'),
 161      '#ahah' => array(
 162         'path' => 'filefield/ahah/'. $element['#type_name'] .'/'. $element['#field_name'] .'/'. $element['#delta'],
 163         'wrapper' => $element['#id'] .'-ahah-wrapper',
 164         'method' => 'replace',
 165         'effect' => 'fade',
 166      ),
 167    );
 168  
 169    return $element;
 170  }
 171  
 172  function _filefield_source_attach_options($path) {
 173    if (!field_file_check_directory($path, FILE_CREATE_DIRECTORY)) {
 174      drupal_set_message(t('Specified file attach path must exist or be writable.'), 'error');
 175      return FALSE;
 176    }
 177  
 178    $options = array();
 179    $file_attach = file_scan_directory($path, '.*', array('.', '..', 'CVS', '.svn'), 0, TRUE, 'filename', 0, 0);
 180  
 181    if (count($file_attach)) {
 182      $options = array('' => t('-- Select file --'));
 183      foreach ($file_attach as $filename => $fileinfo) {
 184        $filename = basename($filename);
 185        $options[$filename] = $filename;
 186      }
 187    }
 188  
 189    natcasesort($options);
 190    return $options;
 191  }
 192  
 193  /**
 194   * A #filefield_value_callback function.
 195   */
 196  function filefield_source_attach_value($element, &$item) {
 197    if (!empty($item['filefield_attach']['filename'])) {
 198      $field = content_fields($element['#field_name'], $element['#type_name']);
 199      $attach_path = _filefield_source_attach_directory($field['widget']);
 200      $filepath = $attach_path . '/' . $item['filefield_attach']['filename'];
 201  
 202      // Clean up the file name extensions and transliterate.
 203      $original_filepath = $filepath;
 204      $new_filepath = filefield_sources_clean_filename($filepath);
 205      rename($filepath, $new_filepath);
 206      $filepath = $new_filepath;
 207  
 208      // Run all the normal validations, minus file size restrictions.
 209      $validators = $element['#upload_validators'];
 210      if (isset($validators['filefield_validate_size'])) {
 211        unset($validators['filefield_validate_size']);
 212      }
 213  
 214      // Save the file to the new location.
 215      if ($file = field_file_save_file($filepath, $validators, filefield_widget_file_path($field))) {
 216        $item = array_merge($item, $file);
 217  
 218        // Delete the original file if "moving" the file instead of copying.
 219        if (empty($field['widget']['filefield_source_attach_mode']) || $field['widget']['filefield_source_attach_mode'] !== 'copy') {
 220          @unlink($filepath);
 221        }
 222      }
 223  
 224      // Restore the original file name if the file still exists.
 225      if (file_exists($filepath) && $filepath != $original_filepath) {
 226        rename($filepath, $original_filepath);
 227      }
 228    }
 229  
 230    $item['filefield_attach']['filename'] = '';
 231  }
 232  
 233  /**
 234   * Theme the output of the autocomplete field.
 235   */
 236  function theme_filefield_source_attach_element($element) {
 237    if (isset($element['attach_message'])) {
 238      $output = $element['attach_message']['#value'];
 239    }
 240    else {
 241      $select = '';
 242      $size = $element['#size'] ? ' size="'. $element['filename']['#size'] .'"' : '';
 243      _form_set_class($element['filename'], array('form-select'));
 244      $multiple = $element['#multiple'];
 245      $output = '<select name="'. $element['filename']['#name'] .''. ($multiple ? '[]' : '') .'"'. ($multiple ? ' multiple="multiple" ' : '') . drupal_attributes($element['filename']['#attributes']) .' id="'. $element['filename']['#id'] .'" '. $size .'>'. form_select_options($element['filename']) .'</select>';
 246    }
 247    $output .= theme('submit', $element['attach']);
 248    $element['#type'] = 'item';
 249    return '<div class="filefield-source filefield-source-attach clear-block">' . theme('form_element', $element, $output) . '</div>';
 250  }
 251  
 252  function _filefield_source_attach_directory($field, $account = NULL) {
 253    $account = isset($account) ? $account : $GLOBALS['user'];  
 254    $path = $field['filefield_source_attach_path'];
 255    $absolute = !empty($field['filefield_source_attach_absolute']);
 256  
 257    // Replace user level tokens.
 258    // Node level tokens require a lot of complexity like temporary storage
 259    // locations when values don't exist. See the filefield_paths module.
 260    if (module_exists('token')) {
 261      $path = token_replace($path, 'user', $account);
 262    }
 263  
 264    return $absolute ? $path : file_directory_path() . '/' . $path;
 265  }


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