Changes to AgileBill

This commit is contained in:
Deon George
2009-08-03 14:10:16 +10:00
parent 0a22cfe22c
commit 27aee719b0
1051 changed files with 219109 additions and 117219 deletions

View File

@@ -1,262 +1,363 @@
<?php
<?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
*
* For questions, help, comments, discussion, etc., please join the
* Agileco community forums at http://forum.agileco.com/
*
* 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
* @author Tony Landis <tony@agileco.com>
* @author Tony Landis <tony@agileco.com>
* @package AgileBill
* @version 1.4.93
* @subpackage Core
*/
class CORE_database
{
function add($VAR, &$construct, $type)
{
include_once(PATH_CORE . 'database_add.inc.php');
return CORE_database_add($VAR, $construct, $type);
/**
* The main AgileBill CORE Database Class
*
* @package AgileBill
* @subpackage Core
*/
class CORE_database {
public function add($VAR,$construct,$type) {
include_once(PATH_CORE.'database_add.inc.php');
return CORE_database_add($VAR,$construct,$type);
}
function update($VAR, &$construct, $type)
{
include_once(PATH_CORE . 'database_update.inc.php');
return CORE_database_update($VAR, $construct, $type);
public function update($VAR,$construct,$type) {
include_once(PATH_CORE.'database_update.inc.php');
return CORE_database_update($VAR,$construct,$type);
}
function search_form($VAR, &$construct, $type)
{
include_once(PATH_CORE . 'database_search_form.inc.php');
return CORE_database_search_form($VAR, $construct, $type);
public function search_form($VAR,$construct,$type) {
include_once(PATH_CORE.'database_search_form.inc.php');
return CORE_database_search_form($VAR,$construct,$type);
}
function search($VAR, &$construct, $type)
{
include_once(PATH_CORE . 'database_search.inc.php');
return CORE_database_search($VAR, $construct, $type);
public function search($VAR,$construct,$type) {
include_once(PATH_CORE.'database_search.inc.php');
return CORE_database_search($VAR,$construct,$type);
}
function search_show($VAR, &$construct, $type)
{
include_once(PATH_CORE . 'database_search_show.inc.php');
return CORE_database_search_show($VAR, $construct, $type);
}
function view($VAR, &$construct, $type)
{
include_once(PATH_CORE . 'database_view.inc.php');
return CORE_database_view($VAR, $construct, $type);
}
function mass_delete($VAR, &$construct, $type)
{
include_once(PATH_CORE . 'database_mass_delete.inc.php');
return CORE_database_mass_delete($VAR, $construct, $type);
}
function delete($VAR, &$construct, $type)
{
include_once(PATH_CORE . 'database_delete.inc.php');
return CORE_database_delete($VAR, $construct, $type);
public function search_show($VAR,$construct,$type) {
include_once(PATH_CORE.'database_search_show.inc.php');
return CORE_database_search_show($VAR,$construct,$type);
}
function join_fields($result, $linked)
{
include_once(PATH_CORE . 'database_join_fields.inc.php');
return CORE_database_join_fields($result, $linked);
}
public function view($VAR,$construct,$type) {
include_once(PATH_CORE.'database_view.inc.php');
return CORE_database_view($VAR,$construct,$type);
}
public function mass_delete($VAR,$construct,$type) {
include_once(PATH_CORE.'database_mass_delete.inc.php');
return CORE_database_mass_delete($VAR,$construct,$type);
}
public function delete($VAR,$construct,$type) {
include_once(PATH_CORE.'database_delete.inc.php');
return CORE_database_delete($VAR,$construct,$type);
}
public function join_fields($result,$linked) {
include_once(PATH_CORE.'database_join_fields.inc.php');
return CORE_database_join_fields($result,$linked);
}
// replaced in v1.4.91 (use sqlSelect)
function sql_select($TableList, $FieldList, $Conditions, $Order, &$db) {
return sqlSelect( $db, $TableList, $FieldList, $Conditions, $Order);
# @todo To deprecate
public function sql_select($TableList,$FieldList,$Conditions,$Order,&$db) {
return sqlSelect($db,$TableList,$FieldList,$Conditions,$Order);
}
/**
* Remove fields from the standard construct type to ingore insert/select/validation rules set in construct
* Remove fields from the standard construct type to ignore insert/select/validation rules set in construct
*
* @param array $ignore_fields
* @param string $construct_fields
* @return array
*/
function ignore_fields($ignore_fields,$construct_fields) {
if(!is_array($construct_fields)) $fields = explode(",", $construct_fields); else $fields = $construct_fields;
foreach($fields as $id=>$fld) {
if(in_array($fld,$ignore_fields)) {
public function ignore_fields($ignore_fields,$construct_fields) {
if (! is_array($construct_fields))
$fields = explode(',',$construct_fields);
else
$fields = $construct_fields;
foreach ($fields as $id=>$fld)
if (in_array($fld,$ignore_fields))
unset($fields[$id]);
}
}
return $fields;
return $fields;
}
}
class CORE_debugger
{
var $sql_count;
function sql_count() {
if(!isset($this->sql_count)) $this->sql_count = 0;
$this->sql_count++;
/**
* The main AgileBill CORE Debugger Class
*
* @package AgileBill
* @subpackage Core
*/
class CORE_debugger {
public function alert($message) {
$this->alert = array($message);
}
function alert($message) {
$this->alert = Array ($message);
}
public function error($module,$method,$message) {
$this->error = sprintf('%s:%s => %s<br/>',$module,$method,$message);
if (defined('ERROR_REPORTING') && ERROR_REPORTING > 0)
$this->alert($this->error);
function error($module, $method, $message) {
$this->error = $module . ':'. $method . ' => &nbsp;&nbsp ' . $message . '<br>';
if(defined("ERROR_REPORTING") && ERROR_REPORTING > 0) $this->alert($this->error);
$db = &DB();
$this->record_id = $db->GenID(AGILE_DB_PREFIX . "" . 'log_error_id');
$q = "INSERT INTO ".AGILE_DB_PREFIX."log_error
SET
id = ". $db->qstr($this->record_id).",
date_orig = ". $db->qstr(time()).",
account_id = ". @$db->qstr(SESS_ACCOUNT).",
module = ". $db->qstr($module).",
method = ". $db->qstr($method).",
message = ". $db->qstr($message).",
site_id = ". @$db->qstr(DEFAULT_SITE);
$result = $db->Execute($q);
$result = $db->Execute(sqlInsert($db,'log_error',array('date_orig'=>time(),'account_id'=>SESS_ACCOUNT,'module'=>$module,'method'=>$method,'message'=>$message)));
}
}
}
function &DB($debug=false) {
/**
* The main AgileBill CORE Database Function
*/
function DB($debug=false) {
static $saved_db_conn;
if (isset($saved_db_conn) && defined("AGILE_DB_CACHE")) {
#echo '<b>Cached:</b><pre>'.print_r($saved_db_conn,true).'</pre><br>';
if($debug) $saved_db_conn->debug=true; else $saved_db_conn->debug=false;
if (isset($saved_db_conn) && defined('AGILE_DB_CACHE')) {
if ($debug)
$saved_db_conn->debug = true;
else
$saved_db_conn->debug = false;
return $saved_db_conn;
}
$saved_db_conn = NewADOConnection(AGILE_DB_TYPE);
if(defined("AGILE_DB_PCONNECT") && AGILE_DB_PCONNECT == true)
if (defined('AGILE_DB_PCONNECT') && AGILE_DB_PCONNECT == true)
$saved_db_conn->PConnect(AGILE_DB_HOST,AGILE_DB_USERNAME,AGILE_DB_PASSWORD,AGILE_DB_DATABASE);
else
$saved_db_conn->Connect(AGILE_DB_HOST,AGILE_DB_USERNAME,AGILE_DB_PASSWORD,AGILE_DB_DATABASE);
#echo '<b>Original:</b><pre>'.print_r($saved_db_conn,true).'</pre><br>';
if($debug) $saved_db_conn->debug=true; else $saved_db_conn->debug=false;
return $saved_db_conn;
}
if ($debug)
$saved_db_conn->debug = true;
else
$saved_db_conn->debug = false;
function sqlGenID(&$db, $table) {
return $db->GenID( AGILE_DB_PREFIX . $table . '_id' );
$saved_db_conn->SetFetchMode(ADODB_FETCH_ASSOC);
return $saved_db_conn;
}
function sqlConditions( &$db, $Conditions=false, $Tables=false )
{
$where = " WHERE ";
/**
* Get the next SQL index ID for a table
*
* @param $db
* @param $table
* @return int
*/
function sqlGenID($db,$table) {
$id = 0;
if($Conditions) {
if(preg_match('/::/', $Conditions) ) {
$s = explode('::', $Conditions);
$ii=1;
$Conditions = '';
for($i=0; $i<count($s); $i++) {
if($ii==1) {
$Conditions .= " {$s[$i]} ";
$ii++;
} else {
$Conditions .= $db->qstr($s[$i]);
$ii=1;
}
}
# Check if our ID table exists, and if not
static $CACHE = array();
$dbname = md5($db->databaseType.$db->host.$db->database.$db->user);
if (! isset($CACHE[$dbname][$table])) {
$CACHE[$dbname][$table] = true;
$rs = $db->Execute(sprintf('SELECT id FROM %s%s_id WHERE 1=0',AGILE_DB_PREFIX,$table));
if ($rs) {
$rs = $db->Execute(sprintf('SELECT MAX(id) AS max FROM %s%s',AGILE_DB_PREFIX,$table));
if ($rs)
$id = $rs->fields['max']+1;
}
$where .= $Conditions . " AND ";
}
if(!is_array($Tables)) {
$where .= " site_id = ". DEFAULT_SITE;
} else {
$tbarr = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S', 'T','U','V');
for($i=0; $i<count($Tables); $i++) {
if( $i > 0 ) $where .= " AND ";
$where .= " {$tbarr[$i]}.site_id = ". DEFAULT_SITE;
}
}
if( $where ) return $where;
return $db->GenID(sprintf('%s%s_id',AGILE_DB_PREFIX,$table),$id);
}
function sqlDelete(&$db, $table, $conditions) {
$conditions = sqlConditions( $db, $conditions);
return "DELETE FROM ".AGILE_DB_PREFIX."$table $conditions";
}
/**
* Generate SQL Conditions for a Query
*/
function sqlConditions($db,$Conditions=false,$Tables=false) {
$db = &DB();
$where = 'WHERE ';
function sqlInsert(&$db, $table, $fields, $id=false) {
if(!$id) $id = sqlGenID( $db,$table);
$fields['id'] = $id;
if(empty($fields['site_id'])) $fields['site_id'] = DEFAULT_SITE;
$tab = AGILE_DB_PREFIX.''.$table;
return $db->GetInsertSQL($tab, $fields, get_magic_quotes_gpc());
}
if ($Conditions) {
if (is_array($Conditions)) {
foreach ($Conditions as $a => $b)
if (is_array($b))
$where .= sprintf("%s IN ('%s') AND ",$a,implode("','",$b));
elseif ($db->qstr($b) == 'NULL')
$where .= sprintf('%s IS NULL AND ',$a);
else
$where .= sprintf('%s=%s AND ',$a,$db->qstr($b));
function sqlUpdate(&$db, $table, $fields, $conditions, $force=false) {
$rs = $db->Execute( sqlSelect( $db, $table, '*', $conditions) );
if(empty($fields['site_id'])) $fields['site_id'] = DEFAULT_SITE;
return $db->GetUpdateSQL( $rs, $fields, false, get_magic_quotes_gpc());
}
} else {
if (preg_match('/::/',$Conditions)) {
$s = explode('::',$Conditions);
$ii = 1;
$Conditions = '';
function sqlSelect(&$db, $TableList, $FieldList, $Conditions, $Order=false, $Limit=false, $DISTINCT='', $GroupBy=false )
{
### Table(s)
if(is_array($TableList)) {
$tbarr = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S', 'T','U','V');
$table = '';
$site_id_where = '';
for($i=0;$i<count($TableList); $i++) {
$as = $tbarr[$i];
if($i>0) {
$table .= ",".AGILE_DB_PREFIX.$TableList[$i] . " AS $as";
} else {
$table .= AGILE_DB_PREFIX.$TableList[$i] . " AS $as";
for ($i=0; $i<count($s); $i++) {
if ($ii==1) {
$Conditions .= $s[$i];
$ii++;
} else {
$Conditions .= $db->qstr($s[$i]);
$ii = 1;
}
}
}
$where .= sprintf('%s AND ',$Conditions);
}
}
# Add the SITE ID
if (! is_array($Tables) || count($Tables) == 1) {
$where .= sprintf('site_id=%s',DEFAULT_SITE);
} else {
$tbarr = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V');
for ($i=0; $i<count($Tables); $i++) {
if ($i>0)
$where .= ' AND ';
$where .= sprintf(' %s.site_id = %s',$tbarr[$i],DEFAULT_SITE);
}
}
$where = str_replace('{p}',AGILE_DB_PREFIX,$where);
return $where;
}
/**
* Generate SQL to delete from the database
*/
function sqlDelete($db,$table,$where) {
$db = &DB();
$where = sqlConditions($db,$where);
return sprintf('DELETE FROM %s%s %s',AGILE_DB_PREFIX,$table,$where);
}
/**
* Generate SQL to insert into the database
*/
function sqlInsert($db,$table,$FieldList,$id=false) {
$db = &DB();
if (! $id)
$id = sqlGenID($db,$table);
$FieldList['id'] = $id;
if (empty($FieldList['site_id']))
$FieldList['site_id'] = DEFAULT_SITE;
$table = AGILE_DB_PREFIX.$table;
return $db->GetInsertSQL($table,$FieldList,get_magic_quotes_gpc());
}
/**
* Generate SQL to update records in the database
*/
function sqlUpdate($table,$FieldList,$options=array()) {
$sql = array();
$force = false;
$db = &DB();
# Transition until all calls to sqlUpdate() are changed
# @todo To deprecate
if (func_num_args() >= 4) {
$args = func_get_args();
$db = array_shift($args);
$table = array_shift($args);
$FieldList = array_shift($args); if (! is_array($FieldList)) $FieldList=array($FieldList);
$options['where'] = array_shift($args);
$options['force'] = count($args) ? array_shift($args) : false;
}
if (isset($options['force']))
$force = $options['force'];
if (isset($options['where']))
$sql['where'] = $options['where'];
$rs = $db->Execute(sqlSelect($table,'*',$sql));
return $db->GetUpdateSQL($rs,$FieldList,$force,get_magic_quotes_gpc());
}
/**
* Generate SQL to select records from the database
*/
function sqlSelect($TableList,$FieldList,$sql=array()) {
# Transition until all calls to sqlSelect() are changed
# @todo To deprecate
if (func_num_args() >= 4) {
$sql = array();
$args = func_get_args();
$db = array_shift($args);
$TableList = array_shift($args);
$FieldList = array_shift($args);
$sql['where'] = array_shift($args);
$sql['orderby'] = count($args) ? array_shift($args) : '';
$sql['limit'] = count($args) ? array_shift($args) : 0;
$sql['distinct'] = count($args) ? array_shift($args) : false;
$sql['groupby'] = count($args) ? array_shift($args) : '';
}
# Table(s)
if (is_array($TableList)) {
$tbarr = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V');
$table = '';
$i = 0;
foreach ($TableList as $index => $value) {
if ($i++>0)
$table .= ',';
$table .= sprintf('%s%s AS %s',AGILE_DB_PREFIX,$value,$tbarr[$index]);
}
} else {
$table = AGILE_DB_PREFIX.$TableList;
}
### Field(s)
if(is_array($FieldList)) {
$fields = '';
for($i=0;$i<count($FieldList); $i++) {
if($i>0)
$fields .= ",".$FieldList[$i];
else
$fields .= $FieldList[$i];
}
} else {
# Field(s)
if (isset($sql['distinct']) && $sql['distinct'])
$fields = 'DISTINCT '.$FieldList;
else
$fields = $FieldList;
}
### Condition(s)
$where = sqlConditions( $db, $Conditions, $TableList);
# Condition(s)
$where = sqlConditions($db,$sql['where'],$TableList);
### Order By
if(!empty($Order)) {
$where .= " ORDER BY $Order ";
}
$line = '';
# Group By
if (isset($sql['groupby']) && $sql['groupby'])
$line .= sprintf(' GROUP BY %s',$sql['groupby']);
### Group By
if(!empty($GroupBy)) {
$where .= " GROUP BY $GroupBy ";
}
# Order By
if (isset($sql['orderby']) && $sql['orderby'])
$line .= sprintf(' ORDER BY %s',$sql['orderby']);
$where = str_replace('{p}', AGILE_DB_PREFIX, $where );
# Limit
if (isset($sql['limit']) && $sql['limit'])
$line .= 'LIMIT '.$sql['limit'];
if(!empty($DISTINCT)) $DISTINCT = 'DISTINCT';
$SQL = sprintf('SELECT %s FROM %s %s %s',$fields,$table,$where,$line);
return "SELECT $DISTINCT $fields FROM $table $where";
if (isset($sql['debug']))
printf('<pre>%s</pre>',$SQL);
return $SQL;
}
?>
?>