Projet

Général

Profil

Paste
Télécharger (4,24 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Provide diff field functions for the file module.
6
 */
7

    
8
/**
9
 * Diff field callback for preloading file entities.
10
 */
11
function file_field_diff_view_prepare(&$old_items, &$new_items, $context) {
12
  $fids = array();
13
  foreach (array_merge_recursive($old_items, $new_items) as $info) {
14
    $fids[$info['fid']] = $info['fid'];
15
  }
16
  $files = file_load_multiple($fids);
17

    
18
  foreach ($old_items as $delta => $info) {
19
    $old_items[$delta]['file'] = isset($files[$info['fid']]) ? $files[$info['fid']] : NULL;
20
  }
21
  foreach ($new_items as $delta => $info) {
22
    $new_items[$delta]['file'] = isset($files[$info['fid']]) ? $files[$info['fid']] : NULL;
23
  }
24
}
25

    
26
/**
27
 * Diff field callback for parsing file field comparative values.
28
 */
29
function file_field_diff_view($items, $context) {
30
  $field = $context['field'];
31
  $instance = $context['instance'];
32
  $settings = $context['settings'];
33

    
34
  $diff_items = array();
35
  foreach ($items as $delta => $item) {
36
    if (isset($item['file'])) {
37
      $output = array();
38

    
39
      // We populate as much as possible to allow the best flexability in any
40
      // string overrides.
41
      $t_args = array();
42
      foreach ($item as $key => $value) {
43
        if (is_scalar($value)) {
44
          $t_args['!' . $key] = $value;
45
        }
46
      }
47
      // Some states do not have the file properties in the item, so put these
48
      // out of the main file object.
49
      if (!empty($item['file'])) {
50
        $file_values = (array) $item['file'];
51
        foreach ($file_values as $key => $value) {
52
          if (is_scalar($value) && !isset($t_args['!' . $key])) {
53
            $t_args['!' . $key] = $value;
54
          }
55
        }
56
      }
57

    
58
      $output['file'] = t('File: !filename', $t_args);
59
      if ($settings['compare_description_field'] && !empty($instance['settings']['description_field'])) {
60
        if (!empty($item['description'])) {
61
          $output['description'] = t('Description: !description', $t_args);
62
        }
63
      }
64
      if ($settings['show_id']) {
65
        $output['fid'] = t('File ID: !fid', $t_args);
66
      }
67
      if ($settings['compare_display_field'] && !empty($field['settings']['display_field'])) {
68
        $output['display'] = $item['display'] ? t('Displayed') : t('Hidden');
69
      }
70
      $separator = $settings['property_separator'] == 'nl' ? "\n" : $settings['property_separator'];
71
      $diff_items[$delta] = implode($separator, $output);
72
    }
73
  }
74

    
75
  return $diff_items;
76
}
77

    
78
/**
79
 * Provide default field comparison options.
80
 */
81
function file_field_diff_default_options($field_type) {
82
  return array(
83
    'show_id' => 0,
84
    'compare_display_field' => 0,
85
    'compare_description_field' => 0,
86
    'property_separator' => '; ',
87
  );
88
}
89

    
90
/**
91
 * Provide a form for setting the field comparison options.
92
 */
93
function file_field_diff_options_form($field_type, $settings) {
94
  $options_form = array();
95
  $options_form['show_id'] = array(
96
    '#type' => 'checkbox',
97
    '#title' => t('Show file ID'),
98
    '#default_value' => $settings['show_id'],
99
  );
100
  $options_form['compare_description_field'] = array(
101
    '#type' => 'checkbox',
102
    '#title' => t('Compare description field'),
103
    '#default_value' => $settings['compare_description_field'],
104
    '#description' => t('This is only used if the "Enable <em>Description</em> field" is checked in the instance settings.'),
105
  );
106
  $options_form['compare_display_field'] = array(
107
    '#type' => 'checkbox',
108
    '#title' => t('Compare display state field'),
109
    '#default_value' => $settings['compare_display_field'],
110
    '#description' => t('This is only used if the "Enable <em>Display</em> field" is checked in the field settings.'),
111
  );
112
  $options_form['property_separator'] = array(
113
    '#type' => 'select',
114
    '#title' => t('Property separator'),
115
    '#default_value' => $settings['property_separator'],
116
    '#description' => t('Provides the ability to show properties inline or across multiple lines.'),
117
    '#options' => array(
118
      ', ' => t('Comma (,)'),
119
      '; ' => t('Semicolon (;)'),
120
      ' ' => t('Space'),
121
      'nl' => t('New line'),
122
    ),
123
  );
124
  // Allow users to set their own separator using variable_set().
125
  if (!isset($options_form['#options'][$settings['property_separator']])) {
126
    $options_form['#options'][$settings['property_separator']] = $settings['property_separator'];
127
  }
128
  return $options_form;
129
}