You are here

function _conflict_get_node_changes in Conflict 7

Helper function. Get differences between two nodes.

1 call to _conflict_get_node_changes()
conflict_node_validate in ./conflict.module
Implements hook_node_validate().

File

./conflict.module, line 173
Fieldwise conflict prevention and resolution. @author Brandon Bergren

Code

function _conflict_get_node_changes($node, $unchanged) {
  $items = array();
  $bundle = field_extract_bundle('node', $node);
  $instances = field_info_instances('node', $bundle);
  $properties = array(
    'title' => array(
      'field_name' => 'title',
      'label' => t('Title'),
    ),
    'created' => array(
      'field_name' => 'date',
      'label' => t('Authored on date'),
    ),
    'uid' => array(
      'field_name' => 'name',
      'label' => t('Authored by'),
    ),
  );
  foreach ($properties as $property => $property_value) {
    $same = $node->{$property} == $unchanged->{$property};
    if (!$same) {
      $items[$property_value['field_name']] = $property_value['label'];
    }
  }
  foreach (field_info_instances('node', $node->type) as $field_name => $instance) {
    $field = field_info_field($field_name);
    $field_language = field_language('node', $node, $field_name);
    $old_value = empty($unchanged->{$field_name}[$field_language]) ? NULL : $unchanged->{$field_name}[$field_language];
    if (!empty($old_value) && $field['type'] == 'taxonomy_term_reference') {

      // Thanks to core being inconsistent, $node->original only contains
      // taxonomy term IDs (tids) for term reference fields, whereas $node
      // itself contains everything (tid, name, description, etc). We need to
      // store the full data as part of the nodechanges field here, since when
      // it comes time to display, if a term has since been removed from the
      // system, we can no longer load it at that point. So, we load
      // everything here to make sure we've got it all for now and forever.
      $tids = array();
      foreach ($old_value as $key => $value) {
        if (empty($value['tid'])) {
          unset($old_value[$key]);
        }
        else {
          $tids[] = $value['tid'];
        }
      }
      if (!empty($tids)) {
        $terms = taxonomy_term_load_multiple($tids);
        foreach ($old_value as $key => $value) {
          $tid = $value['tid'];
          if (!empty($terms[$tid])) {
            $old_value[$key] = (array) $terms[$tid];
          }
        }
      }
    }
    $new_value = empty($node->{$field_name}[$field_language]) ? NULL : $node->{$field_name}[$field_language];
    if (!empty($new_value) && $field['type'] == 'image') {

      // uri is required to show image, see theme_image_formatter(), but does
      // not exist in new value of image field.
      foreach ($new_value as $key => $value) {
        $new_value[$key]['uri'] = file_load($value['fid'])->uri;
      }
    }
    $same = empty($old_value) == empty($new_value);
    if ($same && !empty($old_value)) {
      $old_deltas = array_keys($old_value);
      $new_deltas = array_keys($new_value);
      $same = !array_diff($old_deltas, $new_deltas) && !array_diff($new_deltas, $old_deltas);
      if ($same) {
        $columns = array_keys($field['columns']);
        foreach ($old_value as $delta => $old_items) {
          foreach ($columns as $column) {
            $set = isset($old_items[$column]);
            if ($set != isset($new_value[$delta][$column]) || $set && $old_items[$column] != $new_value[$delta][$column]) {
              $same = FALSE;
              break;
            }
          }
        }
      }
    }
    if (!$same) {
      $items[$field_name] = $instances[$field_name]['label'];
    }
  }
  return $items;
}