You are here

function _freelinking_exists in Freelinking 5

Same name and namespace in other branches
  1. 6 freelinking.module \_freelinking_exists()
  2. 6.2 freelinking.module \_freelinking_exists()
2 calls to _freelinking_exists()
freelinking_page_form in ./freelinking.module
_freelinking_make_link in ./freelinking.module

File

./freelinking.module, line 463

Code

function _freelinking_exists($thetitle) {

  // helper function for freelinking_page
  // looks through the db for nodes matching $title. Returns the nid if such a node exists, otherwise, returns 0
  $title = urldecode($thetitle);
  $query = "SELECT nid, type FROM {node} WHERE title = '%s'";
  $result = db_query($query, $title);

  // FIXME ***
  while ($node = db_fetch_object($result)) {

    // only one, I hope... what if there's more than one?
    $nid = $node->nid;
    $type = $node->type;
  }

  // no nid on a title search; search aliases
  if (empty($nid)) {
    $query = "SELECT src FROM {url_alias} WHERE dst = '%s'";
    $result = db_query($query, $title);
    while ($realpath = db_fetch_object($result)) {
      preg_match("/node\\/([0-9]+)/", $realpath->src, $nids);
      $nid = $nids[1];
      $query = "SELECT type FROM {node} WHERE nid = %d";

      // get type of node
      $result = db_query($query, $nid);
      $node = db_fetch_object($result);
    }

    // endwhile looping through (one) alias
  }

  // endif no match on title
  // now we've got the $nid and $node->type. Check for restrictions
  $noderestrict = variable_get('freelinking_restriction', 'none');
  if ($noderestrict != 'none') {

    // restrictions in effect
    if ($noderestrict != $type) {

      // node type is not allowed
      return 0;
    }

    // endif node type doesn't match
  }

  // restrictions in effect
  return empty($nid) ? 0 : $nid;
}