Completed and refactored search. Also some minor graphical tweaks
This commit is contained in:
parent
15e5a35a6c
commit
a81f445bf9
212
include/db.php
212
include/db.php
@ -103,112 +103,6 @@ function get_fields() {
|
||||
return $out;
|
||||
}
|
||||
|
||||
function search_products($term) {
|
||||
$out = array();
|
||||
$matches = array();
|
||||
$terms = array();
|
||||
if(preg_match_all('/[^[:space:]]+:([^[:space:]]+)?/', $term, $matches)) {
|
||||
$fields = get_fields();
|
||||
foreach($matches[0] as $match) {
|
||||
$pair = explode(':', $match);
|
||||
$key = $pair[0];
|
||||
$value = $pair[1];
|
||||
switch($key) {
|
||||
case 'namn':
|
||||
case 'name':
|
||||
$key = 'name';
|
||||
break;
|
||||
case 'fakturanummer':
|
||||
case 'invoice':
|
||||
$key = 'invoice';
|
||||
break;
|
||||
case 'serienummer':
|
||||
case 'serial':
|
||||
$key = 'serial';
|
||||
break;
|
||||
case 'id':
|
||||
break;
|
||||
default:
|
||||
if(!in_array($key, $fields) && $key != 'tag') {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
$terms[$key] = $value;
|
||||
}
|
||||
}
|
||||
$term = trim(preg_replace('/[^[:space:]]+:([^[:space:]]+)?/',
|
||||
'',
|
||||
$term));
|
||||
if($term && !isset($terms['name'])) {
|
||||
$terms['name'] = $term;
|
||||
}
|
||||
foreach(get_items('product') as $product) {
|
||||
if($product->matches($terms)) {
|
||||
$out[] = $product;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
function search_users($term) {
|
||||
$resultlist = array();
|
||||
$matches = array();
|
||||
$terms = array();
|
||||
if(preg_match_all('/[^[:space:]]+:([^[:space:]]+)?/', $term, $matches)) {
|
||||
foreach($matches[0] as $match) {
|
||||
$pair = explode(':', $match);
|
||||
$key = $pair[0];
|
||||
$value = $pair[1];
|
||||
switch($key) {
|
||||
case 'anteckningar':
|
||||
case 'notes':
|
||||
$key = 'notes';
|
||||
break;
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
$terms[$key] = $value;
|
||||
}
|
||||
}
|
||||
$term = trim(preg_replace('/[^[:space:]]+:([^[:space:]]+)?/', '', $term));
|
||||
if($term) {
|
||||
$terms['displayname'] = $term;
|
||||
$terms['name'] = $term;
|
||||
}
|
||||
foreach(get_items('user') as $user) {
|
||||
if($user->matches($terms)) {
|
||||
$resultlist[] = $user;
|
||||
}
|
||||
}
|
||||
return $resultlist;
|
||||
}
|
||||
|
||||
function search_loans($products) {
|
||||
$search = 'select * from `loan` where ';
|
||||
$iter = 0;
|
||||
$terms = array();
|
||||
$tc = '';
|
||||
foreach($products as $product) {
|
||||
if($iter != 0) {
|
||||
$search .= 'or ';
|
||||
}
|
||||
$search .= '`product` = ?';
|
||||
$terms[] = $product->get_id();
|
||||
$tc .= 'i';
|
||||
$iter++;
|
||||
}
|
||||
$out = array();
|
||||
if($tc) {
|
||||
$search = prepare($search);
|
||||
bind($search, $tc, ...$terms);
|
||||
execute($search);
|
||||
foreach(result_list($search) as $loan) {
|
||||
$out[] = new Loan($loan['id']);
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
class Product {
|
||||
private $id = 0;
|
||||
private $name = '';
|
||||
@ -313,26 +207,61 @@ class Product {
|
||||
}
|
||||
|
||||
public function matches($terms) {
|
||||
foreach($terms as $key => $value) {
|
||||
if($key == 'tag') {
|
||||
return in_array($value, $this->tags);
|
||||
foreach($terms as $fieldtype => $values) {
|
||||
$testfield = null;
|
||||
switch($fieldtype) {
|
||||
case 'tag':
|
||||
foreach($values as $value) {
|
||||
if(!in_array($value, $this->tags)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'status':
|
||||
foreach($values as $value) {
|
||||
$loan = $this->get_active_loan();
|
||||
switch($value) {
|
||||
case 'on_loan':
|
||||
if(!$loan) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'no_loan':
|
||||
if($loan) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'overdue':
|
||||
if(!$loan->is_overdue()) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
case 'words':
|
||||
$testfield = $this->name;
|
||||
break;
|
||||
default:
|
||||
if(property_exists($this, $fieldtype)) {
|
||||
$testfield = $this->$fieldtype;
|
||||
} elseif(array_key_exists($fieldtype, $this->info)) {
|
||||
$tesfield = $this->info[$fieldtype];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
$testfield = '';
|
||||
if(array_key_exists($key, $this->info)) {
|
||||
if(!$value) {
|
||||
return true;
|
||||
if($testfield !== null) {
|
||||
foreach($values as $value) {
|
||||
if($testfield != $value) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$testfield = $this->info[$key];
|
||||
} elseif(property_exists($this, $key)) {
|
||||
$testfield = $this->$key;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
if($value && strpos(strtolower($testfield), $value) !== false) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function get_id() {
|
||||
@ -529,22 +458,35 @@ class User {
|
||||
}
|
||||
|
||||
public function matches($terms) {
|
||||
foreach($terms as $key => $value) {
|
||||
$testfield = '';
|
||||
if($key == 'displayname') {
|
||||
$testfield = $this->get_displayname();
|
||||
} elseif(property_exists($this, $key)) {
|
||||
$testfield = $this->$key;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
if(strpos(strtolower($testfield), $value) !== false) {
|
||||
return true;
|
||||
foreach($terms as $fieldtype => $values) {
|
||||
switch($fieldtype) {
|
||||
case 'words':
|
||||
foreach($values as $value) {
|
||||
if(strpos($this->name, $value) !== false) {
|
||||
continue;
|
||||
}
|
||||
$name = $this->get_displayname();
|
||||
if(strpos($name, $value) !== false) {
|
||||
continue;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if(!property_exists($this, $fieldtype)) {
|
||||
return false;
|
||||
}
|
||||
foreach($values as $value) {
|
||||
if($this->$fieldtype != $value) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public function get_displayname() {
|
||||
global $ldap;
|
||||
try {
|
||||
|
@ -334,26 +334,97 @@ abstract class Page extends Responder {
|
||||
}
|
||||
|
||||
class SearchPage extends Page {
|
||||
private $query = '';
|
||||
private $querystr = '';
|
||||
private $terms = array();
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
$this->subtitle = 'Sökresultat för ';
|
||||
if(isset($_GET['q'])) {
|
||||
$query = $_GET['q'];
|
||||
$this->query = strtolower($query);
|
||||
$this->subtitle .= "'$query'";
|
||||
$this->querystr = $_GET['q'];
|
||||
$this->subtitle .= "'$this->querystr'";
|
||||
$orterms = preg_split('/[[:space:]]+or[[:space:]]+/',
|
||||
strtolower($this->querystr),
|
||||
-1,
|
||||
PREG_SPLIT_NO_EMPTY);
|
||||
foreach($orterms as $orterm) {
|
||||
$searchpart = array();
|
||||
$terms = preg_split('/[[:space:]]+/',
|
||||
$orterm,
|
||||
-1,
|
||||
PREG_SPLIT_NO_EMPTY);
|
||||
foreach($terms as $term) {
|
||||
$key = '';
|
||||
$value = '';
|
||||
if(strpos($term, ':') !== false) {
|
||||
$pair = explode(':', $term);
|
||||
$key = $pair[0];
|
||||
switch($key) {
|
||||
case 'namn':
|
||||
$key = 'name';
|
||||
break;
|
||||
case 'fakturanummer':
|
||||
$key = 'invoice';
|
||||
break;
|
||||
case 'serienummer':
|
||||
$key = 'serial';
|
||||
break;
|
||||
case 'anteckningar':
|
||||
$key = 'notes';
|
||||
break;
|
||||
}
|
||||
$value = $pair[1];
|
||||
if($key == 'status') {
|
||||
switch($value) {
|
||||
case 'inne':
|
||||
$value = 'no_loan';
|
||||
break;
|
||||
case 'ute':
|
||||
case 'utlånad':
|
||||
$value = 'on_loan';
|
||||
break;
|
||||
case 'sen':
|
||||
case 'försenad':
|
||||
case 'försenat':
|
||||
$value = 'overdue';
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$key = 'words';
|
||||
$value = $term;
|
||||
}
|
||||
if(!isset($searchpart[$key])) {
|
||||
$searchpart[$key] = array();
|
||||
}
|
||||
$searchpart[$key][] = $value;
|
||||
}
|
||||
$this->terms[] = $searchpart;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function do_search() {
|
||||
$out = array('users' => array(),
|
||||
'products' => array());
|
||||
if(!$this->query) {
|
||||
if(!$this->querystr) {
|
||||
return $out;
|
||||
}
|
||||
$out['users'] = search_users($this->query);
|
||||
$out['products'] = search_products($this->query);
|
||||
$out['users'] = $this->search('user');
|
||||
$out['products'] = $this->search('product');
|
||||
return $out;
|
||||
}
|
||||
|
||||
private function search($type) {
|
||||
$items = get_items($type);
|
||||
$out = array();
|
||||
foreach($items as $item) {
|
||||
foreach($this->terms as $term) {
|
||||
if($item->matches($term)) {
|
||||
$out[] = $item;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
@ -623,8 +694,12 @@ class HistoryPage extends Page {
|
||||
print($this->build_inventory_table());
|
||||
print(replace(array('title' => 'Skrotade artiklar'),
|
||||
$this->fragments['title']));
|
||||
print($this->build_product_table(
|
||||
get_items('product_discarded')));
|
||||
$discards = get_items('product_discarded');
|
||||
if($discards) {
|
||||
print($this->build_product_table($discards));
|
||||
} else {
|
||||
print('Inga artiklar skrotade.');
|
||||
}
|
||||
break;
|
||||
case 'show':
|
||||
if($this->inventory &&
|
||||
@ -887,7 +962,7 @@ class Ajax extends Responder {
|
||||
$product->set_name($name);
|
||||
}
|
||||
if(!$serial) {
|
||||
return new Failure('Produkten måste ha ett serienummer.');
|
||||
return new Failure('Artikeln måste ha ett serienummer.');
|
||||
}
|
||||
if($serial != $product->get_serial()) {
|
||||
try {
|
||||
@ -897,7 +972,7 @@ class Ajax extends Responder {
|
||||
}
|
||||
}
|
||||
if(!$invoice) {
|
||||
return new Failure('Produkten måste ha ett fakturanummer.');
|
||||
return new Failure('Artikeln måste ha ett fakturanummer.');
|
||||
}
|
||||
if($invoice != $product->get_invoice()) {
|
||||
$product->set_invoice($invoice);
|
||||
|
Loading…
x
Reference in New Issue
Block a user