Skip to content
Snippets Groups Projects
Functions.php 1.93 KiB
<?php

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

function getRecipes($wanted_ingredients, $unwanted_ingredients, $max_unsatisfied, $allow_missing, $allow_unwanted)
{
    include "../Donnees.inc.php";

    $Recipes = '';
    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;
                    break;
                }
                $score++;
            }
        }
        if($valid) 
        {
            foreach($unwanted_ingredients as $uw_ing)
            {
                if(recipeContains($recipe, $uw_ing)) 
                {
                    if(!$allow_unwanted) 
                    {
                        $valid = false;
                        break;
                    }
                    $score++;
                }
            }
            if($valid) 
            {
                if($score <= $max_unsatisfied)
                {
                    $Recipes .= $i . ':' . $score . '|';
                }
            }   
        }
    }
    return $Recipes;
}

function getIngredients($parent_category)
{
    include "../Donnees.inc.php";
    $res = '';

    if(isset($Hierarchie[$parent_category]))
    {
        $ingredients = $Hierarchie[$parent_category]['sous-categorie'];
    
        foreach($ingredients as $i)
        {
            $res .= $i . '|';
        }
    }

    return $res;
}

function getRecipeImage($id)
{
    include "../Donnees.inc.php";

    $name = $Recettes[$id];

    $formatted_name = implode('_', explode(' ', $name)) . '.jpg';

    if(file_exists('../Photos/' . $formatted_name)) return $formatted_name;
    else return '';
}

?>