node.inc 4.8 KB
Newer Older
Dries's avatar
 
Dries committed
1 2
<?php

Dries's avatar
 
Dries committed
3 4
$status = array(dumped => 0, expired => 1, queued => 2, posted => 3, scheduled => 4);

Dries's avatar
 
Dries committed
5
function _node_get($field, $value) {
Dries's avatar
 
Dries committed
6
  $result = db_query("SELECT lid, type FROM node WHERE $field = '$value'");
Dries's avatar
 
Dries committed
7
  if ($node = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
8
    return db_query("SELECT n.*, l.*, u.userid FROM node n LEFT JOIN $node->type l ON n.lid = l.lid AND n.nid = l.nid LEFT JOIN users u ON n.author = u.id WHERE n.$field = '$value' ORDER BY n.timestamp DESC");
Dries's avatar
 
Dries committed
9 10 11 12 13 14 15 16 17 18 19
  }
}

function node_get_object($field, $value) {
  return db_fetch_object(_node_get($field, $value));
}

function node_get_array($field, $value) {
  return db_fetch_array(_node_get($field, $value));
}

Dries's avatar
 
Dries committed
20 21
function node_del($field, $value) {
  global $status;
Dries's avatar
 
Dries committed
22
  if ($node = node_get_object($field, $value)) {
Dries's avatar
 
Dries committed
23
    if ($node->status == $status[dumped]) {
Dries's avatar
 
Dries committed
24 25 26
      db_query("DELETE FROM node WHERE nid = '$node->nid'");
      db_query("DELETE FROM $node->type WHERE lid = '$node->lid' AND nid = '$node->nid'");
      db_query("DELETE FROM comments WHERE lid = '$node->nid'");
Dries's avatar
 
Dries committed
27
      watchdog("message", "node: deleted '$node->title'");
Dries's avatar
 
Dries committed
28 29
      return $node;
    }
Dries's avatar
 
Dries committed
30 31 32 33
  }
}

function node_save($node) {
Dries's avatar
 
Dries committed
34
  global $user, $status;
Dries's avatar
 
Dries committed
35

Dries's avatar
 
Dries committed
36
  $rows = array(nid, pid, lid, log, type, title, score, votes, author, status, timestamp);
Dries's avatar
 
Dries committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

  if ($node[nid]) {
    $u1 = array();
    $u2 = array();

    foreach ($node as $field=>$value) {
      if (in_array($field, $rows)) {
        array_push($u1, check_input($field) ." = '". check_input($value) ."'");
      }
      else {
        array_push($u2, check_input($field) ." = '". check_input($value) ."'");
      }
    }

    $u1 = implode(", ", $u1);
    $u2 = implode(", ", $u2);

Dries's avatar
 
Dries committed
54 55
    db_query("UPDATE node SET $u1 WHERE nid = '$node[nid]'");
    db_query("UPDATE $node[type] SET $u2 WHERE nid = '$node[nid]'");
Dries's avatar
 
Dries committed
56

Dries's avatar
 
Dries committed
57
    watchdog("message", "node: modified '$node[title]'");
Dries's avatar
 
Dries committed
58 59
  }
  else {
Dries's avatar
 
Dries committed
60
    $duplicate = node_get_object("title", $node[title]);
Dries's avatar
 
Dries committed
61

Dries's avatar
 
Dries committed
62
    if ($duplicate && (time() - $duplicate->timestamp < 300)) {
Dries's avatar
 
Dries committed
63
      watchdog("warning", "node: duplicate '$node[title]'");
Dries's avatar
 
Dries committed
64
    }
Dries's avatar
 
Dries committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    else {
      // setup default values:
      $node = array_merge(array(title => "?", author => $user->id, type => "?", pid => 0, log => "node created", status => $status[queued], score => 0, votes => 0, timestamp => time()), $node);

      // prepare queries:
      $f1 = array();
      $v1 = array();
      $f2 = array();
      $v2 = array();

      foreach ($node as $field=>$value) {
        if (in_array($field, $rows)) {
          array_push($f1, check_input($field));
          array_push($v1, "'". check_input($value) ."'");
        }
        else {
          array_push($f2, check_input($field));
          array_push($v2, "'". check_input($value) ."'");
        }
Dries's avatar
 
Dries committed
84
      }
Dries's avatar
 
Dries committed
85 86 87 88 89 90

      $f1 = implode(", ", $f1);
      $v1 = implode(", ", $v1);
      $f2 = implode(", ", $f2);
      $v2 = implode(", ", $v2);

Dries's avatar
 
Dries committed
91
      db_query("INSERT INTO node ($f1) VALUES ($v1)");
Dries's avatar
 
Dries committed
92
      if ($nid = db_insert_id()) {
Dries's avatar
 
Dries committed
93
        $lid = db_query("INSERT INTO $node[type] ($f2, nid) VALUES ($v2, $nid)");
Dries's avatar
 
Dries committed
94
        if ($lid = db_insert_id()) {
Dries's avatar
 
Dries committed
95
          db_query("UPDATE node SET lid = '$lid' WHERE nid = '$nid'");
Dries's avatar
 
Dries committed
96 97
        }
        else {
Dries's avatar
 
Dries committed
98
          db_query("DELETE FROM node WHERE nid = '$nid'");
Dries's avatar
 
Dries committed
99
        }
Dries's avatar
 
Dries committed
100
      }
Dries's avatar
 
Dries committed
101

Dries's avatar
 
Dries committed
102 103
      watchdog("message", "node: added '$node[title]'");
    }
Dries's avatar
 
Dries committed
104 105 106
  }

  if (($node[pid]) && ($node[status] == $status[posted])) {
Dries's avatar
 
Dries committed
107
    db_query("UPDATE node SET status = '$status[expired]' WHERE nid = '$node[pid]'");
Dries's avatar
 
Dries committed
108 109 110
  }
}

Dries's avatar
 
Dries committed
111
function node_view($node, $page) {
Dries's avatar
 
Dries committed
112 113
  if ($node->type) {
    $function = $node->type ."_view";
Dries's avatar
 
Dries committed
114
    return $function($node, $page);
Dries's avatar
 
Dries committed
115
  }
Dries's avatar
 
Dries committed
116 117 118 119 120 121
}

function node_form($node) {
  if ($node[type]) {
    $function = $node[type] ."_form";
    return $function($node);
Dries's avatar
 
Dries committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  }
}

function node_info($node) {
  global $REQUEST_URI;

 ?>
  <SCRIPT>
   <!--//
     function visit(site) {
       if (site != "") {
         parent.location = site
       }
     }
   //-->
  </SCRIPT>
 <?php

Dries's avatar
 
Dries committed
140
  $choices = array("/node.php?id=$node->nid" => t("view node"), "/submit.php?mod=$node->type&op=update&id=$node->nid" => t("suggest update"), "/node.php?op=history&id=$node->nid" => t("view history"));
Dries's avatar
 
Dries committed
141 142 143 144 145 146 147 148 149 150

  $output .= "<FORM METHOD=\"get\" ACTION=\"\">\n";
  foreach ($choices as $key => $value) $options .= "<OPTION VALUE=\"$key\"". ($key == $REQUEST_URI ? " SELECTED" : "") .">". check_select($value) ."</OPTION>\n";
  $output .= " <SELECT NAME=\"op\" ONCHANGE=\"visit(this.options[this.selectedIndex].value)\">$options</SELECT>\n";
  $output .= "</FORM>\n";

  return $output;
}

function node_visible($node) {
Dries's avatar
 
Dries committed
151 152
  global $user, $status;
  return ($node->status == $status[posted]) || ($node->status == $status[queued] && $user->id) || user_access($user, "node");
Dries's avatar
 
Dries committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
}

function node_post_threshold($node, $threshold = 5) {
  return 3;
}

function node_dump_threshold($node, $threshold = - 3) {
  return -2;
}

function node_timout_threshold($node, $threshold = 10) {
  return 9;
}

?>