smarty_search_result(): finally, the last each() call is replaced. yay! \o/

also: code formatting changed slightly
This commit is contained in:
azett 2018-10-23 13:25:07 +02:00
parent eead674de1
commit e2e0e8c0f5

View File

@ -1,26 +1,22 @@
<?php <?php
if (!defined('MOD_INDEX')) { if (!defined('MOD_INDEX')) {
// Example of use // Example of use
require_once 'defaults.php'; require_once 'defaults.php';
require_once (INCLUDES_DIR.'includes.php'); require_once (INCLUDES_DIR . 'includes.php');
require(SMARTY_DIR . 'SmartyValidate.class.php');
require (SMARTY_DIR . 'SmartyValidate.class.php');
system_init(); system_init();
search_main(); search_main();
search_display(); search_display();
} }
function search_title($title, $sep) { function search_title($title, $sep) {
global $lang; global $lang;
return "$title $sep {$lang['search']['head']}"; return "$title $sep {$lang['search']['head']}";
} }
function search_display() {
function search_display() {
global $smarty; global $smarty;
theme_init($smarty); theme_init($smarty);
@ -29,215 +25,187 @@ if (!defined('MOD_INDEX')) {
unset($smarty); unset($smarty);
do_action('shutdown'); do_action('shutdown');
}
function search_main() {
}
function search_main() {
global $lang, $smarty; global $lang, $smarty;
add_action('wp_title', 'search_title', 0, 2); add_action('wp_title', 'search_title', 0, 2);
if(empty($_GET)) { if (empty($_GET)) {
// display form // display form
$title = $lang['search']['head']; $title = $lang ['search'] ['head'];
$content = "shared:search.tpl"; $content = "shared:search.tpl";
} else { } else {
// validate // validate
if(isset($_GET['q']) && $kw = trim($_GET['q'])) { if (isset($_GET ['q']) && $kw = trim($_GET ['q'])) {
$title = $lang['search']['head']; $title = $lang ['search'] ['head'];
$content = "shared:search_results.tpl"; $content = "shared:search_results.tpl";
$kw = strtolower($kw); $kw = strtolower($kw);
search_do($kw); search_do($kw);
} else { } else {
$smarty->assign('error', $lang['search']['error']['keywords']); $smarty->assign('error', $lang ['search'] ['error'] ['keywords']);
$title = $lang['search']['headres']; $title = $lang ['search'] ['headres'];
$content = "shared:search.tpl"; $content = "shared:search.tpl";
} }
} }
$smarty->assign(array('subject'=>$title, 'content'=>$content)); $smarty->assign(array(
'subject' => $title,
'content' => $content
));
return 'default.tpl'; return 'default.tpl';
} }
function search_do($keywords) {
function search_do($keywords) {
global $smarty, $srchresults; global $smarty, $srchresults;
// get parameters // get parameters
$srchkeywords = $keywords; $srchkeywords = $keywords;
$params = array(); $params = array();
$params['start']=0; $params ['start'] = 0;
$params['count']=-1; $params ['count'] = -1;
(!empty($_GET['Date_Day'])) && ($_GET['Date_Day']!='--')? $params['d'] = $_GET['Date_Day']: null; (!empty($_GET ['Date_Day'])) && ($_GET ['Date_Day'] != '--') ? $params ['d'] = $_GET ['Date_Day'] : null;
isset($_GET['Date_Month']) && ($_GET['Date_Month']!='--')? $params['m'] = $_GET['Date_Month'] : null; isset($_GET ['Date_Month']) && ($_GET ['Date_Month'] != '--') ? $params ['m'] = $_GET ['Date_Month'] : null;
!empty($_GET['Date_Year']) && ($_GET['Date_Year']!='--')? $params['y'] = substr($_GET['Date_Year'], 2) : null; !empty($_GET ['Date_Year']) && ($_GET ['Date_Year'] != '--') ? $params ['y'] = substr($_GET ['Date_Year'], 2) : null;
// isset($_GET['cats'])? $params = $_GET['cats']: null; // isset($_GET['cats'])? $params = $_GET['cats']: null;
isset($_GET['cats'])? $params['cats'] = $_GET['cats']: null; isset($_GET ['cats']) ? $params ['cats'] = $_GET ['cats'] : null;
$params ['fullparse'] = false;
$params['fullparse'] = false; if (!empty($_GET ['stype']) && $_GET ['stype'] == 'full') {
$params ['fullparse'] = true;
if(!empty($_GET['stype']) && $_GET['stype']=='full') {
$params['fullparse'] = true;
$fts = "yes"; $fts = "yes";
} else { } else {
$params['fullparse'] = false; $params ['fullparse'] = false;
$fts = "no"; $fts = "no";
} }
$srchparams = $params; $srchparams = $params;
$list = array(); $list = array();
$q = new FPDB_Query($params, null); $q = new FPDB_Query($params, null);
while ($q->hasMore()) { while ($q->hasMore()) {
list($id, $e) = $q->getEntry(); list ($id, $e) = $q->getEntry();
$match = false; $match = false;
if ($keywords == '*') { if ($keywords == '*') {
$match = true; $match = true;
} else { } else {
$match = strpos( $match = strpos(strtolower($e ['subject']), $keywords) !== false;
strtolower($e['subject']),
$keywords
) !== false;
// if (!$match && $params['fullparse']) { // if (!$match && $params['fullparse']) {
if (!$match && ($fts === "yes")) { if (!$match && ($fts === "yes")) {
$match = strpos( $match = strpos(strtolower($e ['content']), $keywords) !== false;
strtolower($e['content']),
$keywords
) !== false;
} }
} }
if ($match) if ($match)
$list[$id] = $e; $list [$id] = $e;
} }
$smarty->register_block('search_result_block', 'smarty_search_results_block'); $smarty->register_block('search_result_block', 'smarty_search_results_block');
$smarty->register_block('search_result', 'smarty_search_result'); $smarty->register_block('search_result', 'smarty_search_result');
if (!$list) if (!$list)
$smarty->assign('noresults', true); $smarty->assign('noresults', true);
$srchresults = $list; $srchresults = $list;
}
function smarty_search_results_block($params, $content, &$smarty, &$repeat) {
}
function smarty_search_results_block($params, $content, &$smarty, &$repeat) {
global $srchresults; global $srchresults;
if ($srchresults) { if ($srchresults) {
return $content; return $content;
} }
}
function smarty_search_result($params, $content, &$smarty, &$repeat) {
}
function smarty_search_result($params, $content, &$smarty, &$repeat) {
/*
// FIXME AZI: each() ersetzen.
Gelernt bisher:
- Diese Funktion wird für jedes Suchergebnis aufgerufen.
- $srchresults ist global und wird vom each() durchlaufen - bei jedem Aufruf geht der Zeiger ein Element weiter
- Frage: Wie kann man das emulieren? Globale Counter-Variable einführen? Oder mit next() das nächste Element von $srchresults holen?
siehe https://secure.php.net/manual/en/function.each.php
*/
global $srchresults, $post; global $srchresults, $post;
$repeat = false; $repeat = false;
if (list($id, $e) = each($srchresults)) { // check if we have at least one more search result
// (current pointer position must not be after the last element)
if (current($srchresults)) {
// get the current search result details
$id = key($srchresults);
$e = $srchresults [$id];
// assign values to template
$smarty->assign('id', $id); $smarty->assign('id', $id);
$post = $e; $post = $e;
$smarty->assign($e); +$smarty->assign($e);
$repeat = true; $repeat = true;
// advance pointer to next search result element
next($srchresults);
}
return $content; return $content;
} }
return $content; /*
} * class rawsearchbot extends fs_filelister {
*
/* * var $_varname = 'cache';
class rawsearchbot extends fs_filelister { * var $_cachefile = null;
* var $_directory = CONTENT_DIR;
var $_varname = 'cache'; * var $_constrained_list = array();
var $_cachefile = null; *
var $_directory = CONTENT_DIR; * function rawsearchbot($keywords, $fullparse, $searchjusthese=array()) {
var $_constrained_list = array(); *
* $this->keywords = strtolower($keywords);
function rawsearchbot($keywords, $fullparse, $searchjusthese=array()) { * $this->fullparse = $fullparse;
* $o = new entry_indexer;
$this->keywords = strtolower($keywords); * $this->entry_arr = $o->getList();
$this->fullparse = $fullparse; *
$o = new entry_indexer; *
$this->entry_arr = $o->getList(); * if ($searchjusthese) {
* $this->_constrained_list = $searchjusthese;
* }
if ($searchjusthese) { * return parent::__construct();
$this->_constrained_list = $searchjusthese; *
} * }
return parent::__construct(); *
* function _checkFile($directory, $file) {
} *
* $f = "$directory/$file";
function _checkFile($directory, $file) { *
* if ( is_dir($f) && ctype_digit($file)) {
$f = "$directory/$file"; * return 1;
* }
if ( is_dir($f) && ctype_digit($file)) { *
return 1; * if (fnmatch('entry*'.EXT, $file)) {
} * $id=basename($file,EXT);
* $arr = $this->entry_arr[$id];
if (fnmatch('entry*'.EXT, $file)) { *
$id=basename($file,EXT); * if (in_array('draft',$arr['categories'])) return 0;
$arr = $this->entry_arr[$id]; *
* if ($this->fullparse) {
if (in_array('draft',$arr['categories'])) return 0; * $str = strtolower(io_load_file(bdb_idtofile($id, BDB_ENTRY)));
*
if ($this->fullparse) { * if (strpos($str, $this->keywords)!==false) {
$str = strtolower(io_load_file(bdb_idtofile($id, BDB_ENTRY))); * $this->_list[$id] = $arr['subject'];
* }
if (strpos($str, $this->keywords)!==false) { *
$this->_list[$id] = $arr['subject']; * } else {
} * if (strpos(strtolower($arr['subject']), $this->keywords)!==false) {
* $this->_list[$id] = $arr['subject'];
} else { * }
if (strpos(strtolower($arr['subject']), $this->keywords)!==false) { * }
$this->_list[$id] = $arr['subject']; * return 0;
} * }
} * }
return 0; *
} * }
}
}
*/ */
?> ?>