You are here

function wikitools_node_validate in Wikitools 6

Same name and namespace in other branches
  1. 5 wikitools.module \wikitools_node_validate()
  2. 6.2 wikitools.module \wikitools_node_validate()
  3. 7 wikitools.module \wikitools_node_validate()

Validate check of node edit form.

1 call to wikitools_node_validate()
wikitools_nodeapi in ./wikitools.module
Implementation of hook_nodeapi().

File

./wikitools.module, line 369
A non-intrusive module to have some wiki-like behaviour.

Code

function wikitools_node_validate($node) {
  if (wikitools_type_affected($node->type)) {

    // Check for unique titles.
    if (wikitools_enforce_unique_titles()) {

      // Build node type condition.
      $types_clause = NULL;
      foreach (wikitools_node_types() as $type) {
        if ($types_clause) {
          $types_clause .= ",'" . db_escape_string($type) . "'";
        }
        else {
          $types_clause = "type IN ('" . db_escape_string($type) . "'";
        }
      }

      // There is at least one node type, so this will always be well-formed.
      $types_clause .= ')';
      $nid = db_result(db_query("SELECT nid FROM {node} WHERE LOWER(title) = LOWER('%s') AND {$types_clause}", $node->title));
      if (!$nid && wikitools_treat_underscore_as_space()) {
        $nid = db_result(db_query("SELECT nid FROM {node} WHERE LOWER(REPLACE(title, '_', ' ')) = LOWER(REPLACE('%s', '_', ' ')) AND {$types_clause}", $node->title));
      }
      if (!$nid && wikitools_treat_dash_as_space()) {
        $nid = db_result(db_query("SELECT nid FROM {node} WHERE LOWER(REPLACE(title, '-', ' ')) = LOWER(REPLACE('%s', '-', ' ')) AND {$types_clause}", $node->title));
      }

      // It is only an error if the node which already exists is not the currently edited node.
      if ($nid && $nid != $node->nid) {
        form_set_error('title', t('A <a href="@page_url">page</a> with this name already exists.', array(
          '@page_url' => url("node/{$nid}"),
        )));
      }
    }

    // Check for disallowed characters in title.
    if ($disallowed_characters = wikitools_disallowed_characters()) {
      for ($i = 0; $i < strlen($node->title); $i++) {
        if (strpos($disallowed_characters, $node->title[$i]) !== FALSE) {
          form_set_error('title', t('The character %c is not allowed in a title', array(
            '%c' => $node->title[$i],
          )));
          break;
        }
      }
    }

    // Check for invalid title names if url subpages are enabled and "/" is allowed in titles.
    if (wikitools_subpages_handling() == 'url' && strpos("/", $disallowed_characters) === FALSE) {
      $title_parts = explode('/', $node->title);
      if (count($title_parts) > 1 && in_array(end($title_parts), wikitools_subpages())) {
        form_set_error('title', t('The title is not allowed to end in: %string', array(
          '%string' => '/' . implode(', /', wikitools_subpages()),
        )));
      }
    }
  }
}