You are here

function i18n_db_rewrite_sql in Internationalization 6

Same name and namespace in other branches
  1. 5.3 i18n.module \i18n_db_rewrite_sql()
  2. 5 i18n.module \i18n_db_rewrite_sql()
  3. 5.2 i18n.module \i18n_db_rewrite_sql()

Implementation of hook_db_rewrite_sql().

Rewrite node queries so language selection options are enforced.

File

./i18n.module, line 435
Internationalization (i18n) module.

Code

function i18n_db_rewrite_sql($query, $primary_table, $primary_key, $args = array()) {

  // If mode is 'off' = no rewrite, we cannot return any empty 'where' string so check here.
  $mode = i18n_selection_mode();
  if ($mode == 'off') {
    return;
  }

  // Disable language conditions for views.
  if (array_key_exists('view', $args)) {
    return;
  }
  switch ($primary_table) {
    case 'n':
    case 'node':

      // No rewrite for queries with subselect ? (views count queries).
      // @ TO DO Actually these queries look un-rewrittable, check with other developers.
      if (preg_match("/FROM \\(SELECT/", $query)) {
        return;
      }

      // No rewrite for translation module queries.
      if (preg_match("/.*FROM {node} {$primary_table} WHERE.*{$primary_table}\\.tnid/", $query)) {
        return;
      }

      // When loading specific nodes, language conditions shouldn't apply.
      if (preg_match("/WHERE.*\\s{$primary_table}.nid\\s*=\\s*(\\d|%d)/", $query)) {
        return;
      }

      // If language conditions already there, get out.
      if (preg_match("/i18n/", $query)) {
        return;
      }

      // Mixed mode is a bit more complex, we need to join in one more table
      // and add some more conditions, but only if language is not default.
      if ($mode == 'mixed') {
        $result['where'] = i18n_db_rewrite_where($primary_table, 'node', 'simple');
        if (i18n_get_lang() != i18n_default_language()) {
          $result['join'] = "LEFT JOIN {node} i18n ON {$primary_table}.tnid > 0 AND {$primary_table}.tnid = i18n.tnid AND i18n.language = '" . i18n_get_lang() . "'";

          // So we show also nodes that have default language.
          $result['where'] .= " OR {$primary_table}.language = '" . i18n_default_language() . "' AND i18n.nid IS NULL";
        }
      }
      else {
        $result['where'] = i18n_db_rewrite_where($primary_table, 'node', $mode);
      }
      return $result;
  }
}