You are here

function track_field_changes_compare_field in Track Field Changes 7

Same name and namespace in other branches
  1. 8 track_field_changes.module \track_field_changes_compare_field()

Compare two fields.

return FALSE if different return TRUE if egal

1 call to track_field_changes_compare_field()
track_field_changes_node_update in ./track_field_changes.module
Implements hook_node_update().

File

./track_field_changes.module, line 201
The Track Field Changes module.

Code

function track_field_changes_compare_field($field_info_type, $old_field, $new_field) {
  if ($field_info_type == 'title') {
    return $old_field == $new_field;
  }
  else {
    if ($old_field == NULL && empty($new_field)) {
      return TRUE;
    }
    if ($new_field == NULL && empty($old_field)) {
      return TRUE;
    }
    if ($new_field == NULL && $old_field == NULL) {
      return TRUE;
    }
    if (empty($new_field) && empty($old_field)) {
      return TRUE;
    }
    if (count($new_field) != count($old_field)) {
      return FALSE;
    }
    switch ($field_info_type) {
      case 'datestamp':
      case 'datetime':
      case 'date':
        for ($i = 0; $i < count($new_field); $i++) {
          if ($old_field[$i]['value'] != $new_field[$i]['value']) {
            return FALSE;
          }
        }
        return TRUE;
        break;
      case 'tablefield':
      case 'text_long':
      case 'list_text':
      case 'list_boolean':
      case 'text_with_summary':
      case 'number_decimal':
      case 'number_float':
      case 'text':
        for ($i = 0; $i < count($new_field); $i++) {
          if ($old_field[$i]['value'] != $new_field[$i]['value']) {
            return FALSE;
          }
          if (isset($old_field[$i]['value2']) && $old_field[$i]['value2'] != $new_field[$i]['value2']) {
            return FALSE;
          }
        }
        return TRUE;
      case 'email':
        for ($i = 0; $i < count($new_field); $i++) {
          if ($old_field[$i]['email'] != $new_field[$i]['email']) {
            return FALSE;
          }
        }
        return TRUE;
      case 'link_field':
        for ($i = 0; $i < count($new_field); $i++) {
          if (empty($old_field) || empty($new_field)) {
            return FALSE;

            //if they are both empty, that case is handled at the beginning of this function
          }

          // not all link fields have a title, i.e. static title
          if (array_key_exists('title', $old_field[$i]) && array_key_exists('title', $new_field[$i])) {
            if ($old_field[$i]['title'] != $new_field[$i]['title']) {
              return FALSE;
            }
          }
        }
        for ($i = 0; $i < count($new_field); $i++) {
          if ($old_field[$i]['url'] != $new_field[$i]['url']) {
            return FALSE;
          }
        }
        return TRUE;
      case 'image':
      case 'file':
        for ($i = 0; $i < count($new_field); $i++) {
          if ($old_field[$i]['fid'] != $new_field[$i]['fid']) {
            return FALSE;
          }
        }
        return TRUE;
      case 'geofield':
        for ($i = 0; $i < count($new_field); $i++) {
          if ($old_field[$i]['wkt'] != $new_field[$i]['wkt']) {
            return FALSE;
          }
        }
        return TRUE;
      case 'entityreference':
        for ($i = 0; $i < count($new_field); $i++) {
          if ($old_field[$i]['target_id'] != $new_field[$i]['target_id']) {
            return FALSE;
          }
        }
        return TRUE;
      case 'taxonomy_term_reference':
        if (sizeof($old_field) != sizeof($new_field)) {
          return FALSE;
        }
        foreach ($old_field as $key => $value) {
          if ($old_field[$key]['tid'] != $new_field[$key]['tid']) {
            return FALSE;
          }
        }
        return TRUE;
      case 'list_integer':
      case 'list_float':
      case 'number_integer':
      case 'user_reference':
        return $old_field == $new_field;
      case 'addressfield':
        for ($i = 0; $i < count($new_field); $i++) {

          //do a compare of each part of address field

          // first get a list of the address parts
          $address_items = array_keys($new_field[$i]);

          // use $new_field in case something has change in the address definition
          foreach ($address_items as $address_item) {
            if ($address_item != 'data') {
              if (empty($new_field[$i][$address_item])) {
                if (array_key_exists($address_item, $old_field[$i])) {
                  if (!empty($old_field[$i][$address_item])) {
                    return FALSE;
                  }
                }
                else {
                  return FALSE;
                }
              }
              else {
                if (empty($old_field[$i][$address_item])) {
                  return FALSE;
                }
                else {
                  if ($old_field[$i][$address_item] != $new_field[$i][$address_item]) {
                    return FALSE;
                  }
                }
              }
            }
          }
        }

        // if we made it this far, no changes
        return TRUE;
      default:
        return TRUE;
    }
  }
}