You are here

function exif_nodeapi in Exif 6

Same name and namespace in other branches
  1. 5 exif.module \exif_nodeapi()

implementation of hook_nodeapi

File

./exif.module, line 45
implementing the drupal api

Code

function exif_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  if ($teaser) {
    return;
  }
  switch ($op) {
    case 'update':

      //we are only going to update if we have said so
      if (!variable_get('exif_update', TRUE)) {
        break;
      }
    case 'insert':
      if (!_exif_check_for_exif_data($node->type)) {
        return;
      }
      $info = content_types($node->type);
      $fields = $info['fields'];
      $exif = _exif_get_class();

      //get all the fields that will be filled with exif data
      $ar_exif_fields = $exif
        ->getExifFields($fields);

      //get the path to the image
      $image_path = _exif_get_image_path($fields, $node);
      $fid = db_result(db_query("SELECT fid FROM {files} WHERE filepath = '%s'", $image_path));
      $file = file_create_path($image_path);
      $data1 = _exif_reformat($exif
        ->readExifTags($file, $ar_exif_fields));
      $data2 = $exif
        ->readIPTCTags($file, $ar_exif_fields);
      if (class_exists('SXMPFiles')) {
        $data3 = $exif
          ->readXMPTags($file, $ar_exif_fields);
        $data = array_merge($data1, $data2, $data3);
      }
      else {
        $data = array_merge($data1, $data2);
      }

      // Loop through every exif enabled field and set its value to the
      // corresponding exif value. If no exif value was found, set the field
      // value to NULL, to avoid strange behaviour in other field modules
      // (date).
      foreach ($ar_exif_fields as $ar_exif_field) {
        $exif_name = $ar_exif_field['section'] . '_' . $ar_exif_field['tag'];
        $exif_value = isset($data[$exif_name]) ? $data[$exif_name] : NULL;
        $field_name = 'field_' . $exif_name;
        if (!$exif_value) {
          if (variable_get('exif_empty_values', TRUE)) {
            $node->{$field_name}[0]['value'] = NULL;
          }
          continue;
        }
        $field = $fields[$field_name];

        // Setup the field value array for delta = 0.
        switch ($exif_name) {
          case 'exif_datetimeoriginal':
          case 'exif_datetimedigitized':
          case 'ifd0_datetime':
            $first_delta = _exif_date_handler($field, $exif_value);
            break;
          default:
            $first_delta = array(
              'value' => $data[$exif_name],
            );
            break;
        }
        $node->{$field_name}[0] = $first_delta;
      }
      break;
  }
}