You are here

function image_legacy_field_convert_object_pre_load in Image 7

Implement hook_field_convert_object_pre_load().

Clean up the {file_managed} table entries for the current image node.

File

./image_legacy.field_convert.inc, line 184

Code

function image_legacy_field_convert_object_pre_load($id, $plan) {

  // The incoming object id is a node id.
  $nid = $id;

  // Get all files for this image node.
  $query = db_select('files', 'f');
  $query
    ->join('image', 'i', 'f.fid = i.fid');
  $query
    ->condition('i.nid', $nid, '=')
    ->fields('f');
  $result = $query
    ->execute();
  $files = $result
    ->fetchAllAssoc('fid', PDO::FETCH_ASSOC);

  //dsm($files);

  // Convert entries in {files} to ones in {managed_file}.
  // Bits cribbed from system_update_7061() which REALLY SHOULD BE PROVIDING
  // THAT CODE AS HELPER FUNCTIONS FOR REUSE BY CONTRIB.
  $basename = variable_get('file_directory_path', conf_path() . '/files');
  $scheme = file_default_scheme() . '://';
  foreach ($files as $file) {

    //watchdog('fc', "handling file $file[fid] , $file[filename]");
    if ($file['filename'] == '_original') {

      // Original file: clean it up.
      // Unfudge the filename: Image module has kludgingly stored the image
      // derivative size in this column since time immemorial.
      $file['filename'] = basename($file['filepath']);

      // We will convert filepaths to uri using the default scheme
      // and stripping off the existing file directory path.
      $file['uri'] = $scheme . str_replace($basename, '', $file['filepath']);
      $file['uri'] = file_stream_wrapper_uri_normalize($file['uri']);
      unset($file['filepath']);

      // Insert the file into the {file_managed} table.
      $new_fid = db_insert('file_managed')
        ->fields(array(
        'uid' => $file['uid'],
        'filename' => $file['filename'],
        'uri' => $file['uri'],
        'filemime' => $file['filemime'],
        'filesize' => $file['filesize'],
        'status' => $file['status'],
        'timestamp' => $file['timestamp'],
      ))
        ->execute();

      //dsm($new_fid);

      // Add the usage entry for the file.
      $file = (object) $file;
      file_usage_add($file, 'image', 'node', $nid);

      // Update {image} with the new file ID.
      // NOTE: this is possibly redundant, what with {file_usage} which effectively
      // holds the same information, which we only need for the next step in
      // image_legacy_field_convert_load() anyway.
      $num_updated = db_update('image')
        ->fields(array(
        'fid' => $new_fid,
      ))
        ->condition('nid', $nid)
        ->condition('image_size', '_original')
        ->execute();
    }
    else {

      // Derivative file.
      // Delete it.
      file_unmanaged_delete($file['filepath']);
    }
  }
  return;

  /*
  Note: the above code filters the files by their kludgy filename property.
  Should this prove problematic, the following code get the derivative size
  of each file direct from the image table.
  I'm leaving it here in case it is needed.
  */

  /*
  $query = db_select('file', 'f');
  $query->join('image', 'i', 'f.fid = i.fid');
  $query->condition('i.nid', $id, '=')
    ->fields('f')
    ->fields('i');
  $result = $query->execute();

  $files = $result->fetchAll();

  // Clean up each file.
  foreach ($files as $file) {

    if ($file->image_size == '_original') {
      // Original file: clean it up.
      // Unfudge the filename: Image module has kludgingly stored the image
      // derivative size in this column since time immemorial.
      $file->filename = basename($file->uri);

      // Argh WTF this sets the filesize to 0.
      //file_save($file);

      // Update the filename.
      $query = db_update('file')
        ->fields(array(
          'filename' => $file->filename,
        ))
        ->condition('fid', $file->fid, '=')
        ->execute();

    }
    else {
      // Derivative file.
      // Delete it.
      $file_object
      $query = db_delete('file')
        ->condition('fid', $file->fid, '=')
        ->execute();
    }

    dsm($file);
  }

  return;
  */
}