Skip to content

Commit

Permalink
Added support for the advanced query
Browse files Browse the repository at this point in the history
  • Loading branch information
jbogdani committed Sep 30, 2021
1 parent 087c964 commit cec94f6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
58 changes: 58 additions & 0 deletions lib/Article.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @author Julian Bogdani <jbogdani@gmail.com>
* @copyright 2007-2021 Julian Bogdani
Expand Down Expand Up @@ -463,6 +464,63 @@ public static function getByTag($tags, $lang = false, $lax = false, $admin = fal
return $art_list;
}

/**
* Performs an advanced search
*
* @param array $search
* @param string $conn
* @param string|null $lang
* @param integer $start
* @param integer $max
* @param boolean $returnCount
* @return void
*/
public static function advSearch(
array $search,
string $conn = null,
string $lang = null,
int $start = 0,
int $max = 30,
bool $returnCount = false
) {

$values = [];
$sql_part = [];
if(!in_array($conn, ['AND', 'OR'])){
$conn = 'OR';
}

foreach ($search as $fld => $str) {
// Fieldname must be a string, not a number index
if (!is_string($fld)){
throw new Exception("Invalid search array: keys must be strings and must contain field names");
}
// Searched string must not be empty
if ($str !== ''){
array_push($sql_part, "`" . $fld . "` LIKE ? ");
array_push($values, "%" . $str . "%");
}
}

$sql = implode(" {$conn} ", $sql_part);

if ($returnCount) {
return R::count(self::art_tb(), $sql, $values);
}

$sql .= " LIMIT ?, ?";

array_push($values, $start, $max);

$articles = R::find(
self::art_tb(),
$sql,
$values
);

return self::parseArt($articles, $lang);
}


/**
* Returns array of beams matching string in title, summary, text or keywords
Expand Down
24 changes: 17 additions & 7 deletions lib/Out.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ private function loadSettings($get, $lang = false)

// 2. search
$this->cfg['context'] = 'search';
$this->cfg['searchString'] = urldecode($get['search']);
$this->cfg['searchString'] = $get['search'];
$this->cfg['searchParams'] = $get['searchParams'];
} elseif ($get['tags'] || $last === '') {

// 3. tags
Expand Down Expand Up @@ -641,14 +642,23 @@ public function getLanguages()
*/
public function getSearchResults($string = false, $page = false, $max = false)
{
$string = $string ? $string : $this->getSearchString();
$string = preg_replace('/^"(.+)"$/', '$1', $string, -1, $tot);

$this->totalArticles = Article::search($string, false, false, false, false, true);

$string = $string ?: $this->cfg['searchString'];
list($start, $max) = $this->page2limit($page, $max);

return Article::search($string, ($tot > 0), $this->getLang('input'), $start, $max);
if ( $string === 'adv' ) {
if (!$this->cfg['searchParams']){
error_log("Invalid search parameters.");
return false;
}
$this->totalArticles = Article::advSearch($this->cfg['searchParams'], null, null, false, false, true);
return Article::advSearch($this->cfg['searchParams'], null, $this->getLang('input'), $start, $max, false);
} else {
$string = preg_replace('/^"(.+)"$/', '$1', $string, -1, $tot);

$this->totalArticles = Article::search($string, false, false, false, false, true);
return Article::search($string, $this->getLang('input'), false, $start, $max);
}

}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public static function run()
return [
'lang' => $lng,
'search' => $string,
'page' => $pg
'page' => $pg,
'searchParams' => $_GET
];
}, 'search');

Expand Down
1 change: 1 addition & 0 deletions version
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
5.8.0[] = "Added support for the advanced query"
5.7.1[] = "Removed debugging error_log in template_ctrl"
5.7.0[] = "Changed format of logAttempts, and rised time buffer to 2 seconds"
5.6.2[] = "Optimized calculation of path of assets"
Expand Down

0 comments on commit cec94f6

Please sign in to comment.