Skip to content

Instantly share code, notes, and snippets.

@bigprof
Last active October 1, 2022 12:34
Show Gist options
  • Select an option

  • Save bigprof/1dc6bc2d36353bc1cffb6fc48c2a4cc7 to your computer and use it in GitHub Desktop.

Select an option

Save bigprof/1dc6bc2d36353bc1cffb6fc48c2a4cc7 to your computer and use it in GitHub Desktop.
Basic script to perform global search on all tables of an AppGini application.
<?php
/**
* Basic script to perform global search on all tables of an AppGini application.
* Create a new file inside the hooks folder and name it 'global-search.php' then copy this code to it.
* Related post: https://forums.appgini.com/phpbb/viewtopic.php?f=2&t=1689&p=4510
*/
/* Assuming this custom file is placed inside 'hooks' */
define('PREPEND_PATH', '../');
$hooks_dir = dirname(__FILE__);
include("{$hooks_dir}/../defaultLang.php");
include("{$hooks_dir}/../language.php");
include("{$hooks_dir}/../lib.php");
include_once("{$hooks_dir}/../header.php");
/* check access: modify this part according to who you want to allow to access this page */
$mi = getMemberInfo();
// if(!in_array($mi['username'], array('john.doe', 'jane.doe'))){
// if(!$mi['username'] || $mi['username'] == 'guest'){
if(!in_array($mi['group'], array('Admins', 'Data entry'))){
echo error_message("Access denied");
include_once("{$hooks_dir}/../footer.php");
exit;
}
$search = $_REQUEST['search'];
echo show_search_form($search);
$results = process_search($search);
/*
Before rendering search results, you can optionally modify the $results array here to manipulate the way
results are displayed ...
The structure of the results array is $results[$tablename][$i]['record'][$fieldname]
You could explore it by using:
echo '<pre>';
print_r($results);
echo '</pre>';
Example: to trim the Notes field of the employees table to the first 20 characters only:
trim_results($results, 'employees', 'Notes', 20);
You could repeat the above line many times for trimming other fields.
*/
echo render_search_results($results);
include_once("{$hooks_dir}/../footer.php");
/* use this function to trim the displayed length of a specific field in a specific table to given length */
function trim_results(&$results, $table, $field, $length) {
for($i = 0; $i < count($results[$table]); $i++) {
$ellipsis = '';
$str =& $results[$table][$i]['record'][$field];
if(strlen($str) > $length) $ellipsis = ' ...';
$str = substr($str, 0, $length) . $ellipsis;
}
}
/* function to display search form */
function show_search_form($search = ''){
ob_start();
?>
<div class="page-header"><h1>Global Search</h1></div>
<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="row">
<div class="col-sm-6 col-sm-offset-2">
<input type="text" class="form-control" id="search"
name="search" placeholder="What are you looking for?"
value="<?php echo html_attr($search); ?>"
style="font-size: 2em;"
autofocus>
</div>
<div class="col-sm-2">
<button style="font-size: 2em;" type="submit" class="btn btn-primary btn-block"><i class="glyphicon glyphicon-search"></i> Search</button>
</div>
</div>
</form>
<?php
return ob_get_clean();
}
/* function to process search */
function process_search($search = ''){
if(!$search) return false;
/* get tables accessible by current user */
$tables = getTableList();
if(!count($tables)) return false;
/* perform search */
$results = array();
foreach($tables as $tn => $tdata){
$res = sql(get_search_query($tn, $search), $eo);
while($row = db_fetch_assoc($res)){
$results[$tn][] = array(
'id' => $row['PRIMARY_KEY_VALUE'],
'record' => array_slice($row, 1, NULL, true)
);
}
}
return $results;
}
/* function to render search results */
function render_search_results($results, $search = '') {
if(!is_array($results)) return '';
if(!count($results) || !$results) return '';
$tables = getTableList();
$html = '<h2>Showing matches from ' . count($results) . ' tables</h2>';
foreach($results as $tn => $tres){
if(!count($tres)) continue;
ob_start();
?>
<button type="button" class="btn btn-info btn-lg vspacer-lg">
<?php echo $tables[$tn][0]; ?>
<span class="badge"><?php echo count($tres); ?></span>
</button>
<div class="table-responsive">
<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th></th>
<?php foreach($tres[0]['record'] as $label => $v){ ?>
<th><?php echo $label; ?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php foreach($tres as $rec){ ?>
<?php $link = "../{$tn}_view.php?SelectedID=" . urlencode($rec['id']); ?>
<tr>
<td><a href="<?php echo $link; ?>" class="btn btn-default" target="_blank"><i class="glyphicon glyphicon-search"></i></a></td>
<?php foreach($rec['record'] as $v){ ?>
<td><?php echo $v; ?></td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php
$html .= ob_get_clean();
}
return $html;
}
/* function to get a list of query fields of a given table */
function list_of_fields($tn){
$fields = preg_split('/ as \'.*?\',? ?/', get_sql_fields($tn));
if(!count($fields) || $fields === false) return false;
array_pop($fields); // remove last element as it's an empty string
return $fields;
}
/* function to prepare search query */
function get_search_query($tn, $search){
if(!$search) return false;
$fields = list_of_fields($tn);
if(!$fields) return false;
$safe_search = makeSafe($search);
$where = " AND CONCAT_WS('||', " . implode(', ', $fields) . ") LIKE '%{$safe_search}%'";
$pk = "`{$tn}`.`" . getPKFieldName($tn) . "` as 'PRIMARY_KEY_VALUE'";
$query = "SELECT {$pk}, " . get_sql_fields($tn) . " FROM " . get_sql_from($tn) . $where;
return $query;
}
@AppGiniCourse
Copy link
Copy Markdown

AppGiniCourse commented Mar 7, 2018

Interested to learn more about customizing AppGini applications? Why not check our AppGini customization course on Udemy? This link gives you 20% off!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment