Project

General

Profile

Paste
Download (7.59 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / flag / includes / views / flag.views_convert.inc @ 4cfd8be6

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

    
61
        case 'count':
62
          $new_field = array(
63
            'label' => $field['label'],
64
            'id' => 'count',
65
            'table' => 'flag_counts',
66
            'field' => 'count',
67
            'relationship' => 'flag_counts_rel',
68
          );
69
          $new_rel = 'flag_counts_rel';
70
          break;
71

    
72
        case 'name':
73
          $new_field = array(
74
            'label' => $field['label'],
75
            'link_to_user' => 1,
76
            'id' => 'name',
77
            'table' => 'users',
78
            'field' => 'name',
79
            'relationship' => 'uid',
80
          );
81
          $new_rel = 'uid';
82
          break;
83
      }
84
      break;
85

    
86
    case 'filter':
87
    case 'exposed_filter':
88
      switch ($id) {
89
        case 'uid':
90
          // The flagging uid filter means "Include content only flagged by
91
          // the current user". In D6, this is no longer a filter, but instead
92
          // part of the relationship. Make the relationship join on the uid.
93
          if ($field['value'] == '***CURRENT_USER***') {
94
            $new_rel = 'flag_content_rel';
95
            $flag_content_rel_user = 'current';
96
          }
97
          // Remove the old filter.
98
          $view->set_item('default', $type, $id, NULL);
99
          break;
100

    
101
        case 'timestamp':
102
          $new_field = array(
103
            'operator' => $field['operator'],
104
            'value' => flag_views_convert_timestamp_value($field['value']),
105
            'group' => 0,
106
            'id' => 'timestamp',
107
            'table' => 'flagging',
108
            'field' => 'timestamp',
109
            'relationship' => 'flag_content_rel',
110
            'exposed' => $type == 'exposed_filter' ? 1 : 0,
111
          );
112
          $new_rel = 'flag_content_rel';
113
          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');
114
          break;
115

    
116
        case 'count':
117
          $new_field = array(
118
            'operator' => $field['operator'],
119
            'value' => array('value' => $field['value']),
120
            'group' => 0,
121
            'id' => 'count',
122
            'table' => 'flag_counts',
123
            'field' => 'count',
124
            'relationship' => 'flag_counts_rel',
125
            'exposed' => $type == 'exposed_filter' ? 1 : 0,
126
          );
127
          $new_rel = 'flag_counts_rel';
128
          break;
129
      }
130
      break;
131

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

    
146
    case 'sort':
147
      switch ($id) {
148
        case 'count':
149
          $new_field = array(
150
            'order' => $field['sortorder'],
151
            'id' => 'count',
152
            'table' => 'flag_counts',
153
            'field' => 'count',
154
            'relationship' => 'flag_counts_rel',
155
          );
156
          $new_rel = 'flag_counts_rel';
157
          break;
158

    
159
        case 'timestamp':
160
          $new_field = array(
161
            'order' => $field['sortorder'],
162
            'id' => 'timestamp',
163
            'table' => 'flagging',
164
            'field' => 'timestamp',
165
            'relationship' => 'flag_content_rel',
166
          );
167
          $new_rel = 'flag_content_rel';
168
          break;
169
      }
170
      break;
171
  }
172

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

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

    
218
  // Modify relationships if needed.
219
  if (isset($flag_content_rel_user) && isset($view->display[$display]->display_options['relationships']['flag_content_rel'])) {
220
    $view->display[$display]->display_options['relationships']['flag_content_rel']['user_scope'] = 'current';
221
  }
222
}
223

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