Changeset 151


Ignore:
Timestamp:
Jun 6, 2006 3:19:17 AM (18 years ago)
Author:
scdev
Message:

Q - Changed one more SessionCache? -> Cache, small bug fixt to Prefs, added operation help to modulemaker scripts.

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/bin/module_maker/_config.inc.php

    r136 r151  
    33 * code by strangecode :: www.strangecode.com :: this document contains copyrighted information. */
    44
    5 // Determine common site directory.
    6 $common_base = realpath($_SERVER['argv'][1]);
    7 
    85// Test argument.
    9 if ($_SERVER['argc'] > 1 && '' != $common_base && is_dir($common_base)) {
     6if ($_SERVER['argc'] > 1 && isset($_SERVER['argv'][1]) && '' != $_SERVER['argv'][1] && is_dir($_SERVER['argv'][1])) {
     7    // Determine common site directory.
     8    $common_base = realpath($_SERVER['argv'][1]);
     9   
    1010    // First arg is path to current site. Realpath removes trailing /s
    1111    define('COMMON_BASE', $common_base);
  • trunk/bin/module_maker/list_template.cli.php

    r136 r151  
    1616    $db_tbl = $_SERVER['argv'][2];
    1717} else {
    18     die(sprintf("Usage: %s site_directory db_table [operation]\n", basename($_SERVER['argv'][0])));
     18    die(sprintf("Usage: %s site_directory db_table [operation]\nValid operations include: %s", basename($_SERVER['argv'][0]), join(', ', $valid_ops)));
    1919}
    2020
  • trunk/bin/module_maker/module.cli.php

    r143 r151  
    1818    die(basename($_SERVER['argv'][0]) . " Error: invalid arguments. Try like this:
    1919
    20     " . basename($_SERVER['argv'][0]) . " site_directory name_singular name_plural [clean]
     20    " . basename($_SERVER['argv'][0]) . " site_directory name_singular name_plural [operation]
     21   
     22Valid operations include: " . join(', ', $valid_ops) . "
    2123
    2224The following files will be generated by this script:
  • trunk/bin/module_maker/skel/admin.php

    r143 r151  
    3333$fv = new FormValidator();
    3434
    35 $cache =& SessionCache::getInstance();
     35$cache =& Cache::getInstance();
    3636
    3737%SORT_ORDER%
     
    250250    global $lock;
    251251    $db =& DB::getInstance();
    252     $cache =& SessionCache::getInstance();
     252    $cache =& Cache::getInstance();
    253253   
    254254    $lock->select('%DB_TBL%', '%PRIMARY_KEY%', $id);
     
    285285    global $auth;
    286286    $db =& DB::getInstance();
    287     $cache =& SessionCache::getInstance();
     287    $cache =& Cache::getInstance();
    288288   
    289289    // Break the cache because we are changing the list data.
     
    306306    global $auth, $lock;
    307307    $app =& App::getInstance();
    308     $cache =& SessionCache::getInstance();
     308    $cache =& Cache::getInstance();
    309309   
    310310    $lock->select('%DB_TBL%', '%PRIMARY_KEY%', $frm['%PRIMARY_KEY%']);
     
    334334    $db =& DB::getInstance();
    335335    $prefs =& Prefs::getInstance();
    336     $cache =& SessionCache::getInstance();
     336    $cache =& Cache::getInstance();
    337337   
    338338    $where_clause = '';
     
    411411{
    412412    $db =& DB::getInstance();
    413     $cache =& SessionCache::getInstance();
     413    $cache =& Cache::getInstance();
    414414   
    415415    if (!is_array($ranks)) {
  • trunk/bin/module_maker/sql.cli.php

    r147 r151  
    1616    $db_tbl = $_SERVER['argv'][2];
    1717} else {
    18     die(sprintf("Usage: %s site_directory db_table [operation]\n", basename($_SERVER['argv'][0])));
     18    die(sprintf("Usage: %s site_directory db_table [operation]\nValid operations include: %s", basename($_SERVER['argv'][0]), join(', ', $valid_ops)));
    1919}
    2020
  • trunk/bin/module_maker/validation.cli.php

    r145 r151  
    187187        }
    188188        if ($max_length > 0 && $len_type == 'string') {
    189             $o[] = "\$fv->stringLength('$field', 0, $max_length, sprintf(_(\"%s must be %f-to-%f characters in length.\"), _(\"$title\"), 0, $max_length));";
     189            $o[] = "\$fv->stringLength('$field', 0, $max_length, sprintf(_(\"%s must be %d-to-%d characters in length.\"), _(\"$title\"), 0, $max_length));";
    190190        }
    191191        if ($len_type == 'num') {
    192             $o[] = "\$fv->numericRange('$field', $min, $max, sprintf(_(\"%s must be a number between %f and %f.\"), _(\"$title\"), $min, $max));";
     192            $o[] = "\$fv->numericRange('$field', $min, $max, sprintf(_(\"%s must be a number between %d and %d.\"), _(\"$title\"), $min, $max));";
    193193        }
    194194
  • trunk/lib/FormValidator.inc.php

    r144 r151  
    1818
    1919$fv->empty('field_name', sprintf(_("%s cannot be blank."), _("Field name")));
    20 $fv->stringLength('field_name', 0, 255, sprintf(_("%s must be %f-to-%f characters in length."), _("Field name"), 0, 255));
     20$fv->stringLength('field_name', 0, 255, sprintf(_("%s must be %d-to-%d characters in length."), _("Field name"), 0, 255));
    2121$fv->isInteger('field_name', sprintf(_("%s must be an integer."), _("Field name")));
    2222$fv->checkRegex('field_name', '/^\d{4}$|^$/', true, sprintf(_("%s must be in MMYY format."), _("Field name")));
    23 $fv->numericRange('field_name', 0, 65535, sprintf(_("%s must be a number between %f and %f."), _("Field name"), 0, 65535));
     23$fv->numericRange('field_name', 0, 65535, sprintf(_("%s must be a number between %d and %d."), _("Field name"), 0, 65535));
    2424$fv->validatePhone('field_name');
    2525$fv->validateEmail('field_name');
  • trunk/lib/Prefs.inc.php

    r150 r151  
    1212 * Example of use:
    1313---------------------------------------------------------------------
    14 // Load preferences for the user.
     14// Load preferences for the user's session.
    1515require_once 'codebase/lib/Prefs.inc.php';
    1616$prefs = new Prefs('bURB');
     
    2424));
    2525$prefs->load();
     26
     27// Update preferences. Make sure to validate this input first!
     28$prefs->set('search_num_results', getFormData('search_num_results'));
     29$prefs->set('datalog_num_entries', getFormData('datalog_num_entries'));
     30$prefs->save();
     31
    2632---------------------------------------------------------------------
    2733 */
     
    6066       
    6167        // Initialized the prefs array.
    62         if (!isset($_SESSION[$this->_ns]['data'])) {
     68        if (!isset($_SESSION[$this->_ns])) {
    6369            $this->clear();
    6470        }
     
    143149
    144150    /**
    145      * Sets the default value of a preference. The pref will be set only if
    146      * is not set already.
     151     * Sets the default values for preferences. If a preference is not explicitly
     152     * set, the value set here will be used. Can be called multiple times to merge additional
     153     * defaults together.
     154     *
     155     * @param  array $defaults  Array of key-value pairs
     156     */
     157    function setDefaults($defaults)
     158    {
     159        if (isset($defaults) && is_array($defaults)) {
     160            $_SESSION[$this->_ns]['defaults'] = array_merge($_SESSION[$this->_ns]['defaults'], $defaults);
     161        }
     162    }
     163
     164    /**
     165     * Store a key-value pair in the session. If the value is different than what is set by setDefaults
     166     * the value will be scheduled to be saved in the database.
     167     * This function determins what data is saved to the database. Ensure clean values!
    147168     *
    148169     * @param  string $key       The name of the preference to modify.
    149170     * @param  string $val       The new value for this preference.
    150171     */
    151     function setDefaults($defaults)
    152     {
    153         if (isset($defaults) && is_array($defaults)) {
    154             // Apply defaults to the session, setting only non-existing values.
    155             $_SESSION[$this->_ns]['data'] = array_merge($defaults, $_SESSION[$this->_ns]['data']);
    156         }
    157     }
    158 
    159     /**
    160      * Sets the given preferences to the specific value,
    161      *
    162      * @param  string $key       The name of the preference to modify.
    163      * @param  string $val       The new value for this preference.
    164      */
    165172    function set($key, $val)
    166173    {
    167         if ('' != $key && '' != $val) {
    168             $_SESSION[$this->_ns]['data'][$key] = $val;
    169         }
    170     }
    171 
    172     /**
    173      * Returns the value of the requested preference.
     174        $app =& App::getInstance();
     175        if ('' == $key) {
     176            $app->logMsg(sprintf('Key is empty (provided with value: %s)', $val), LOG_NOTICE, __FILE__, __LINE__);
     177            return false;
     178        }
     179        if (!isset($_SESSION[$this->_ns]['defaults'][$key]) || $_SESSION[$this->_ns]['defaults'][$key] != $val || isset($_SESSION[$this->_ns]['persistent'][$key])) {
     180            $_SESSION[$this->_ns]['persistent'][$key] = $val;           
     181        }
     182    }
     183
     184    /**
     185     * Returns the value of the requested preference. Persistent values take precedence, but if none is set
     186     * a default value is returned, or if not that, null.
    174187     *
    175188     * @param string $key       The name of the preference to retrieve.
     
    179192    function get($key)
    180193    {
    181         return (isset($_SESSION[$this->_ns]['data'][$key])) ? $_SESSION[$this->_ns]['data'][$key] : null;
    182     }
    183 
    184     /**
    185      * To see if a preference has been set.
     194        $app =& App::getInstance();
     195        if (isset($_SESSION[$this->_ns]['persistent'][$key])) {
     196            return $_SESSION[$this->_ns]['persistent'][$key];
     197        } else if (isset($_SESSION[$this->_ns]['defaults'][$key])) {
     198            return $_SESSION[$this->_ns]['defaults'][$key];
     199        } else {
     200            $app->logMsg(sprintf('Key not defined in default or persistent prefs cache: %s', $key), LOG_NOTICE, __FILE__, __LINE__);
     201            return null;
     202        }
     203    }
     204
     205    /**
     206     * To see if a persistent preference has been set.
    186207     *
    187208     * @param string $key       The name of the preference to check.
    188      *
    189      * @return boolean          True if the preference isset and not empty
    190      *                          false otherwise.
     209     * @return boolean          True if the preference isset and not empty false otherwise.
    191210     */
    192211    function exists($key)
    193212    {
    194         return isset($_SESSION[$this->_ns]['data'][$key]);
    195     }
    196 
    197     /**
    198      * Clear a set preference value.
     213        return isset($_SESSION[$this->_ns]['persistent'][$key]);
     214    }
     215
     216    /**
     217     * Clear a set preference value. This will also remove the value from the database.
    199218     *
    200219     * @param string $key       The name of the preference to delete.
     
    202221    function delete($key)
    203222    {
    204         unset($_SESSION[$this->_ns]['data'][$key]);
    205     }
    206 
    207     /**
    208      * Empty the $_SESSION cache. This should be executed with the same consideration as $auth->clear()
     223        unset($_SESSION[$this->_ns]['persistent'][$key]);
     224    }
     225
     226    /**
     227     * Resets the $_SESSION cache. This should be executed with the same consideration
     228     * as $auth->clear(), such as when logging out.
    209229     */
    210230    function clear()
     
    213233            'loaded' => false,
    214234            'load_datetime' => '1970-01-01',
    215             'data' => array()
     235            'defaults' => array(),
     236            'persistent' => array(),
    216237        );
    217238    }
    218239   
    219240    /*
    220     * Retreives all prefs from the database and stores them in the $_SESSION.
     241    * Retrieves all prefs from the database and stores them in the $_SESSION.
    221242    *
    222243    * @access   public
     
    246267        // User_id must not be empty.
    247268        if ('' == $this->getParam('user_id')) {
    248             $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_ERR, __FILE__, __LINE__);
     269            $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_WARNING, __FILE__, __LINE__);
    249270            return false;
    250271        }
     
    253274        $this->clear();
    254275       
    255         // Retreive all prefs for this user and namespace.
     276        // Retrieve all prefs for this user and namespace.
    256277        $qid = $db->query("
    257278            SELECT pref_key, pref_value
     
    262283        ");
    263284        while (list($key, $val) = mysql_fetch_row($qid)) {
    264             $_SESSION[$this->_ns]['data'][$key] = $val;
     285            $_SESSION[$this->_ns]['persistent'][$key] = $val;
    265286        }
    266287       
    267         $app->logMsg(sprintf('%s prefs loaded from database.', sizeof($_SESSION[$this->_ns]['data'])), LOG_DEBUG, __FILE__, __LINE__);
     288        $app->logMsg(sprintf('Loaded %s prefs from database.', sizeof($_SESSION[$this->_ns]['persistent'])), LOG_DEBUG, __FILE__, __LINE__);
    268289       
    269290        // Data loaded only once per session.
     
    317338        // User_id must not be empty.
    318339        if ('' == $this->getParam('user_id')) {
    319             $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_ERR, __FILE__, __LINE__);
     340            $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_WARNING, __FILE__, __LINE__);
    320341            return false;
    321342        }
     
    323344        $this->initDB();
    324345
    325         if (isset($_SESSION[$this->_ns]['data']) && is_array($_SESSION[$this->_ns]['data'])) {
     346        if (isset($_SESSION[$this->_ns]['persistent']) && is_array($_SESSION[$this->_ns]['persistent'])) {
    326347            // Delete old prefs from database.
    327348            $db->query("
     
    333354            // Insert new prefs.
    334355            $insert_values = array();
    335             foreach ($_SESSION[$this->_ns]['data'] as $key => $val) {
    336                 if ('' != trim($key) && '' != trim($val)) {
    337                     $insert_values[] = sprintf("('%s', '%s', '%s', '%s')", $db->escapeString($this->getParam('user_id')), $db->escapeString($this->_ns), $db->escapeString($key), $db->escapeString($val));
    338                 }
     356            foreach ($_SESSION[$this->_ns]['persistent'] as $key => $val) {
     357                $insert_values[] = sprintf("('%s', '%s', '%s', '%s')", $db->escapeString($this->getParam('user_id')), $db->escapeString($this->_ns), $db->escapeString($key), $db->escapeString($val));
    339358            }
    340359            $db->query("
     
    344363            ");
    345364           
    346             $app->logMsg(sprintf('Saved %s preferences to database.', sizeof($insert_values)), LOG_DEBUG, __FILE__, __LINE__);
     365            $app->logMsg(sprintf('Saved %s prefs to database.', sizeof($insert_values)), LOG_DEBUG, __FILE__, __LINE__);
    347366            return true;
    348367        }
Note: See TracChangeset for help on using the changeset viewer.