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
| function searchFathers($childId, $childName='', $childBirthdate=null, $fatherMinAge=null, $fatherMaxAge=null) {
global $db;
$childBirthdate = convertDateFromEurToSQL($childBirthdate, '');
$where = [];
$where[] = "id_abo=:id_abo AND id!=:id AND gender='M'";
// Si un père est décédé plus de 11 mois avant la naissance de l'enfant, il ne peut pas être son père
$where[] = "((death_date > :childBirthdate - INTERVAL 11 MONTH) OR death_date IS NULL)";
// Un père ne peut pas avoir plus de 90 ans à la naissance de son enfant
$where[] = "birth_date > (:childBirthdate - INTERVAL 90 YEAR)";
if (isset($fatherMinAge, $fatherMaxAge))
$where[] = "birth_date BETWEEN (:childBirthdate - INTERVAL :fatherMaxAge YEAR) AND (:childBirthdate - INTERVAL :fatherMinAge YEAR)";
// Si l'enfant porte le nom de son père (cas général)
if ($childName)
$where[] = "last_name=:last_name";
$strWhere = implode(" AND ", $where);
$strWhere = "WHERE $strWhere";
$query = "
SELECT id, last_name, first_name, middle_name, birth_date, death_date
FROM dat_persons
$strWhere
ORDER BY last_name, birth_date, first_name
;";
$result = $db->prepare($query);
$result->bindParam('id_abo', $_SESSION['user']['id_abo']);
$result->bindParam('id', $childId);
$result->bindParam('childBirthdate', $childBirthdate);
if ($childName)
$result->bindParam('last_name', $childName);
if (isset($fatherMinAge, $fatherMaxAge)) {
$result->bindParam('fatherMinAge', $fatherMinAge);
$result->bindParam('fatherMaxAge', $fatherMaxAge);
}
$result->execute();
return $result->fetchAll(PDO::FETCH_ASSOC);
} |
Partager