[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/cck/includes/ -> content.diff.inc (source)

   1  <?php
   2  // $Id: content.diff.inc,v 1.2.2.7 2010/06/09 01:13:08 karens Exp $
   3  
   4  /**
   5   * @file hook_diff() implementations for CCK (especially fields).
   6   *
   7   * These should use a field-hook so the data for the diff is
   8   * field-type specific.
   9   */
  10  
  11  /**
  12   * Implementation of hook_diff()
  13   */
  14  function content_diff($old_node, $new_node) {
  15    $result = array();
  16    // Prevent against invalid 'nodes' built by broken 3rd party code.
  17    if (isset($new_node->type)) {
  18      $type = content_types($new_node->type);
  19      $field_types = _content_field_types();
  20      foreach ($type['fields'] as $field) {
  21        // Ignore fields the current user is not allowed to view.
  22        if (!content_access('view', $field, NULL, $new_node)) {
  23          continue;
  24        }
  25        $function = $field_types[$field['type']]['module'] . '_content_diff_values';
  26        $function = function_exists($function) ? $function : 'content_content_diff_values';
  27        $old_values = array();
  28        $new_values = array();
  29        if (isset($old_node->$field['field_name'])) {
  30          $old_values = $function($old_node, $field, $old_node->$field['field_name']);
  31        }
  32        if (isset($new_node->$field['field_name'])) {
  33          $new_values = $function($new_node, $field, $new_node->$field['field_name']);
  34        }
  35        if ($old_values || $new_values) {
  36          $result[$field['field_name']] = array(
  37            '#name' => $field['widget']['label'],
  38            '#old' => $old_values,
  39            '#new' => $new_values,
  40            '#weight' => $field['widget']['weight'],
  41            '#format' => array(
  42              'show_header' => FALSE,
  43            ),
  44          );
  45        }
  46      }
  47    }
  48    return $result;
  49  }
  50  
  51  /**
  52   * Default 'implementation' of hook_content_diff_values.
  53   *
  54   * Note that diff.module takes care of running check_plain on the output.
  55   */
  56  function content_content_diff_values($node, $field, $items) {
  57    $return = array();
  58    foreach ($items as $item) {
  59      foreach (explode("\n", $item['value']) as $i) {
  60        $return[] = $i;
  61      }
  62    }
  63    return $return;
  64  }
  65  
  66  if (module_exists('userreference')) {
  67    /**
  68     * Implementation of hook_content_diff_values.
  69     */
  70    function userreference_content_diff_values($node, $field, $items) {
  71      static $titles = array();
  72      // Gather ids.
  73      $ids = array();
  74      foreach ($items as $item) {
  75        if ($item['uid'] && is_numeric($item['uid'])) {
  76          $ids[] = $item['uid'];
  77        }
  78      }
  79      // Fetch titles we don't know yet.
  80      $queried_ids = array_diff($ids, array_keys($titles));
  81      if ($queried_ids) {
  82        $result = db_query('SELECT uid, name FROM {users} WHERE uid IN ('. db_placeholders($queried_ids) .')', $queried_ids);
  83        while ($row = db_fetch_array($result)) {
  84          $titles[$row['uid']] = $row['name'];
  85        }
  86      }
  87      // Return result.
  88      $return = array();
  89      foreach ($items as $item) {
  90        if ($item['uid'] && isset($titles[$item['uid']])) {
  91          $return[] = $titles[$item['uid']];
  92        }
  93      }
  94      return $return;
  95    }
  96  }
  97  
  98  if (module_exists('nodereference')) {
  99    /**
 100     * Implementation of hook_content_diff_values.
 101     */
 102    function nodereference_content_diff_values($node, $field, $items) {
 103      static $titles = array();
 104      // Gather ids.
 105      $ids = array();
 106      foreach ($items as $item) {
 107        if ($item['nid'] && is_numeric($item['nid'])) {
 108          $ids[] = $item['nid'];
 109        }
 110      }
 111      // Fetch titles we don't know yet.
 112      $queried_ids = array_diff($ids, array_keys($titles));
 113      if ($queried_ids) {
 114        $result = db_query('SELECT nid, title FROM {node} WHERE nid IN ('. db_placeholders($queried_ids) .')', $queried_ids);
 115        while ($row = db_fetch_array($result)) {
 116          $titles[$row['nid']] = $row['title'];
 117        }
 118      }
 119      // Return result.
 120      $return = array();
 121      foreach ($items as $item) {
 122        if ($item['nid'] && isset($titles[$item['nid']])) {
 123          $return[] = $titles[$item['nid']];
 124        }
 125      }
 126      return $return;
 127    }
 128  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7