<?php
/**
* HV_WDDX_Metadata - Manage data described in WDDX format.
* @version 1.0alpha
*
* Copyright (C) 2003 Herman Veluwenkamp <hide@address.com>
*
* This program 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.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 24
* 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 this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
define('HV_WDDX_METADATA_IGNORE_VALIDATION', 0);
define('HV_WDDX_METADATA_DO_VALIDATION', 1);
/**
* Manage data described in WDDX format.
*/
class HV_WDDX_Metadata extends PEAR {
var $conf;
var $encoding = '<?xml version="1.0" encoding="ISO-8859-15"?>';
/**
* Initiliase error handling. Refer to PEAR documentation for information regarding error handling.
*/
function HV_WDDX_Metadata($mode = PEAR_ERROR_TRIGGER, $options = E_USER_ERROR) {
$this->PEAR();
$this->setErrorHandling($mode, $options);
}
/**
* Set WDDX configuration.
*/
function setConfig($conf) {
$this->conf = $conf;
}
/**
* Set database for populating option lists.
*/
function setOptionDatabase(&$db) {
$this->db = $db;
}
/**
* Insert $data into WDDX config and grab options from database if specified.
*/
function populate($data=NULL) {
$config = wddx_deserialize($this->conf);
//if (empty($config)) trigger_error('empty WDDX config' , E_USER_ERROR);
if (!empty($this->conf)) {
$new_config = array();
foreach ($config as $name => $value) {
$new_config[$name] = $config[$name];
if (isset($data[$name])) {
$new_config[$name]['selected'] = $data[$name];
}
if (isset($config[$name]['option-db-select'])) {
$query = $config[$name]['option-db-select'];
$this->db->query($query);
$new_config[$name]['option'] = $this->db->result;
}
}
$new_config = wddx_serialize_value($new_config);
$new_config = preg_replace_callback('/<char code=[\'"](\w{2,8})["\']\/>/', 'HV_WDDX_Metadata_pregreplace', $new_config);
$new_config = $this->encoding."\n".$new_config;
$this->conf = $new_config;
}
}
/**
* Insert $data into config and optionally validate.
*/
function parse($data, $validate=HV_WDDX_METADATA_DO_VALIDATION) {
$config = wddx_deserialize($this->conf);
$new_config = array();
$validated_ok = true; // flag for validation errors
foreach ($config as $name => $value) {
$type = $config[$name]['type'];
$new_config[$name] = $config[$name];
if (empty($data[$name])) $selected = false;
else $selected = $data[$name];
switch ($type) {
case 'text':
case 'password':
case 'date':
case 'time':
case 'textbox':
if ($selected) {
if (is_array($selected)) {
$new_config[$name]['selected'] = $selected[0];
$test_value = $selected[0];
} else {
$new_config[$name]['selected'] = $selected;
$test_value = $selected;
}
} else {
unset($new_config[$name]['selected']);
$test_value = '';
}
break;
case 'popup':
case 'radio':
if ($selected) {
if (is_array($selected)) {
$new_config[$name]['selected'] = $selected[0];
$test_value = 1;
} else {
$new_config[$name]['selected'] = $selected;
$test_value = 1;
}
} else {
unset($new_config[$name]['selected']);
$test_value = 0;
}
break;
case 'listbox':
case 'checkbox':
case 'checklist':
if ($selected) {
if (is_array($selected)) {
$new_config[$name]['selected'] = $selected;
$test_value = sizeof($selected);
} else {
$new_config[$name]['selected'] = explode(',', $selected);
$test_value = 1;
}
} else {
unset($new_config[$name]['selected']);
$test_value = '';
}
break;
}
if ($validate && !preg_match($new_config[$name]['validation-regexp'], $test_value)) {
$new_config[$name]['validation-error'] = $new_config[$name]['validation-message'];
$validated_ok = false;
}
}
$new_config = wddx_serialize_value($new_config);
$new_config = preg_replace_callback('/<char code=[\'"](\w{2,8})["\']\/>/', 'HV_WDDX_Metadata_pregreplace', $new_config);
$new_config = $this->encoding."\n".$new_config;
$this->conf = $new_config;
return $validated_ok;
}
}
/**
* Converts <char code=""/> embedded in WDDX into characters for display.
*/
function HV_WDDX_Metadata_pregreplace($match) {
return chr(hexdec($match[1]));
}
?>