some search.php refactoring

This commit is contained in:
following
2013-06-18 14:39:14 +02:00
parent 9a43ed2540
commit 1bed97e4d9
4 changed files with 144 additions and 98 deletions
+2
View File
@@ -2504,6 +2504,7 @@ INSERT INTO `sys_trans` (`id`, `text`, `last_modified`) VALUES ('2082', 'Locked'
INSERT INTO `sys_trans` (`id`, `text`, `last_modified`) VALUES ('2083', 'NEW', '2013-04-25 23:00:00');
INSERT INTO `sys_trans` (`id`, `text`, `last_modified`) VALUES ('2084', 'show archived', '2013-04-25 23:00:00');
INSERT INTO `sys_trans` (`id`, `text`, `last_modified`) VALUES ('2085', 'hide archived', '2013-04-25 23:00:00');
INSERT INTO `sys_trans` (`id`, `text`, `last_modified`) VALUES ('2086', 'unknown search type', '2013-04-25 23:00:00');
-- Table sys_trans_ref
SET NAMES 'utf8';
@@ -6460,6 +6461,7 @@ INSERT INTO `sys_trans_text` (`trans_id`, `lang`, `text`, `last_modified`) VALUE
INSERT INTO `sys_trans_text` (`trans_id`, `lang`, `text`, `last_modified`) VALUES ('2083', 'DE', 'NEU', '2013-04-25 23:00:00');
INSERT INTO `sys_trans_text` (`trans_id`, `lang`, `text`, `last_modified`) VALUES ('2084', 'DE', 'archivierte einblenden', '2013-04-25 23:00:00');
INSERT INTO `sys_trans_text` (`trans_id`, `lang`, `text`, `last_modified`) VALUES ('2085', 'DE', 'archivierte ausblenden', '2013-04-25 23:00:00');
INSERT INTO `sys_trans_text` (`trans_id`, `lang`, `text`, `last_modified`) VALUES ('2086', 'DE', 'unbekannter Suchtyp', '2013-04-25 23:00:00');
INSERT INTO `sys_trans_text` (`trans_id`, `lang`, `text`, `last_modified`) VALUES ('1', 'EN', 'Reorder IDs \r', '2010-09-02 00:15:30');
INSERT INTO `sys_trans_text` (`trans_id`, `lang`, `text`, `last_modified`) VALUES ('2', 'EN', 'The database could not be reconnected.', '2010-08-28 11:48:07');
INSERT INTO `sys_trans_text` (`trans_id`, `lang`, `text`, `last_modified`) VALUES ('3', 'EN', 'Testing please do not login', '2010-08-28 11:48:07');
+2 -1
View File
@@ -17,7 +17,6 @@
$outputformat_notexist = t('The selected output format is unknown!');
$error_query_not_found = t('The search operation could not be executed, please reenter the search data.');
$safelink = '&nbsp;[<a href="query.php?action=save&queryid={queryid}">' . t('Save') . '</a>]';
$caches_newstring = '<b class="newsymbol">&nbsp;' . t('NEW') . '&nbsp;</b>&nbsp;';
$caches_olddays = 14; // changed from 7 to 14 -- following 2013/6/17
@@ -95,6 +94,8 @@
$search_in_gm = '<a href="http://maps.google.de/maps?f=q&hl=de&q=' . urlencode("http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']."&output=kml") . '" title="' . $translate->t('Show in Google Maps','','',0) . '">' . $translate->t('(in GM)','','',0) . '</a>';
$search_in_gm_zip = '<a href="http://maps.google.de/maps?f=q&hl=de&q=' . urlencode("http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']."&output=kml&zip=1&count=max") . '" title="' . $translate->t('Show in Google Maps','','',0) . '">' . $translate->t('(in GM)','','',0) . '</a>';
$unknown_searchtype = t('unknown search type');
function dateDiff($interval, $dateTimeBegin, $dateTimeEnd)
{
-6
View File
@@ -326,12 +326,6 @@
//count($caches) - 1
tpl_set_var('pages', $pages);
// speichern-link
if ($usr === false)
tpl_set_var('safelink', '');
else
tpl_set_var('safelink', mb_ereg_replace('{queryid}', $options['queryid'], $safelink));
// downloads
tpl_set_var('queryid', $options['queryid']);
tpl_set_var('startat', $startat);
+140 -91
View File
@@ -1,38 +1,37 @@
<?php
/***************************************************************************
./search.php
-------------------
begin : July 25 2004
For license information see doc/license.txt
For license information see doc/license.txt
****************************************************************************/
Unicode Reminder メモ
/****************************************************************************
Search options will be loaded from
- a saved or caches query in queries table, if either 'queryid' parameter
or 'lastqueryid' cookie is present and the query exists; otherwise from
- supplied HTTP parameters or
- hard-coded default values
Unicode Reminder メモ
search and export page for caches, users, logs and pictures possible output
formats are currently XHTML and XML. The search options can be loaded from
stored query in the database, dump of the options in HTTP-POST/GET variable
or HTML form fields
TODO:
- fehlermeldungen bei falschen koordinaten
- entfernungsberechnung "auslagern" (getSqlDistanceFormula überall verwenden)
- nochmals alles testen
'showresult' parameter triggers building the SQL query from search options
and calling the module specified by the 'output' parameter, which will
execute the SQL and process the results.
If 'showresult' != 1, the query options form is presented to the user.
****************************************************************************/
//prepare the templates and include all neccessary
require_once('./lib/common.inc.php');
require_once('./lib/search.inc.php');
//=========================================================
// 1. Database initialization
//=========================================================
// SQL-Debug?
$sqldebug = false;
global $sql_debug;
$sql_debug = $sqldebug;
// output for map-server must be sent to the master!
// (obsolete code for old map.php)
if (isset($_REQUEST['output']) && ($_REQUEST['output'] == 'map'))
db_connect_primary_slave();
@@ -42,13 +41,17 @@
sqldbg_begin();
}
//Preprocessing
if ($error == false)
if ($error == false) // what is $error ?
{
//=========================================================
// 2. initialize searching and template variables
//=========================================================
$tplname = 'search';
require($stylepath . '/search.inc.php');
//km => target-unit
// km => target-unit
$multiplier['km'] = 1;
$multiplier['sm'] = 0.62137;
$multiplier['nm'] = 0.53996;
@@ -94,7 +97,9 @@
newquery:
if ($queryid == 0)
{
// das Suchformular wird initialisiert (keine Vorbelegungen vorhanden)
// initialize search form with defaults, as we have no parameters
// or saved query to start from
$_REQUEST['cache_attribs'] = '';
$rs = sql('SELECT `id` FROM `cache_attrib` WHERE `default`=1 AND NOT IFNULL(`hidden`, 0)=1');
while ($r = sql_fetch_assoc($rs))
@@ -116,10 +121,20 @@
}
$queryid = $queryid + 0;
//=========================================================
// 3. Build search options ($options) array
//=========================================================
if ($queryid != 0)
{
//load options from db
$query_rs = sql("SELECT `user_id`, `options` FROM `queries` WHERE id='&1' AND (`user_id`=0 OR `user_id`='&2')", $queryid, $usr['userid']+0);
// load search options from saved/cached query
$query_rs = sql("
SELECT `user_id`, `options`
FROM `queries`
WHERE id='&1' AND (`user_id`=0 OR `user_id`='&2')",
$queryid, $usr['userid']+0);
if (mysql_num_rows($query_rs) == 0)
{
@@ -146,11 +161,11 @@
sql("UPDATE `queries` SET `last_queried`=NOW() WHERE `id`='&1'", $queryid);
// änderbare werte überschreiben
// overwrite variable options
if (isset($_REQUEST['output']))
$options['output'] = $_REQUEST['output'];
if (isset($_REQUEST['showresult'])) // Ocprop
if (isset($_REQUEST['showresult']))
{
$options['showresult'] = $_REQUEST['showresult'];
}
@@ -162,7 +177,7 @@
}
}
// finderid in finder umsetzen
// get findername from finderid
$options['finderid'] = isset($options['finderid']) ? $options['finderid'] + 0 : 0; // Ocprop
if(isset($options['finder']) && $options['finderid'] > 0)
{
@@ -176,7 +191,7 @@
mysql_free_result($rs_name);
}
// ownerid in owner umsetzen
// get ownername from ownerid
$options['ownerid'] = isset($options['ownerid']) ? $options['ownerid'] + 0 : 0; // Ocprop
if(isset($options['owner']) && $options['ownerid'] > 0)
{
@@ -191,8 +206,10 @@
}
}
}
else
else // $queryid == 0
{
// build search options from GET/POST parameters or default values
// hack
if(isset($_REQUEST['searchto']) && ($_REQUEST['searchto'] != ''))
{
@@ -358,6 +375,8 @@
tpl_errorMsg('search', 'Unknown search option');
else
{
// Set default search type; this prevents errors in outputSearchForm()
// when initializing searchtype-dependent options:
$options['searchtype'] = 'byname';
$options['cachename'] = '';
}
@@ -378,49 +397,39 @@
$options['terrainmax'] = isset($_REQUEST['terrainmax']) ? $_REQUEST['terrainmax']+0 : 0;
$options['recommendationmin'] = isset($_REQUEST['recommendationmin']) ? $_REQUEST['recommendationmin']+0 : 0;
if ($options['showresult'] != 0)
{
//save the search-options in the database
if (isset($options['queryid']) && (isset($options['userid'])))
{
if ($options['userid'] != 0)
sql("UPDATE `queries` SET `options`='&1', `last_queried`=NOW() WHERE `id`='&2' AND `user_id`='&3'", serialize($options), $options['queryid'], $options['userid']);
}
else
{
$bSkipQueryId = isset($_REQUEST['skipqueryid']) ? $_REQUEST['skipqueryid']+0 : 0;
if ($bSkipQueryId == 0)
{
sql('INSERT INTO `queries` (`user_id`, `options`, `last_queried`) VALUES (0, \'&1\', NOW())', serialize($options));
$options['queryid'] = mysql_insert_id();
}
else
{
$options['queryid'] = 0;
}
}
}
else
{
$options['queryid'] = 0;
}
}
$options['queryid'] = 0;
} // $queryid == 0
$bSkipQueryId = isset($_REQUEST['skipqueryid']) ? $_REQUEST['skipqueryid']+0 : 0;
if ($bSkipQueryId == 0)
//=========================================================
// 4. query caching
//=========================================================
$bRememberQuery = isset($_REQUEST['skipqueryid']) ? !$_REQUEST['skipqueryid'] : true;
// This is used by the map, which implements its own query-caching.
if ($bRememberQuery)
{
if ($options['showresult'] != 0) // 'showresult' = "execute query"
{
sql("INSERT INTO `queries` (`user_id`, `options`, `last_queried`) VALUES (0, '&1', NOW())", serialize($options));
$options['queryid'] = mysql_insert_id();
}
set_cookie_setting('lastqueryid', $options['queryid']);
}
// remove old queries (after 1 hour without use)
// (execute only every 50 search calls)
// remove old queries (after 1 hour without use);
// execute only every 50 search calls
if (rand(1, 50) == 1)
{
$removedate = date('Y-m-d H:i:s', time() - 3600);
sql('DELETE FROM `queries` WHERE `last_queried` < \'&1\' AND `user_id`=0', $removedate);
sql("DELETE FROM `queries` WHERE `last_queried` < NOW() - INTERVAL 1 HOUR AND `user_id`=0");
}
// set new values to default if they are not stored in the DB
//=========================================================
// 5. set defaults for new search options
// which may not be present in a stored/cached query
//=========================================================
if (!isset($options['orderRatingFirst'])) $options['orderRatingFirst'] = false;
if (!isset($options['f_otherPlatforms'])) $options['f_otherPlatforms'] = 0;
if (!isset($options['difficultymin'])) $options['difficultymin'] = 0;
@@ -432,10 +441,14 @@
if (!isset($options['cachesize'])) $options['cachesize'] = '';
if (!isset($options['bbox'])) $options['bbox'] = false;
//prepare output
if(!isset($options['showresult'])) $options['showresult']='0';
if (!isset($options['showresult'])) $options['showresult']='0';
if ($options['showresult'] == 1)
{
//===============================================================
// X6. build SQL statement from search options
//===============================================================
if(!isset($options['output'])) $options['output']='';
if ((mb_strpos($options['output'], '.') !== false) ||
(mb_strpos($options['output'], '/') !== false) ||
@@ -445,7 +458,7 @@
$options['output'] = 'HTML';
}
//make a list of cache-ids that are in the result
// make a list of cache-ids that are in the result
if(!isset($options['expert'])) $options['expert']='';
if ($options['expert'] == 0) // Ocprop
{
@@ -547,7 +560,7 @@
AND `longitude` < ' . ($lon + $max_lon_diff) . '
AND `latitude` > ' . ($lat - $max_lat_diff) . '
AND `latitude` < ' . ($lat + $max_lat_diff) . '
HAVING `distance` < ' . $distance);
HAVING `distance` < ' . ($distance+0));
sql_slave('ALTER TABLE result_caches ADD PRIMARY KEY ( `cache_id` )');
$sql_select[] = '`result_caches`.`cache_id`';
@@ -687,7 +700,7 @@
AND `longitude` < ' . ($lon + $max_lon_diff) . '
AND `latitude` > ' . ($lat - $max_lat_diff) . '
AND `latitude` < ' . ($lat + $max_lat_diff) . '
HAVING `distance` < ' . $distance);
HAVING `distance` < ' . ($distance+0));
sql_slave('ALTER TABLE result_caches ADD PRIMARY KEY ( `cache_id` )');
$sql_select[] = '`result_caches`.`cache_id`';
@@ -800,7 +813,7 @@
AND `longitude` < ' . ($lon + $max_lon_diff) . '
AND `latitude` > ' . ($lat - $max_lat_diff) . '
AND `latitude` < ' . ($lat + $max_lat_diff) . '
HAVING `distance` < ' . $distance);
HAVING `distance` < ' . ($distance+0));
sql_slave('ALTER TABLE result_caches ADD PRIMARY KEY ( `cache_id` )');
$sql_select[] = '`result_caches`.`cache_id`';
@@ -890,24 +903,24 @@
}
else
{
tpl_errorMsg('search', 'Unbekannter Suchtyp');
tpl_errorMsg('search', $unknown_searchtype);
}
// additional options
if(!isset($options['f_userowner'])) $options['f_userowner']='0'; // Ocprop
if($options['f_userowner'] != 0) { $sql_where[] = '`caches`.`user_id`!=\'' . $usr['userid'] .'\''; }
if (!isset($options['f_userowner'])) $options['f_userowner']='0'; // Ocprop
if ($options['f_userowner'] != 0) { $sql_where[] = '`caches`.`user_id`!=\'' . $usr['userid'] .'\''; }
if(!isset($options['f_userfound'])) $options['f_userfound']='0'; // Ocprop
if($options['f_userfound'] != 0)
if (!isset($options['f_userfound'])) $options['f_userfound']='0'; // Ocprop
if ($options['f_userfound'] != 0)
{
$sql_where[] = '`caches`.`cache_id` NOT IN (SELECT `cache_logs`.`cache_id` FROM `cache_logs` WHERE `cache_logs`.`user_id`=\'' . sql_escape($usr['userid']) . '\' AND `cache_logs`.`type` IN (1, 7))';
}
if(!isset($options['f_inactive'])) $options['f_inactive']='0'; // Ocprop
if($options['f_inactive'] != 0) $sql_where[] = '`caches`.`status`=1';
if (!isset($options['f_inactive'])) $options['f_inactive']='0'; // Ocprop
if ($options['f_inactive'] != 0) $sql_where[] = '`caches`.`status`=1';
if(isset($usr))
if (isset($usr))
{
if(!isset($options['f_ignored'])) $options['f_ignored']='0';
if (!isset($options['f_ignored'])) $options['f_ignored']='0';
if($options['f_ignored'] != 0)
{
// only use this filter, if it is realy needed - this enables better caching in map2.php with ignored-filter
@@ -919,20 +932,20 @@
}
}
}
if(!isset($options['f_otherPlatforms'])) $options['f_otherPlatforms']='0';
if($options['f_otherPlatforms'] != 0)
if (!isset($options['f_otherPlatforms'])) $options['f_otherPlatforms']='0';
if ($options['f_otherPlatforms'] != 0)
{
// $sql_where[] = '`caches`.`wp_nc`=\'\' AND `caches`.`wp_gc`=\'\'';
// ignore NC listings, which are mostly unmaintained or dead
$sql_where[] = "`caches`.`wp_gc_maintained`=''";
}
if(!isset($options['country'])) $options['country']='';
if($options['country'] != '')
if (!isset($options['country'])) $options['country']='';
if ($options['country'] != '')
{
$sql_where[] = '`caches`.`country`=\'' . sql_escape($options['country']) . '\'';
}
if($options['cachetype'] != '')
if ($options['cachetype'] != '')
{
$types = explode(';', $options['cachetype']);
if (count($types) < sql_value_slave("SELECT COUNT(*) FROM `cache_type`", 0))
@@ -942,7 +955,7 @@
}
}
if($options['cachesize'] != '')
if ($options['cachesize'] != '')
{
$sizes = explode(';', $options['cachesize']);
if (count($sizes) < sql_value_slave("SELECT COUNT(*) FROM `cache_size`", 0))
@@ -974,7 +987,7 @@
$sql_where[] = '`stat_caches`.`toprating`>=\'' . sql_escape($options['recommendationmin']) . '\'';
}
if(isset($options['cache_attribs']) && count($options['cache_attribs']) > 0)
if (isset($options['cache_attribs']) && count($options['cache_attribs']) > 0)
{
foreach ($options['cache_attribs'] AS $attr)
{
@@ -983,7 +996,7 @@
}
}
if(isset($options['cache_attribs_not']) && count($options['cache_attribs_not']) > 0)
if (isset($options['cache_attribs_not']) && count($options['cache_attribs_not']) > 0)
{
foreach ($options['cache_attribs_not'] AS $attr)
{
@@ -1021,14 +1034,28 @@
$group .
$having;
//echo "DEBUG ".$sqlFilter." DEBUG<br>";
// echo "DEBUG ".$sqlFilter." DEBUG<br>";
}
else
{
tpl_errorMsg('search', 'Unbekannter Suchtyp');
}
//go to final output preparation
//=================================================================
// X7. run postprocessing module depending on 'output' option
//
// The following variables (list may be incomplete) are used
// by the postprocessing modules:
//
// $_REQUEST['startat']
// $_REQUEST['count']
// $lat_rad, $lon_rad
// $options['sortby']
// $options['orderRatingFirst']
// $options['queryid']
// $sqlFilter
//=================================================================
// Ocprop: HTML, gpx
if (!file_exists($opt['rootpath'] . 'lib/search.' . mb_strtolower($options['output']) . '.inc.php'))
{
@@ -1038,13 +1065,17 @@
}
else
{
//process and output the search result
// run search query and output the results
require($opt['rootpath'] . 'lib/search.' . mb_strtolower($options['output']) . '.inc.php');
exit;
}
}
else
else // $options['showresult'] == 0
{
//=============================================================
// F6. present search options form to the user
//=============================================================
$options['show_all_countries'] = isset($_REQUEST['show_all_countries']) ? $_REQUEST['show_all_countries'] : 0;
if (isset($_REQUEST['show_all_countries_submit']))
@@ -1064,10 +1095,20 @@
exit;
}
}
}
} // $error != false
//=============================================================
// F8. show search options form or error message
//=============================================================
tpl_BuildTemplate();
//=============================================================
// F7. prepare search options form
//=============================================================
function outputSearchForm($options)
{
global $login;
@@ -1585,6 +1626,12 @@ function outputSearchForm($options)
exit;
}
//=============================================================
// Prompt the user with a list of locations when the entered
// 'ort' or 'plz' is not unique.
//=============================================================
function outputUniidSelectionForm($uniSql, $urlparams)
{
global $tplname, $locline, $stylepath, $bgcolor1, $bgcolor2, $gns_countries;
@@ -1744,6 +1791,7 @@ function outputUniidSelectionForm($uniSql, $urlparams)
exit;
}
function outputLocidSelectionForm($locSql, $urlparams)
{
global $tplname, $locline, $stylepath, $bgcolor1, $bgcolor2;
@@ -1836,4 +1884,5 @@ function outputLocidSelectionForm($locSql, $urlparams)
tpl_BuildTemplate();
exit;
}
?>