Location: PHPKode > projects > Buzzword > buzzword-0.8.12/includes/preferences.inc
<?php

/*
 * buzzword
 * Copyright (c) 2003 Jon Tai
 *
 * $Id: preferences.inc 352 2004-04-21 08:56:57Z jon $
 *
 * This file is part of buzzword.
 *
 * buzzword is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * buzzword is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with buzzword; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

function get_pref($name) {
    $row = get_record('buzzword_preference', $name);
    if (!$row) {
        $sql = 'SELECT type, value ';
        $sql .= 'FROM '.DB_PREFIX.'preferences ';
        $sql .= 'WHERE name = '.mysql_quote_string($name);
        $result = mysql_query($sql);
        $row = mysql_fetch_assoc($result);

        mysql_free_result($result);

        if ($row)
            cache_record('buzzword_preference', $name, $row);
    }

    switch ($row['type']) {
        case 'b':
        return ($row['value'] == 'true');
        break;

        case 'i':
        return (int) $row['value'];
        break;

        case 'd':
        return (double) $row['value'];
        break;

        default:
        return (string) $row['value'];
        break;
    }
}

function set_pref($name, $type, $value, $allow_update = TRUE) {
    flush_record('buzzword_preference', $name);

    switch ($type) {
        case 'b':
        $value = ($value) ? mysql_quote_string('true') : mysql_quote_string('false');
        break;

        case 'i':
        $value = (int) $value;
        break;

        case 'd':
        $value = mysql_quote_string((double) $value);
        break;

        default:
        $value = mysql_quote_string($value);
        break;
    }

    $name = mysql_quote_string($name);
    $type = mysql_quote_string($type);

    $sql = 'SELECT preference_key ';
    $sql .= 'FROM '.DB_PREFIX.'preferences ';
    $sql .= "WHERE name = $name";
    $result = mysql_query($sql);

    if (mysql_num_rows($result) == 0) { 
        $preference_key = mysql_unique_key(DB_PREFIX.'preferences', 'preference_key');

        $sql = 'INSERT INTO '.DB_PREFIX.'preferences ';
        $sql .= '(preference_key, name, type, value) VALUES ';
        $sql .= "($preference_key, $name, $type, $value)";
        mysql_query($sql);
    } else if ($allow_update) {

        $sql = 'UPDATE '.DB_PREFIX.'preferences SET ';
        $sql .= "type = $type, ";
        $sql .= "value = $value ";
        $sql .= "WHERE name = $name";
        mysql_query($sql);
    }
}

function set_default_pref($name, $type, $value) {
    set_pref($name, $type, $value, FALSE);
}

?>
Return current item: Buzzword