1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use App\Cocktail;
use App\Ingredient;
use App\Libraries\Helper;
class CocktailController extends Controller
{
public function index(Request $request)
{
/**
* shorthand
*/
$input = $request->input("q");
if ( isset($input) && "" != $input ){
return redirect()->route('cocktail_search', ['i[]' => $input ]);
}
if ( Cache::has('cocktail_count') ) {
$cocktail_count= Cache::get('cocktail_count');
} else {
$cocktails = new Cocktail;
$cocktail_count = $cocktails->count();
Cache::put('cocktail_count', $cocktail_count, 6000); // 100 minutes
}
return view('cocktail_index', [ "cocktail_count" => $cocktail_count ]);
}
public function search(Request $request) {
$ingredients = $request->input("i");
if ( ! isset($ingredients) || empty($ingredients) ) {
$ingredients = array( $request->input("q") );
}
$excludes = $request->input("i_ex");
if ( ! isset($excludes) || empty($excludes) ) {
$excludes = array("");
}
$cocktails = new Cocktail;
foreach($ingredients as $q) {
foreach($excludes as $ex) {
$ex = Helper::escapeLike($ex);
$ex = "%".$ex."%";
$cocktails = $cocktails->where(function($query) use ($q, $ex){
$query->whereHas("getIngredients", function ($query) use ($q){
if ( Str::contains($q, ' oder ') ) {
$exploded = explode(' oder ', $q);
$query->where( function ($query) use ($exploded){
foreach($exploded as $e) {
$e = Helper::escapeLike($e);
$e = "%".$e."%";
$query->orWhere('name', 'like', $e);
}
});
} else {
$q = Helper::escapeLike($q);
$q = "%".$q."%";
$query->where('name', 'like', $q);
}
});
if ( "%%" != $ex ) {
$query->whereDoesntHave("getIngredients", function ($query) use ($ex){
$query->where('name', 'like', $ex);
});
}
});
}
}
foreach($ingredients as $key => $q) {
if ( Str::contains($q, ' oder ') ) {
unset($ingredients[$key]);
$exploded = explode(' oder ', $q);
foreach($exploded as $key => $val )
$exploded[$key] = trim($exploded[$key] );
$ingredients = array_merge($ingredients, $exploded);
}
}
$count = $cocktails->count();
$search_phrase = "";
foreach(array_filter($ingredients) as $term) {
if ( $search_phrase != "" )
$search_phrase = $search_phrase . " und ";
$search_phrase = $search_phrase . ucwords($term);
}
$search_phrase = rtrim($search_phrase, " und ");
$exclude_phrase = "";
if ( isset($excludes) ) {
foreach(array_filter($excludes) as $term) {
if ( $exclude_phrase != "" )
$exclude_phrase = $exclude_phrase . " und ";
$exclude_phrase = $exclude_phrase . ucwords($term);
}
$exclude_phrase = rtrim($exclude_phrase, " und ");
}
if ( "" != $exclude_phrase)
$search_phrase .= " und nicht nach " . $exclude_phrase;
return view('cocktail_list', [ "cocktails" => $cocktails, "count" => $count, "search_terms" => array_filter($ingredients), "search_phrase" => $search_phrase, "exclude_terms" => array_filter($excludes) ]);
}
}
|