By: martijn
Date: 2006-09-30
Time: 12:44
|
Need help with config layout
Hi,
I'd like some advise on how to organize an configuration file.
My configuration file currently looks like:
<?php
//Sessions initialisation
session_start();
ob_start();
//Sessions variables
$x = $_GET['x'];
$u = $_GET['u'];
chdir('./sources/includes/');
//DB class
// Include ezSQL core
include_once "ez_sql_core.php";
// Include ezSQL database specific component (in this case mySQL)
include_once "ez_sql_mysql.php";
// Initialise database object and establish a connection
// at the same time - db_user / db_password / db_name / db_host
$db = new ezSQL_mysql();
$db->quick_connect('root','***','g24unew', 'localhost');
//Portal class
include_once('class.portal.php');
$portal = new portal();
//Template class
include_once('tbs_class.php');
include_once('tbs_plugin_bypage.php'); // Plug-in By-Page
include_once('tbs_plugin_navbar.php'); // Plug-in Navigation Bar
include_once('tbs_plugin_cache.php'); // Plug-in CacheSystem
$tmpl = new clsTinyButStrong;
chdir('../../');
//Variables for configuration
$site_name = $portal->Config('title');
$portalversion = $portal->Config('version');
$status = $portal->Config('status');
$script_path = $portal->Config('path');
$header_desc = $portal->Config('head_desc');
$uname = $portal->Config('username');
$about = $portal->Config('site_desc');
$userstat = $portal->Config('logstat');
$slogan = $portal->Config('slogan');
$tmpl->PlugIn(TBS_INSTALL, TBS_CACHE, './cache', 'cache_*.tmp');
?>
|
The part of the 'Configuration variables' is what I would like to make more efficient.
$portal->Config('...'); basically just fetches the info from the database using switches.
I would like to put it into an array but I'm not sure how to do this (efficiently).
It needs to be easily acessable from templates.
Thanks in advance :)
|
By: Skrol29
Date: 2006-10-01
Time: 12:02
|
Re: Need help with config layout
Hi,
It could be more efficient if you could retreive this values in only one query. I don't know the Portal class, so I can't explain more in detail.
Concerning TBS, it also could be more efficient if you use the MergeField method with a function in order to merge values coming from $portal->Config().
Example:
PHP:
$TBS->MergeField('conf','f_portal_conf',true);
...
function f_portal_conf($key) {
global $portal;
return $portal->Config($key);
}
|
HTML:
[conf.title]
[conf.status]
...
|
|
By: martijn
Date: 2006-10-04
Time: 08:17
|
Re: Need help with config layout
Hi, this does work, but it doesn't want to be global trough the config file.
I'd need to copy the MergeField code in every file in order to use it.
Do you happen to know a way to make MergeBlocks and MergeFields global?
For instance:
<?php
include_once('sources/config.php');
// $tmpl->PlugIn(TBS_CACHE, 'mainpage', 300);
//Load the template
$tmpl->LoadTemplate('template/home.html');
$tmpl->MergeField('conf','f_portal_conf',true);
//Database data
$data3 = $tmpl->MergeBlock('files','mysql',"SELECT * FROM `files` ORDER BY id DESC LIMIT 0,5");
$data4 = $tmpl->MergeBlock('latest','mysql',"SELECT * FROM `news` WHERE published = 1 ORDER BY id DESC LIMIT 0,5");
//Render
$site_name = $site_name." - ".$slogan;
$tmpl->Show();
?> |
^ If I specify the MergeField like this it works, but only for this index.php file.
Thanks in advance
|
By: martijn
Date: 2006-10-04
Time: 08:22
|
Re: Need help with config layout
And this is my portal class, Config part:
<?php
class portal
{
function Config($switch){
switch($switch){
case 'version':
$query = mysql_query("SELECT value FROM `config` WHERE name = 'version' LIMIT 0,1");
$result = mysql_fetch_array($query);
$version = $result['value'];
return $version;
break;
case 'title':
$query = mysql_query("SELECT value FROM `config` WHERE name = 'site_naam' LIMIT 0,1");
$result = mysql_fetch_array($query);
$title = $result['value'];
return $title;
break;
case 'status':
$query = mysql_query("SELECT value FROM `config` WHERE name = 'offline' LIMIT 0,1");
$result = mysql_fetch_array($query);
$status = $result['value'];
return $status;
break;
case 'path':
$query = mysql_query("SELECT value FROM `config` WHERE name = 'site_locatie' LIMIT 0,1");
$result = mysql_fetch_array($query);
$path = $result['value'];
return $path;
break;
case 'head_desc':
$query = mysql_query("SELECT value FROM `config` WHERE name = 'header_desc' LIMIT 0,1");
$result = mysql_fetch_array($query);
$header = $result['value'];
return $header;
case 'username':
if($_SESSION['username']){
$uname = $_SESSION['username'];
}
else{
$uname = "Guest";
}
return $uname;
break;
case 'logstat':
if($_SESSION['loggedin'] == "1"){
$login = "1";
}
else if(!$_SESSION['loggedin']){
$login = "0";
}
return $login;
break;
case 'site_desc':
$query = mysql_query("SELECT value FROM `config` WHERE name = 'site_desc' LIMIT 0,1");
$result = mysql_fetch_array($query);
$about = $result['value'];
return $about;
break;
case 'slogan':
$query = mysql_query("SELECT value FROM `config` WHERE name = 'site_slogan' LIMIT 0,1");
$result = mysql_fetch_array($query);
$slogan = $result['value'];
return $slogan;
break;
default:
return "Invalid value specified";
break;
}
}
...
?> |
|
By: Skrol29
Date: 2006-10-07
Time: 03:15
|
Re: Need help with config layout
>Hi, this does work, but it doesn't want to be global trough the config file.
>I'd need to copy the MergeField code in every file in order to use it.
Then try to make a small TBS plug-in for that. You could enhance the LoadTemplate() method, for example.
|
By: martijn
Date: 2006-10-08
Time: 13:22
|
Re: Need help with config layout
Hi, I tried your suggestion. It didn't give errors but also didn't work as expected...
<?php
define('TBS_MyConfig','clsTbsPlugIng_MyConfig'); // That is the plug-in's key
class clsTbsPlugIng_MyConfig{
function OnInstall() {
return array('BeforeLoadTemplate');
} // That is the OnInstall event
function BeforeLoadTemplate(&$File,&$HtmlCharSet){
function conf($key) {
switch($key){
case 'version':
$query = mysql_query("SELECT value FROM `config` WHERE name = 'version' LIMIT 0,1");
$result = mysql_fetch_array($query);
$key = $result['value'];
return $key;
break;
case 'title':
$query = mysql_query("SELECT value FROM `config` WHERE name = 'site_naam' LIMIT 0,1");
$result = mysql_fetch_array($query);
$key = $result['value'];
return $key;
break;
case 'status':
$query = mysql_query("SELECT value FROM `config` WHERE name = 'offline' LIMIT 0,1");
$result = mysql_fetch_array($query);
$key = $result['value'];
return $key;
break;
case 'path':
$query = mysql_query("SELECT value FROM `config` WHERE name = 'site_locatie' LIMIT 0,1");
$result = mysql_fetch_array($query);
$key = $result['value'];
return $key;
break;
case 'head_desc':
$query = mysql_query("SELECT value FROM `config` WHERE name = 'header_desc' LIMIT 0,1");
$result = mysql_fetch_array($query);
$key = $result['value'];
return $key;
case 'username':
if($_SESSION['username']){
$key = $_SESSION['username'];
}
else{
$key = "Guest";
}
return $key;
break;
case 'logstat':
if($_SESSION['loggedin'] == "1"){
$key = "1";
}
else if(!$_SESSION['loggedin']){
$key = "0";
}
return $key;
break;
case 'site_desc':
$query = mysql_query("SELECT value FROM `config` WHERE name = 'site_desc' LIMIT 0,1");
$result = mysql_fetch_array($query);
$key = $result['value'];
return $key;
break;
case 'slogan':
$query = mysql_query("SELECT value FROM `config` WHERE name = 'site_slogan' LIMIT 0,1");
$result = mysql_fetch_array($query);
$key = $result['value'];
return $key;
break;
default:
return "Invalid value specified";
break;
}
}
$results = $this->TBS->MergeField('conf','conf',true);
}
}
?> |
Any ideas? It still isn't being parsed...
|
By: martijn
Date: 2006-10-12
Time: 13:52
|
Re: Need help with config layout
Sorry to bump this thread, but I'm really stuck with this problem :(
|
By: Skrol29
Date: 2006-10-12
Time: 14:21
|
Re: Need help with config layout
Hi,
It seems that your plug-in does nothing.
Try this one:
<?php
define('TBS_MyConfig','clsTbsPlugIng_MyConfig'); // That is the plug-in's key
$GLOBALS['_TBS_AutoInstallPlugIns'][] = TBS_MyConfig;
class clsTbsPlugIng_MyConfig {
function OnInstall() {
return array('AfterLoadTemplate');
} // That is the OnInstall event
function AfterLoadTemplate(&$File,&$HtmlCharSet){
// Load all configuration items
$conf_lst = array();
$query = mysql_query("SELECT name,value FROM config");
while ($result = mysql_fetch_array($query)) {
$conf_lst[$result['name']] = $result['value'];
}
mysql_freeresult($query);
// Other items
$conf_lst['username'] = ($_SESSION['username']) ? $_SESSION['username'] : 'Guest';
$conf_lst['logstat'] = ($_SESSION['loggedin'] == "1") ? "1" : "0";
// Merge all conf items
$this->TBS->MergeField('conf',$conf_lst);
}
}
?>
|
(not tested)
Take car that the plug-in is auto-installed if you insert it before the TBS instanciation.
Example:
include_once('tbs_class.php');
include_once('my_plugin.php');
// connection to the database
...
// Merge
$TBS = new clsTinyButStrong;
$TBS->LoadTemplate('hello.html');
|
|
By: martijn
Date: 2006-10-12
Time: 18:41
|
Re: Need help with config layout
Thanks a whole bunch! It works! :)
This makes things much cleaner and more uniform when coding templates :D
|
By: martijn
Date: 2007-07-02
Time: 20:15
|
Re: Need help with config layout
I drastically changed the structure of my files now, so here is my current way of configuration management.
The function:
function configLoader($prefs){
$file = simplexml_load_file($prefs); // Load the simplexml function
foreach($file->definition as $def) { // Load every 'definition' part
eval("define('{$def->name}', '{$def->value}');"); // Output them as constants
}
}
|
The class initiator uses above function like this:
function __construct(){
$this->configLoader('../data/config.xml');
}
|
And the XML is structurized like this:
<!-- Configuration file for G24U Portal -->
<configuration>
<!-- Site name -->
<definition>
<name>SITE_NAME</name>
<value>G24U</value>
</definition>
<!-- Path to the template directory -->
<definition>
<name>TPL_PATH</name>
<value>./sources/template/</value>
</definition>
<definition>
<name></name>
<value></value>
</definition>
<!-- Path to the emoticon directory -->
<definition>
<name>SMILE_PATH</name>
<value>./images/smileys/</value>
</definition>
<!-- Status of the site, online = 1, offline = 0 -->
<definition>
<name>STATUS</name>
<value>1</value>
</definition>
</configuration>
|
Then I use the following in the template:
[var..cst.SITE_NAME] (Prints "G24U")
|
I hope someone got some use for this in their own projects ;)
|
|
Posting in progress.
Please wait...
|