/
findAll.php
92 lines (71 loc) · 2.73 KB
/
findAll.php
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
<?php
require_once '../../bootstrap.php';
if(array_key_exists("offset", $_REQUEST)){
$offset = ((int) $_REQUEST["offset"]);
}
else {
$offset = 0;
}
if(array_key_exists("limit", $_REQUEST)){
$limit = (int) $_REQUEST["limit"];
}
else{
$limit = 30;
}
$query = RecipeQuery::create();
if(array_key_exists("filter", $_REQUEST)){
$filter = $_REQUEST["filter"];
if(array_key_exists("recipeTypes", $filter)){
foreach($filter["recipeTypes"] as $categoryFilter){
$query = $query->filterBycategory($categoryFilter);
}
}
if(array_key_exists("countries", $filter)){
foreach($filter["countries"] as $countriesFilter){
$query = $query->filterByorigin($countriesFilter);
}
}
if(array_key_exists("maxCost", $filter)){
$query = $query->filterBycost(array('max' => $filter["maxCost"]));
}
if(array_key_exists("maxDifficulty", $filter)){
$query = $query->filterBydifficulty(array('max' => $filter["maxDifficulty"]));
}
if(array_key_exists("maxTime", $filter)){
$query = $query->filterBytime(array('max' => $filter["maxTime"]));
}
if(array_key_exists("maxCalories", $filter)){
$query = $query->filterBycalories(array('max' => $filter["maxCalories"]));
}
if(array_key_exists("ingredients", $filter)){
foreach($filter["ingredients"] as $ingFilter){
$ids = ingredientQuery::create()
->filterByingredientId($ingFilter)
->select(array("recipeId"))
->find();
$query = $query->filterByPrimaryKeys($ids);
}
}
if(array_key_exists("tags", $filter)){
foreach($filter["tags"] as $tagFilter){
$ids = tagsQuery::create()
->filterBytagId($tagFilter)
->select(array("recipeId"))
->find();
$query = $query->filterByPrimaryKeys($ids);
}
}
}
$query = $query->select(array('id', 'name'));
$count = $query->count();
$recipes = $query->offset($offset)
->limit($limit)
->orderByName()
->find();
$data = $recipes->getData();
$result = array("count" => $count,
"offset" => $offset,
"limit" => $limit,
"data" => $data);
echo json_encode($result);
?>