You are here

function imagefield_crop_field_update in Imagefield Crop 7.2

Same name and namespace in other branches
  1. 7.3 imagefield_crop.module \imagefield_crop_field_update()

Implements hook_field_update().

File

./imagefield_crop.module, line 248
Functionality and Drupal hook implementations for the Imagefield Crop module.

Code

function imagefield_crop_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);

  // On new revisions, all files are considered to be a new usage and no
  // deletion of previous file usages are necessary.
  $scale = NULL;
  if ($instance['widget']['settings']['resolution']) {
    $context = array(
      'entity' => $entity,
    );
    drupal_alter('imagefield_crop_instance', $instance, $context);
    list($scale['width'], $scale['height']) = explode('x', $instance['widget']['settings']['resolution']);
  }

  // Create a bare-bones entity so that we can load its previous values.
  $original = entity_create_stub_entity($entity_type, array(
    $id,
    $vid,
    $bundle,
  ));
  field_attach_load($entity_type, array(
    $id => $original,
  ), FIELD_LOAD_CURRENT, array(
    'field_id' => $field['id'],
  ));
  $original_items = array();
  if (!empty($original->{$field['field_name']}[$langcode])) {
    foreach ($original->{$field['field_name']}[$langcode] as $original_item) {
      $original_items[$original_item['fid']] = $original_item;
    }
  }
  if (!empty($entity->revision)) {
    foreach ($items as &$item) {
      $file = file_load($item['fid']);

      // if the file isn't gif or it can be converted, then we can perform further file processing.
      $can_resize = $file && ($file->filemime != 'image/gif' || $instance['widget']['settings']['gif_processing'] == 'convert');
      if (isset($item['cropbox_changed']) && $item['cropbox_changed']) {

        // If fid isn't in the original id's, saved in the previous revision,
        // then its new file, and we need to process it.
        if (!array_key_exists($item['fid'], $original_items)) {

          // Check whether image can be resized and cropped.
          if ($can_resize) {
            $new_source_file = imagefield_crop_create_copy($file);
            file_usage_add($new_source_file, 'imagefield_crop', 'file', $file->fid);
            _imagefield_crop_resize(drupal_realpath($new_source_file->uri), $item, $scale, $file);
            $file->uri = file_unmanaged_copy($file->uri, $file->uri);
            file_save($file);
          }
          file_usage_add($file, 'file', $entity_type, $id);
        }
        else {

          // Check whether image can be resized and cropped. If not, just add usage.
          if ($can_resize) {
            $file_to_crop = _imagefield_crop_file_to_crop($file->fid);
            $new_file = imagefield_crop_create_copy($file);
            _imagefield_crop_resize(drupal_realpath($file_to_crop->uri), $item, $scale, $new_file);
            file_usage_add($file_to_crop, 'imagefield_crop', 'file', $new_file->fid);
            file_usage_add($new_file, 'file', $entity_type, $id);
            $new_file->uri = file_unmanaged_copy($new_file->uri, $new_file->uri);
            file_save($new_file);
            $item['fid'] = $new_file->fid;
          }
          else {
            file_usage_add($file, 'file', $entity_type, $id);
          }
        }
      }
      else {

        // Check whether image can be resized and cropped.
        if ($can_resize) {
          $file_to_crop = _imagefield_crop_file_to_crop($file->fid);
          file_usage_add($file_to_crop, 'imagefield_crop', 'file', $file->fid);
        }
        file_usage_add($file, 'file', $entity_type, $id);
      }
      image_path_flush($file->uri);
    }
    return;
  }
  $current_fids = array();
  foreach ($items as $item) {
    $current_fids[] = $item['fid'];

    // Update files if cropping area was changed or new file has been uploaded.
    if (isset($item['cropbox_changed']) && $item['cropbox_changed']) {
      $file = file_load($item['fid']);
      $can_resize = $file && ($file->filemime != 'image/gif' || $instance['widget']['settings']['gif_processing'] == 'convert');

      // Process new files.
      if (!array_key_exists($item['fid'], $original_items)) {

        // Check whether image can be resized and cropped.
        if ($can_resize) {
          $new_source_file = imagefield_crop_create_copy($file);
          file_usage_add($new_source_file, 'imagefield_crop', 'file', $file->fid);
          _imagefield_crop_resize(drupal_realpath($new_source_file->uri), $item, $scale, $file);
          $file->uri = file_unmanaged_copy($file->uri, $file->uri);
          file_save($file);
        }
        file_usage_add($file, 'file', $entity_type, $id);
      }
      else {

        // Check whether image can be resized and cropped.
        if ($can_resize) {
          $file_to_crop = _imagefield_crop_file_to_crop($file->fid);
          _imagefield_crop_resize(drupal_realpath($file_to_crop->uri), $item, $scale, $file);
        }
        $file->uri = file_unmanaged_copy($file->uri, $file->uri);
        file_save($file);
      }
      image_path_flush($file->uri);
    }
  }

  // Avoid wrong deletion due the mechanics of module workbench_moderation.
  if (empty($entity->workbench_moderation['updating_live_revision'])) {

    // Delete old files if any.
    foreach ($original_items as $orig_fid => $original_item) {
      if (!in_array($orig_fid, $current_fids)) {
        if (!($source_file = @_imagefield_crop_file_to_crop($original_item['fid']))) {

          // The file was deleted, no need to delete the file.
          continue;
        }

        // Decrement the file usage count by 1 and delete the file if possible.
        file_field_delete_file($original_item, $field, $entity_type, $id);
        if ($source_file) {
          file_usage_delete($source_file, 'imagefield_crop', 'file', $original_item['fid']);
          file_delete($source_file);
        }
      }
    }
  }
}