Index: trunk/lib/RecordVersion.inc.php
===================================================================
--- trunk/lib/RecordVersion.inc.php	(revision 111)
+++ trunk/lib/RecordVersion.inc.php	(revision 136)
@@ -1,5 +1,8 @@
 <?php
 /**
- * The RecordVersion:: class provides a system for saving, reviewing, and
+ * RecordVersion.inc.php
+ * code by strangecode :: www.strangecode.com :: this document contains copyrighted information
+ *
+ * The RecordVersion class provides a system for saving, reviewing, and
  * restoring versions of a record of any DB table. All the data in the record is
  * serialized, compressed, and saved in a blob in the version_tbl. Restoring a
@@ -14,5 +17,4 @@
  * @version 2.1
  */
-
 class RecordVersion {
 
@@ -33,5 +35,5 @@
      * This method enforces the singleton pattern for this class.
      *
-     * @return  object  Reference to the global RecordVersion object.
+     * @return  object  Reference to the global RecordLock object.
      * @access  public
      * @static
@@ -39,11 +41,11 @@
     function &getInstance($auth_object)
     {
-        static $instances = array();
-
-        if (!isset($instances[$auth_object->getVal('auth_name')])) {
-            $instances[$auth_object->getVal('auth_name')] = new RecordVersion($auth_object);
-        }
-
-        return $instances[$auth_object->getVal('auth_name')];
+        static $instance = null;
+
+        if ($instance === null) {
+            $instance = new RecordVersion($auth_object);
+        }
+
+        return $instance;
     }
 
@@ -55,6 +57,8 @@
     function RecordVersion($auth_object)
     {
-        if (!is_a($auth_object, 'Auth_SQL')) {
-            trigger_error('Constructor not provided a valid Auth_SQL object.', E_USER_ERROR);
+		$app =& App::getInstance();
+
+        if (!method_exists($auth_object, 'getVal') || !method_exists($auth_object, 'getUsername')) {
+            trigger_error('Constructor not provided a valid Auth_* object.', E_USER_ERROR);
         }
 
@@ -62,6 +66,6 @@
 
         // Get create tables config from global context.
-        if (!is_null(App::getParam('db_create_tables'))) {
-            $this->setParam(array('create_table' => App::getParam('db_create_tables')));
+        if (!is_null($app->getParam('db_create_tables'))) {
+            $this->setParam(array('create_table' => $app->getParam('db_create_tables')));
         }
     }
@@ -76,12 +80,15 @@
     function initDB($recreate_db=false)
     {
+		$app =& App::getInstance();
+		$db =& DB::getInstance();
+
         static $_db_tested = false;
 
         if ($recreate_db || !$_db_tested && $this->getParam('create_table')) {
             if ($recreate_db) {
-                DB::query("DROP TABLE IF EXISTS " . $this->getParam('db_table'));
-                App::logMsg(sprintf('Dropping and recreating table %s.', $this->getParam('db_table')), LOG_DEBUG, __FILE__, __LINE__);
+                $db->query("DROP TABLE IF EXISTS " . $this->getParam('db_table'));
+                $app->logMsg(sprintf('Dropping and recreating table %s.', $this->getParam('db_table')), LOG_DEBUG, __FILE__, __LINE__);
             }
-            DB::query("CREATE TABLE IF NOT EXISTS " . $this->getParam('db_table') . " (
+            $db->query("CREATE TABLE IF NOT EXISTS " . $this->getParam('db_table') . " (
                 version_id int NOT NULL auto_increment,
                 record_table varchar(255) NOT NULL default '',
@@ -99,5 +106,5 @@
             )");
 
-            if (!DB::columnExists($this->getParam('db_table'), array(
+            if (!$db->columnExists($this->getParam('db_table'), array(
                 'version_id',
                 'record_table',
@@ -110,5 +117,5 @@
                 'version_datetime',
             ), false, false)) {
-                App::logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), LOG_ALERT, __FILE__, __LINE__);
+                $app->logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), LOG_ALERT, __FILE__, __LINE__);
                 trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), E_USER_ERROR);
             }
@@ -131,16 +138,18 @@
 
     /**
-     * Return the value of a param setting.
-     *
-     * @access  public
-     * @param   string  $params Which param to return.
-     * @return  mixed   Configured param value.
+     * Return the value of a parameter, if it exists.
+     *
+     * @access public
+     * @param string $param        Which parameter to return.
+     * @return mixed               Configured parameter value.
      */
     function getParam($param)
     {
+		$app =& App::getInstance();
+    
         if (isset($this->_params[$param])) {
             return $this->_params[$param];
         } else {
-            App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
+            $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_NOTICE, __FILE__, __LINE__);
             return null;
         }
@@ -159,9 +168,12 @@
     function create($record_table, $record_key, $record_val, $title='', $notes='')
     {
+		$app =& App::getInstance();
+		$db =& DB::getInstance();
+
         $this->initDB();
 
         // Get current record.
         if (!$record = $this->getCurrent($record_table, $record_key, $record_val)) {
-            App::logMsg(sprintf('Could not create %s version, record not found: %s, %s, %s.', $title, $record_table, $record_key, $record_val), LOG_ERR, __FILE__, __LINE__);
+            $app->logMsg(sprintf('Could not create %s version, record not found: %s, %s, %s.', $title, $record_table, $record_key, $record_val), LOG_ERR, __FILE__, __LINE__);
             return false;
         }
@@ -171,5 +183,5 @@
 
         // Save as new version.
-        DB::query("
+        $db->query("
             INSERT INTO " . $this->getParam('db_table') . " (
                 record_table,
@@ -182,16 +194,16 @@
                 version_datetime
             ) VALUES (
-                '" . DB::escapeString($record_table) . "',
-                '" . DB::escapeString($record_key) . "',
-                '" . DB::escapeString($record_val) . "',
-                '" . DB::escapeString(gzcompress(serialize($record), 9)) . "',
-                '" . DB::escapeString($title) . "',
-                '" . DB::escapeString($notes) . "',
-                '" . DB::escapeString($this->_auth->getVal('user_id')) . "',
+                '" . $db->escapeString($record_table) . "',
+                '" . $db->escapeString($record_key) . "',
+                '" . $db->escapeString($record_val) . "',
+                '" . $db->escapeString(gzcompress(serialize($record), 9)) . "',
+                '" . $db->escapeString($title) . "',
+                '" . $db->escapeString($notes) . "',
+                '" . $db->escapeString($this->_auth->getVal('user_id')) . "',
                 NOW()
             )
         ");
 
-        return mysql_insert_id(DB::getDBH());
+        return mysql_insert_id($db->getDBH());
     }
 
@@ -205,14 +217,17 @@
     function restore($version_id)
     {
+		$app =& App::getInstance();
+		$db =& DB::getInstance();
+
         $this->initDB();
 
         // Get version data.
-        $qid = DB::query("
+        $qid = $db->query("
             SELECT * FROM " . $this->getParam('db_table') . "
-            WHERE version_id = '" . DB::escapeString($version_id) . "'
+            WHERE version_id = '" . $db->escapeString($version_id) . "'
         ");
         if (!$record = mysql_fetch_assoc($qid)) {
-            App::raiseMsg(sprintf(_("Version ID %s%s not found."), $version_id, (empty($record['version_title']) ? '' : ' (' . $record['version_title'] . ')')), MSG_WARNING, __FILE__, __LINE__);
-            App::logMsg(sprintf(_("Version ID %s%s not found."), $version_id, (empty($record['version_title']) ? '' : ' (' . $record['version_title'] . ')')), LOG_WARNING, __FILE__, __LINE__);
+            $app->raiseMsg(sprintf(_("Version ID %s%s not found."), $version_id, (empty($record['version_title']) ? '' : ' (' . $record['version_title'] . ')')), MSG_WARNING, __FILE__, __LINE__);
+            $app->logMsg(sprintf(_("Version ID %s%s not found."), $version_id, (empty($record['version_title']) ? '' : ' (' . $record['version_title'] . ')')), LOG_WARNING, __FILE__, __LINE__);
             return false;
         }
@@ -220,12 +235,12 @@
 
         // Ensure saved db columns match current table schema.
-        if (!DB::columnExists($record['record_table'], array_keys($data), $this->getParam('db_schema_strict'))) {
-            App::raiseMsg(sprintf(_("Version ID %s%s is not compatible with the current database table."), $version_id, (empty($record['version_title']) ? '' : ' (' . $record['version_title'] . ')')), MSG_ERR, __FILE__, __LINE__);
-            App::logMsg(sprintf(_("Version ID %s%s restoration failed, DB schema does not match for table %s."), $version_id, (empty($record['version_title']) ? '' : ' (' . $record['version_title'] . ')'), $record['record_table']), LOG_ALERT, __FILE__, __LINE__);
+        if (!$db->columnExists($record['record_table'], array_keys($data), $this->getParam('db_schema_strict'))) {
+            $app->raiseMsg(sprintf(_("Version ID %s%s is not compatible with the current database table."), $version_id, (empty($record['version_title']) ? '' : ' (' . $record['version_title'] . ')')), MSG_ERR, __FILE__, __LINE__);
+            $app->logMsg(sprintf(_("Version ID %s%s restoration failed, DB schema does not match for table %s."), $version_id, (empty($record['version_title']) ? '' : ' (' . $record['version_title'] . ')'), $record['record_table']), LOG_ALERT, __FILE__, __LINE__);
             return false;
         }
 
         // SQLize the keys of the specified versioned record.
-        $replace_keys = join(",\n", array_map(array('DB', 'escapeString'), array_keys($data)));
+        $replace_keys = join(",\n", array_map(array($db, 'escapeString'), array_keys($data)));
 
         // SQLize the keys of the values of the specified versioned record. (These are more complex because we need to account for SQL null values.)
@@ -233,10 +248,10 @@
         $comma = '';
         foreach ($data as $v) {
-            $replace_values .= is_null($v) ? "$comma\nNULL" : "$comma\n'" . DB::escapeString($v) . "'";
+            $replace_values .= is_null($v) ? "$comma\nNULL" : "$comma\n'" . $db->escapeString($v) . "'";
             $comma = ',';
         }
 
         // Replace current record with specified versioned record.
-        DB::query("
+        $db->query("
             REPLACE INTO " . $record['record_table'] . " (
                 $replace_keys
@@ -263,12 +278,14 @@
     function deleteOld($record_table, $record_key, $record_val)
     {
+		$db =& DB::getInstance();
+	
         $this->initDB();
 
         // Get total number of versions for this record.
-        $qid = DB::query("
+        $qid = $db->query("
             SELECT COUNT(*) FROM " . $this->getParam('db_table') . "
-            WHERE record_table = '" . DB::escapeString($record_table) . "'
-            AND record_key = '" . DB::escapeString($record_key) . "'
-            AND record_val = '" . DB::escapeString($record_val) . "'
+            WHERE record_table = '" . $db->escapeString($record_table) . "'
+            AND record_key = '" . $db->escapeString($record_key) . "'
+            AND record_val = '" . $db->escapeString($record_val) . "'
         ");
         list($v_count) = mysql_fetch_row($qid);
@@ -278,9 +295,9 @@
                 // To prevent a record bomb, limit max number of versions to max_qty.
                 // First query for oldest records, selecting enough to bring total number down to min_qty.
-                $qid = DB::query("
+                $qid = $db->query("
                     SELECT version_id FROM " . $this->getParam('db_table') . "
-                    WHERE record_table = '" . DB::escapeString($record_table) . "'
-                    AND record_key = '" . DB::escapeString($record_key) . "'
-                    AND record_val = '" . DB::escapeString($record_val) . "'
+                    WHERE record_table = '" . $db->escapeString($record_table) . "'
+                    AND record_key = '" . $db->escapeString($record_key) . "'
+                    AND record_val = '" . $db->escapeString($record_val) . "'
                     ORDER BY version_datetime ASC
                     LIMIT " . ($v_count - $this->getParam('min_qty')) . "
@@ -289,5 +306,5 @@
                     $old_versions[] = $old_id;
                 }
-                DB::query("
+                $db->query("
                     DELETE FROM " . $this->getParam('db_table') . "
                     WHERE version_id IN ('" . join("','", $old_versions) . "')
@@ -295,9 +312,9 @@
             } else {
                 // Delete versions older than min_days, while still keeping min_qty.
-                $qid = DB::query("
+                $qid = $db->query("
                     SELECT version_id FROM " . $this->getParam('db_table') . "
-                    WHERE record_table = '" . DB::escapeString($record_table) . "'
-                    AND record_key = '" . DB::escapeString($record_key) . "'
-                    AND record_val = '" . DB::escapeString($record_val) . "'
+                    WHERE record_table = '" . $db->escapeString($record_table) . "'
+                    AND record_key = '" . $db->escapeString($record_key) . "'
+                    AND record_val = '" . $db->escapeString($record_val) . "'
                     AND DATE_ADD(version_datetime, INTERVAL '" . $this->getParam('min_days') . "' DAY) < NOW()
                     ORDER BY version_datetime ASC
@@ -308,5 +325,5 @@
                 }
                 if (sizeof($old_versions) > 0) {
-                    DB::query("
+                    $db->query("
                         DELETE FROM " . $this->getParam('db_table') . "
                         WHERE version_id IN ('" . join("','", $old_versions) . "')
@@ -328,13 +345,15 @@
     function getList($record_table, $record_key, $record_val)
     {
+		$db =& DB::getInstance();
+	
         $this->initDB();
 
         // Get versions of this record.
-        $qid = DB::query("
+        $qid = $db->query("
             SELECT version_id, saved_by_admin_id, version_datetime, version_title
             FROM " . $this->getParam('db_table') . "
-            WHERE record_table = '" . DB::escapeString($record_table) . "'
-            AND record_key = '" . DB::escapeString($record_key) . "'
-            AND record_val = '" . DB::escapeString($record_val) . "'
+            WHERE record_table = '" . $db->escapeString($record_table) . "'
+            AND record_key = '" . $db->escapeString($record_key) . "'
+            AND record_val = '" . $db->escapeString($record_val) . "'
             ORDER BY version_datetime DESC
         ");
@@ -357,10 +376,12 @@
     function getVerson($version_id)
     {
+		$db =& DB::getInstance();
+	
         $this->initDB();
 
         // Get version data.
-        $qid = DB::query("
+        $qid = $db->query("
             SELECT * FROM " . $this->getParam('db_table') . "
-            WHERE version_id = '" . DB::escapeString($version_id) . "'
+            WHERE version_id = '" . $db->escapeString($version_id) . "'
         ");
         return mysql_fetch_assoc($qid);
@@ -376,10 +397,12 @@
     function getData($version_id)
     {
+		$db =& DB::getInstance();
+	
         $this->initDB();
 
         // Get version data.
-        $qid = DB::query("
+        $qid = $db->query("
             SELECT * FROM " . $this->getParam('db_table') . "
-            WHERE version_id = '" . DB::escapeString($version_id) . "'
+            WHERE version_id = '" . $db->escapeString($version_id) . "'
         ");
         $record = mysql_fetch_assoc($qid);
@@ -400,9 +423,11 @@
     function getCurrent($record_table, $record_key, $record_val)
     {
-        $this->initDB();
-
-        $qid = DB::query("
-            SELECT * FROM " . DB::escapeString($record_table) . "
-            WHERE " . DB::escapeString($record_key) . " = '" . DB::escapeString($record_val) . "'
+		$db =& DB::getInstance();
+	
+        $this->initDB();
+
+        $qid = $db->query("
+            SELECT * FROM " . $db->escapeString($record_table) . "
+            WHERE " . $db->escapeString($record_key) . " = '" . $db->escapeString($record_val) . "'
         ");
         if ($record = mysql_fetch_assoc($qid)) {
