You are here

function domain_path_nodeapi in Domain Path 6

Implements hook_nodeapi().

File

./domain_path.module, line 191
Path alias handling for multiple domains.

Code

function domain_path_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'validate':
      $form = $a3;
      $ignore = $form['domain_path']['domain_path_ignore']['#value'];
      if (!empty($ignore)) {
        return;
      }
      $set_message = FALSE;
      $dids = array_keys(domain_domains());
      $paths = array();
      foreach ($dids as $did) {
        $paths[$did] = $form['domain_paths'][$did]['#value'];
      }
      $alias = $form['path']['path']['#value'];

      // Make sure we don't duplicate anything.
      foreach ($paths as $domain_id => $path) {
        $key = $domain_id == -1 ? 0 : $domain_id;
        if (!empty($path) && $path == $alias) {
          form_set_error("domain_path][{$key}", t('Domain path may not match default path alias. You may leave the element blank.'));
          $set_message = TRUE;
        }
        elseif (!empty($path)) {
          $query = db_query("SELECT COUNT(dpid) FROM {domain_path}\n            WHERE domain_id = %d\n            AND alias = %s\n            AND language = %s\n            AND source != %s", $key, $path, isset($node->language) ? $node->language : LANGUAGE_NONE, "node/{$node->nid}");
          if (db_result($query) > 0) {
            form_set_error("domain_path][{$key}", t('Domain path matches   an existing domain path alias.'));
          }
        }
      }

      // Check for duplicates.
      if (module_exists('pathauto') && !empty($node->pathauto_perform_alias)) {
        module_load_include('inc', 'pathauto');
        $placeholders = pathauto_get_placeholders('node', $node);
        $placeholders = array_combine($placeholders['tokens'], $placeholders['values']);
        $new_path = pathauto_create_alias('node', 'return', $placeholders, 'node/' . $node->nid, $node->nid, $node->type, $node->language);
      }
      else {
        $new_path = $node->path;
      }

      // Check to see if pathauto added a -0 to the alias.
      // In these cases, we try to notify the end user.
      $path = explode('-', $new_path);
      $end = array_pop($path);
      $num = intval($end);
      if (drupal_strlen($num) == drupal_strlen($end) && ($num == $end || $end === 0)) {
        $alias = implode('-', $path);
      }
      else {
        $alias = $new_path;
      }

      // Ensure that the submitted alias does not exist yet.
      $query = db_query_range("SELECT 1 FROM {url_alias} WHERE dst = '%s' AND language = '%s' AND src <> '%s'", $alias, $node->language, 'node/' . $node->nid, 0, 1);
      $count = db_result($query);
      if ($count) {

        // If there are no aliases for the active domains, we have to ensure
        // that this is not a duplicate alias.
        $domains = array_filter($node->domains);
        $set_paths = array_filter($paths);
        if (empty($domains)) {
          form_set_error('domain_path', t('The path alias is already in use. You may provide a domain-specific alias below.'));
          $set_message = TRUE;
        }
        elseif (empty($set_paths)) {
          foreach ($domains as $domain_id) {
            $key = $domain_id == -1 ? 0 : $domain_id;
            form_set_error("domain_path][{$key}", t('The path alias %alias is already in use. You should provide a domain-specific alias.', array(
              '%alias' => $alias,
            )));
            $set_message = TRUE;
          }
        }
      }
      if ($set_message) {
        drupal_set_message(t('If you choose to use the current alias, the path to this page will be: %alias', array(
          '%alias' => $new_path,
        )), 'error', FALSE);
      }
      break;
    case 'insert':
    case 'update':
      if (empty($node->domain_path)) {
        return;
      }

      // Delete existing aliases.
      domain_path_nodeapi($node, 'delete');

      // If not set to revert, then save changes.
      if (empty($node->domain_path['domain_path_ignore'])) {
        unset($node->domain_path['domain_path_ignore']);
        foreach ($node->domain_path as $domain_id => $path) {
          if (!empty($path)) {
            $record = array(
              'domain_id' => $domain_id,
              'source' => "node/{$node->nid}",
              'alias' => $path,
              'language' => isset($node->language) ? $node->language : LANGUAGE_NONE,
              'entity_type' => 'node',
              'entity_id' => $node->nid,
            );
            drupal_write_record('domain_path', $record);
          }
        }
      }

      // Rebuild the node alias.
      cache_clear_all('domain_path', 'cache');
      domain_path_paths('nid', NULL, TRUE);
      break;
    case 'delete':
      db_query("DELETE FROM {domain_path} where entity_type = 'node' AND entity_id = %d", $node->nid);
      cache_clear_all('domain_path', 'cache');
      break;
  }
}