You are here

function track_field_changes_node_update in Track Field Changes 7

Implements hook_node_update().

File

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

Code

function track_field_changes_node_update($node) {

  // Each node type which are audited.
  $node_types = variable_get('track_field_changes_node_types', array());

  // If content type is audited.
  if (in_array($node->type, $node_types)) {

    // Basic or/and Advanced audit.
    $basic_audit = variable_get('track_field_changes_basic_revision_' . $node->type, FALSE);
    $fields_audit = variable_get('track_field_changes_track_revision_fields_' . $node->type, FALSE);

    // Get log.
    $log = isset($node->track_field_changes) ? $node->track_field_changes : '';
    if ($basic_audit) {
      track_field_changes_insert_db($node->nid, 'log', '', '', 'br', $log);
    }
    if ($fields_audit) {
      $selected_fields = track_field_changes_get_selected_field($node->type);

      // Clone ?
      $newnode = $node;
      $originalnode = $node->original;
      foreach ($selected_fields as $field_name) {
        if ($field_name == 'title') {
          $field_info_type = 'title';
          $newfield = $newnode->title;
          $originalfield = $originalnode->title;
        }
        else {
          $field_info = field_info_field($field_name);
          $field_info_type = $field_info['type'];
          $language = field_language('node', $newnode, $field_name);
          $newfield = field_get_items('node', $newnode, $field_name, $language);
          $language = field_language('node', $originalnode, $field_name);
          $originalfield = field_get_items('node', $originalnode, $field_name, $language);
        }
        if (!track_field_changes_compare_field($field_info_type, $originalfield, $newfield)) {
          $field_before = array();
          $field_before['field_name'] = $field_name;
          $field_before['value'] = $originalfield;
          $field_after = array();
          $field_after['field_name'] = $field_name;
          $field_after['value'] = $newfield;
          if ($field_info_type == 'file') {
            for ($i = 0; $i < count($field_after['value'][LANGUAGE_NONE]); $i++) {
              $file = file_load($field_after['value'][LANGUAGE_NONE][$i][fid]);
              $field_after['value'][LANGUAGE_NONE][$i]['filename'] = $file->filename;
              $field_after['value'][LANGUAGE_NONE][$i]['uri'] = $file->uri;
              $field_after['value'][LANGUAGE_NONE][$i]['filemime'] = $file->filemime;
              $field_after['value'][LANGUAGE_NONE][$i]['filesize'] = $file->filesize;
              $field_after['value'][LANGUAGE_NONE][$i]['status'] = $file->status;
              $field_after['value'][LANGUAGE_NONE][$i]['timestamp'] = $file->timestamp;
            }
          }
          track_field_changes_insert_db($node->nid, $field_name, serialize($field_before), serialize($field_after), 'fr', $log);
        }
      }
    }
  }
}