title.module 3.44 KB
Newer Older
1
<?php
2
// $Id$
3

4 5 6 7
function title_help($section) {
  $output = "";

  switch ($section) {
8
    case 'admin/system/modules#description':
9 10 11 12 13
      $output = t("Enables users to link to stories, articles or similar content by title.");
      break;
  }

  return $output;
14 15
}

Dries's avatar
Dries committed
16 17 18
function title_link($type) {
  if ($type == "system") {
    if (user_access("access content")) {
19
      menu("title", t("search"), "title_page", 0, MENU_HIDE);
Dries's avatar
Dries committed
20 21 22 23
    }
  }
}

24 25
function title_page() {

26 27
  $title = urldecode(arg(1));
  $result = db_query("SELECT n.*, u.name, u.uid FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.title = '%s' AND n.status = 1 ORDER BY n.created DESC", $title);
28

29
  $title = trim(str_replace(array('_', '%', '*'), ' ', $title));
30 31 32 33
  if (db_num_rows($result) == 0) {
    // No node with exact title found, try substring.
    $result = db_query("SELECT n.*, u.name, u.uid FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.title LIKE '%%%s%%' AND n.status = 1 ORDER BY n.created DESC", $title);
  }
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  if (db_num_rows($result) == 0 && module_exist("search")) {
    // still no matches ... return a full text search
    search_view($title);
  }
  else if (db_num_rows($result) == 1) {
    $node = db_fetch_object($result);
    $node = node_load(array("nid" => $node->nid));
    print theme("page", node_show($node, NULL), $node->title);
  }
  else {
    $header = array(t("Type"), t("Title"), t("Author"));
    while ($node = db_fetch_object($result)) {
      $type = ucfirst(module_invoke($node->type, "node", "name"));
      $title = l($node->title, "node/view/$node->nid");
      $author = format_name($node);
      $rows[] = array(array("data" => $type, "class" => "type"), array("data" => $title, "class" => "content"), array("data" => $author, "class" => "author"));
51 52
    }

53 54 55
    $output  = "<div id=\"title\">";
    $output .= theme("table", $header, $rows);
    $output .= "</div>";
56

57 58
    drupal_set_title(t("Matching Posts"));
    print theme("page", $output);
59 60 61
  }
}

62 63 64 65 66 67 68 69 70 71 72 73 74
function title_filter($op, $text = "") {
  switch ($op) {
    case "name":
      return t("Title filter");
    case "process":
      return _title_filter_process($text);
    case "settings":
      return _title_filter_settings($text);
    default:
      return $text;
  }
}

75
// filter [node title|description] links. '|description' is optional.
76
function _title_filter_process($text) {
77
  $pattern = '\[([^\|\]\n]+)(?>\|?)([^\]\n]*)\]';  // $1 == title: matches at least 1 char up to the first '|' or ']'
78
                                           // $2 == text: matches all after a following '|' (if there is) up to the next ']'. may include '|'s.
79
  $replacement = 'l(\'$2\' ? \'$2\' : \'$1\', \'title/\'. urlencode(\'$1\'))';
80
  return preg_replace("/$pattern/e", $replacement, $text);
81 82
}

83
function _title_filter_settings() {
84
  return form_group(t("Title filter"), t("Wiki-like [node title|text] links are enabled. These shortcuts generate a link labeled 'text' to the node with the title 'node title'. If you omit '|text', the label becomes 'node title'. You may use a substring of a node title if desired. When multiple matching titles are found, a list of matching nodes will be displayed. If no matching titles are found, a full-text search is returned."));
85 86 87
}

function title_compose_tips() {
88
  return array(t("You may quickly link to another node using this syntax: <em>[node title|text]</em>. This will generate a link labeled 'text' to the node with the title 'node title'. If you omit '|text', the label becomes 'node title'."));
89
}
90 91

?>