You are here

function publication_date_nodeapi in Publication Date 6

Implementation of hook_nodeapi(). After each modification / insert / delete, update the publication date

File

./publication_date.module, line 16
Add a field containing the publication date.

Code

function publication_date_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'load':
      $node->published_at = _publication_date_get_date($node->nid);
      break;
    case 'insert':
    case 'update':

      // save publication date
      if ($node->status == 1) {

        // already in database ?
        $date = _publication_date_get_date($node->nid);
        if (!$date) {

          // no, we insert it
          db_query("INSERT INTO {publication_date} (nid, published_at) VALUES (%d, %d)", $node->nid, time());
        }
      }
      else {

        // already in base ?
        $date = _publication_date_get_date($node->nid);
        if ($date) {

          // yes, so we remove it
          db_query("DELETE FROM {publication_date} WHERE nid = %d", $node->nid);
        }
      }
      break;
    case 'delete':
      $date = _publication_date_get_date($node->nid);
      if ($date) {
        db_query("DELETE FROM {publication_date} WHERE nid = %d", $node->nid);
      }
      break;
  }
}