[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/filefield/ -> filefield_field.inc (source)

   1  <?php
   2  // $Id: filefield_field.inc,v 1.45 2010/12/08 03:31:41 quicksketch Exp $
   3  
   4  /**
   5   * @file
   6   * FileField CCK field hooks and callbacks.
   7   */
   8  
   9  /**
  10   * Implementation of CCK's hook_field_settings($op = 'form').
  11   */
  12  function filefield_field_settings_form($field) {
  13    drupal_add_js(drupal_get_path('module', 'filefield') .'/filefield.js');
  14  
  15    $form = array();
  16  
  17    $form['list_field'] = array(
  18      '#type' => 'radios',
  19      '#title' => t('List field'),
  20      '#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
  21      '#default_value' => $field['list_field'] === '' ? 0 : (int) $field['list_field'],
  22      '#description' => t('Display a checkbox where users may choose if a file should be shown when viewing the content. Most formatters other than <em>Generic file</em> do not support this option.'),
  23      '#attributes' => array('class' => 'filefield-list-field'),
  24    );
  25    $form['list_default'] = array(
  26      '#type' => 'checkbox',
  27      '#title' => t('Files listed by default'),
  28      '#default_value' => $field['list_default'] === '' ? 1 : (int) $field['list_default'],
  29    );
  30    $form['description_field'] = array(
  31      '#type' => 'radios',
  32      '#title' => t('Description field'),
  33      '#default_value' => $field['description_field'] === '' ? 0 : (int) $field['description_field'],
  34      '#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
  35      '#description' => t('Display a text field where users may enter a description about the uploaded file. The description text is used when using the <em>Generic file</em> formatter to link to the file, the file name will be used otherwise. Most other formatters do not use the description field on output.'),
  36    );
  37  
  38    return $form;
  39  }
  40  
  41  /**
  42   * Implementation of CCK's hook_field_settings($op = 'save').
  43   */
  44  function filefield_field_settings_save($field) {
  45    return array('list_field', 'list_default', 'description_field');
  46  }
  47  
  48  /**
  49   * Implementation of CCK's hook_field_settings($op = 'database_columns').
  50   */
  51  function filefield_field_settings_database_columns($field) {
  52    return array(
  53      'fid' => array('type' => 'int', 'not null' => FALSE, 'views' => TRUE),
  54      'list' => array('type' => 'int', 'size' => 'tiny', 'not null' => FALSE, 'views' => TRUE),
  55      'data' => array('type' => 'text', 'serialize' => TRUE, 'views' => TRUE),
  56    );
  57  }
  58  
  59  /**
  60   * Implementation of CCK's hook_field_settings($op = 'views_data').
  61   */
  62  function filefield_field_settings_views_data($field) {
  63    $data = content_views_field_views_data($field);
  64    $db_info = content_database_info($field);
  65    $table_alias = content_views_tablename($field);
  66  
  67    // By defining the relationship, we already have a "Has file" filter
  68    // plus all the filters that Views already provides for files.
  69    // No need for having a filter by ourselves.
  70    unset($data[$table_alias][$field['field_name'] .'_fid']['filter']);
  71  
  72    // Add a relationship for related file.
  73    $data[$table_alias][$field['field_name'] .'_fid']['relationship'] = array(
  74      'base' => 'files',
  75      'field' => $db_info['columns']['fid']['column'],
  76      'handler' => 'content_handler_relationship',
  77      'label' => t($field['widget']['label']),
  78      'content_field_name' => $field['field_name'],
  79      'skip base' => array('files'),
  80    );
  81  
  82    // Use the Views boolean handler for filtering the list value.
  83    $data[$table_alias][$field['field_name'] .'_list']['filter']['handler'] = 'views_handler_filter_boolean_operator';
  84  
  85    // Add our special handler for serialized data.
  86    $data[$table_alias][$field['field_name'] .'_data']['field'] = array(
  87      'title' => $data[$table_alias][$field['field_name'] .'_data']['title'],
  88      'title short' => $data[$table_alias][$field['field_name'] .'_data']['title short'],
  89      'help' => t('Can be used to display specific alt, title, or description information. May cause duplicate rows if grouping fields.'),
  90      'field' => $db_info['columns']['data']['column'],
  91      'table' => $db_info['table'],
  92      'handler' => 'filefield_handler_field_data',
  93      'click sortable' => FALSE,
  94      'access callback' => 'content_access',
  95      'access arguments' => array('view', $field),
  96    );
  97  
  98    // Remove handlers that are probably unnecessary.
  99    unset($data[$table_alias][$field['field_name'] .'_data']['filter']);
 100    unset($data[$table_alias][$field['field_name'] .'_data']['argument']);
 101    unset($data[$table_alias][$field['field_name'] .'_list']['argument']);
 102  
 103    // Set up relationship with file views.
 104    $data[$table_alias]['table']['join']['files'] = array(
 105      'table' => $db_info['table'],
 106      'left_field' => 'fid',
 107      'field' => $db_info['columns']['fid']['column'],
 108    );
 109    $data[$table_alias]['vid'] = array(
 110      'title' => t($field['widget']['label']),
 111      'help' => t('The node the uploaded file is attached to'),
 112      'relationship' => array(
 113        'label' => t($field['widget']['label']),
 114        'base' => 'node',
 115        'base field' => 'vid',
 116        // This allows us to not show this relationship if the base is already
 117        // node so users won't create circular relationships.
 118        'skip base' => array('node', 'node_revisions'),
 119      ),
 120    );
 121  
 122    return $data;
 123  }
 124  
 125  /**
 126   * Implementation of CCK's hook_field($op = 'load').
 127   */
 128  function filefield_field_load($node, $field, &$items, $teaser, $page) {
 129    if (empty($items)) {
 130      return array();
 131    }
 132    foreach ($items as $delta => $item) {
 133      // Despite hook_content_is_empty(), CCK still doesn't filter out
 134      // empty items from $op = 'load', so we need to do that ourselves.
 135      if (empty($item['fid']) || !($file = field_file_load($item['fid']))) {
 136        $items[$delta] = NULL;
 137      }
 138      else {
 139        $item['data'] = unserialize($item['data']);
 140        // Temporary fix to unserialize data serialized multiple times.
 141        // See the FileField issue http://drupal.org/node/402860.
 142        // And the CCK issue http://drupal.org/node/407446.
 143        while (!empty($item['data']) && is_string($item['data'])) {
 144          $item['data'] = unserialize($item['data']);
 145        }
 146        // Merge any data added by modules in hook_file_load().
 147        if (isset($file['data']) && isset($item['data'])) {
 148          $file['data'] = array_merge((array) $file['data'], (array) $item['data']);
 149        }
 150        $items[$delta] = array_merge($file, $item);
 151      }
 152    }
 153  
 154    return array($field['field_name'] => $items);
 155  }
 156  
 157  /**
 158   * Implementation of CCK's hook_field($op = 'insert').
 159   */
 160  function filefield_field_insert($node, $field, &$items, $teaser, $page) {
 161    return filefield_field_update($node, $field, $items, $teaser, $page);
 162  }
 163  
 164  /**
 165   * Implementation of CCK's hook_field($op = 'update').
 166   */
 167  function filefield_field_update($node, $field, &$items, $teaser, $page) {
 168  
 169    // Accumulator to gather current fid to compare with the original node
 170    // for deleting replaced files.
 171    $curfids = array();
 172    foreach ($items as $delta => $item) {
 173      $items[$delta] = field_file_save($node, $item);
 174      // Remove items from the array if they have been deleted.
 175      if (empty($items[$delta]) || empty($items[$delta]['fid'])) {
 176        $items[$delta] = NULL;
 177      }
 178      else {
 179        $curfids[] = $items[$delta]['fid'];
 180      }
 181    }
 182  
 183    // If this is a new node there are no old items to worry about.
 184    // On new revisions, old files are always maintained in the previous revision.
 185    if ($node->is_new || !empty($node->revision) || !empty($node->skip_filefield_delete)) {
 186      return;
 187    } 
 188  
 189    // Delete items from original node.
 190    $orig = node_load($node->nid); 
 191    // If there are, figure out which ones must go.
 192    if (!empty($orig->$field['field_name'])) {
 193      foreach ($orig->$field['field_name'] as $oitem) {
 194        if (isset($oitem['fid']) && !in_array($oitem['fid'], $curfids)) {
 195          // For hook_file_references, remember that this is being deleted.
 196          $oitem['field_name'] = $field['field_name'];
 197          $oitem['delete_vid'] = $orig->vid;
 198          filefield_field_delete_file($oitem, $field); 
 199        }
 200      }
 201    }
 202  }
 203  
 204  /**
 205   * Implementation of CCK's hook_field($op = 'delete_revision').
 206   */
 207  function filefield_field_delete_revision($node, $field, &$items, $teaser, $page) {
 208    foreach ($items as $delta => $item) {
 209      // For hook_file_references, remember that this is being deleted.
 210      $item['field_name'] = $field['field_name'];
 211      $item['delete_vid'] = $node->vid;
 212      if (filefield_field_delete_file($item, $field)) {
 213        $items[$delta] = NULL;
 214      }
 215    }
 216  }
 217  
 218  /**
 219   * Implementation of CCK's hook_field($op = 'delete').
 220   */
 221  function filefield_field_delete($node, $field, &$items, $teaser, $page) {
 222    foreach ($items as $delta => $item) {
 223      // For hook_file_references(), remember that this is being deleted.
 224      $item['field_name'] = $field['field_name'];
 225      // Pass in the nid of the node that is being removed so all references can
 226      // be counted in hook_file_references().
 227      $item['delete_nid'] = $node->nid;
 228      filefield_field_delete_file($item, $field);
 229    }
 230  
 231    // Delete all the remaining items present only in older revisions.
 232    $db_info = content_database_info($field);
 233    $result = db_query('SELECT vid, f.* FROM {' . $db_info['table'] . '} t INNER JOIN {files} f ON t.' . $db_info['columns']['fid']['column'] . ' = f.fid WHERE nid = %d AND vid != %d', $node->nid, $node->vid);
 234    while ($item = db_fetch_array($result)) {
 235      if (isset($item['fid'])) {
 236        $item['field_name'] = $field['field_name'];
 237        $item['delete_vid'] = $item['vid'];
 238        filefield_field_delete_file($item, $field); 
 239      }
 240    }
 241  }
 242  
 243  /**
 244   * Check that FileField controls a file before attempting to deleting it.
 245   */
 246  function filefield_field_delete_file($file, $field) {
 247    $file = (object) $file;
 248  
 249    // Remove the field_name and delete_nid properties so that references can be
 250    // counted including the files to be deleted.
 251    $field_name = isset($file->field_name) ? $file->field_name : NULL;
 252    $delete_nid = isset($file->delete_nid) ? $file->delete_nid : NULL;
 253    unset($file->field_name, $file->delete_nid);
 254  
 255    // To prevent FileField from deleting files it doesn't know about, check the
 256    // FileField reference count. Temporary files can be deleted because they
 257    // are not yet associated with any content at all.
 258    if ($file->status == 0 || filefield_get_file_reference_count($file, $field) > 0) {
 259      $file->field_name = $field_name;
 260      $file->delete_nid = $delete_nid;
 261      return field_file_delete($file);
 262    }
 263  
 264    // Even if the file is not deleted, return TRUE to indicate the FileField
 265    // record can be removed from the FileField database tables.
 266    return TRUE;
 267  }
 268  
 269  /**
 270   * Implementation of CCK's hook_field($op = 'sanitize').
 271   */
 272  function filefield_field_sanitize($node, $field, &$items, $teaser, $page) {
 273    foreach ($items as $delta => $item) {
 274      // Cleanup $items during node preview.
 275      if (empty($item['fid']) || !empty($item['delete'])) {
 276        // Check for default images at the widget level.
 277        // TODO: Provide an API to ImageField to do this itself?
 278        if (!empty($field['widget']['use_default_image']) && !empty($field['widget']['default_image']['filepath']) && $delta == 0) {
 279          $items[$delta] = $field['widget']['default_image'];
 280          $items[$delta]['default'] = TRUE;
 281        }
 282        else {
 283          $items[$delta] = NULL;
 284          continue;
 285        }
 286      }
 287  
 288      // Add nid so formatters can create a link to the node.
 289      $items[$delta]['nid'] = $node->nid;
 290  
 291      // Get the 'data' column stored by CCK into an array. This is necessary
 292      // for Views, which doesn't call the "load" $op and to fix an issue with
 293      // CCK double-serializing data.
 294      // See the FileField issue http://drupal.org/node/402860.
 295      // And the CCK issue http://drupal.org/node/407446.
 296      while (!empty($items[$delta]['data']) && is_string($items[$delta]['data'])) {
 297        $items[$delta]['data'] = unserialize($items[$delta]['data']);
 298      }
 299  
 300      // Load the complete file if a filepath is not available.
 301      if (!empty($item['fid']) && empty($item['filepath'])) {
 302        $file = (array) field_file_load($item['fid']);
 303        if (isset($file['data'])) {
 304          $file['data'] = array_merge($file['data'], $items[$delta]['data']);
 305        }
 306        $items[$delta] = array_merge($file, $items[$delta]);
 307      }
 308  
 309      // Verify the file exists on the server.
 310      if (!empty($item['filepath']) && !file_exists($item['filepath'])) {
 311        watchdog('filefield', 'FileField was trying to display the file %file, but it does not exist.', array('%file' => $item['filepath']), WATCHDOG_WARNING);
 312      }
 313    }
 314  }


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