Index: mailhandler.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/mailhandler/Attic/mailhandler.module,v
retrieving revision 1.96.2.30
diff -u -r1.96.2.30 mailhandler.module
--- mailhandler.module	4 Aug 2010 18:10:25 -0000	1.96.2.30
+++ mailhandler.module	26 Jan 2011 18:00:53 -0000
@@ -1,6 +1,9 @@
 <?php
 // $Id: mailhandler.module,v 1.96.2.30 2010/08/04 18:10:25 developmentseed Exp $
 
+/**
+ * Drupal Hooks.
+ */
 
 /**
  * Implementation of hook_cron(). Process msgs from all enabled mailboxes.
@@ -105,17 +107,6 @@
 }
 
 /**
- * Load mailbox array, is a menu loader callback
- * @param $mid
- *   Int mailbox ID to load
- * @return
- *   Array of mailbox configuration
- */
-function mailhandler_mailbox_load($mid) {
-  return db_fetch_array(db_query("SELECT * FROM {mailhandler} WHERE mid = %d", $mid));
-}
-
-/**
  * Implementation of hook_help().
  */
 function mailhandler_help($path = 'admin/help#mailhandler', $arg) {
@@ -173,17 +164,160 @@
 
 
 /**
- * Fetch data for a specific mailbox from the database.
- * 
- * TODO: remove this callback now that we have mailhandler_mailbox_load which is also a menu loader, 
- * though, there are other modules that may depend on this legacy callback, like "mail comment"
+ * Mailhandler Mailbox database operation.
+ *
+ * The CRUD implemented here mimics the way CRUD operations are being done in
+ * Drupal 7, to make both versions similar and easy to maintain.
+ */
+
+/**
+ * Load a mailbox array from database.
+ *
+ * Return a mailbox information based on the ID passed as parameter.
+ *
+ * @param $mid
+ *   Int mailbox ID to load
+ * @param $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.
+ * @param $reset
+ *   Whether to reset the internal mailhandler_mailbox_load cache. Unimplemented
+ *   to make module versions easy to maintain.
+ * @return
+ *   Array of mailbox configuration
+ */
+function mailhandler_mailbox_load($mid, $conditions = array(), $reset = FALSE) {
+  $mids = (isset($mid) ? array($mid) : array());
+  $conditions = array();
+  $mailbox = mailhandler_mailbox_load_multiple($mids, $conditions, $reset);
+  return $mailbox ? (array) reset($mailbox) : FALSE;
+}
+
+/**
+ * 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.
+ *
+ * @param $mids
+ *   An array of mailboxes IDs. If specified, the conditions will be applied to
+ *   this group of mailboxes.
+ * @param $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.
+ * @param $reset
+ *   Whether to reset the internal mailhandler_mailbox_load cache.
+ * @return
+ *   An array of mailboxes indexed by mid
+ */
+function mailhandler_mailbox_load_multiple($mids = array(), $conditions = array(), $reset = FALSE) {
+  $mailboxes = array();
+
+  // Compose query based on $conditions array. To build the query use these
+  // variables.
+  $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;
+    }
+  }
+
+  // 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.
+  while ($mailbox = db_fetch_object($result)) {
+    $mailboxes[$mailbox->mid] = $mailbox;
+  }
+
+  return $mailboxes;
+}
+
+/**
+ * Save a Mailhandler Mailbox in the database.
+ *
+ * When $mailbox has 'mid' attribute defined, the database entry with this ID
+ * will be updated, otherwise a new mailbox will be created.
+ *
+ * @param $mailbox
+ *   A mailbox settings array.
+ */
+function mailhandler_mailbox_save(&$mailbox) {
+  // Determine this is an update or a new mailbox.
+  $update = (isset($mailbox['mid']) && $mailbox['mid']) ? array('mid') : array();
+
+  // Set default values for missing mailbox attributes.
+  $mailbox += mailhandler_mailbox_defaults();
+
+  // Save entry into database.
+  drupal_write_record('mailhandler', $mailbox, $update);
+}
+
+/**
+ * Return mailhandler mailbox default attributes.
+ *
+ * @return
+ *   Associative array with mailhandler mailbox default attributes.
+ */
+function mailhandler_mailbox_defaults() {
+  return array(
+    'mid' => NULL,
+    'mail' => '',
+    'mailto' => '',
+    'folder' => 'INBOX',
+    'imap' => 0,
+    'domain' => '',
+    'port' => '',
+    'name' => '',
+    'pass' => '',
+    'extraimap' => '',
+    'mime' => 'TEXT/HTML,TEXT/PLAIN',
+    'security' => 0,
+    'replies' => 1,
+    'fromheader' => '',
+    'commands' => '',
+    'sigseparator' => '',
+    'delete_after_read' => 1,
+    'enabled' => 1,
+    'format' => variable_get('filter_default_format', 1),
+    'authentication' => 'mailhandler_default',
+  );
+}
+
+
+/**
+ * Fetch data for a specific mailbox from the database. (DEPRECATED)
+ *
+ * Wrapper for mailhandler_mailbox_load(), remove when no other modules are 
+ * using it: currently only "mail comment" is known to use it.
  */
 function mailhandler_get_mailbox($mid) {
-  return db_fetch_array(db_query("SELECT * FROM {mailhandler} WHERE mid = %d", $mid));
+  return mailhandler_mailbox_load($mid);
 }
 
 
 /**
+ * Mailhandler mailbox Processing functions.
+ */
+
+/**
  * Return a default content type if the user has not chosen a specific type on the settings page
  * In order of priority, return blog, story, page
  * This assumes that one of these basic types is in use on a site (page and story are active by default)
