Halfway implemented a new search interface

This commit is contained in:
root 2021-05-25 11:35:21 +02:00
parent 3bb3484724
commit bef253940c
5 changed files with 126 additions and 58 deletions

27
include/Entity.php Normal file

@ -0,0 +1,27 @@
<?php
class Entity {
protected function __construct() {
}
protected function specify_search($searchterms, $searchfields) {
if(array_key_exists('fritext', $searchterms)) {
$freeterm = $searchterms['fritext'];
unset($searchterms['fritext']);
foreach($searchfields as $field) {
if(array_key_exists($field, $searchterms)) {
$term = $searchterms[$field];
if(is_array($term)) {
$term[] = $freeterm;
} else {
$searchterms[$field] = array($term, $freeterm);
}
} else {
$searchterms[$field] = $freeterm;
}
}
}
return $searchterms;
}
}
?>

@ -1,5 +1,5 @@
<?php <?php
class Product { class Product extends Entity {
private $id = 0; private $id = 0;
private $brand = ''; private $brand = '';
private $name = ''; private $name = '';
@ -43,6 +43,7 @@ class Product {
} }
public function __construct($clue, $type = 'id') { public function __construct($clue, $type = 'id') {
parent::__construct();
$search = null; $search = null;
switch($type) { switch($type) {
case 'id': case 'id':
@ -110,34 +111,36 @@ class Product {
} }
public function matches($terms) { public function matches($terms) {
$terms = $this->specify_search($terms, array('brand',
'name',
'serial',
'invoice',
'status',
'tag'));
$matches = array();
foreach($terms as $field => $values) { foreach($terms as $field => $values) {
$matchvalues = array();
if(property_exists($this, $field)) { if(property_exists($this, $field)) {
$matchvalues[] = $this->$field; if(match($values, $this->$field)) {
$matches[$field] = $this->$field;
}
} else if(array_key_exists($field, $this->get_info())) { } else if(array_key_exists($field, $this->get_info())) {
$matchvalues[] = $this->get_info()[$field]; if(match($values, $this->get_info()[$field])) {
} else { $matches[$field] = $this->get_info()[$field];
switch($field) { }
case 'tag': } else if($field == 'tag') {
$matchvalues = $this->get_tags(); foreach($this->get_tags() as $tag) {
case 'status': if(match($values, $tag)) {
$matchvalues[] = $this->get_status(); $matches['tags'] = $this->get_tags();
case 'fritext': break;
$matchvalues[] = $this->brand; }
$matchvalues[] = $this->name; }
$matchvalues[] = $this->serial; } else if($field == 'status') {
$matchvalues[] = $this->invoice; if(match($values, $this->get_status())) {
$matchvalues = array_merge($matchvalues, $matches['status'] = $this->get_status();
$this->get_tags(),
array_values(
$this->get_info()));
} }
} }
if(!match($values, $matchvalues)) {
return false;
}
} }
return true; return $matches;
} }
public function get_id() { public function get_id() {

@ -132,8 +132,9 @@ class SearchPage extends Page {
$items = get_items($type); $items = get_items($type);
$out = array(); $out = array();
foreach($items as $item) { foreach($items as $item) {
if($item->matches($terms)) { $result = $item->matches($terms);
$out[] = $item; if($result) {
$out[] = array($item, $result);
} }
} }
return $out; return $out;
@ -158,11 +159,17 @@ class SearchPage extends Page {
} }
$products = 'Inga artiklar hittade.'; $products = 'Inga artiklar hittade.';
if($this->product_hits) { if($this->product_hits) {
$products = $this->build_product_table($this->product_hits); $products = '';
foreach($this->product_hits as $hit) {
$products .= $this->render_product($hit[0], $hit[1]);
}
} }
$users = 'Inga användare hittade.'; $users = 'Inga användare hittade.';
if($this->user_hits) { if($this->user_hits) {
$users = $this->build_user_table($this->user_hits); $users = '';
foreach($this->user_hits as $hit) {
$users .= $this->render_user($hit[0], $hit[1]);
}
} }
print(replace(array('terms' => $terms, print(replace(array('terms' => $terms,
'hidden' => $hidden, 'hidden' => $hidden,
@ -170,5 +177,29 @@ class SearchPage extends Page {
'user_results' => $users), 'user_results' => $users),
$this->fragments['search_form'])); $this->fragments['search_form']));
} }
private function render_product($product, $matches) {
$link = replace(array('id' => $product->get_id(),
'name' => $product->get_name(),
'page' => 'products'),
$this->fragments['item_link']);
$data = print_r($matches, true);
return $link . '<br/>'
. $data . '<br/>';
}
private function render_user($user, $matches) {
$link = replace(array('id' => $user->get_id(),
'name' => $user->get_name(),
'page' => 'users'),
$this->fragments['item_link']);
$data = print_r($matches, true);
return $link . '<br/>'
. $data . '<br/>';
}
} }
?> ?>

@ -1,5 +1,5 @@
<?php <?php
class User { class User extends Entity {
private $id = 0; private $id = 0;
private $name = ''; private $name = '';
private $notes = ''; private $notes = '';
@ -13,6 +13,7 @@ class User {
} }
public function __construct($clue, $type = 'id') { public function __construct($clue, $type = 'id') {
parent::__construct();
$find = null; $find = null;
switch($type) { switch($type) {
case 'id': case 'id':
@ -47,25 +48,34 @@ class User {
} }
public function matches($terms) { public function matches($terms) {
$terms = $this->specify_search($terms, array('name',
'email',
'notes'));
$matches = array();
foreach($terms as $field => $values) { foreach($terms as $field => $values) {
$matchvalues = array(); switch($field) {
if($field == 'name') { case 'name':
$matchvalues[] = $this->name; if(match($values, $this->name)) {
$matchvalues[] = $this->get_displayname(); $matches['name'] = $this->name;
} else if(property_exists($this, $field)) { }
$matchvalues[] = $this->$field; if(match($values, $this->get_displayname())) {
} else if($field == 'fritext') { $matches['displayname'] = $this->get_displayname();
$matchvalues[] = $this->name; }
$matchvalues[] = $this->get_displayname(); break;
$matchvalues[] = $this->notes; case 'email':
} else { if($this->get_email(false) && match($values,
return false; $this->get_email())) {
} $matches['email'] = $this->get_email();
if(!match($values, $matchvalues)) { }
return false; break;
case 'notes':
if(match($values, $this->notes)) {
$matches['notes'] = $this->notes;
}
break;
} }
} }
return true; return $matches;
} }
public function get_displayname() { public function get_displayname() {
@ -76,11 +86,14 @@ class User {
} }
} }
public function get_email() { public function get_email($format = true) {
try { try {
return $this->ldap->get_user_email($this->name); return $this->ldap->get_user_email($this->name);
} catch(Exception $e) { } catch(Exception $e) {
return 'Mailadress saknas'; if($format) {
return 'Mailadress saknas';
}
return false;
} }
} }

@ -253,19 +253,13 @@ function suggest_content($fieldname) {
return $out; return $out;
} }
function match($testvalues, $matchvalues) { function match($searchterms, $subject) {
# match only presence of field (if no value given) if(!is_array($searchterms)) {
if(!$testvalues && $matchvalues) { $searchterms = array($searchterms);
return true;
} }
if(!is_array($testvalues)) { foreach($searchterms as $term) {
$testvalues = array($testvalues); if(fnmatch('*'.$term.'*', $subject, FNM_CASEFOLD)) {
} return true;
foreach($testvalues as $value) {
foreach($matchvalues as $candidate) {
if(fnmatch('*'.$value.'*', $candidate, FNM_CASEFOLD)) {
return true;
}
} }
} }
return false; return false;