Index: trunk/bin/module_maker/_config.inc.php
===================================================================
--- trunk/bin/module_maker/_config.inc.php	(revision 150)
+++ trunk/bin/module_maker/_config.inc.php	(revision 151)
@@ -3,9 +3,9 @@
  * code by strangecode :: www.strangecode.com :: this document contains copyrighted information. */
 
-// Determine common site directory.
-$common_base = realpath($_SERVER['argv'][1]);
-
 // Test argument.
-if ($_SERVER['argc'] > 1 && '' != $common_base && is_dir($common_base)) {
+if ($_SERVER['argc'] > 1 && isset($_SERVER['argv'][1]) && '' != $_SERVER['argv'][1] && is_dir($_SERVER['argv'][1])) {
+    // Determine common site directory.
+    $common_base = realpath($_SERVER['argv'][1]);
+    
     // First arg is path to current site. Realpath removes trailing /s
     define('COMMON_BASE', $common_base);
Index: trunk/bin/module_maker/list_template.cli.php
===================================================================
--- trunk/bin/module_maker/list_template.cli.php	(revision 150)
+++ trunk/bin/module_maker/list_template.cli.php	(revision 151)
@@ -16,5 +16,5 @@
     $db_tbl = $_SERVER['argv'][2];
 } else {
-    die(sprintf("Usage: %s site_directory db_table [operation]\n", basename($_SERVER['argv'][0])));
+    die(sprintf("Usage: %s site_directory db_table [operation]\nValid operations include: %s", basename($_SERVER['argv'][0]), join(', ', $valid_ops)));
 }
 
Index: trunk/bin/module_maker/module.cli.php
===================================================================
--- trunk/bin/module_maker/module.cli.php	(revision 150)
+++ trunk/bin/module_maker/module.cli.php	(revision 151)
@@ -18,5 +18,7 @@
     die(basename($_SERVER['argv'][0]) . " Error: invalid arguments. Try like this:
 
-    " . basename($_SERVER['argv'][0]) . " site_directory name_singular name_plural [clean]
+    " . basename($_SERVER['argv'][0]) . " site_directory name_singular name_plural [operation]
+    
+Valid operations include: " . join(', ', $valid_ops) . "
 
 The following files will be generated by this script:
Index: trunk/bin/module_maker/skel/admin.php
===================================================================
--- trunk/bin/module_maker/skel/admin.php	(revision 150)
+++ trunk/bin/module_maker/skel/admin.php	(revision 151)
@@ -33,5 +33,5 @@
 $fv = new FormValidator();
 
-$cache =& SessionCache::getInstance();
+$cache =& Cache::getInstance();
 
 %SORT_ORDER%
@@ -250,5 +250,5 @@
     global $lock;
 	$db =& DB::getInstance();
-	$cache =& SessionCache::getInstance();
+	$cache =& Cache::getInstance();
 	
     $lock->select('%DB_TBL%', '%PRIMARY_KEY%', $id);
@@ -285,5 +285,5 @@
     global $auth;
 	$db =& DB::getInstance();
-	$cache =& SessionCache::getInstance();
+	$cache =& Cache::getInstance();
 	
     // Break the cache because we are changing the list data.
@@ -306,5 +306,5 @@
 	global $auth, $lock;
 	$app =& App::getInstance();
-    $cache =& SessionCache::getInstance();
+    $cache =& Cache::getInstance();
     
     $lock->select('%DB_TBL%', '%PRIMARY_KEY%', $frm['%PRIMARY_KEY%']);
@@ -334,5 +334,5 @@
 	$db =& DB::getInstance();
 	$prefs =& Prefs::getInstance();
-	$cache =& SessionCache::getInstance();
+	$cache =& Cache::getInstance();
 	
     $where_clause = '';
@@ -411,5 +411,5 @@
 {
 	$db =& DB::getInstance();
-	$cache =& SessionCache::getInstance();
+	$cache =& Cache::getInstance();
 	
     if (!is_array($ranks)) {
Index: trunk/bin/module_maker/sql.cli.php
===================================================================
--- trunk/bin/module_maker/sql.cli.php	(revision 150)
+++ trunk/bin/module_maker/sql.cli.php	(revision 151)
@@ -16,5 +16,5 @@
     $db_tbl = $_SERVER['argv'][2];
 } else {
-    die(sprintf("Usage: %s site_directory db_table [operation]\n", basename($_SERVER['argv'][0])));
+    die(sprintf("Usage: %s site_directory db_table [operation]\nValid operations include: %s", basename($_SERVER['argv'][0]), join(', ', $valid_ops)));
 }
 
Index: trunk/bin/module_maker/validation.cli.php
===================================================================
--- trunk/bin/module_maker/validation.cli.php	(revision 150)
+++ trunk/bin/module_maker/validation.cli.php	(revision 151)
@@ -187,8 +187,8 @@
         }
         if ($max_length > 0 && $len_type == 'string') {
-            $o[] = "\$fv->stringLength('$field', 0, $max_length, sprintf(_(\"%s must be %f-to-%f characters in length.\"), _(\"$title\"), 0, $max_length));";
+            $o[] = "\$fv->stringLength('$field', 0, $max_length, sprintf(_(\"%s must be %d-to-%d characters in length.\"), _(\"$title\"), 0, $max_length));";
         }
         if ($len_type == 'num') {
-            $o[] = "\$fv->numericRange('$field', $min, $max, sprintf(_(\"%s must be a number between %f and %f.\"), _(\"$title\"), $min, $max));";
+            $o[] = "\$fv->numericRange('$field', $min, $max, sprintf(_(\"%s must be a number between %d and %d.\"), _(\"$title\"), $min, $max));";
         }
 
Index: trunk/lib/FormValidator.inc.php
===================================================================
--- trunk/lib/FormValidator.inc.php	(revision 150)
+++ trunk/lib/FormValidator.inc.php	(revision 151)
@@ -18,8 +18,8 @@
 
 $fv->empty('field_name', sprintf(_("%s cannot be blank."), _("Field name")));
-$fv->stringLength('field_name', 0, 255, sprintf(_("%s must be %f-to-%f characters in length."), _("Field name"), 0, 255));
+$fv->stringLength('field_name', 0, 255, sprintf(_("%s must be %d-to-%d characters in length."), _("Field name"), 0, 255));
 $fv->isInteger('field_name', sprintf(_("%s must be an integer."), _("Field name")));
 $fv->checkRegex('field_name', '/^\d{4}$|^$/', true, sprintf(_("%s must be in MMYY format."), _("Field name")));
-$fv->numericRange('field_name', 0, 65535, sprintf(_("%s must be a number between %f and %f."), _("Field name"), 0, 65535));
+$fv->numericRange('field_name', 0, 65535, sprintf(_("%s must be a number between %d and %d."), _("Field name"), 0, 65535));
 $fv->validatePhone('field_name');
 $fv->validateEmail('field_name');
Index: trunk/lib/Prefs.inc.php
===================================================================
--- trunk/lib/Prefs.inc.php	(revision 150)
+++ trunk/lib/Prefs.inc.php	(revision 151)
@@ -12,5 +12,5 @@
  * Example of use:
 ---------------------------------------------------------------------
-// Load preferences for the user.
+// Load preferences for the user's session.
 require_once 'codebase/lib/Prefs.inc.php';
 $prefs = new Prefs('bURB');
@@ -24,4 +24,10 @@
 ));
 $prefs->load();
+
+// Update preferences. Make sure to validate this input first!
+$prefs->set('search_num_results', getFormData('search_num_results'));
+$prefs->set('datalog_num_entries', getFormData('datalog_num_entries'));
+$prefs->save();
+
 ---------------------------------------------------------------------
  */
@@ -60,5 +66,5 @@
         
         // Initialized the prefs array.
-        if (!isset($_SESSION[$this->_ns]['data'])) {
+        if (!isset($_SESSION[$this->_ns])) {
             $this->clear();
         }
@@ -143,33 +149,40 @@
 
     /**
-     * Sets the default value of a preference. The pref will be set only if
-     * is not set already.
+     * Sets the default values for preferences. If a preference is not explicitly 
+     * set, the value set here will be used. Can be called multiple times to merge additional
+     * defaults together.
+     *
+     * @param  array $defaults  Array of key-value pairs 
+     */
+    function setDefaults($defaults)
+    {
+        if (isset($defaults) && is_array($defaults)) {
+            $_SESSION[$this->_ns]['defaults'] = array_merge($_SESSION[$this->_ns]['defaults'], $defaults);
+        }
+    }
+
+    /**
+     * Store a key-value pair in the session. If the value is different than what is set by setDefaults
+     * the value will be scheduled to be saved in the database.
+     * This function determins what data is saved to the database. Ensure clean values!
      *
      * @param  string $key       The name of the preference to modify.
      * @param  string $val       The new value for this preference.
      */
-    function setDefaults($defaults)
-    {
-        if (isset($defaults) && is_array($defaults)) {
-            // Apply defaults to the session, setting only non-existing values.
-            $_SESSION[$this->_ns]['data'] = array_merge($defaults, $_SESSION[$this->_ns]['data']);
-        }
-    }
-
-    /**
-     * Sets the given preferences to the specific value,
-     *
-     * @param  string $key       The name of the preference to modify.
-     * @param  string $val       The new value for this preference.
-     */
     function set($key, $val)
     {
-        if ('' != $key && '' != $val) {
-            $_SESSION[$this->_ns]['data'][$key] = $val;
-        }
-    }
-
-    /**
-     * Returns the value of the requested preference.
+        $app =& App::getInstance();
+        if ('' == $key) {
+            $app->logMsg(sprintf('Key is empty (provided with value: %s)', $val), LOG_NOTICE, __FILE__, __LINE__);
+            return false;
+        }
+        if (!isset($_SESSION[$this->_ns]['defaults'][$key]) || $_SESSION[$this->_ns]['defaults'][$key] != $val || isset($_SESSION[$this->_ns]['persistent'][$key])) {
+            $_SESSION[$this->_ns]['persistent'][$key] = $val;            
+        }
+    }
+
+    /**
+     * Returns the value of the requested preference. Persistent values take precedence, but if none is set
+     * a default value is returned, or if not that, null.
      *
      * @param string $key       The name of the preference to retrieve.
@@ -179,22 +192,28 @@
     function get($key)
     {
-        return (isset($_SESSION[$this->_ns]['data'][$key])) ? $_SESSION[$this->_ns]['data'][$key] : null;
-    }
-
-    /**
-     * To see if a preference has been set.
+        $app =& App::getInstance();
+        if (isset($_SESSION[$this->_ns]['persistent'][$key])) {
+            return $_SESSION[$this->_ns]['persistent'][$key];
+        } else if (isset($_SESSION[$this->_ns]['defaults'][$key])) {
+            return $_SESSION[$this->_ns]['defaults'][$key];
+        } else {
+            $app->logMsg(sprintf('Key not defined in default or persistent prefs cache: %s', $key), LOG_NOTICE, __FILE__, __LINE__);
+            return null;
+        }
+    }
+
+    /**
+     * To see if a persistent preference has been set.
      *
      * @param string $key       The name of the preference to check.
-     *
-     * @return boolean          True if the preference isset and not empty
-     *                          false otherwise.
+     * @return boolean          True if the preference isset and not empty false otherwise.
      */
     function exists($key)
     {
-        return isset($_SESSION[$this->_ns]['data'][$key]);
-    }
-
-    /**
-     * Clear a set preference value.
+        return isset($_SESSION[$this->_ns]['persistent'][$key]);
+    }
+
+    /**
+     * Clear a set preference value. This will also remove the value from the database. 
      *
      * @param string $key       The name of the preference to delete.
@@ -202,9 +221,10 @@
     function delete($key)
     {
-        unset($_SESSION[$this->_ns]['data'][$key]);
-    }
-
-    /**
-     * Empty the $_SESSION cache. This should be executed with the same consideration as $auth->clear()
+        unset($_SESSION[$this->_ns]['persistent'][$key]);
+    }
+
+    /**
+     * Resets the $_SESSION cache. This should be executed with the same consideration 
+     * as $auth->clear(), such as when logging out.
      */
     function clear()
@@ -213,10 +233,11 @@
             'loaded' => false,
             'load_datetime' => '1970-01-01',
-            'data' => array()
+            'defaults' => array(),
+            'persistent' => array(),
         );
     }
     
     /*
-    * Retreives all prefs from the database and stores them in the $_SESSION.
+    * Retrieves all prefs from the database and stores them in the $_SESSION.
     *
     * @access   public
@@ -246,5 +267,5 @@
         // User_id must not be empty.
         if ('' == $this->getParam('user_id')) {
-            $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_ERR, __FILE__, __LINE__);
+            $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_WARNING, __FILE__, __LINE__);
             return false;
         }
@@ -253,5 +274,5 @@
         $this->clear();
         
-        // Retreive all prefs for this user and namespace.
+        // Retrieve all prefs for this user and namespace.
 		$qid = $db->query("
 		    SELECT pref_key, pref_value
@@ -262,8 +283,8 @@
 		");
 		while (list($key, $val) = mysql_fetch_row($qid)) {
-            $_SESSION[$this->_ns]['data'][$key] = $val;
+            $_SESSION[$this->_ns]['persistent'][$key] = $val;
 		}
 		
-		$app->logMsg(sprintf('%s prefs loaded from database.', sizeof($_SESSION[$this->_ns]['data'])), LOG_DEBUG, __FILE__, __LINE__);
+		$app->logMsg(sprintf('Loaded %s prefs from database.', sizeof($_SESSION[$this->_ns]['persistent'])), LOG_DEBUG, __FILE__, __LINE__);
 		
 		// Data loaded only once per session.
@@ -317,5 +338,5 @@
         // User_id must not be empty.
         if ('' == $this->getParam('user_id')) {
-            $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_ERR, __FILE__, __LINE__);
+            $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_WARNING, __FILE__, __LINE__);
             return false;
         }
@@ -323,5 +344,5 @@
         $this->initDB();
 
-        if (isset($_SESSION[$this->_ns]['data']) && is_array($_SESSION[$this->_ns]['data'])) {
+        if (isset($_SESSION[$this->_ns]['persistent']) && is_array($_SESSION[$this->_ns]['persistent'])) {
             // Delete old prefs from database.
             $db->query("
@@ -333,8 +354,6 @@
             // Insert new prefs.
             $insert_values = array();
-            foreach ($_SESSION[$this->_ns]['data'] as $key => $val) {
-                if ('' != trim($key) && '' != trim($val)) {
-                    $insert_values[] = sprintf("('%s', '%s', '%s', '%s')", $db->escapeString($this->getParam('user_id')), $db->escapeString($this->_ns), $db->escapeString($key), $db->escapeString($val));
-                }
+            foreach ($_SESSION[$this->_ns]['persistent'] as $key => $val) {
+                $insert_values[] = sprintf("('%s', '%s', '%s', '%s')", $db->escapeString($this->getParam('user_id')), $db->escapeString($this->_ns), $db->escapeString($key), $db->escapeString($val));
             }
             $db->query("
@@ -344,5 +363,5 @@
             ");
             
-            $app->logMsg(sprintf('Saved %s preferences to database.', sizeof($insert_values)), LOG_DEBUG, __FILE__, __LINE__);
+            $app->logMsg(sprintf('Saved %s prefs to database.', sizeof($insert_values)), LOG_DEBUG, __FILE__, __LINE__);
             return true;
         }
