Projet

Général

Profil

Paste
Télécharger (12,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / diff / diff.diff.inc @ 661d64c9

1
<?php
2

    
3
/**
4
 * @file
5
 * Includes the hooks defined by diff_hook_info().
6
 */
7

    
8
/**
9
 * Implements hook_entity_diff().
10
 *
11
 * Helper function to invoke the depreciated hook_diff() for node entities.
12
 *
13
 * This manually invokes hook_diff() to avoid a function name clash with the
14
 * PHP 5 (>= 5.3.0) date_diff() function or the Dates modules implementation.
15
 */
16
function diff_entity_diff($old_entity, $new_entity, $context) {
17
  $return = array();
18

    
19
  $entity_type = $context['entity_type'];
20
  $info = entity_get_info($entity_type);
21
  if (!empty($info['fieldable'])) {
22
    $return = diff_entity_fields_diff($old_entity, $new_entity, $context);
23
  }
24

    
25
  return $return;
26
}
27

    
28
/**
29
 * Internal callback to handle fieldable entities.
30
 *
31
 * Field comparison is handled for core modules, but is expandable to any other
32
 * fields if the module defines MODULE_field_diff_view().
33
 *
34
 * @param object $old_entity
35
 *   The older entity entity revision.
36
 * @param object $new_entity
37
 *   The newer entity entity revision.
38
 * @param array $context
39
 *   An associative array containing:
40
 *   - entity_type: The entity type; e.g., 'node' or 'user'.
41
 *   - old_entity: The older entity.
42
 *   - new_entity: The newer entity.
43
 *   - view_mode: The view mode to use. Defaults to FALSE.
44
 * @param string $default_langcode
45
 *   (optional) Language code to force comparison in.
46
 *
47
 * @return array
48
 *   An associative array of values keyed by the field name and delta value.
49
 */
50
function diff_entity_fields_diff($old_entity, $new_entity, $context, $default_langcode = NULL) {
51
  $result = array();
52

    
53
  $entity_type = $context['entity_type'];
54
  $view_mode = $context['view_mode'];
55

    
56
  $field_context = $context;
57

    
58
  $actual_mode = FALSE;
59
  list(, , $bundle_name) = entity_extract_ids($entity_type, $new_entity);
60
  $instances = field_info_instances($entity_type, $bundle_name);
61

    
62
  // Some fields piggy back the display settings, so we need to fake these by
63
  // ensuring that the field mode is always set.
64
  if (empty($view_mode)) {
65
    $actual_mode = 'diff_standard';
66
    $field_context['custom_settings'] = FALSE;
67
  }
68
  $view_mode_settings = field_view_mode_settings($entity_type, $bundle_name);
69
  $actual_mode = (!empty($view_mode_settings[$view_mode]['custom_settings'])) ? $view_mode : 'default';
70
  if (!isset($field_context['custom_settings'])) {
71
    $field_context['custom_settings'] = $actual_mode && $actual_mode == $view_mode;
72
  }
73

    
74
  $field_context['old_entity'] = $old_entity;
75
  $field_context['new_entity'] = $new_entity;
76
  $field_context['bundle_name'] = $bundle_name;
77

    
78
  foreach ($instances as $instance) {
79
    // Any view mode is supported in relation to hiding fields, but only if
80
    // selected (todo see if this is a valid option).
81
    if ($actual_mode && $instance['display'][$actual_mode]['type'] == 'hidden') {
82
      continue;
83
    }
84
    $field_name = $instance['field_name'];
85
    $field = field_info_field($field_name);
86
    $field_context['field'] = $field;
87
    $field_context['instance'] = $instance;
88
    $field_context['display'] = $instance['display'][$actual_mode];
89

    
90
    // We provide a loose check on the field access.
91
    if (field_access('view', $field, $entity_type) || field_access('edit', $field, $entity_type)) {
92
      $langcode = $default_langcode ? $default_langcode : field_language($entity_type, $new_entity, $field_name);
93

    
94
      $field_context['language'] = $langcode;
95
      $field_context['field'] = $field;
96
      $field_context['instance'] = $instance;
97

    
98
      $old_items = array();
99
      if (!empty($old_entity->{$field_name}[$langcode])) {
100
        $old_items = $old_entity->{$field_name}[$langcode];
101
      }
102

    
103
      $new_items = array();
104
      if (!empty($new_entity->{$field_name}[$langcode])) {
105
        $new_items = $new_entity->{$field_name}[$langcode];
106
      }
107

    
108
      // Load files containing the field callbacks.
109
      _diff_autoload($field);
110

    
111
      $field_context['settings'] = diff_get_field_settings($field_context);
112

    
113
      // Reference fields can optionally prepare objects in bulk to reduce
114
      // overheads related to multiple database calls. If a field considers
115
      // that the delta values is meaningless, they can order and rearrange
116
      // to provide cleaner results.
117
      $func = $field['module'] . '_field_diff_view_prepare';
118
      if (function_exists($func)) {
119
        $func($old_items, $new_items, $field_context);
120
      }
121
      // Allow other modules to act safely on behalf of the core field module.
122
      drupal_alter('field_diff_view_prepare', $old_items, $new_items, $field_context);
123

    
124
      // These functions compiles the items into comparable arrays of strings.
125
      $func = $field['module'] . '_field_diff_view';
126
      if (!function_exists($func)) {
127
        $func = 'diff_field_diff_view';
128
      }
129

    
130
      // Copy the static ID cache to ensure this is the same for each comparison.
131
      $original_html_ids = drupal_static('drupal_html_id');
132
      $html_ids = &drupal_static('drupal_html_id');
133

    
134
      // These callbacks should be independent of revision.
135
      $old_context = $field_context;
136
      $old_context['entity'] = $old_entity;
137
      $old_values = $func($old_items, $old_context);
138

    
139
      // Restores the ID cache to the original.
140
      $html_ids = $original_html_ids;
141

    
142
      $new_context = $field_context;
143
      $new_context['entity'] = $new_entity;
144
      $new_values = $func($new_items, $new_context);
145

    
146
      // Allow other modules to act safely on behalf of the core field module.
147
      drupal_alter('field_diff_view', $old_values, $old_items, $old_context);
148
      drupal_alter('field_diff_view', $new_values, $new_items, $new_context);
149

    
150
      $max = max(array(count($old_values), count($new_values)));
151
      if ($max) {
152
        $result[$field_name] = array(
153
          '#name' => $instance['label'],
154
          '#old' => array(),
155
          '#new' => array(),
156
          '#settings' => $field_context['settings'],
157
        );
158
        for ($delta = 0; $delta < $max; $delta++) {
159
          if (isset($old_values[$delta])) {
160
            $result[$field_name]['#old'][] = is_array($old_values[$delta]) ? implode("\n", $old_values[$delta]) : $old_values[$delta];
161
          }
162
          if (isset($new_values[$delta])) {
163
            $result[$field_name]['#new'][] = is_array($new_values[$delta]) ? implode("\n", $new_values[$delta]) : $new_values[$delta];
164
          }
165
        }
166
        $result[$field_name]['#old'] = implode("\n", $result[$field_name]['#old']);
167
        $result[$field_name]['#new'] = implode("\n", $result[$field_name]['#new']);
168

    
169
        if ($actual_mode) {
170
          $result[$field_name]['#weight'] = $instance['display'][$actual_mode]['weight'];
171
        }
172

    
173
      }
174
    }
175
  }
176
  return $result;
177
}
178

    
179
/**
180
 * A generic handler for parsing field values.
181
 *
182
 * This callback can only handle the most basic of fields that populates the
183
 * safe_value during field load or use the value column for data storage.
184
 *
185
 * @param array $items
186
 *   An array of field items.
187
 * @param array $context
188
 *   An associative array containing:
189
 *   - entity: The entity that the items belong to.
190
 *   - entity_type: The entity type; e.g., 'node' or 'user'.
191
 *   - bundle: The bundle name.
192
 *   - field: The field that the items belong to.
193
 *   - instance: The instance that the items belong to.
194
 *   - language: The language associated with $items.
195
 *   - old_entity: The older entity.
196
 *   - new_entity: The newer entity.
197
 *
198
 * @return array
199
 *   An array of strings representing the value, keyed by delta index.
200
 */
201
function diff_field_diff_view($items, $context) {
202
  // Prevent unnecessary rendering of the field. This also prevents issues
203
  // where field_view_field() will use a language fallback for display that
204
  // may not match the requested diff comparison language.
205
  if (!$items) {
206
    return array();
207
  }
208

    
209
  $diff_items = array();
210
  $entity = clone $context['entity'];
211
  $langcode = field_language($context['entity_type'], $entity, $context['field']['field_name']);
212
  $view_mode = empty($context['view_mode']) ? 'diff_standard' : $context['view_mode'];
213
  $element = field_view_field($context['entity_type'], $entity, $context['field']['field_name'], $view_mode, $langcode);
214

    
215
  foreach (element_children($element) as $delta) {
216
    $diff_items[$delta] = drupal_render($element[$delta]);
217
  }
218
  return $diff_items;
219
}
220

    
221
/**
222
 * Helper function to get the settings for a given field or formatter.
223
 *
224
 * @param array $field_context
225
 *   This will get the settings for a field.
226
 *   - field (required): The field that the items belong to.
227
 *   - entity: The entity that we are looking up.
228
 *   - instance: The instance that the items belong to.
229
 *   - view_mode: The view mode to use. Defaults to FALSE.
230
 *
231
 * @return array
232
 *   The settings for this field type.
233
 */
234
function diff_get_field_settings($field_context) {
235
  $field = $field_context['field'];
236

    
237
  // Update saved settings from the global settings for this field type.
238
  $settings = variable_get("diff_{$field['module']}_field_{$field['type']}_default_options", array());
239

    
240
  $settings = _diff_field_default_settings($field['module'], $field['type'], $settings);
241

    
242
  // Allow modules to alter the field settings based on the current context.
243
  drupal_alter('diff_field_settings', $settings, $field_context);
244

    
245
  return $settings;
246
}
247

    
248
/**
249
 * Helper function to initiate any global form elements.
250
 */
251
function diff_global_settings_form(&$subform, $form_state, $type, $settings) {
252
  $subform['show_header'] = array(
253
    '#type' => 'checkbox',
254
    '#title' => t('Show field title'),
255
    '#default_value' => $settings['show_header'],
256
    '#weight' => -5,
257
  );
258
  $subform['markdown'] = array(
259
    '#type' => 'select',
260
    '#title' => t('Markdown callback'),
261
    '#default_value' => $settings['markdown'],
262
    '#options' => array(
263
      'drupal_html_to_text' => t('Drupal HTML to Text'),
264
      'filter_xss' => t('Filter XSS (some tags)'),
265
      'diff_filter_xss' => t('Filter XSS (all tags)'),
266
    ),
267
    '#description' => t('These provide ways to clean markup tags to make comparisons easier to read.'),
268
    '#empty_option' => t('- Do not process -'),
269
  );
270
  $subform['line_counter'] = array(
271
    '#type' => 'radios',
272
    '#title' => t('Line counter'),
273
    '#default_value' => $settings['line_counter'],
274
    '#description' => t('This outputs the (approximate) line numbers as a heading before every change.'),
275
    '#options' => array(
276
      '' => t('None. Counter ignore and not incremented.'),
277
      'hidden' => t('Count lines but do not show line headers.'),
278
      'line' => t('Count and show lines, restarting counter at 0.'),
279
      'line_continuous' => t('Count and show lines, incrementing counter from last item.'),
280
    ),
281
  );
282

    
283
}
284

    
285
/**
286
 * Helper function to populate the settings array.
287
 */
288
function _diff_field_default_settings($module, $field_type, $settings = array()) {
289
  // Load files containing the field callbacks.
290
  _diff_autoload($module);
291

    
292
  // Populate any missing values from CALLBACK_field_diff_default_options().
293
  $func = $module . '_field_diff_default_options';
294
  if (function_exists($func)) {
295
    $settings += $func($field_type);
296
  }
297

    
298
  // Check for Diff support. If it doesn't exist, the default markdown should
299
  // escape the field display, otherwise a raw format should be used.
300
  $func = $module . '_field_diff_view';
301

    
302
  // Global settings.
303
  $settings += array(
304
    'markdown' => function_exists($func) ? '' : 'drupal_html_to_text',
305
    'line_counter' => '',
306
    'show_header' => 1,
307
  );
308

    
309
  return $settings;
310
}
311

    
312
/**
313
 * Private helper function to load field includes.
314
 *
315
 * @param array|string $field_or_module
316
 *   The field definition array or the module that implements the field.
317
 */
318
function _diff_autoload($field_or_module) {
319
  $includes = &drupal_static(__FUNCTION__, FALSE);
320
  if (!$includes) {
321
    $includes = array(
322
      'file' => module_exists('file'),
323
      'image' => module_exists('image'),
324
      'list' => module_exists('list'),
325
      'taxonomy' => module_exists('taxonomy'),
326
      'text' => module_exists('text'),
327
      'number' => module_exists('number'),
328
    );
329
  }
330

    
331
  $module = is_string($field_or_module) ? $field_or_module : $field_or_module['module'];
332

    
333
  // Since field hooks are not real hooks, we manually load the field modules
334
  // MODULE.diff.inc. We handle the five core field defining modules.
335
  if (!isset($includes[$module])) {
336
    module_load_include('diff.inc', $module);
337
    $includes[$module] = 0;
338
  }
339
  elseif (!empty($includes[$module])) {
340
    module_load_include('inc', 'diff', 'includes/' . $module);
341
    $includes[$module] = 0;
342
  }
343
}
344

    
345
/**
346
 * Helper function to parse out the state in the diff results.
347
 */
348
function diff_extract_state($diff, $state = 'raw') {
349
  $states = array(
350
    0 => NULL,
351
    1 => NULL,
352
  );
353
  if (isset($diff['#states'][$state])) {
354
    if (isset($diff['#states'][$state]['#old'])) {
355
      $states[0] = $diff['#states'][$state]['#old'];
356
    }
357
    if (isset($diff['#states'][$state]['#new'])) {
358
      $states[1] = $diff['#states'][$state]['#new'];
359
    }
360
  }
361
  return $states;
362
}