Ignore:
Timestamp:
Jun 3, 2006 7:47:48 PM (18 years ago)
Author:
scdev
Message:

Q - Merged branches/2.0singleton into trunk. Completed updating classes to use singleton methods. Implemented tests. Fixed some bugs. Changed some interfaces.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/RecordLock.inc.php

    r111 r136  
    11<?php
    22/**
    3  * The RecordLock:: class provides a system for locking abstract DB rows.
     3 * RecordLock.inc.php
     4 * code by strangecode :: www.strangecode.com :: this document contains copyrighted information
     5 *
     6 * The RecordLock class provides a system for locking abstract DB rows.
    47 *
    58 * @author  Quinn Comendant <quinn@strangecode.com>
    6  * @version 2.0
     9 * @version 2.1
    710 */
    811class RecordLock {
     
    3235    function &getInstance($auth_object)
    3336    {
    34         static $instances = array();
    35 
    36         if (!isset($instances[$auth_object->getVal('auth_name')])) {
    37             $instances[$auth_object->getVal('auth_name')] = new RecordLock($auth_object);
    38         }
    39 
    40         return $instances[$auth_object->getVal('auth_name')];
     37        static $instance = null;
     38
     39        if ($instance === null) {
     40            $instance = new RecordLock($auth_object);
     41        }
     42
     43        return $instance;
    4144    }
    4245
     
    4447     * Constructor. Pass an Auth object on which to perform user lookups.
    4548     *
    46      * @param mixed  $auth_object  An Auth_SQL object.
     49     * @param mixed  $auth_object  An Auth_SQL or Auth_FILE object.
    4750     */
    4851    function RecordLock($auth_object)
    4952    {
    50         if (!is_a($auth_object, 'Auth_SQL')) {
    51             trigger_error('Constructor not provided a valid Auth_SQL object.', E_USER_ERROR);
     53        $app =& App::getInstance();
     54
     55        if (!method_exists($auth_object, 'getVal') || !method_exists($auth_object, 'getUsername')) {
     56            trigger_error('Constructor not provided a valid Auth_* object.', E_USER_ERROR);
    5257        }
    5358
     
    5560
    5661        // Get create tables config from global context.
    57         if (!is_null(App::getParam('db_create_tables'))) {
    58             $this->setParam(array('create_table' => App::getParam('db_create_tables')));
     62        if (!is_null($app->getParam('db_create_tables'))) {
     63            $this->setParam(array('create_table' => $app->getParam('db_create_tables')));
    5964        }
    6065    }
     
    6974    function initDB($recreate_db=false)
    7075    {
     76        $app =& App::getInstance();
     77        $db =& DB::getInstance();
     78
    7179        static $_db_tested = false;
    7280
    7381        if ($recreate_db || !$_db_tested && $this->getParam('create_table')) {
    7482            if ($recreate_db) {
    75                 DB::query("DROP TABLE IF EXISTS " . $this->getParam('db_table'));
    76                 App::logMsg(sprintf('Dropping and recreating table %s.', $this->getParam('db_table')), LOG_DEBUG, __FILE__, __LINE__);
     83                $db->query("DROP TABLE IF EXISTS " . $this->getParam('db_table'));
     84                $app->logMsg(sprintf('Dropping and recreating table %s.', $this->getParam('db_table')), LOG_DEBUG, __FILE__, __LINE__);
    7785            }
    78             DB::query("CREATE TABLE IF NOT EXISTS " . $this->getParam('db_table') . " (
     86            $db->query("CREATE TABLE IF NOT EXISTS " . $this->getParam('db_table') . " (
    7987                lock_id int NOT NULL auto_increment,
    8088                record_table varchar(255) NOT NULL default '',
     
    9098            )");
    9199
    92             if (!DB::columnExists($this->getParam('db_table'), array(
     100            if (!$db->columnExists($this->getParam('db_table'), array(
    93101                'lock_id',
    94102                'record_table',
     
    99107                'lock_datetime',
    100108            ), false, false)) {
    101                 App::logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), LOG_ALERT, __FILE__, __LINE__);
     109                $app->logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), LOG_ALERT, __FILE__, __LINE__);
    102110                trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), E_USER_ERROR);
    103111            }
     
    120128
    121129    /**
    122      * Return the value of a param setting.
    123      *
    124      * @access  public
    125      * @param   string  $params Which param to return.
    126      * @return  mixed   Configured param value.
     130     * Return the value of a parameter, if it exists.
     131     *
     132     * @access public
     133     * @param string $param        Which parameter to return.
     134     * @return mixed               Configured parameter value.
    127135     */
    128136    function getParam($param)
    129137    {
     138        $app =& App::getInstance();
     139   
    130140        if (isset($this->_params[$param])) {
    131141            return $this->_params[$param];
    132142        } else {
    133             App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
     143            $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_NOTICE, __FILE__, __LINE__);
    134144            return null;
    135145        }
     
    147157    function select($record_table_or_lock_id, $record_key=null, $record_val=null)
    148158    {
     159        $app =& App::getInstance();
     160        $db =& DB::getInstance();
     161
    149162        $this->initDB();
    150163
     
    154167        if (is_numeric($record_table_or_lock_id) && !isset($record_key) && !isset($record_val)) {
    155168            // Get lock data by lock_id.
    156             $qid = DB::query("
     169            $qid = $db->query("
    157170                SELECT * FROM " . $this->getParam('db_table') . "
    158                 WHERE lock_id = '" . DB::escapeString($record_table_or_lock_id) . "'
     171                WHERE lock_id = '" . $db->escapeString($record_table_or_lock_id) . "'
    159172            ");
    160173        } else {
    161174            // Get lock data by record specs
    162             $qid = DB::query("
     175            $qid = $db->query("
    163176                SELECT * FROM " . $this->getParam('db_table') . "
    164                 WHERE record_table = '" . DB::escapeString($record_table_or_lock_id) . "'
    165                 AND record_key = '" . DB::escapeString($record_key) . "'
    166                 AND record_val = '" . DB::escapeString($record_val) . "'
     177                WHERE record_table = '" . $db->escapeString($record_table_or_lock_id) . "'
     178                AND record_key = '" . $db->escapeString($record_key) . "'
     179                AND record_val = '" . $db->escapeString($record_val) . "'
    167180            ");
    168181        }
    169182        if ($this->data = mysql_fetch_assoc($qid)) {
    170             App::logMsg(sprintf('Selecting %slocked record: %s %s %s', ($this->data['set_by_admin_id'] == $this->_auth->getVal('user_id') ? 'self-' : ''), $record_table_or_lock_id, $record_key, $record_val), LOG_DEBUG, __FILE__, __LINE__);
     183            $app->logMsg(sprintf('Selecting %slocked record: %s %s %s', ($this->data['set_by_admin_id'] == $this->_auth->getVal('user_id') ? 'self-' : ''), $record_table_or_lock_id, $record_key, $record_val), LOG_DEBUG, __FILE__, __LINE__);
    171184            /// FIX ME: What if admin set lock, but public user is current lock user?
    172185            $this->data['editor'] = $this->_auth->getUsername($this->data['set_by_admin_id']);
    173186            return true;
    174187        } else {
    175             App::logMsg(sprintf('No locked record: %s %s %s', $record_table_or_lock_id, $record_key, $record_val), LOG_DEBUG, __FILE__, __LINE__);
     188            $app->logMsg(sprintf('No locked record: %s %s %s', $record_table_or_lock_id, $record_key, $record_val), LOG_DEBUG, __FILE__, __LINE__);
    176189            return false;
    177190        }
     
    196209    function isMine()
    197210    {
     211        $db =& DB::getInstance();
     212   
    198213        $this->initDB();
    199214
    200215        if (isset($this->data['lock_id'])) {
    201             $qid = DB::query("SELECT * FROM " . $this->getParam('db_table') . " WHERE lock_id = '" . DB::escapeString($this->data['lock_id']) . "'");
     216            $qid = $db->query("SELECT * FROM " . $this->getParam('db_table') . " WHERE lock_id = '" . $db->escapeString($this->data['lock_id']) . "'");
    202217            if ($lock = mysql_fetch_assoc($qid)) {
    203218                return ($lock['set_by_admin_id'] == $this->_auth->getVal('user_id'));
     
    222237    function set($record_table, $record_key, $record_val, $title='')
    223238    {
     239        $db =& DB::getInstance();
     240   
    224241        $this->initDB();
    225242
     
    228245
    229246        // Remove previous locks if exist. Is this better than using a REPLACE INTO?
    230         DB::query("
     247        $db->query("
    231248            DELETE FROM " . $this->getParam('db_table') . "
    232             WHERE record_table = '" . DB::escapeString($record_table) . "'
    233             AND record_key = '" . DB::escapeString($record_key) . "'
    234             AND record_val = '" . DB::escapeString($record_val) . "'
     249            WHERE record_table = '" . $db->escapeString($record_table) . "'
     250            AND record_key = '" . $db->escapeString($record_key) . "'
     251            AND record_val = '" . $db->escapeString($record_val) . "'
    235252        ");
    236253
    237254        // Set new lock.
    238         DB::query("
     255        $db->query("
    239256            INSERT INTO " . $this->getParam('db_table') . " (
    240257                record_table,
     
    245262                lock_datetime
    246263            ) VALUES (
    247                 '" . DB::escapeString($record_table) . "',
    248                 '" . DB::escapeString($record_key) . "',
    249                 '" . DB::escapeString($record_val) . "',
    250                 '" . DB::escapeString($title) . "',
    251                 '" . DB::escapeString($this->_auth->getVal('user_id')) . "',
     264                '" . $db->escapeString($record_table) . "',
     265                '" . $db->escapeString($record_key) . "',
     266                '" . $db->escapeString($record_val) . "',
     267                '" . $db->escapeString($title) . "',
     268                '" . $db->escapeString($this->_auth->getVal('user_id')) . "',
    252269                NOW()
    253270            )
    254271        ");
    255         $lock_id = mysql_insert_id(DB::getDBH());
     272        $lock_id = mysql_insert_id($db->getDBH());
    256273
    257274        // Must register this locked record as the current.
     
    266283    function remove()
    267284    {
     285        $app =& App::getInstance();
     286        $db =& DB::getInstance();
     287
    268288        $this->initDB();
    269289
     
    272292
    273293        // Delete a specific lock.
    274         DB::query("
     294        $db->query("
    275295            DELETE FROM " . $this->getParam('db_table') . "
    276             WHERE lock_id = '" . DB::escapeString($this->data['lock_id']) . "'
     296            WHERE lock_id = '" . $db->escapeString($this->data['lock_id']) . "'
    277297        ");
    278298
    279         App::logMsg(sprintf('Removing lock: %s', $this->data['lock_id']), LOG_DEBUG, __FILE__, __LINE__);
     299        $app->logMsg(sprintf('Removing lock: %s', $this->data['lock_id']), LOG_DEBUG, __FILE__, __LINE__);
    280300    }
    281301
     
    285305    function removeAll($user_id=null)
    286306    {
     307        $app =& App::getInstance();
     308        $db =& DB::getInstance();
     309
    287310        $this->initDB();
    288311
     
    292315        if (isset($user_id)) {
    293316            // Delete specific user's locks.
    294             DB::query("DELETE FROM " . $this->getParam('db_table') . " WHERE set_by_admin_id = '" . DB::escapeString($user_id) . "'");
    295             App::logMsg(sprintf('Record locks owned by %s %s have been deleted', $this->_auth->getVal('auth_name'), $this->_auth->getUsername($user_id)), LOG_DEBUG, __FILE__, __LINE__);
     317            $db->query("DELETE FROM " . $this->getParam('db_table') . " WHERE set_by_admin_id = '" . $db->escapeString($user_id) . "'");
     318            $app->logMsg(sprintf('Record locks owned by %s %s have been deleted', $this->_auth->getVal('auth_name'), $this->_auth->getUsername($user_id)), LOG_DEBUG, __FILE__, __LINE__);
    296319        } else {
    297320            // Delete ALL locks.
    298             DB::query("DELETE FROM " . $this->getParam('db_table') . "");
    299             App::logMsg(sprintf('All record locks deleted by %s %s', $this->_auth->getVal('auth_name'), $this->_auth->getVal('username')), LOG_DEBUG, __FILE__, __LINE__);
     321            $db->query("DELETE FROM " . $this->getParam('db_table') . "");
     322            $app->logMsg(sprintf('All record locks deleted by %s %s', $this->_auth->getVal('auth_name'), $this->_auth->getVal('username')), LOG_DEBUG, __FILE__, __LINE__);
    300323        }
    301324    }
     
    306329    function _auto_timeout()
    307330    {
     331        $db =& DB::getInstance();
     332   
    308333        static $_timeout_run = false;
    309334
     
    312337        if (!$_timeout_run) {
    313338            // Delete all old locks.
    314             DB::query("
     339            $db->query("
    315340                DELETE FROM " . $this->getParam('db_table') . "
    316341                WHERE DATE_ADD(lock_datetime, INTERVAL '" . $this->getParam('auto_timeout') . "' SECOND) < NOW()
     
    325350    function dieErrorPage()
    326351    {
    327         App::dieURL(sprintf('%s?lock_id=%s&boomerang=%s', $this->getParam('error_url'), $this->data['lock_id'], urlencode(absoluteMe())));
     352        $app =& App::getInstance();
     353
     354        $app->dieURL(sprintf('%s?lock_id=%s&boomerang=%s', $this->getParam('error_url'), $this->data['lock_id'], urlencode(absoluteMe())));
    328355    }
    329356
     
    333360    function printErrorHTML()
    334361    {
     362        $app =& App::getInstance();
     363
    335364        ?>
    336365        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    337         <?php App::printHiddenSession() ?>
     366        <?php $app->printHiddenSession() ?>
    338367        <input type="hidden" name="lock_id" value="<?php echo $this->getID(); ?>" />
    339368
Note: See TracChangeset for help on using the changeset viewer.