Implemented advanced search and removed discarded products from the default listing, moving them into the history page

This commit is contained in:
Erik Thuning 2018-09-07 17:17:50 +02:00
parent 34ded12e0a
commit 71b853c973
2 changed files with 152 additions and 32 deletions

@ -104,23 +104,79 @@ function get_fields() {
}
function search_products($term) {
$search = prepare("select * from `product` where `name` like ?");
bind($search, 's', '%'.$term.'%');
execute($search);
$out = array();
foreach(result_list($search) as $row) {
$out[] = new Product($row['id']);
$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) {
$userlist = get_items('user');
$resultlist = array();
foreach($userlist as $user) {
$uname = $user->get_name();
$dname = strtolower($user->get_displayname());
if(strpos($uname, $term) !== false || strpos($dname, $term) !== false) {
$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;
}
}
@ -255,7 +311,30 @@ class Product {
$this->tags = $newtags;
return true;
}
public function matches($terms) {
foreach($terms as $key => $value) {
if($key == 'tag') {
return in_array($value, $this->tags);
}
$testfield = '';
if(array_key_exists($key, $this->info)) {
if(!$value) {
return true;
}
$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;
}
public function get_id() {
return $this->id;
}
@ -449,6 +528,23 @@ class User {
return true;
}
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;
}
}
return false;
}
public function get_displayname() {
global $ldap;
try {

@ -165,20 +165,26 @@ abstract class Page extends Responder {
$this->fragments['item_link']);
$available = 'Tillgänglig';
$status = 'available';
$loan = $product->get_active_loan();
if($loan) {
$user = $loan->get_user();
$userlink = replace(array('name' => $user->get_displayname(),
'id' => $user->get_id(),
'page' => 'users'),
$this->fragments['item_link']);
$available = 'Utlånad till '.$userlink;
if($loan->is_overdue()) {
$status = 'overdue';
$available .= ', försenad';
} else {
$status = 'on_loan';
$available .= ', åter '.$loan->get_duration()['end'];
$discarded = $product->get_discardtime();
if($discarded) {
$available = 'Skrotad '.$discarded;
$status = 'discarded';
} else {
$loan = $product->get_active_loan();
if($loan) {
$user = $loan->get_user();
$userlink = replace(array('name' => $user->get_displayname(),
'id' => $user->get_id(),
'page' => 'users'),
$this->fragments['item_link']);
$available = 'Utlånad till '.$userlink;
if($loan->is_overdue()) {
$status = 'overdue';
$available .= ', försenad';
} else {
$status = 'on_loan';
$available .= ', åter '.$loan->get_duration()['end'];
}
}
}
$rows .= replace(array('available' => $available,
@ -334,11 +340,12 @@ class SearchPage extends Page {
parent::__construct();
$this->subtitle = 'Sökresultat för ';
if(isset($_GET['q'])) {
$this->query = $_GET['q'];
$this->subtitle .= "'$this->query'";
$query = $_GET['q'];
$this->query = strtolower($query);
$this->subtitle .= "'$query'";
}
}
private function do_search() {
$out = array('users' => array(),
'products' => array());
@ -368,6 +375,9 @@ class SearchPage extends Page {
if($nohits) {
print('Inga träffar.');
}
print(replace(array('title' => 'Hjälp'),
$this->fragments['subtitle']));
print($this->fragments['search_help']);
}
}
@ -431,8 +441,10 @@ class ProductPage extends Page {
'tags' => $tags,
'info' => $info),
$this->fragments['product_details']);
$out .= replace(array('id' => $this->product->get_id()),
$this->fragments['discard_button']);
if(!$this->product->get_discardtime()) {
$out .= replace(array('id' => $this->product->get_id()),
$this->fragments['discard_button']);
}
$out .= replace(array('title' => 'Lånehistorik'),
$this->fragments['subtitle']);
$loan_table = 'Inga lån att visa.';
@ -609,11 +621,16 @@ class HistoryPage extends Page {
switch($this->action) {
case 'list':
print($this->build_inventory_table());
print(replace(array('title' => 'Skrotade artiklar'),
$this->fragments['title']));
print($this->build_product_table(
get_items('product_discarded')));
break;
case 'show':
if($this->inventory &&
Inventory::get_active() !== $this->inventory) {
print($this->build_inventory_details($this->inventory, false));
print($this->build_inventory_details($this->inventory,
false));
}
break;
}
@ -860,8 +877,11 @@ class Ajax extends Responder {
}
}
$product = new Product($id);
if($product->get_discardtime()) {
return new Failure('Skrotade artiklar får inte modifieras.');
}
if(!$name) {
return new Failure('Produkten måste ha ett namn.');
return new Failure('Artikeln måste ha ett namn.');
}
if($name != $product->get_name()) {
$product->set_name($name);
@ -943,6 +963,10 @@ class Ajax extends Responder {
private function discard_product() {
$product = new Product($_POST['id']);
if(!$product->get_discardtime()) {
if($product->get_active_loan()) {
return new Failure('Artikeln har ett aktivt lån.<br/>'
.'Lånet måste avslutas innan artikeln skrotas.');
}
$product->discard();
return new Success('Artikeln skrotad.');
} else {