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('
'); foreach(array('user', 'product') as $type) { // print('=== DEBUG $type: '); // print_r($type); // print(' ===

'); $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 => $value) { if(!is_array($value)) { $value = array($value); } foreach($value as $term) { switch ($term[0]) { case "+": if (!array_key_exists($key, $mustMatchArray)) { $mustMatchArray[$key] = array(); } $mustMatchArray[$key][] = substr($term, 1); break; case "!": case "-": if (!array_key_exists($key, $cannotMatchArray)) { $cannotMatchArray[$key] = array(); } $cannotMatchArray[$key][] = substr($term, 1); break; case "~": if (!array_key_exists($key, $mayMatchArray)) { $mayMatchArray[$key] = array(); } $mayMatchArray[$key][] = substr($term, 1); break; default: if (!array_key_exists($key, $mayMatchArray)) { $mayMatchArray[$key] = array(); } $mayMatchArray[$key][] = $term; break; } } } $items = get_items($type); $sanitizedItems = array(); foreach($items as $item) { $result = $item->matches($mustMatchArray, True); if($result) { $sanitizedItems[] = 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); // } // } // } // } } print('DEBUG $sanitizedItems: '); var_dump($sanitizedItems); print('

'); // $out = array(); // foreach($sanitizedItems as $sanitizedItem) { // print('DEBUG $sanitizedItem: '); // var_dump($sanitizedItem); // print('

'); // if($sanitizedItem->matches($cannotMatchArray)) { // // === IF TRUE DO NOTHING === // } // else { // $result = $sanitizedItem->matches($mayMatchArray); // if($result) { // $out[] = array($sanitizedItem, $result); // } // } // } // return array(); // return $out; return $sanitizedItems; } 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 . '
' . $data . '
'; } 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 . '
' . $data . '
'; } } ?>