You are here

db6.inc in Date Reminder 6.2

File

includes/db6.inc
View source
<?php

/**
 * @file
 * This file contains all of the functions that deal with the database
 * for Date Reminder. The database apis are majorly different between
 * Drupal 6 and Drupal 7, so isolating the database calls in one file
 * keeps most of the module common between the two releases.
 *
 * This version of the file is for Drupal 6.
 */
module_load_include('inc', 'datereminder', 'includes/defines');

/**
 * Clean up any references to a given node or nodes.
 *
 * @param array $nids
 *   nid (or array of nids) to clean up.
 * @param int $en
 *   If not NULL, also remove this nid from {datereminder_enable}.
 *   Defaults to NULL.
 *
 * Note, we'll clean reminders even if this type didn't acutally have
 * reminders enabled. It's always possible that something messed up and
 * left some reminders lying around.
 */
function _datereminder_clean_node_reminders($nids, $en = NULL) {
  if (!is_array($nids)) {
    $nids = array(
      $nids,
    );
  }
  $ph = db_placeholders($nids);
  db_query('DELETE FROM {datereminder} WHERE nid IN (' . $ph . ')', $nids);
  if ($en == NULL) {
    db_query('DELETE FROM {datereminder_enable} WHERE nid IN (' . $ph . ')', $nids);
  }
}

/**
 * Clean out reminders for everything associated with this type.
 *
 * @todo
 *   Note that this is for when reminders are disabled for a type.
 */
function _datereminder_clean_type_reminders($type) {
  if (_datereminder_type_enabled($type) != DATEREMINDER_TYPE_DISABLED) {

    // There must be a way to do this using a join, but I can't figure it out.
    $q = db_query('SELECT nid from {node} WHERE type = \'%s\'', $type);
    $nids = array();
    while ($anode = db_fetch_object($q)) {
      $nids[] = $anode->nid;
    }
    if (count($nids) > 0) {
      _datereminder_clean_node_reminders($nids);
    }
  }
}

/**
 * Enable/disable reminders for a node.
 *
 * Note that this should only be used for node types with reminders
 * enabled. (Or at least in RETAIN state.) If a type is set to DISASBLED,
 * all node-specific information should be cleaned out by calling
 * _datereminder_clean_type_reminders().
 *
 * @param node $node
 *   The node
 * @param int $enabled
 *   Value to set enable flag to for this node. If NULL, remove all
 *   record of this node in the datereminder tables.
 *
 * @todo
 *   This ought to use the database apis instead of the explicit
 *   insert...duplicate key. Probably best to try an update first,
 *   then an insert if that fails. It will only fail once per node.
 *
 *   Probably most of this except the actual database part should be moved
 *   to node.inc.
 */
function _datereminder_set_node_enabled($node, $enabled = NULL) {
  $nid = $node->nid;
  switch ($enabled) {
    case NULL:
    case DATEREMINDER_TYPE_DISABLED:
      _datereminder_clean_node_reminders($nid, $enabled);
      break;
    default:

      // Setting to one of the enabled states. We should probably read,
      // then update or insert. Maybe do update, then insert on failure?
      $q = 'INSERT INTO {datereminder_enable} (nid, enabled)';
      $q .= ' VALUE (%d,%d) ON DUPLICATE KEY UPDATE enabled = %d';
      db_query($q, $nid, $enabled, $enabled);
  }
}

/**
 * Load node enable status from the database.
 *
 * @param int $nid
 *   nid of the node.
 *
 * @return int
 *   Returns the DATEREMINDER_TYPE_* value for this node from
 *   {datereminder_enable}. If NULL, this nid isn't in the database.
 */
function _datereminder_get_node_enabled($nid) {
  $q = db_query('SELECT (enabled) FROM {datereminder_enable} WHERE nid = %d', $node->nid);
  $e = db_fetch_object($q);
  if (empty($e)) {

    // If the reminder isn't explicitly in the database, that means it's
    // disabled, even if "enabled" is default for the type. Reminders won't
    // be turned on for pre-existing nodes until it is explicitly edited.
    return NULL;
  }
  else {
    return $e->enabled;
  }
}

/**
 * Load reminders filtering one one or more keys.
 *
 * @param array $selectors
 *   a key:value array specifying what to load from
 *   the reminders table. Anything in the reminder table is fair game, but
 *   generally the keys are 'nid', 'rid',  or 'uid'.
 * @param node $node
 *   Optional parameter. If set, $r->node is set for any loaded reminder
 *   with the right nid.
 * @param string $sortkey
 *   Optional parameter. If set, returned nodes are sorted in this order.
 *
 * @return array
 *   an array of reminder objects as selected from the database, keyed by rid.
 *   Each object also includes the node title and user name.
 *
 * @example: $reminders = _datereminder_load_reminders(array('uid' => $uid));
 */
function _datereminder_load_reminders($selectors, $node = NULL, $sortkey = NULL) {
  global $user;
  $s = 'SELECT r.*, title, name FROM {node} n INNER JOIN {datereminder} r ';
  $s .= 'USING(nid) INNER JOIN {users} u WHERE r.uid = u.uid';
  $vals = array();
  foreach ($selectors as $k => $v) {
    $s .= " AND r.{$k} = %d";
    $vals[] = $v;
  }
  if ($sortkey != NULL) {
    $s .= " ORDER by r.{$sortkey}";
  }
  $rs = db_rewrite_sql($s);
  $q = db_query($rs, $vals);
  $ret = array();
  while ($r = db_fetch_object($q)) {
    if (isset($node) && $node->nid == $r->nid) {
      $r->node = $node;
    }
    if ($r->uid == $user->uid) {
      $r->user = $user;
    }
    $ret[$r->rid] = $r;
  }
  return $ret;
}

/**
 * Write back user reminder information.
 */
function _datereminder_set_reminders(&$reminders) {
  foreach (array_keys($reminders) as $k) {
    $r = $reminders[$k];

    // Should we save this reminder?
    if ($r->leadtime == 0 || !isset($r->next_due)) {

      // No reminder, so delete any existing entry.
      if ($r->rid > 0) {
        db_query('DELETE FROM {datereminder} WHERE rid = %d', $r->rid);
      }
      unset($reminders[$k]);
    }
    else {
      if ($r->rid > 0) {
        drupal_write_record('datereminder', $r, 'rid');
      }
      else {

        // This is a new entry.
        $ok = drupal_write_record('datereminder', $r);
        if ($ok) {

          // Update worked, so adjust array indix to reflect new rid.
          unset($reminders[$k]);
          $reminders[$r->rid] = $r;
        }
      }
    }
  }
}

/**
 * Delete a list of reminder ids.
 *
 * @param array $rids
 *   List of rids to delete.
 */
function _datereminder_delete_rids($rids) {
  db_query('DELETE FROM {datereminder} WHERE rid IN (' . db_placeholders($rids) . ')', $rids);
}

/**
 * Delete reminders for a set of uids.
 *
 * @param array $uids
 *   List of uids whose reminders should be deleted.
 */
function _datereminder_delete_uids($uids) {
  db_query('DELETE FROM {datereminder} WHERE uid IN (' . db_placeholders($uids) . ')', $uids);
}

/**
 * Return array of reminders with next_due older than $timestamp.
 *
 * @param int $dt
 *   Timestamp for due reminders.
 *
 * @return array
 *   Array of past-due reminders, indexed by rid.
 */
function _datereminder_get_due_reminders($dt) {
  $q = db_query('SELECT * from {datereminder} WHERE next_due <= %d', intval($dt));
  $rems = array();
  while ($r = db_fetch_object($q)) {
    $rems[$r->rid] = $r;
  }
  return $rems;
}

Functions

Namesort descending Description
_datereminder_clean_node_reminders Clean up any references to a given node or nodes.
_datereminder_clean_type_reminders Clean out reminders for everything associated with this type.
_datereminder_delete_rids Delete a list of reminder ids.
_datereminder_delete_uids Delete reminders for a set of uids.
_datereminder_get_due_reminders Return array of reminders with next_due older than $timestamp.
_datereminder_get_node_enabled Load node enable status from the database.
_datereminder_load_reminders Load reminders filtering one one or more keys.
_datereminder_set_node_enabled Enable/disable reminders for a node.
_datereminder_set_reminders Write back user reminder information.