You are here

function mailhandler_mailbox_load_multiple in Mailhandler 6

Load mailboxes from the database.

This function should be used whenever you need to load more than one mailbox from the database. Mailboxes are loaded into memory and will not require database access if loaded again during the same page request.

Parameters

$mids: An array of mailboxes IDs. If specified, the conditions will be applied to this group of mailboxes.

$conditions: An associative array of conditions on the {mailhandler} table, where the keys are the database fields and the values are the values those fields must have. This filter will return only mailboxes matching all the values.

$reset: Whether to reset the internal mailhandler_mailbox_load cache.

Return value

An array of mailboxes indexed by mid

Related topics

3 calls to mailhandler_mailbox_load_multiple()
mailhandler_cron in ./mailhandler.module
Implementation of hook_cron().
mailhandler_list_mailboxes in ./mailhandler.admin.inc
Displays a list of mailboxes with operations.
mailhandler_mailbox_load in ./mailhandler.module
Load a mailbox array from database.

File

./mailhandler.module, line 250
Mailhandler module code.

Code

function mailhandler_mailbox_load_multiple($mids = array(), $conditions = array(), $reset = FALSE) {
  $mailboxes = array();

  // Use these variables to compose the query based on $conditions array.
  $schema = drupal_get_schema('mailhandler');
  $fields = drupal_schema_fields_sql('mailhandler');
  $conds = $params = array();

  // Select from this group of mailboxes if defined.
  if (count($mids)) {
    $conds[] = 'mid IN (' . db_placeholders($mids, 'int') . ')';
    foreach ($mids as $mid) {
      $params[] = intval($mid);
    }
  }

  // Apply the conditions filter.
  foreach ($conditions as $key => $value) {
    if (in_array($key, $fields)) {
      $conds[] = $key . " = " . db_type_placeholder($schema['fields'][$key]['type']);
      $params[] = $value;
    }
  }

  // Record debug information.
  if (count($conditions)) {
    mailhandler_watchdog_record('Mailboxes loading conditions: @conditions', array(
      '@conditions' => var_export(array_keys($conditions), TRUE),
    ), WATCHDOG_DEBUG);
  }

  // Build and execute the query.
  $cond = count($conds) ? ' WHERE ' . implode(' AND ', $conds) : '';
  $result = db_query('SELECT ' . implode(', ', $fields) . ' FROM {mailhandler}' . $cond, $params);

  // Populate the return array.
  $search = array();
  while ($mailbox = db_fetch_object($result)) {
    $mailboxes[$mailbox->mid] = $mailbox;
  }

  // Record debug information.
  mailhandler_watchdog_record('Mailboxes loaded: @mailboxes', array(
    '@mailboxes' => var_export(array_keys($mailboxes), TRUE),
  ), WATCHDOG_DEBUG);
  return $mailboxes;
}