boka3/include/SearchPage.php

326 lines
11 KiB
PHP

<?php
class SearchPage extends Page {
private $terms = array();
private $product_hits = array();
private $user_hits = array();
public function __construct() {
parent::__construct();
unset($_GET['page']);
if(isset($_GET['q']) && !$_GET['q']) {
unset($_GET['q']);
}
$this->terms = $this->translate_terms($_GET);
if($this->terms) {
$this->subtitle = 'Sökresultat';
$hits = $this->do_search();
if(isset($hits['product'])) {
$this->product_hits = $hits['product'];
}
if(isset($hits['user'])) {
$this->user_hits = $hits['user'];
}
}
}
private function do_search() {
$out = array();
if(!$this->terms) {
return $out;
}
print('<br>');
foreach(array('user', 'product') as $type) {
// print('=== DEBUG $type: ');
// print_r($type);
// print(' ===<br><br>');
$result = $this->search($type, $this->terms);
if($result) {
$out[$type] = $result;
}
}
return $out;
}
private function search($type, $terms) {
/*
==================================================
ORIGINAL CODE || BACKUP || FOR REFERENCE
$items = get_items($type);
$out = array();
foreach($items as $item) {
$result = $item->matches($terms);
if($result) {
$out[] = array($item, $result);
}
}
return $out;
==================================================
*/
$mustMatchArray = array();
$cannotMatchArray = array();
$mayMatchArray = array();
foreach($terms as $key => $term) {
foreach($term as $value => $parsedTerm) {
switch ($parsedTerm[0]) {
case "+":
if (!array_key_exists($key, $mustMatchArray)) {
$mustMatchArray[$key] = array();
}
$mustMatchArray[$key][] = substr($parsedTerm, 1);
break;
case "!":
case "-":
if (!array_key_exists($key, $cannotMatchArray)) {
$cannotMatchArray[$key] = array();
}
$cannotMatchArray[] = array($key => substr($parsedTerm, 1));
break;
case "~":
if (!array_key_exists($key, $mayMatchArray)) {
$mayMatchArray[$key] = array();
}
$mayMatchArray[] = array($key => substr($parsedTerm, 1));
break;
default:
if (!array_key_exists($key, $mayMatchArray)) {
$mayMatchArray[$key] = array();
}
$mayMatchArray[] = array($key => $parsedTerm);
break;
}
}
}
$items = get_items($type);
$sanitizedItems = array();
foreach($items as $item) {
$result = $item->matches($mustMatchArray, True);
if($result) {
// $sanitizedItems[] = array($item, $result);
$out[] = array($item, $result);
}
// $mustMatchCheck = array();
// foreach($mustMatchArray as $mustMatchTerm) {
// $matchResult = $item->matches($mustMatchTerm);
// if($matchResult) {
// $mustMatchCheck[] = True;
// } else {
// $mustMatchCheck[] = False;
// }
// }
// if(in_array(False, $mustMatchCheck, True) === False) {
// $sanitizedItems[] = array($item, $matchResult);
// }
// $mustExcludeCheck = array();
// foreach($mustExcludeArray as $mustExcludeTerm) {
// if($item->matches($mustExcludeTerm)) {
// $mustExcludeCheck[] = False;
// } else {
// $mustExcludeCheck[] = True;
// }
// }
// if (in_array(False, $mustIncludeCheck, True) === False) {
// if(in_array(False, $mustExcludeCheck, True) === True) {
// // === IF TRUE DO NOTHING ===
// } else {
// foreach ($canIncludeArray as $canIncludeTerm) {
// $result = $item->matches($canIncludeTerm);
// if($result) {
// $out[] = array($item, $result);
// }
// }
// }
// }
}
// foreach($sanitizedItems as $sanitizedItem) {
// print('DEBUG $sanitizedItem: ');
// var_dump($sanitizedItem);
// print('<br><br>');
// if($sanitizedItem->matches($cannotMatchArray)) {
// // === IF TRUE DO NOTHING ===
// }
// else {
// $result = $sanitizedItem->matches($mayMatchArray);
// if($result) {
// $out[] = array($sanitizedItem, $result);
// }
// }
// }
// return array();
return $out;
}
private function translate_terms($terms) {
$matches = array();
if(isset($terms['q']) && preg_match('/([^:]+):(.*)/',
$terms['q'],
$matches)) {
unset($terms['q']);
$terms[$matches[1]] = $matches[2];
}
$translated = array();
foreach($terms as $key => $value) {
$newkey = $key;
switch($key) {
case 'q':
$newkey = 'fritext';
break;
case 'tillverkare':
case 'märke':
$newkey = 'brand';
break;
case 'namn':
$newkey = 'name';
break;
case 'faktura':
case 'fakturanummer':
$newkey = 'invoice';
break;
case 'serienummer':
$newkey = 'serial';
break;
case 'tagg':
$newkey = 'tag';
break;
case 'status':
$value = $this->translate_values($value);
break;
}
if(!array_key_exists($newkey, $translated)) {
$translated[$newkey] = $value;
} else {
$temp = $translated[$newkey];
$translated[$newkey] = array_merge((array)$temp, (array)$value);
}
}
return $translated;
}
private function translate_values($value) {
if(!is_array($value)) {
$value = array($value);
}
$translated = array();
foreach($value as $item) {
$newitem = $item;
switch($item) {
case 'ute':
case 'utlånad':
case 'utlånat':
case 'lånad':
case 'lånat':
$newitem = 'on_loan';
break;
case 'inne':
case 'ledig':
case 'ledigt':
case 'tillgänglig':
case 'tillgängligt':
$newitem = 'available';
break;
case 'sen':
case 'sent':
case 'försenad':
case 'försenat':
case 'överdraget':
$newitem = 'overdue';
break;
case 'skrotad':
case 'skrotat':
case 'slängd':
case 'slängt':
$newitem = 'discarded';
break;
case 'lagning':
case 'reparation':
$newitem = 'service';
break;
}
$translated[] = $newitem;
}
return $translated;
}
protected function render_body() {
$hidden = 'hidden';
$terms = '';
if($this->terms) {
$hidden = '';
foreach($this->terms as $key => $value) {
if(!is_array($value)) {
$value = array($value);
}
foreach($value as $item) {
$terms .= replace(array('term' => ucfirst($key).": $item",
'key' => $key,
'value' => $item),
$this->fragments['search_term']);
}
}
}
$products = 'Inga artiklar hittade.';
if($this->product_hits) {
$products = '';
foreach($this->product_hits as $hit) {
$products .= $this->render_product($hit[0], $hit[1]);
}
}
$users = 'Inga användare hittade.';
if($this->user_hits) {
$users = '';
foreach($this->user_hits as $hit) {
$users .= $this->render_user($hit[0], $hit[1]);
}
}
print(replace(array('terms' => $terms,
'hidden' => $hidden,
'product_results' => $products,
'user_results' => $users),
$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/>';
}
}
?>