Functions.php 3.95 KB
Newer Older
ALGLAVE Ivan's avatar
ALGLAVE Ivan committed
1
2
3
4
5
6
7
8
<?php

function recipeContains($recipe, $ingredient)
{
    foreach($recipe['index'] as $ing) if($ing === $ingredient) return true;
    return false;
}

ALGLAVE Ivan's avatar
ALGLAVE Ivan committed
9
10
11
function getRecipesForJS($wanted_ingredients, $unwanted_ingredients, $max_unsatisfied, $allow_missing, $allow_unwanted)
{
    $validRecipes = getRecipes($wanted_ingredients, $unwanted_ingredients, $max_unsatisfied, $allow_missing, $allow_unwanted);
12
    $vrArray = [];
ALGLAVE Ivan's avatar
ALGLAVE Ivan committed
13
14
    foreach($validRecipes as $recipe) 
    {
15
        $vrArray[] = $recipe['id'] . ':' . $recipe['score'];
ALGLAVE Ivan's avatar
ALGLAVE Ivan committed
16
    }
17
    return implode('|', $vrArray);
ALGLAVE Ivan's avatar
ALGLAVE Ivan committed
18
19
}

20
function getRecipes($wanted_ing, $unwanted_ing, $max_unsatisfied, $allow_missing, $allow_unwanted)
ALGLAVE Ivan's avatar
ALGLAVE Ivan committed
21
{
22
    include "Donnees.inc.php";
ALGLAVE Ivan's avatar
ALGLAVE Ivan committed
23

24
25
26
    $wanted_ingredients = array_filter(explode('|', $wanted_ing));
    $unwanted_ingredients = array_filter(explode('|', $unwanted_ing));

ALGLAVE Ivan's avatar
ALGLAVE Ivan committed
27
    $Recipes = array();
ALGLAVE Ivan's avatar
ALGLAVE Ivan committed
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
    for($i = 0; $i < count($Recettes); $i++)
    {
        $recipe = $Recettes[$i];

        $score = 0;
        $valid = true;

        foreach($wanted_ingredients as $w_ing)
        {
            if(!recipeContains($recipe, $w_ing)) 
            {
                if(!$allow_missing) 
                {
                    $valid = false;
                }
                $score++;
            }
        }
        if($valid) 
        {
            foreach($unwanted_ingredients as $uw_ing)
            {
                if(recipeContains($recipe, $uw_ing)) 
                {
                    if(!$allow_unwanted) 
                    {
                        $valid = false;
                    }
                    $score++;
                }
            }
            if($valid) 
            {
                if($score <= $max_unsatisfied)
                {
ALGLAVE Ivan's avatar
ALGLAVE Ivan committed
63
64
65
66
                    $Recipes[] = array(
                        'id'=> $i,
                        'score'=> $score
                    );
ALGLAVE Ivan's avatar
ALGLAVE Ivan committed
67
68
69
70
71
72
73
74
75
                }
            }   
        }
    }
    return $Recipes;
}

function getIngredients($parent_category)
{
76
77
    include "Donnees.inc.php";
    $res = 'f';
ALGLAVE Ivan's avatar
ALGLAVE Ivan committed
78

79
    if(isset($Hierarchie[$parent_category]))
ALGLAVE Ivan's avatar
ALGLAVE Ivan committed
80
    {
81
        $ingredients = $Hierarchie[$parent_category]['sous-categorie'];
82
83

        sort($ingredients);
84
    
85
        $res = implode('|', $ingredients);
ALGLAVE Ivan's avatar
ALGLAVE Ivan committed
86
87
88
89
90
    }

    return $res;
}

91
function getRecipeImage($name)
ALGLAVE Ivan's avatar
d    
ALGLAVE Ivan committed
92
{
93
94
95
96
97
98
99
100
    $formatted_name = explode(' ', $name);
    $formatted_name = implode('_', $formatted_name) . '.jpg';
    return $formatted_name;
}

function getSubIngredients($cat)
{
    include "Donnees.inc.php";
ALGLAVE Ivan's avatar
d    
ALGLAVE Ivan committed
101

102
    $sub_list[] = $cat;
103
104

    if(!isset($Hierarchie[$cat]['sous-categorie'])) $sub_list[] = $cat;
105
106
    else foreach($Hierarchie[$cat]['sous-categorie'] as $ing){
        foreach(getSubIngredients($ing) as $sub){
107
108
109
110
111
            if(!in_array($sub, $sub_list)) $sub_list[] = $sub;
        }
    }
    return $sub_list;
}
ALGLAVE Ivan's avatar
d    
ALGLAVE Ivan committed
112

113
114
function getAllIngredients()
{
115
116
117
    $res = getSubIngredients('Aliment');
    sort($res);
    return $res;
ALGLAVE Ivan's avatar
d    
ALGLAVE Ivan committed
118
119
}

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Renvoie un tableau de sous-categories de l'aliment en cours, null si l'aliment n'en a pas
function get_sous_categories($aliment) {
    include "Donnees.inc.php";
    $res = null;
    if(isset($Hierarchie[$aliment]['sous-categorie'])) {
         $res = $Hierarchie[$aliment]['sous-categorie'];
    }
    return $res;
}

// Renvoie un tableau de super-categories de l'aliment en cours, null si l'aliment n'en a pas
function get_super_categories($aliment) {
    include "Donnees.inc.php";
    $res = null;
    if(isset($Hierarchie[$aliment]['super-categorie'])) {
        $res = $Hierarchie[$aliment]['super-categorie'];
    }
    return $res;
}

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Renvoie un tableau de tous les aliments feuille de la catégorie en paramètre 
function get_all_aliments($categorie) {
    include "Donnees.inc.php";
    $res = [];
    if(isset($Hierarchie[$categorie]['sous-categorie'])) {
        $sous_cat = $Hierarchie[$categorie]['sous-categorie'];
        foreach($sous_cat as $sc) {
            $res = array_merge($res, get_all_aliments($sc));
        }
    } else {
        $res[] = $categorie;
    }
    return $res;
}

ALGLAVE Ivan's avatar
ALGLAVE Ivan committed
155
?>