This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/core/database_update.inc.php

203 lines
6.5 KiB
PHP
Raw Normal View History

<?php
/**
* AgileBill - Open Billing Software
*
* This body of work is free software; you can redistribute it and/or
* modify it under the terms of the Open AgileBill License
* License as published at http://www.agileco.com/agilebill/license1-4.txt
2009-08-03 04:10:16 +00:00
*
* Originally authored by Tony Landis, AgileBill LLC
*
* Recent modifications by Deon George
*
* @author Deon George <deonATleenooksDOTnet>
* @copyright 2009 Deon George
* @link http://osb.leenooks.net
*
* @link http://www.agileco.com/
* @copyright 2004-2008 Agileco, LLC.
* @license http://www.agileco.com/agilebill/license1-4.txt
2009-08-03 04:10:16 +00:00
* @author Tony Landis <tony@agileco.com>
* @package AgileBill
2009-08-03 04:10:16 +00:00
* @subpackage Core
*/
2009-08-03 04:10:16 +00:00
/**
* The main AgileBill CORE Database UPDATE Method
*
* @uses CORE_validate
* @uses CORE_trigger
*/
2009-08-03 04:10:16 +00:00
function CORE_database_update($VAR,$construct,$type) {
global $C_translate;
2009-08-03 04:10:16 +00:00
# Temp during code rework
if (! is_array($construct->val_error))
$construct->val_error = array();
# Set the field list for this method
$arr = $construct->method[$type];
# Define the validation class
include_once(PATH_CORE.'validate.inc.php');
$validate = new CORE_validate($VAR,$construct->module);
$construct->validated = true;
# Quick Validation to see if we have too many variables.
2010-11-29 22:41:08 +00:00
if (! isset($VAR['_ignoreval']))
foreach ($VAR as $field_name => $value)
if (preg_match("/^{$construct->module}_/",$field_name))
if (! in_array(preg_replace("/^{$construct->module}_/",'',$field_name),$arr))
array_push($construct->val_error,array(
'field'=>sprintf('%s_%s',$construct->table,$field_name),
'field_trans'=>$field_name,
'error'=>sprintf('WARNING: Variable passed to %s but it will be ignored.',__METHOD__),
'method'=>sprintf('%s:%s(%s)',__FILE__,__METHOD__,__LINE__)
));
2009-08-03 04:10:16 +00:00
# Define this record id
$id = $VAR[$construct->module.'_id'];
# Perform each field validation
while (list($key,$field_name) = each($arr)) {
# Get the field value
$field_var = sprintf('%s_%s',$construct->module,$field_name);
# Check if the conversion type required is not one ignored on updates
$ignore_con = false;
2009-08-03 04:10:16 +00:00
$ignore_convert = array('sha','md5','rc5','crypt');
for ($ic=0; $ic<count($ignore_convert); $ic++)
2010-11-29 22:41:08 +00:00
if (isset($construct->field[$field_name]['convert']) && ! isset($VAR['_noconvert']))
2009-08-03 04:10:16 +00:00
if ($construct->field[$field_name]['convert'] == $ignore_convert[$ic])
$ignore_con = true;
2009-08-03 04:10:16 +00:00
if (! $ignore_con) {
# check if this value is unique
2009-08-03 04:10:16 +00:00
if (isset($construct->field[$field_name]['unique']) && isset($VAR[$field_var])) {
if (! $validate->validate_unique($construct->table,$field_name,$id,$VAR[$field_var])) {
$construct->validated = false;
array_push($construct->val_error,array(
'field'=>sprintf('%s_%s',$construct->module,$field_name),
'field_trans'=>$C_translate->translate('field_'.$field_name,$construct->module,''),
'error'=>$C_translate->translate('validate_unique','',''),
'method'=>sprintf('%s:%s(%s)',__FILE__,__METHOD__,__LINE__)
));
}
}
2009-08-03 04:10:16 +00:00
# Check if the submitted value meets the specifed requirements
if (isset($construct->field[$field_name]['validate'])) {
if (isset($VAR[$field_var]) && ($VAR[$field_var] != '')) {
if (! $validate->validate($field_name,$construct->field[$field_name],$VAR[$field_var],$construct->field[$field_name]['validate'])) {
$construct->validated = false;
2009-08-03 04:10:16 +00:00
array_push($construct->val_error,array(
'field'=>sprintf('%s_%s',$construct->module,$field_name),
'field_trans'=>$C_translate->translate('field_'.$field_name,$construct->module,''),
'error'=>$validate->error[$field_name],
'method'=>sprintf('%s:%s(%s)',__FILE__,__METHOD__,__LINE__)
));
}
2009-08-03 04:10:16 +00:00
} else {
$construct->validated = false;
2009-08-03 04:10:16 +00:00
array_push($construct->val_error,array(
'field'=>sprintf('%s_%s',$construct->module,$field_name),
2010-11-29 22:41:08 +00:00
'field_trans'=>$C_translate->translate('field_'.$field_name,$construct->module,'') ? $C_translate->translate('field_'.$field_name,$construct->module,'') : $field_name,
2009-08-03 04:10:16 +00:00
'error'=>$C_translate->translate('validate_any','',''),
'method'=>sprintf('%s:%s(%s)',__FILE__,__METHOD__,__LINE__)
));
2010-11-29 22:41:08 +00:00
}
}
}
}
2009-08-03 04:10:16 +00:00
/* If validation has failed, skip the db insert & set the errors & original fields as Smarty objects,
and change the page to be loaded.*/
if (! $construct->validated) {
global $smarty;
2009-08-03 04:10:16 +00:00
# Set the errors as a Smarty Object
$smarty->assign('form_validation',$construct->val_error);
2009-08-03 04:10:16 +00:00
# Change the page to be loaded
$VAR['_page'] = $construct->module.':view';
2009-08-03 04:10:16 +00:00
# Define any triggers
if (isset($construct->trigger[$type])) {
include_once(PATH_CORE.'trigger.inc.php');
$trigger = new CORE_trigger;
2009-08-03 04:10:16 +00:00
$trigger->trigger($construct->trigger[$type],0,$VAR);
}
2009-08-03 04:10:16 +00:00
# Strip slashes
global $C_vars;
$C_vars->strip_slashes_all();
2009-08-03 04:10:16 +00:00
return false;
} else {
# Begin the new database class
$db = &DB();
2009-08-03 04:10:16 +00:00
# Loop through the field list to create the sql queries
$field_list = array();
reset($arr);
2009-08-03 04:10:16 +00:00
while (list($key,$field_name) = each($arr)) {
# Get the field value
$field_var = sprintf('%s_%s',$construct->module,$field_name);
if (isset($VAR[$field_var]) && $VAR[$field_var] != 'IGNORE-ARRAY-VALUE') {
# Check if HTML allowed
if (@$construct->field[$field_name]['html'] != 1 && ! is_array($VAR[$field_var]))
$insert_value = htmlspecialchars($VAR[$field_var]);
else
2009-08-03 04:10:16 +00:00
$insert_value = $VAR[$field_var];
2009-08-03 04:10:16 +00:00
# Perform data conversions
2010-11-29 22:41:08 +00:00
if (isset($construct->field[$field_name]['convert']) && trim($construct->field[$field_name]['convert']) && ! isset($VAR['_noconvert']))
2009-08-03 04:10:16 +00:00
$insert_value = $validate->convert($field_name,$insert_value,$construct->field[$field_name]['convert']);
2009-08-03 04:10:16 +00:00
$field_list[$field_name] = $insert_value;
2009-08-03 04:10:16 +00:00
} elseif (@$construct->field[$field_name]['convert'] == 'array' && @$VAR[$field_var] != 'IGNORE-ARRAY-VALUE')
# Handle blank array string
$field_list[$field_name] = serialize(array());
}
2009-08-03 04:10:16 +00:00
# Execute the query
$result = $db->Execute(sqlUpdate($db,$construct->table,$field_list,array('id'=>$id)));
2009-08-03 04:10:16 +00:00
# Error reporting
if ($result === false) {
global $C_debug;
2009-08-03 04:10:16 +00:00
$C_debug->error(__FILE__,__METHOD__,$db->ErrorMsg());
if (isset($construct->trigger[$type])) {
include_once(PATH_CORE.'trigger.inc.php');
$trigger = new CORE_trigger;
2009-08-03 04:10:16 +00:00
$trigger->trigger($construct->trigger[$type],0,$VAR);
}
2009-08-03 04:10:16 +00:00
return false;
}
2009-08-03 04:10:16 +00:00
# Define any triggers
if (isset($construct->trigger[$type])) {
include_once(PATH_CORE.'trigger.inc.php');
$trigger = new CORE_trigger;
$trigger->trigger($construct->trigger[$type],1,$VAR);
}
2009-08-03 04:10:16 +00:00
return true;
}
}
2009-08-03 04:10:16 +00:00
?>