2008-11-26 22:50:40 +00:00
|
|
|
<?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
|
2008-11-26 22:50:40 +00:00
|
|
|
*
|
|
|
|
* @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>
|
2008-11-26 22:50:40 +00:00
|
|
|
* @package AgileBill
|
2009-08-03 04:10:16 +00:00
|
|
|
* @subpackage Core
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The main AgileBill CORE Database SEARCH_SHOW Method
|
|
|
|
*
|
|
|
|
* @uses CORE_search
|
|
|
|
* @uses CORE_trigger
|
2008-11-26 22:50:40 +00:00
|
|
|
*/
|
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
function CORE_database_search_show($VAR, &$construct, $type) {
|
|
|
|
# Set the field list for this method:
|
2008-11-26 22:50:40 +00:00
|
|
|
$arr = $construct->method[$type];
|
|
|
|
$field_list = '';
|
2009-08-03 04:10:16 +00:00
|
|
|
$construct->linked = array();
|
|
|
|
|
2008-11-26 22:50:40 +00:00
|
|
|
$i=0;
|
2009-08-03 04:10:16 +00:00
|
|
|
while (list($key,$value) = each($arr)) {
|
|
|
|
$field_var = sprintf('%s_%s',$construct->table,$value);
|
|
|
|
|
|
|
|
if ($i == 0)
|
|
|
|
$field_list .= sprintf('%s%s.%s',AGILE_DB_PREFIX,$construct->table,$value);
|
2008-11-26 22:50:40 +00:00
|
|
|
else
|
2009-08-03 04:10:16 +00:00
|
|
|
$field_list .= sprintf(',%s%s.%s',AGILE_DB_PREFIX,$construct->table,$value);
|
|
|
|
|
|
|
|
# Determine if this record is linked to another table/field
|
|
|
|
if ($construct->field[$value]['asso_table'] != '')
|
|
|
|
array_push($construct->linked,array('field'=>$value,'link_table'=>$construct->field[$value]['asso_table'],'link_field'=>$construct->field[$value]['asso_field']));
|
|
|
|
|
2008-11-26 22:50:40 +00:00
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
# Get the search details:
|
|
|
|
if (isset($VAR['search_id'])) {
|
|
|
|
include_once(PATH_CORE.'search.inc.php');
|
2008-11-26 22:50:40 +00:00
|
|
|
$search = new CORE_search;
|
2009-08-03 04:10:16 +00:00
|
|
|
|
2008-11-26 22:50:40 +00:00
|
|
|
$search->get($VAR['search_id']);
|
2009-08-03 04:10:16 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
# Invalid search!
|
|
|
|
echo '<br/>The search terms submitted were invalid!<br/>';
|
|
|
|
|
|
|
|
if (isset($construct->trigger[$type])) {
|
|
|
|
include_once(PATH_CORE.'trigger.inc.php');
|
|
|
|
$trigger = new CORE_trigger;
|
|
|
|
|
|
|
|
$trigger->trigger($construct->trigger[$type],0,$VAR);
|
2008-11-26 22:50:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check that this search has not been taken over by another account
|
|
|
|
if ($search->session != SESS && $search->account != SESS_ACCOUNT) {
|
|
|
|
global $C_debug;
|
|
|
|
$C_debug->alert('You are not authorized to view this search!');
|
2009-08-03 04:10:16 +00:00
|
|
|
|
2008-11-26 22:50:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
# Get the sort order details:
|
|
|
|
if (isset($VAR['order_by']) && $VAR['order_by'] != '') {
|
|
|
|
$order_by = sprintf(' ORDER BY %s',$VAR['order_by']);
|
2008-11-26 22:50:40 +00:00
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
$smarty_order = $VAR['order_by'];
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$order_by = sprintf(' ORDER BY %s',$construct->order_by);
|
|
|
|
$smarty_order = $search->order_by;
|
|
|
|
}
|
2008-11-26 22:50:40 +00:00
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
# Determine the sort order
|
|
|
|
if (isset($VAR['desc'])) {
|
2008-11-26 22:50:40 +00:00
|
|
|
$order_by .= ' DESC';
|
|
|
|
$smarty_sort = 'desc=';
|
2009-08-03 04:10:16 +00:00
|
|
|
|
|
|
|
} elseif (isset($VAR['asc'])) {
|
2008-11-26 22:50:40 +00:00
|
|
|
$order_by .= ' ASC';
|
|
|
|
$smarty_sort = 'asc=';
|
2009-08-03 04:10:16 +00:00
|
|
|
|
2008-11-26 22:50:40 +00:00
|
|
|
} else {
|
2009-08-03 04:10:16 +00:00
|
|
|
if (! preg_match('/date/',$smarty_order)) {
|
2008-11-26 22:50:40 +00:00
|
|
|
$order_by .= ' ASC';
|
|
|
|
$smarty_sort = 'asc=';
|
2009-08-03 04:10:16 +00:00
|
|
|
|
|
|
|
} else {
|
2008-11-26 22:50:40 +00:00
|
|
|
$order_by .= ' DESC';
|
|
|
|
$smarty_sort = 'desc=';
|
|
|
|
}
|
2009-08-03 04:10:16 +00:00
|
|
|
}
|
2008-11-26 22:50:40 +00:00
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
# Generate the full query
|
|
|
|
$db = &DB();
|
2008-11-26 22:50:40 +00:00
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
$q = str_replace('%%fieldList%%',$field_list,$search->sql);
|
|
|
|
$q = str_replace('%%tableList%%',AGILE_DB_PREFIX.$construct->table,$q);
|
|
|
|
$q = str_replace('%%whereList%%','',$q);
|
|
|
|
|
|
|
|
$q .= sprintf(' %s.site_id = %s',AGILE_DB_PREFIX.$construct->table,DEFAULT_SITE);
|
2008-11-26 22:50:40 +00:00
|
|
|
$q .= $order_by;
|
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
# Determine the offset & limit
|
|
|
|
$current_page = 1;
|
|
|
|
$offset = -1;
|
|
|
|
|
|
|
|
if (! empty($VAR['page']))
|
|
|
|
$current_page = $VAR['page'];
|
|
|
|
if (empty($search->limit))
|
|
|
|
$search->limit=25;
|
|
|
|
|
|
|
|
if ($current_page>1)
|
|
|
|
$offset = (($current_page*$search->limit)-$search->limit);
|
2008-11-26 22:50:40 +00:00
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
$result = $db->SelectLimit($q,$search->limit,$offset);
|
2008-11-26 22:50:40 +00:00
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
# Error reporting
|
|
|
|
if ($result === false) {
|
2008-11-26 22:50:40 +00:00
|
|
|
global $C_debug;
|
2009-08-03 04:10:16 +00:00
|
|
|
$C_debug->error(__FILE__,__METHOD__,$db->ErrorMsg());
|
2008-11-26 22:50:40 +00:00
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
if (isset($construct->trigger[$type])) {
|
|
|
|
include_once(PATH_CORE.'trigger.inc.php');
|
|
|
|
$trigger = new CORE_trigger;
|
|
|
|
|
|
|
|
$trigger->trigger($construct->trigger[$type],0,$VAR);
|
2008-11-26 22:50:40 +00:00
|
|
|
}
|
2009-08-03 04:10:16 +00:00
|
|
|
|
|
|
|
return;
|
2008-11-26 22:50:40 +00:00
|
|
|
}
|
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
# Run any custom validation on this result for this module
|
|
|
|
$i = 0;
|
|
|
|
$class_name = true;
|
|
|
|
if (isset($construct->custom_EXP)) {
|
|
|
|
while (! $result->EOF) {
|
|
|
|
for ($ei=0; $ei<count($construct->custom_EXP); $ei++) {
|
|
|
|
$field = $construct->custom_EXP[$ei]['field'];
|
|
|
|
$value = $construct->custom_EXP[$ei]['value'];
|
2008-11-26 22:50:40 +00:00
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
if ($result->fields[$field] == $value) {
|
2008-11-26 22:50:40 +00:00
|
|
|
$smart[$i] = $result->fields;
|
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
if ($class_name) {
|
2008-11-26 22:50:40 +00:00
|
|
|
$smart[$i]['_C'] = 'row1';
|
2009-08-03 04:10:16 +00:00
|
|
|
$class_name = false;
|
2008-11-26 22:50:40 +00:00
|
|
|
} else {
|
|
|
|
$smart[$i]['_C'] = 'row2';
|
2009-08-03 04:10:16 +00:00
|
|
|
$class_name = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$i++;
|
2008-11-26 22:50:40 +00:00
|
|
|
$ei = count($construct->custom_EXP);
|
2009-08-03 04:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-26 22:50:40 +00:00
|
|
|
$result->MoveNext();
|
|
|
|
}
|
2009-08-03 04:10:16 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
while (! $result->EOF) {
|
2008-11-26 22:50:40 +00:00
|
|
|
$smart[$i] = $result->fields;
|
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
if ($class_name) {
|
2008-11-26 22:50:40 +00:00
|
|
|
$smart[$i]['_C'] = 'row1';
|
2009-08-03 04:10:16 +00:00
|
|
|
$class_name = false;
|
2008-11-26 22:50:40 +00:00
|
|
|
} else {
|
|
|
|
$smart[$i]['_C'] = 'row2';
|
2009-08-03 04:10:16 +00:00
|
|
|
$class_name = true;
|
2008-11-26 22:50:40 +00:00
|
|
|
}
|
2009-08-03 04:10:16 +00:00
|
|
|
|
2008-11-26 22:50:40 +00:00
|
|
|
$result->MoveNext();
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
# Get any linked fields
|
|
|
|
if ($i > 0) {
|
2008-11-26 22:50:40 +00:00
|
|
|
$db_join = new CORE_database;
|
|
|
|
$construct->result = $db_join->join_fields($smart, $construct->linked);
|
2009-08-03 04:10:16 +00:00
|
|
|
|
2008-11-26 22:50:40 +00:00
|
|
|
} else {
|
|
|
|
$construct->result = $smart;
|
2009-08-03 04:10:16 +00:00
|
|
|
}
|
2008-11-26 22:50:40 +00:00
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
# Get the result count:
|
2008-11-26 22:50:40 +00:00
|
|
|
$results = $result->RecordCount();
|
|
|
|
|
|
|
|
# define the DB vars as a Smarty accessible block
|
|
|
|
global $smarty;
|
|
|
|
|
|
|
|
# define the results
|
2009-08-03 04:10:16 +00:00
|
|
|
$smarty->assign('search_show',$construct->result);
|
|
|
|
$smarty->assign('page',$VAR['page']);
|
|
|
|
$smarty->assign('order',$smarty_order);
|
|
|
|
$smarty->assign('sort',$smarty_sort);
|
|
|
|
$smarty->assign('limit',$search->limit);
|
2008-11-26 22:50:40 +00:00
|
|
|
$smarty->assign('search_id',$search->id);
|
2009-08-03 04:10:16 +00:00
|
|
|
$smarty->assign('results',$search->results);
|
2008-11-26 22:50:40 +00:00
|
|
|
|
|
|
|
# get the total pages for this search:
|
|
|
|
if (empty($search->limit))
|
|
|
|
$construct->pages = 1;
|
|
|
|
else
|
|
|
|
$construct->pages = intval($search->results / $search->limit);
|
2009-08-03 04:10:16 +00:00
|
|
|
|
|
|
|
if ($search->results % $search->limit)
|
|
|
|
$construct->pages++;
|
|
|
|
|
|
|
|
# Total pages
|
|
|
|
$smarty->assign('pages',$construct->pages);
|
|
|
|
|
|
|
|
# Current page
|
|
|
|
$smarty->assign('page',$current_page);
|
|
|
|
|
|
|
|
$page_arr = array();
|
|
|
|
for ($i=0; $i<=$construct->pages; $i++)
|
|
|
|
if ($construct->page != $i)
|
|
|
|
array_push($page_arr,$i);
|
|
|
|
|
|
|
|
# Page array for menu
|
|
|
|
$smarty->assign('page_arr',$page_arr);
|
|
|
|
|
|
|
|
if(isset($construct->trigger[$type])) {
|
|
|
|
include_once(PATH_CORE.'trigger.inc.php');
|
|
|
|
$trigger = new CORE_trigger;
|
|
|
|
|
|
|
|
$trigger->trigger($construct->trigger[$type],1,$VAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $construct->result;
|
2008-11-26 22:50:40 +00:00
|
|
|
}
|
2009-08-03 04:10:16 +00:00
|
|
|
?>
|