Skip to content

Commit

Permalink
Configuration through webinterface
Browse files Browse the repository at this point in the history
Initial setup to move configuration from config.inc.php to a more
userfriendly, web-based, setup.

This changes creates the basic infrastructure and moves 4 settings
(ZOPH_TITLE, CSS_SHEET, MAPS and CAMERA_TZ) to the new setup.

#28
  • Loading branch information
jeroenrnl committed Oct 3, 2012
1 parent 01d6408 commit d693f0b
Show file tree
Hide file tree
Showing 32 changed files with 827 additions and 71 deletions.
3 changes: 2 additions & 1 deletion php/admin.inc.php
Expand Up @@ -65,7 +65,8 @@ private static function createArray() {
new admin("groups", "create or modify user groups", "groups.php", "groups.png"),
new admin("pages", "create or modify zoph pages", "pages.php", "pages.png"),
new admin("pagesets", "create or modify pagesets", "pagesets.php", "pagesets.png"),
new admin("tracks", "create or modify GPS tracks", "tracks.php", "tracks.png")
new admin("tracks", "create or modify GPS tracks", "tracks.php", "tracks.png"),
new admin("config", "modify configuration items", "config.php", "config.png")
);
}
}
Expand Down
13 changes: 12 additions & 1 deletion php/classes/TimeZone.inc.php
Expand Up @@ -66,7 +66,18 @@ public static function getSelectArray() {
array_unshift($zones, "");
return $zones;
}


/**
* Get array of timezones with timezone names as key
* @return array zones with names as key
*/
public static function getTzArray() {
$zones=self::getSelectArray();
$zones=array_values($zones);
$zones=array_combine($zones, $zones);
return $zones;
}

/**
* Get Key from timezone name
* @param string timezone
Expand Down
169 changes: 169 additions & 0 deletions php/classes/conf.inc.php
@@ -0,0 +1,169 @@
<?php
/*
* Via this class Zoph can read configurations from the database
* the configurations themselves are stored in confItem objects
*
* This file is part of Zoph.
*
* Zoph 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.
*
* Zoph 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 Zoph; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package Zoph
* @author Jeroen Roos
*/

require_once("database.inc.php");

class conf {
private static $groups=array();

private static $loaded=false;

public static function init() {
self::getDefault();
self::buildConfig();
self::loadFromDB();
}

public static function loadFromDB() {
$sql="SELECT conf_id, value FROM " . DB_PREFIX . "conf";

$result=query($sql, "Cannot load configuration from database");

while($row= fetch_row($result)) {
$key=$row[0];
$value=$row[1];
$item=conf::getItemByName($key);
$item->setValue($value);
}
self::$loaded=true;

}

public static function loadFromRequestVars(array $vars) {
foreach($vars as $key=>$value) {
if(substr($key,0,1) == "_") { continue; }
$key=str_replace("_", ".", $key);
try {
$item=conf::getItemByName($key);
$item->setValue($value);
$item->update();
} catch(ConfigurationException $e) {
log::msg("Configuration cannot be updated: " . $e->getMessage(), log::ERROR, log::CONFIG);
}
}
}

public static function getItemByName($name) {
$name_arr=explode(".", $name);
$group=array_shift($name_arr);
if(isset(self::$groups[$group]) && isset(self::$groups[$group][$name])) {
return self::$groups[$group][$name];
} else {
throw new ConfigurationException("Unknown configuration item " . $id);
}
}

public static function get($key) {
$item=conf::getItemByName($key);
return $item->getValue();

}

public static function getAll() {
return self::$groups;
}

public static function addGroup($name, $desc = "") {
$group = new confGroup();

$group->setName($name);
$group->setDesc($desc);


self::$groups[$name]=$group;
return $group;
}

private static function buildConfig() {
foreach(self::$groups as $group) {
foreach($group as $item) {
$name=$item->getName();
$value=$item->getDefault();
}
}
}

private static function getDefault() {
$interface = self::addGroup("interface", "Zoph interface settings");

$int_title = new confItemString();
$int_title->setName("interface.title");
$int_title->setLabel("title");
$int_title->setDesc("The title for the application. This is what appears on the home page and in the browser's title bar.");
$int_title->setDefault("Zoph");
$int_title->setRegex("^[\x20-\x7E]+$");
$int_title->setRegex("^.*$");
$interface[]=$int_title;

$int_css = new confItemString();
$int_css->setName("interface.css");
$int_css->setLabel("style sheet");
$int_css->setDesc("The CSS file Zoph uses");
$int_css->setDefault("css.php");
$int_css->setRegex("^[A-Za-z0-9_\.]+$");
$interface[]=$int_css;


$path = self::addGroup("path", "File and directory locations");


$path_images = new confItemString();
$path_images->setName("path.images");
$path_images->setLabel("Images directory");
$path_images->setDesc("Location of the images on the filesystem");
$path_images->setDefault("/data/images");
$path_images->setRegex("^\/[A-Za-z0-9_\.\/]+$");
$path_images->setTitle("Alphanumeric characters (A-Z, a-z and 0-9), forward slash (/), dot (.), and underscore (_).");
$path[]=$path_images;


$maps = self::addGroup("maps", "Mapping support");

$maps_provider = new confItemSelect();
$maps_provider->setName("maps.provider");
$maps_provider->setDesc("Enable or disable mapping support and choose the mapping provider");
$maps_provider->setLabel("Mapping provider");
$maps_provider->addOption("", "Disabled");
$maps_provider->addOption("google", "Google Maps");
$maps_provider->addOption("googlev3", "Google Maps v3");
$maps_provider->addOption("yahoo", "Yahoo maps");
$maps_provider->addOption("cloudmade", "Cloudmade (OpenStreetMap)");
$maps_provider->setDefault("");

$maps[]=$maps_provider;

$date = self::addGroup("date", "Date and time");

$date_tz = new confItemSelect();
$date_tz->setName("date.tz");
$date_tz->setLabel("Timezone");
$date_tz->setDesc("This setting determines the timezone to which your camera is set. Leave empty if you do not want to use this feature and always set your camera to the local timezone");

$date_tz->addOptions(TimeZone::getTzArray());
$date_tz->setDefault("");

$date[]=$date_tz;
}
}

99 changes: 99 additions & 0 deletions php/classes/confGroup.inc.php
@@ -0,0 +1,99 @@
<?php
/**
* A confGroup is groups several configurationitems (@see confItem) together.
*
* This file is part of Zoph.
*
* Zoph 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.
*
* Zoph 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 Zoph; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package Zoph
* @author Jeroen Roos
*/

class confGroup implements ArrayAccess, IteratorAggregate {

private $desc;
private $name;
private $items=array();


function __construct() {

}

public function setName($name) {
$this->name=$name;
}

public function setDesc($desc) {
$this->desc=$desc;
}

public function getName() {
return $this->name;
}

public function getDesc() {
return $this->desc;
}


public function offsetExists($off) {
return isset($this->items[$off]);
}

public function offsetGet($off) {
return $this->items[$off];
}

public function offsetSet($off, $value) {
if(is_null($off)) {
if($value instanceof confItem) {
$off=$value->getName();
}
}
if(!is_null($off)) {
$this->items[$off]=$value;
} else {
$this->items[]=$value;
}
}

public function offsetUnset($off) {
unset($this->items[$off]);
}

/**
* For IteratorAggregate interface
* allow us to do foreach() on this object
*/
public function getIterator() {
return new ArrayIterator($this->items);
}

private function readFromDB() {

}

public function display() {
$tpl=new block("confGroup", array(
"title" => $this->getName(),
"desc" => $this->getDesc(),
"items" => $this->items
));
return $tpl;
}


}

0 comments on commit d693f0b

Please sign in to comment.