You are here

function imagefield_crop_field_update in Imagefield Crop 7.3

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

Implements hook_field_update().

File

./imagefield_crop.module, line 158

Code

function imagefield_crop_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  $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;
    }
  }
  $entity_w = entity_metadata_wrapper($entity_type, $entity);
  file_field_update($entity_type, $entity, $field, $instance, $langcode, $items);
  $presets = imagefield_crop_presets_load_multiple_by_name(array_keys($instance['widget']['settings']['presets']));
  $scale = NULL;
  $key = $entity_w
    ->entityKey('revision') ? $entity_w
    ->entityKey('revision') : $entity_w
    ->entityKey('id');
  foreach ($items as $delta => &$item) {
    $variants = imagefield_crop_variants_load_multiple($item['fid'], $vid);
    foreach ($item['crop_config'] as $pid => $preset) {
      $variant = !empty($variants[$pid]) ? $variants[$pid] : '';
      if (!is_object($preset)) {
        $preset = json_decode($preset);
        $first = reset(array_keys($item['crop_config']));
        $item['crop_config'][$first]->active = TRUE;
      }
      if (!empty($preset->cropbox_changed) || !empty($preset->new)) {
        $scale = $presets[$pid]->data['crop-type'] == 'resolution' ? $presets[$pid]->data['resolution'] : NULL;
        if (!in_array($item['fid'], array_keys($original_items)) || !empty($preset->new) || empty($variant)) {
          $src = file_load($item['fid']);
          $orig = imagefield_crop_create_copy($src);
          file_usage_add($orig, 'imagefield_crop', 'file', $src->fid);
          _imagefield_crop_resize(drupal_realpath($orig->uri), $preset, $scale, $orig);
          file_save($src);
        }
        else {
          $src = file_load($item['fid']);
          $orig = file_load($variant->fid);
          list($name, $ext) = explode('.', drupal_basename($orig->uri));
          $orig->uri = file_build_uri($name . '_' . time() . '.' . $ext);
          file_delete($orig);
          _imagefield_crop_resize(drupal_realpath($src->uri), $preset, $scale, $orig);
          file_save($src);
        }
        unset($preset->cropbox_changed);
        $preset->uri = $orig->uri;
        $record['fid'] = $orig->fid;
        $preset->updated = time();
      }
      $record['pid'] = $pid;
      $record['source_fid'] = $item['fid'];
      $record['vid'] = $entity->{$key};
      if (!empty($preset->uri)) {
        $record['data'] = $preset;
      }
      else {

        // If no any changes was made to the image -  copy old imagecrop data from loaded variant or if
        // data has pre populated by drupal and variant don't exist copy data from image crop preset
        $record['data'] = !empty($variant) ? $variant->data : $preset->data;
      }
      if (!empty($preset->new) || empty($variant)) {
        unset($record['data']->new);
        drupal_write_record('imagefield_crop_variants', $record);
      }
      elseif (!empty($variant)) {
        drupal_write_record('imagefield_crop_variants', $record, array(
          'source_fid',
          'vid',
          'pid',
        ));
      }
      $record = array();
    }
  }
}