You are here

function imagefield_crop_widget in Imagefield Crop 5

Same name and namespace in other branches
  1. 6 imagefield_crop.module \imagefield_crop_widget()

Implementation of hook_widget().

File

./imagefield_crop.module, line 167

Code

function imagefield_crop_widget($op, &$node, $field, &$node_field) {
  switch ($op) {
    case 'default value':
      return array();
    case 'prepare form values':
      _imagefield_crop_widget_prepare_form_values($node, $field, $node_field);
      break;
    case 'form':
      return _imagefield_crop_widget_form($node, $field, $node_field);
      break;
    case 'validate':
      if ($field['required']) {
        if (!count($node_field)) {
          form_set_error($field['field_name'], $field['widget']['label'] . ' is required.');
        }
      }
      break;
    case 'submit':

      /**
       * move cropped file into the file's place
       */
      foreach ($node_field as $delta => $file) {
        $cropped = $file['crop']['filepath'];
        if (is_file($cropped)) {
          if (!file_copy($cropped, $file['filepath'], FILE_EXISTS_REPLACE)) {
            form_set_error(NULL, 'Could not copy cropped file');
          }
          file_delete($file['crop']['filepath']);

          //flush the imagecache presets
          if (module_exists('imagecache')) {
            imagecache_image_flush($file['filepath']);
          }
        }
        file_delete($file['filepath'] . '.unscaled');
      }
      break;
  }
}