Projet

Général

Profil

Paste
Télécharger (9,46 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / i18n / i18n_select / i18n_select.module @ 76df55b7

1
<?php
2
/**
3
 * @file
4
 * Multilingual content selection module.
5
 *
6
 * Alters content queries to add language conditions.
7
 *
8
 * Queries tagged with 'i18n_select' or that already have a language condition will not be altered.
9
 */
10

    
11
// No language selection
12
define('I18N_SELECT_NONE', 0);
13
// Content with current language and undefined language
14
define('I18N_SELECT_NORMAL', 1);
15
// Select default language when current language is missing
16
define('I18N_SELECT_MISSING', 2);
17

    
18
/**
19
 * Enable on every page except the listed pages.
20
 */
21
define('I18N_SELECT_PAGE_NOTLISTED', 0);
22
/**
23
 * Enable on only the listed pages.
24
 */
25
define('I18N_SELECT_PAGE_LISTED', 1);
26
/**
27
 * Enable if the associated PHP code returns TRUE.
28
 */
29
define('I18N_SELECT_PAGE_PHP', 2);
30

    
31
/**
32
 * Implements hook_init().
33
 */
34
function i18n_select_init() {
35
  // Determine selection mode for this page
36
  i18n_select(i18n_select_page());
37
}
38

    
39
/**
40
 * Implements hook_block_list_alter().
41
 *
42
 * Dirty trick to enable selection for blocks though it may be disabled for the current page.
43
 */
44
function i18n_select_block_list_alter(&$blocks) {
45
  // Still, skip for form submission. There are pages like the ones produced
46
  // by overlay that render the blocks before the page.
47
  // See overlay_init(), overlay_overlay_child_initialize()
48
  if (empty($_POST) && !isset($_GET['token']) && variable_get('i18n_select_page_block', TRUE)) {
49
    i18n_select(TRUE);
50
  }
51
}
52

    
53
/**
54
 * Implements hook_menu().
55
 */
56
function i18n_select_menu() {
57
  $items['admin/config/regional/i18n/select'] = array(
58
    'title' => 'Selection',
59
    'description' => 'Configure extended options for multilingual content and translations.',
60
    'page callback' => 'drupal_get_form',
61
    'page arguments' => array('i18n_select_admin_settings'),
62
    'access arguments' => array('administer site configuration'),
63
    'file' => 'i18n_select.admin.inc',
64
    'type' => MENU_LOCAL_TASK,
65
  );
66
  return $items;
67
}
68

    
69
/**
70
 * Get current mode for i18n selection
71
 *
72
 * @param $type
73
 *   Selection type: 'nodes', 'taxonomy', etc..
74
 */
75
function i18n_select_mode($type = NULL) {
76
  if (i18n_select() && (!$type || variable_get('i18n_select_' . $type, TRUE))) {
77
    return I18N_SELECT_NORMAL;
78
  }
79
  else {
80
    return I18N_SELECT_NONE;
81
  }
82
}
83

    
84
/**
85
 * Check current path to enable selection
86
 *
87
 * This works pretty much like block visibility
88
 *
89
 * @return boolean
90
 *   TRUE if content selection should be enabled for this page.
91
 */
92
function i18n_select_page() {
93
  static $mode;
94

    
95
  if (!isset($mode)) {
96
    $mode = &drupal_static(__FUNCTION__);
97
    $visibility = variable_get('i18n_select_page_mode', I18N_SELECT_PAGE_NOTLISTED);
98
    if ($pages = variable_get('i18n_select_page_list', 'admin/*')) {
99
      // Convert path to lowercase. This allows comparison of the same path
100
      // with different case. Ex: /Page, /page, /PAGE.
101
      $pages = drupal_strtolower($pages);
102
      if ($visibility < I18N_SELECT_PAGE_PHP) {
103
        // Convert the Drupal path to lowercase
104
        $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
105
        // Compare the lowercase internal and lowercase path alias (if any).
106
        $page_match = drupal_match_path($path, $pages);
107
        if ($path != $_GET['q']) {
108
          $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
109
        }
110
        // When $visibility has a value of 0 (I18N_SELECT_PAGE_NOTLISTED),
111
        // the block is displayed on all pages except those listed in $pages.
112
        // When set to 1 (I18N_SELECT_PAGE_LISTED), it is displayed only on those
113
        // pages listed in $pages.
114
        $mode = !($visibility xor $page_match);
115
      }
116
      elseif (module_exists('php')) {
117
        $mode = php_eval($pages);
118
      }
119
      else {
120
        $mode = FALSE;
121
      }
122
    }
123
    else {
124
      // No pages defined, still respect the setting (unlike blocks)
125
      $mode = $visibility == I18N_SELECT_PAGE_NOTLISTED;
126
    }
127
  }
128

    
129
  return $mode;
130
}
131

    
132
/**
133
 * Implementation of hook_query_node_access_alter().
134
 *
135
 * Rewrite node queries so language selection options are enforced.
136
 */
137
function i18n_select_query_node_access_alter(QueryAlterableInterface $query) {
138
  if (i18n_select_mode('nodes') && i18n_select_check_query($query, 'nid') &&
139
    ($table_alias = i18n_select_check_table($query, 'node', 'nid'))) {
140
    $query->condition($table_alias . '.language', i18n_select_langcodes());
141
    // Mark query as altered
142
    $query->addTag('i18n_select');
143
  }
144
}
145

    
146
/**
147
 * Implementation of hook_query_term_access_alter().
148
 *
149
 * Rewrite taxonomy term queries so language selection options are enforced.
150
 */
151
function i18n_select_query_term_access_alter(QueryAlterableInterface $query) {
152
  if (module_exists('i18n_taxonomy') && i18n_select_mode('taxonomy') && i18n_select_check_query($query, 'tid') &&
153
    ($table_alias = i18n_select_check_table($query, 'taxonomy_term_data', 'tid'))) {
154
    $query->condition($table_alias . '.language', i18n_select_langcodes());
155
    // Mark query as altered
156
    $query->addTag('i18n_select');
157
  }
158
}
159

    
160
/**
161
 * Check table exists in query and get alias for it.
162
 *
163
 * @param $query
164
 *   Query object
165
 * @param $table_name
166
 *   Table name to find.
167
 * @param $field_name
168
 *   field to join the table if needed.
169
 *
170
 * @return
171
 *   Table alias if found, none if not.
172
 */
173
function i18n_select_check_table($query, $table_name, $field_name) {
174
  $tables = $query->getTables();
175
  foreach ($tables as $table) {
176
    if (!($table instanceof SelectQueryInterface) && $table['table'] == $table_name) {
177
      return _i18n_select_table_alias($table);
178
    }
179
  }
180
  // Join the table if we can find the key field on any of the tables
181
  // And all the conditions have a table alias (or there's a unique table).
182
  if (count($tables) == 1) {
183
    $table = reset($tables);
184
    $table_alias = _i18n_select_table_alias($table);
185
    if (i18n_select_check_conditions($query, $table_alias)) {
186
      $join_table = $table_alias;
187
    }
188
  }
189
  elseif (i18n_select_check_conditions($query)) {
190
    // Try to find the right field in the table schema.
191
    foreach ($tables as $table) {
192
      $schema = drupal_get_schema($table['table']);
193
      if ($schema && !empty($schema['fields'][$field_name])) {
194
        $join_table = _i18n_select_table_alias($table);
195
        break;
196
      }
197
    }
198
  }
199
  if (!empty($join_table)) {
200
    return $query->join($table_name, $table_name, $join_table . '.' . $field_name . ' = %alias.' . $field_name);
201
  }
202
  else {
203
    return FALSE;
204
  }
205
}
206

    
207
/**
208
 * Get table alias
209
 */
210
function _i18n_select_table_alias($table) {
211
  return !empty($table['alias']) ? $table['alias'] : $table['table'];
212
}
213

    
214
/**
215
 * Check all query conditions have a table alias.
216
 *
217
 * @param $table_alias
218
 *   Optional table alias for fields without table.
219
 *
220
 * @return boolean
221
 *   TRUE if table conditions are ok, FALSE otherwise.
222
 */
223
function i18n_select_check_conditions($query, $table_alias = NULL) {
224
  $conditions =& $query->conditions();
225
  foreach ($conditions as $index => $condition) {
226
    if (is_array($condition) && !empty($condition['field'])) {
227
      if (strpos($condition['field'], '.') === FALSE) {
228
        if ($table_alias) {
229
          // Change the condition to include a table alias.
230
          $conditions[$index]['field'] = $table_alias . '.' . $condition['field'];
231
        }
232
        else {
233
          // We won't risk joining anything here.
234
          return FALSE;
235
        }
236
      }
237
    }
238
  }
239
  return TRUE;
240
}
241

    
242
/**
243
 * Check whether we should apply language conditions here:
244
 * - The query has not been tagged with 'i18n_select'
245
 * - The query doesn't have already a language condition
246
 * - All the conditions have a table alias or there's only one table.
247
 * - We are not loading specific objects (no condition for index field).
248
 *
249
 * @param $query
250
 *   Query object.
251
 * @param $index_field
252
 *   Object index field to check we don't have 'IN' conditions for it.
253
 */
254
function i18n_select_check_query($query, $index_field = NULL) {
255
  static $tags;
256
  // Skip queries with certain tags
257
  if (!isset($tags)) {
258
    $tags = ($skip = variable_get('i18n_select_skip_tags', 'views')) ? array_map('trim', explode(',', $skip)) : array();
259
    $tags[] = 'i18n_select';
260
  }
261
  foreach ($tags as $tag) {
262
    if ($query->hasTag($tag)) {
263
      return FALSE;
264
    }
265
  }
266
  // Check all the conditions to see whether the query is suitable for altering.
267
  foreach ($query->conditions() as $condition) {
268
    if (is_array($condition)) {
269
      // @todo For some complex queries, like search ones, field is a DatabaseCondition object
270
      if (!isset($condition['field']) || !is_string($condition['field'])) {
271
        // There's a weird condition field, we won't take any chances.
272
        return FALSE;
273
      }
274
      else {
275
        // Just check the condition doesn't include the language field
276
        if (strpos($condition['field'], '.') === FALSE) {
277
          $field = $condition['field'];
278
        }
279
        else {
280
          list($table, $field) = explode('.', $condition['field']);
281
        }
282
        if ($field == 'language') {
283
          return FALSE;
284
        }
285
        // Check 'IN' conditions for index field, usually entity loading for specific objects.
286
        if ($field == $index_field && $condition['operator'] == 'IN') {
287
          return FALSE;
288
        }
289
      }
290
    }
291
  }
292
  // If the language field is present we don't want to do any filtering.
293
  $fields = $query->getFields();
294
  if (isset($fields['language'])) {
295
    return FALSE;
296
  }
297

    
298
  return TRUE;
299
}
300

    
301
/**
302
 * Get main language for content selection
303
 */
304
function i18n_select_language() {
305
  return $GLOBALS[LANGUAGE_TYPE_CONTENT];
306
}
307

    
308
/**
309
 * Get language codes for content selection to use in query conditions
310
 */
311
function i18n_select_langcodes() {
312
  return array(i18n_select_language()->language, LANGUAGE_NONE);
313
}
314