Index: mailhandler.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/mailhandler/Attic/mailhandler.module,v
retrieving revision 1.96.2.32
diff -u -r1.96.2.32 mailhandler.module
--- mailhandler.module	26 Jan 2011 23:53:38 -0000	1.96.2.32
+++ mailhandler.module	27 Jan 2011 11:20:55 -0000
@@ -15,6 +15,11 @@
  * @see http://drupal.org/node/38943 Command documentation.
  */
 
+
+/**
+ * Drupal hooks.
+ */
+
 /**
  * Implementation of hook_cron(). Process msgs from all enabled mailboxes.
  */
@@ -117,17 +122,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) {
@@ -185,16 +179,158 @@
 
 
 /**
- * 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();
+
+  // 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;
+    }
+  }
+
+  // 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;
+  }
+
+  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(
+    '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)
+ *
+ * To be removed once it is not in use by other module. Currently this is just a
+ * wrapper around mailhandler_mailbox_load().
  */
 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

