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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
| <?php
// Affichage principal d'une personne
##################################################################### SECTION TETE : PARAMETRAGE AUTORISATION ##########################################################
if (!isset($isIncluded)) {
header('location: ' . INDEX_PAGE);
exit;
}
##################################################################### Fin SECTION TETE : PARAMETRAGE AUTORISATION ######################################################
##################################################################### SECTION MODELE ###################################################################################
connect();
function getDefaultPersonId($idAbo) {
global $db;
$query = "
SELECT id
FROM dat_persons
WHERE id_abo=:id_abo
ORDER BY id
LIMIT 1
;";
$result = $db->prepare($query);
$result->bindParam('id_abo', $_SESSION['user']['id_abo']);
$result->execute();
return $result->fetch();
}
function getPerson($id) {
global $db;
$dateFormat = LOCAL_DATE_FORMAT;
$query = "
SELECT id, gender, last_name, first_name, middle_name, DATE_FORMAT(birth_date, '$dateFormat') AS birth_date, birth_place,
DATE_FORMAT(christening_date, '$dateFormat') AS christening_date, DATE_FORMAT(death_date, '$dateFormat') AS death_date, death_place,
profession, comment, father_id, mother_id, birth_order
FROM dat_persons
WHERE id=:id
LIMIT 1
;";
$result = $db->prepare($query);
$result->bindParam('id', $id);
$result->execute();
return $result->fetch();
}
function idExists($id) {
global $db;
$query = "
SELECT count(*) AS counter
FROM dat_persons
WHERE id=:id
LIMIT 1
;";
$result = $db->prepare($query);
$result->bindParam('id', $id);
$result->execute();
return $result->fetch()->counter;
}
function getSearchData($criteria) {
global $db;
$criteriaEqual = $criteria;
$criteriaLike = "%$criteria%";
$dateFormat = LOCAL_DATE_FORMAT;
$query = "
SELECT id, first_name, last_name, DATE_FORMAT(birth_date, '$dateFormat') AS birthdate
FROM dat_persons
WHERE id_abo=:id_abo
AND (
id=:criteriaEqual
OR first_name LIKE :criteriaLike
OR last_name LIKE :criteriaLike
)
ORDER BY last_name, first_name, birth_date
;";
$result = $db->prepare($query);
$result->bindParam('id_abo', $_SESSION['user']['id_abo']);
$result->bindParam('criteriaEqual', $criteriaEqual);
$result->bindParam('criteriaLike', $criteriaLike);
$result->execute();
return $result->fetchAll();
}
function getOptionsContent($arrIds) {
global $db;
$dateFormat = LOCAL_DATE_FORMAT;
$strIds = implode(', ', $arrIds);
$query = "
SELECT id, CONCAT(COALESCE(last_name, ''), ' ', COALESCE(first_name, '')) AS name, DATE_FORMAT(birth_date, '$dateFormat') AS birthdate
FROM dat_persons
WHERE id_abo=:id_abo AND id IN($strIds)
ORDER BY FIELD(id, $strIds)
;";
$result = $db->prepare($query);
$result->bindParam('id_abo', $_SESSION['user']['id_abo']);
$result->execute();
return $result->fetchAll();
}
##################################################################### FIN section MODELE ###############################################################################
##################################################################### SECTION CONTROLE #################################################################################
// .....................................................................
// Génère les listes de cookies pour les listes d'options d'accès rapide
// (Récents et favoris)
function refreshCookies($list, $idMain, $max) {
$latest = $favourites = [];
// Lecture et décomposition du cookie
$cookieName = $_SESSION['user']['id_abo'];
if ( ! empty($_COOKIE[$cookieName])) {
$cookies = unserialize($_COOKIE[$cookieName]);
if (!empty($cookies['latest']))
$latest = $cookies['latest'];
if (!empty($cookies['favourites']))
$favourites = $cookies['favourites'];
}
// Mise à jour de la liste $list
switch ($list) {
case 'latest': $tmp = $latest; break;
case 'favourites': $tmp = $favourites; break;
}
if (empty($tmp))
$tmp[0] = $idMain;
else {
array_unshift($tmp, $idMain);
$tmp = array_unique($tmp);
foreach ($tmp as $key=>&$id) {
if (!idExists($id))
unset($tmp[$key]);
}
if (count($tmp) >$max)
array_pop($tmp);
}
$tmp = array_values($tmp); // Mise en ordre consécutifs des clefs numériques
// Enregistrement du cookie
switch ($list) {
case 'latest': $latest = $tmp; break;
case 'favourites': $favourites = $tmp; break;
}
setcookie($cookieName, serialize(['latest'=>$latest, 'favourites'=>$favourites]), COOKIE_HOLDTIME);
}
// .....................................................................
define('THIS', 'parentPage.php?child=2'); // Base de cette page
if ($_SESSION['child'] != 3)
unset($_SESSION['editPerson']);
$abo = $_SESSION['user']['id_abo'];
if (isset($_COOKIE)) {
if (isset($_COOKIE[$abo])) {
$arrCookies = unserialize($_COOKIE[$abo]);
if (isset($arrCookies['latest']))
$arrLatest = $arrCookies['latest'];
if (isset($arrCookies['favourites']))
$arrFavourites = $arrCookies['favourites'];
}
}
$spy = []; // stocke les données à afficher en var_dump pour éviter un conflit avec les redirection 'header'
// Recherche de l'id de la personne à afficher
$loop = false;
redo:
$idMain = 0;
//$_GET['idMain'] = '9000'; // Pour test uniquement
if (isset($_GET['idMain'])) {
$idMain = $_GET['idMain'];
}
elseif (isset($arrLatest)) {
foreach ($arrLatest as $key=>$id) {
if (idExists($id)) {
$idMain = $id;
break;
}
}
}
elseif ($defaultPers = getDefaultPersonId($_SESSION['user']['id_abo']))
if ($defaultPers)
$idMain = (int)$defaultPers->id;
if (! (int) $idMain) {
header('location: parentPage.php?child=0');
exit;
}
// Collecte des données de la personne à afficher
$mainPerson = getPerson($idMain);
// La personne n'existe pas (ou plus)
if (empty($mainPerson)) {
unset($_GET['idMain'], $_SESSION['idMain']);
unset($arrLatest[array_search($idMain, $arrLatest)]);
$arrLatest = array_unique($arrLatest);
$arrLatest = array_values($arrLatest);
$arrCookies = [$arrLatest, $arrFavourites];
setcookie($abo, serialize($arrCookies), COOKIE_HOLDTIME);
if (!$loop) {
$loop = true;
goto redo;
}
}
// Mise à jour des récents
refreshCookies('latest', $idMain, 15);
// Gestion des encarts
// Supprimé pour simplifier la question sur le forum
// Action du bouton edit
if (isset($_POST['edit'])) {
unset($_POST['edit']);
header("location: parentPage.php?child=3&idMain=$idMain");
exit;
}
// Action du bouton new
if (isset($_POST['new'])) {
unset($_POST['new']);
header("location: parentPage.php?child=3&idMain=0");
exit;
}
// Action du bouton del
if (isset($_POST['del'])) {
unset($_POST['del']);
$_SESSION['toDel'] = $idMain;
header("location: parentPage.php?child=4");
exit;
}
// Action du bouton q (q = appelation habituelle des boutons de recherche)
// Génère une liste de liens
if (!empty($_POST['q'])) {
$result = getSearchData($_POST['q']);
if ($result) {
$searchResult = '';
$pattern = "<a href='%s'>%s, %s, %s, %s</a><br/>";
foreach ($result as $item) {
$searchResult .= sprintf($pattern, "parentPage.php?child=2&idMain=$item->id", $item->id, $item->last_name, $item->first_name, $item->birthdate);
}
unset($item);
}
$_POST['q'] = '';
}
$chkFavourite = ((!empty($arrFavourites) and in_array($idMain, $arrFavourites))) ? ' checked': '';
// 2 LIGNES POUR TEST
//$chkFavourite = '';
//$chkFavourite = ' checked';
// Action de la liste d'options 'latest'
if (!empty($_POST['latest'])) {
header("location: parentPage.php?child=2&idMain={$_POST['latest']}");
}
// Action de la liste d'options 'favourites'
if (!empty($_POST['favourites'])) {
header("location: parentPage.php?child=2&idMain={$_POST['favourites']}");
}
// Traitement de la case à cocher 'favourite'
$spy[__line__] = $_POST;
if (isset($_POST['flag'])) {
unset($_POST['flag']);
// Ajout à la liste des favoris
if (isset($_POST['favourite'])) {
$spy[__line__] = "Ajout";
refreshCookies('favourites', $idMain, 15);
}
// Retrait de la liste des favoris
else {
$spy[__line__] = "Suppression";
if (isset($arrFavourites)) {
unset($arrFavourites[array_search($idMain, $arrFavourites)]);
$arrFavourites = array_unique($arrFavourites);
$arrFavourites = array_values($arrFavourites);
$arrCookies = ['latest'=>$arrLatest, 'favourites'=>$arrFavourites];
$spy[__line__] = $arrCookies;
$spy[__line__] = serialize($arrCookies);
//setcookie($abo, serialize($arrCookies), COOKIE_HOLDTIME);
}
}
//header("location: parentPage.php?child=2&idMain=$idMain");
}
// Génération des listes d'accès rapide pour le formulaire de classe search
// Utilise les classes optListSelect et optListOption
// Création de la liste d'options 'latest'
$lstLatest = new optListSelect('latest');
$lstLatest->addOption(new optListOption('', ''));
if (!empty($arrLatest)) {
$latest = getOptionsContent($arrLatest);
foreach ($latest as $item) {
$lstLatest->addOption(new optListOption($item->id.', '.$item->name.', '.$item->birthdate, $item->id));
}
unset($item);
}
// Création de la liste d'options 'favourites'
$lstFavourites = new optListSelect('favourites');
$lstFavourites->addOption(new optListOption('', ''));
if (!empty($arrFavourites)) {
$favourites = getOptionsContent($arrFavourites);
foreach ($favourites as $item) {
$lstFavourites->addOption(new optListOption($item->id.', '.$item->name.', '.$item->birthdate, $item->id));
}
unset($item);
}
##################################################################### FIN section CONTROLE #############################################################################
##################################################################### SECTION VUE ######################################################################################
?>
<?php var_dump($spy) ?>
<h2><?= AFF_MAIN_H2; ?></h2>
<?php
// Affichage des erreurs
// Supprimé pour simplifier la question sur le forum
?>
<form method="post" class="search">
<div><input type="search" id="q" name="q" /> <button name="qBtn"><img src="<?= DIR_IMAGES.'glass.png' ?>" /></button></div>
<div><label for="latest"><?= AFF_LBL_LATEST ?></label> <?= $lstLatest ?></div> <!-- La variable lstLatest contient la liste name='latest' complète -->
<div><label for="favourites"><?= AFF_LBL_FAVOURITE ?></label> <?= $lstFavourites ?></div> <!-- La variable lstFavourites contient la liste name='favourite' complète -->
</form>
<?php if(!empty($searchResult))
echo "<p>".$searchResult."</p>";
?>
<div id="mainDisplay">
<?php if ($mainPerson): ?>
<div id="mainPerson">
<h3><?= AFF_MAIN_H3_1; ?></h3>
<table>
<!-- Affichage des données issues de la base de données -->
<!-- Contenu supprimé pour simplifier la question sur le forum -->
</table>
<!-- Affichage du formulaire d'affectation de la personne à la liste des favoris -->
<form method="post" id="favourite" class="bottomFormMain">
<input type="hidden" name="flag" />
<p><?= insertFrSpace(AFF_LBL_CHECKBOX) ?> <input type="checkbox" name="favourite"<?= $chkFavourite ?> /></p> <!-- -->
</form>
<!-- Formulaire d'édition des personnes -->
<form method="post" class="bottomFormMain">
<p><input type="submit" name="edit" value="<?= AFF_BTN_UPDATE; ?>" /> <input type="submit" name="del" value ="<?= AFF_BTN_ERASE; ?>" />
<?= AFF_BTN_TXT; ?></p>
</form>
</div>
<div id="insert">
<!-- Affichage de l'insert avec ses données -->
<!-- Contenu supprimé pour simplifier la question sur le forum -->
</div>
</div>
<form method="post" style="margin-left:25px" class="bottomFormMain">
<input type="submit" name="new" value="<?= AFF_BTN_CREATE; ?>" /> <?= AFF_BTN_TXT2; ?>
</form>
<script src="displMain.js"></script> |
Partager