WBCE CMS Forum

WBCE CMS – Way Better Content Editing.

You are not logged in.

#1 25.09.2015 21:57:52

norhei
Developer

Settings

As i tried to make a admin tool  for setting of maintainance mode, i realized that there is nothing for easy management of WBs system settings.
You still have to do it manually.  But i remembered that some module where using some set_settings() get_settings() functions(e.g. jsadmin). So i did a deep thought an decided that it would be a good idea that the whole settings management would be a lot more fun it we got some dedicated functions to handle this.   

If we use constants we schould do it in a good and clean way, so i put a few lines together and would like to know if you got some more ideas i can put into this.   

// create or edit a setting
Settings::Set("wb_new_setting","the value");

// using a setting
if (WB_NEW_SETTING =="the value") echo "Horay";

// there is a get function but this is mostly used internal
$myValue= Settings::Get ("wb_new_setting");

// deleting
Settings::Delete("wb_new_setting");

// if used in modules please prepend (shortened)module name to avoid collisions
Settings::Set("wysi_new_setting","another value");

Please keep in mind that WB stores settings always as strings.

Ok, here comes the class please keep in Mind that this is an untested draft just written down.

<?php 
/*
Simple static Class for handling settings in Core and Modules
All setting names may only contain a-z 0-9 and "_" 
Core Settings are prepended by "wb_". (e.g. wb_maintainance_mode)
Module settings are prepended by module name or maybe by a shortened form of the module name
(e.g. wysi_my_setting for wysiwyg)
Please keep in mind that WB stores settings only as strings.

Copyright Norbert Heimsath
License GPLv2 or any later
*/

class Settings {

    // sets or overwrites a config setting
    public static function Set($name, $value) {
	    global $database;

        // need to make sure , we only store a string
        if (is_array($value))    return "Arrays can't be stored in constants";
        if (is_object($value))   return "Objects can't be stored in constants";
        if (is_resource($value)) return "Resources can't be stored in constants";   
     
        if     (is_bool($value)) $value = $value ? 'true' : 'false';
        elseif (is_bool($value)) $value = $value;
        else                     $value=var_export($value, true)

        // Already set ? Database always returns a string here not a boolean.
	    $prev_value = Settings::Get($name, false);

        // If its a boolean there was nothing set.
	    if($prev_value === false) {
            $sql="INSERT INTO ".TABLE_PREFIX."settings (name,value) VALUES ('$name','$value')";
		    $database->query($sql);
	    } else {
            $sql="UPDATE ".TABLE_PREFIX."settings SET value = '$value' WHERE name = '$name'";
		    $database->query($sql);
	    }
        return false;
    }


    // only used as helper, as all setings converted to constants
    public static function Get($name, $default= false) {
        global $database; 

        $sql="SELECT value FROM ".TABLE_PREFIX."settings WHERE name = '".$name."'";
	    $rs = $database->query($sql);

	    if($row = $rs->fetchRow()) return $row['value'];

	    return $default;
    }

    public static function Delete($name) {
	    global $database;

        // is it set ?
	    $prev_value = Settings::Delete($name);

  	    if($prev_value === false) { 
            return "Setting not set";
        }      
        else {
            $sql="DELETE FROM ".TABLE_PREFIX."settings WHERE name = '$name'";
            $database->query($sql);
        }
        return false;
    }


    // function to setup constants in init 
    public static function Setup() {
        // Get website settings (title, keywords, description, header, footer...)
        $sql = 'SELECT `name`, `value` FROM `' . TABLE_PREFIX . 'settings`';
        if (($get_settings = $database->query($sql))) {
            $x = 0; //counter for debug

            while ($setting = $get_settings->fetchRow(MYSQL_ASSOC)) {
                $setting_name = strtoupper($setting['name']);
                $setting_value = $setting['value'];
                if ($setting_value == 'false') {
                    $setting_value = false;
                }
                if ($setting_value == 'true') {
                    $setting_value = true;
                }
                if (!defined($setting_name)) //already set manually in config ?
                    define($setting_name, $setting_value);
                $x++;
            }
        } 
        else {
            die($database->get_error());
        }
        return false;
    }

    
    // a function to display all settings in DB same as setup but returns a nice list
    public static function Info() {
        $sql = 'SELECT `name`, `value` FROM `' . TABLE_PREFIX . 'settings`';
        if (($get_settings = $database->query($sql))) {
            $out = "<h3>All Settings in DB</h3>";

            while ($setting = $get_settings->fetchRow(MYSQL_ASSOC)) {
                $setting_name = strtoupper($setting['name']);
                $setting_value = $setting['value'];
               
                $out= "$setting_name = $setting_value <br />"
                    define($setting_name, $setting_value);
            }
        } 
        else {
            die($database->get_error());
        }
        return $out;
    }
}

Last edited by norhei (25.09.2015 22:02:04)

Offline

#2 26.09.2015 21:34:18

norhei
Developer

Re: Settings

Obendrein frage ich mich ob die Modulentwickler lieber Konstanten für Ihre Settings hätten oder lieber etwas das einfach einen Array mit Ihren Settings ausspuckt??? Prinzipiell könnte ich auch beides einbauen.

Offline

#3 06.10.2015 21:15:09

norhei
Developer

Re: Settings

WBCE Settings now can be set using a little static class:

// create or edit a setting
Settings::Set("wb_new_setting","the value");

Settings can be accessed in two ways. But the get method is only needed in case of apeforms as the constant is only available in next pageload.

// using a setting
if (WB_NEW_SETTING =="the value") echo "Horay";

// there is a get function but this is mostly used internal
$myValue= Settings::Get ("wb_new_setting");

Deleting a setting is pretty easy too.

// deleting
Settings::Delete("wb_new_setting");

If used in modules please prepend (shortened)module name to avoid collisions

// setting something for wysiwyg 
Settings::Set("wysi_new_setting","another value");

Please keep in mind that WB stores settings always as strings. (e.g. boolean true as "true" )

IMPORTANT!!!
Always use the "wb_"  prefix for core settings and the "name_" prefix for modules.


Sooner or later this class will be extended .

Offline

Board footer

Powered by FluxBB

up