Projet

Général

Profil

Paste
Télécharger (7,59 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / flag / includes / views / flag.views_convert.inc @ 74f6bef0

1
<?php
2

    
3
/**
4
 * @file
5
 * Update Views 1 views provided by both views_bookmark and flag modules.
6
 */
7

    
8
/**
9
 * Implements hook_views_convert().
10
 *
11
 * Intervene to convert field values from the Views 1 format to the
12
 * Views 2 format. Intervene only if $view->add_item() won't produce
13
 * the right results, usually needed to set field options or values.
14
 */
15
function flag_views_convert($display, $type, &$view, $field, $id) {
16
  static $flag_name;
17

    
18
  // First, replace any sign of views_bookmark with flag's Views 1 equivelant.
19
  $key_search = array(
20
    '^views_bookmark_ops_([0-9]+)' => 'flag_ops_',
21
    '^views_bookmark_nodes_([0-9]+)' => 'flag_content_',
22
    '^views_bookmark_users_([0-9]+)' => 'flag_users_',
23
    '^views_bookmark_node_count_([0-9]+)' => 'flag_counts_',
24
  );
25
  foreach ($field as $property => $value) {
26
    foreach ($key_search as $search => $replace) {
27
      if (!empty($value) && is_string($value) && preg_match('/' . $search . '/', $value, $matches)) {
28
        $flag = flag_get_flag(NULL, $matches[1]);
29
        $replace = $replace . $flag->name;
30
        $field[$property] = preg_replace('/' . $search . '/', $replace, $value);
31
      }
32
    }
33
  }
34

    
35
  // Create a table/field identifier for this field.
36
  foreach (array('flag_ops_', 'flag_content_', 'flag_users_', 'flag_counts_') as $table) {
37
    if (strpos($field['tablename'], $table) === 0) {
38
      $name = str_replace($table, '', $field['tablename']);
39
      if (!empty($name) && !isset($flag_name)) {
40
        $flag_name = $name;
41
      }
42
    }
43
  }
44

    
45
  // Now update values, options, etc. to those selected in the imported view.
46
  switch ($type) {
47
    case 'field':
48
      switch ($id) {
49
        case 'ops':
50
          $new_field = array(
51
            'label' => $field['label'],
52
            'id' => 'ops',
53
            'table' => 'flagging',
54
            'field' => 'ops',
55
            'relationship' => 'flag_content_rel',
56
          );
57
          $new_rel = 'flag_content_rel';
58
          $flag_content_rel_user = 'current';
59
          break;
60
        case 'count':
61
          $new_field = array(
62
            'label' => $field['label'],
63
            'id' => 'count',
64
            'table' => 'flag_counts',
65
            'field' => 'count',
66
            'relationship' => 'flag_counts_rel',
67
          );
68
          $new_rel = 'flag_counts_rel';
69
          break;
70
        case 'name':
71
          $new_field = array(
72
            'label' => $field['label'],
73
            'link_to_user' => 1,
74
            'id' => 'name',
75
            'table' => 'users',
76
            'field' => 'name',
77
            'relationship' => 'uid',
78
          );
79
          $new_rel = 'uid';
80
          break;
81
      }
82
      break;
83

    
84
    case 'filter':
85
    case 'exposed_filter':
86
      switch ($id) {
87
        case 'uid':
88
          // The flagging uid filter means "Include content only flagged by
89
          // the current user". In D6, this is no longer a filter, but instead
90
          // part of the relationship. Make the relationship join on the uid.
91
          if ($field['value'] == '***CURRENT_USER***') {
92
            $new_rel = 'flag_content_rel';
93
            $flag_content_rel_user = 'current';
94
          }
95
          // Remove the old filter.
96
          $view->set_item('default', $type, $id, NULL);
97
          break;
98
        case 'timestamp':
99
          $new_field = array(
100
            'operator' => $field['operator'],
101
            'value' => flag_views_convert_timestamp_value($field['value']),
102
            'group' => 0,
103
            'id' => 'timestamp',
104
            'table' => 'flagging',
105
            'field' => 'timestamp',
106
            'relationship' => 'flag_content_rel',
107
            'exposed' => $type == 'exposed_filter' ? 1 : 0,
108
          );
109
          $new_rel = 'flag_content_rel';
110
          drupal_set_message(t('Flag is not able to convert the <em>Flagged time</em> filter. It\'s value is currently empty, but needs to be populated to work properly.'), 'warning');
111
          break;
112
        case 'count':
113
          $new_field = array(
114
            'operator' => $field['operator'],
115
            'value' => array('value' => $field['value']),
116
            'group' => 0,
117
            'id' => 'count',
118
            'table' => 'flag_counts',
119
            'field' => 'count',
120
            'relationship' => 'flag_counts_rel',
121
            'exposed' => $type == 'exposed_filter' ? 1 : 0,
122
          );
123
          $new_rel = 'flag_counts_rel';
124
          break;
125
      }
126
      break;
127

    
128
    case 'argument':
129
      // Flag in Drupal 5 only provides one argument, and in Views 1 arguments
130
      // weren't given and ID, so we use the field type.
131
      if (strpos($field['type'], 'flag_content_') === 0) {
132
        $new_field = array(
133
          'id' => 'uid',
134
          'table' => 'users',
135
          'field' => 'uid',
136
          'relationship' => 'uid',
137
        );
138
        $new_rel = 'uid';
139
      }
140
      break;
141

    
142
    case 'sort':
143
      switch ($id) {
144
        case 'count':
145
          $new_field = array(
146
            'order' => $field['sortorder'],
147
            'id' => 'count',
148
            'table' => 'flag_counts',
149
            'field' => 'count',
150
            'relationship' => 'flag_counts_rel',
151
          );
152
          $new_rel = 'flag_counts_rel';
153
          break;
154
        case 'timestamp':
155
          $new_field = array(
156
            'order' => $field['sortorder'],
157
            'id' => 'timestamp',
158
            'table' => 'flagging',
159
            'field' => 'timestamp',
160
            'relationship' => 'flag_content_rel',
161
          );
162
          $new_rel = 'flag_content_rel';
163
          break;
164
      }
165
      break;
166
  }
167

    
168
  // Add any new fields.
169
  if (isset($new_field)) {
170
    // Exposed filters are now wrapped into filters.
171
    $type = $type == 'exposed_filter' ? 'filter' : $type;
172
    // Update the type in the view options.
173
    $view->set_item('default', $type, $id, $new_field);
174
  }
175

    
176
  // Add any new relationships (but only once per view).
177
  if (isset($new_rel) && !isset($view->display[$display]->display_options['relationships'][$new_rel])) {
178
    if ($new_rel == 'flag_content_rel') {
179
      $view->display[$display]->display_options['relationships'][$new_rel] = array(
180
        'label' => $flag_name,
181
        'required' => 1,
182
        'flag' => $flag_name,
183
        'user_scope' => 'any',
184
        'id' => 'flag_content_rel',
185
        'table' => 'node',
186
        'field' => 'flag_content_rel',
187
        'relationship' => 'none',
188
      );
189
    }
190
    elseif ($new_rel == 'flag_counts_rel') {
191
      $view->display[$display]->display_options['relationships'][$new_rel] = array(
192
        'label' => $flag_name . ' counts',
193
        'required' => 0,
194
        'flag' => $flag_name,
195
        'id' => 'flag_counts_rel',
196
        'table' => 'node',
197
        'field' => 'flag_counts_rel',
198
        'relationship' => 'none',
199
      );
200
    }
201
    elseif ($new_rel == 'uid') {
202
      $view->display[$display]->display_options['relationships'][$new_rel] = array(
203
        'label' => isset($flag_name) ? $flag_name . ' user' : 'user',
204
        'required' => 0,
205
        'id' => 'uid',
206
        'table' => 'flagging',
207
        'field' => 'uid',
208
        'relationship' => 'flag_content_rel',
209
      );
210
    }
211
  }
212

    
213
  // Modify relationships if needed.
214
  if (isset($flag_content_rel_user) && isset($view->display[$display]->display_options['relationships']['flag_content_rel'])) {
215
    $view->display[$display]->display_options['relationships']['flag_content_rel']['user_scope'] = 'current';
216
  }
217
}
218

    
219
/**
220
 * In Views 1, dates were simply stored as a single string. In Views 2, the
221
 * value is stored as an array of options.
222
 */
223
function flag_views_convert_timestamp_value($value) {
224
  $type = 'date';
225
  if (stripos($value, 'now') !== FALSE) {
226
    $type = 'offset';
227
    $value = trim(str_ireplace('now', '', $value));
228
  }
229
  return array(
230
    'type' => $type,
231
    'value' => $value,
232
    'min' => '',
233
    'max' => '',
234
  );
235
}