You are here

function domain_taxonomy_url_rewrite_outbound in Domain Taxonomy 7

Same name and namespace in other branches
  1. 6 domain_taxonomy.module \domain_taxonomy_url_rewrite_outbound()
  2. 7.3 domain_taxonomy.module \domain_taxonomy_url_rewrite_outbound()

File

./domain_taxonomy.module, line 621

Code

function domain_taxonomy_url_rewrite_outbound(&$path, &$options, $original_path) {
  global $_domain;

  // If the domain_id is not set, then the Domain module is not active, and we cannot run this function.
  if (!isset($_domain['domain_id'])) {
    return;
  }

  // Set static variables for the node lookups, to remove redundant queries.
  static $domain_site, $domain, $nodepaths, $path_rewrite;
  $check = domain_grant_all();
  $seo = variable_get('domain_taxonomy_seo', 0);
  $use_source = TRUE;
  if ($check || $seo || $use_source) {
    $pattern = explode('/', $original_path);

    // Advanced pattern matching, we find the term id based on token %t in the path string.
    if (!isset($nodepaths)) {
      $pathdata = variable_get('domain_taxonomy_paths', "taxonomy/term/%t\r\ntaxonomy/edit/term/%t\r\nforum/%t");
      $path_match = preg_replace('/(\\r\\n?|\\n)/', '|', $pathdata);
      $nodepaths = explode("|", $path_match);
    }
    $tid = FALSE;
    foreach ($nodepaths as $match) {
      $match_array = explode('/', $match);
      $placeholder = array_search('%t', $match_array);
      if (isset($pattern[$placeholder])) {
        $match_array[$placeholder] = $pattern[$placeholder];
        if (is_numeric($pattern[$placeholder]) && $match_array == $pattern) {
          $tid = (int) $pattern[$placeholder];
          break;
        }
      }
    }
    $target_domain_id = $_domain['domain_id'];

    // This path has matched a term id, so it may need to be rewritten.
    if ($tid) {
      $root = domain_lookup(variable_get('domain_default_source', 0));

      // Remove redundancy from the domain_site check.
      if (!isset($domain_site[$tid])) {

        // If this check works, we don't need to rewrite the path unless SEO rules demand it.
        $domain_site[$tid] = db_result(db_query("SELECT grant_view FROM {domain_taxonomy_access} WHERE tid = %d AND gid = 0 AND realm = '%s'", $tid, 'domain_site'));
      }
      if (!$domain_site[$tid] || $use_source) {

        // Remove rendundancy from the domain_id check.
        if (!isset($domain[$tid])) {

          // The Domain Source module is optional, and allows nodes to be assigned to specific domains for the
          // purpose of this check.
          if ($use_source) {
            $source = db_result(db_query("SELECT domain_id FROM {domain_taxonomy_source} WHERE tid = %d", $tid));
            $domain[$tid] = domain_lookup($source);
          }
          else {

            // Load the domain data for this node -- but only take the first match.
            $id = db_result(db_query_range("SELECT gid FROM {domain_taxonomy_access} WHERE tid = %d AND realm = '%s' AND grant_view = 1 ORDER BY gid", $tid, 'domain_id', 0, 1));
            $domain[$tid] = domain_lookup($id);
          }
        }

        // Can we and do we need to rewrite this path?
        if ($domain[$tid] != -1 && $domain[$tid]['domain_id'] != $_domain['domain_id']) {
          $options['absolute'] = TRUE;

          // In this case, the $base_url cannot have a trailing slash
          $options['base_url'] = rtrim($domain[$tid]['path'], '/');

          // Domain Source trumps the seo rules below.
          if (isset($source)) {
            $seo = FALSE;
          }
          $target_domain_id = $domain[$tid]['domain_id'];
        }
      }
      elseif ($root != -1 && $seo && $_domain['domain_id'] != $root['domain_id']) {
        $options['absolute'] = TRUE;

        // In this case, the $base_url cannot have a trailing slash
        $options['base_url'] = rtrim($root['path'], '/');
        $target_domain_id = $root['domain_id'];
      }
    }
  }
}