Plateforme web de commande de panier bio
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

602 lines
24 KiB

2 years ago
2 years ago
2 years ago
  1. <?php
  2. define('DEFAULT_TITLE', 'Mon panier bio');
  3. define('REQUEST_REGEX', '/^\/(?<supplier>[^\/]+)\/?(?<event>[^\/]+)?\/?$/');
  4. define('SUPPLIER_REGEX', '/^[A-Za-z]\w{0,31}$/');
  5. define('EVENT_REGEX', '/^\d{4}\-[01]\d\-[0123]\d$/');
  6. define('ACTION_REGEX', '/^[a-z]{1,16}$/i');
  7. $baseUrl = trim((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], '/');
  8. $requestUrl = trim(str_replace($_SERVER['QUERY_STRING'], '', $baseUrl), '?');
  9. if (preg_match(REQUEST_REGEX, $requestUrl, $match)) {
  10. $requestSupplier = array_key_exists('supplier', $match) ? $match['supplier'] : null;
  11. $requestEvent = array_key_exists('event', $match) ? $match['event'] : null;
  12. if (!is_null($requestEvent))
  13. $requestUrl = rtrim(str_replace($requestEvent, '', $requestUrl), '/');
  14. if (!is_null($requestSupplier))
  15. $requestUrl = rtrim(str_replace($requestSupplier, '', $requestUrl), '/');
  16. }
  17. function generateUrl($supplier = null, $event = null) {
  18. global $requestUrl;
  19. if (is_null($supplier))
  20. return $requestUrl;
  21. if (is_null($event))
  22. return sprintf('%s/%s', $requestUrl, $supplier);
  23. return sprintf('%s/%s/%s', $requestUrl, $supplier, $event);
  24. }
  25. function findNext($start, $frequency, $excludes = [], $vsNow = true, $maxIterations = 1000, $direction = +1) {
  26. $now = new \DateTime('now');
  27. $current = clone $start;
  28. $frequency = \DateInterval::createFromDateString($frequency);
  29. do {
  30. if ($direction === abs($direction)) {
  31. if (!$vsNow and ($maxIterations-- > 0)) {
  32. $current->add($frequency);
  33. } else {
  34. while (
  35. ($current->getTimestamp() < $now->getTimestamp())
  36. and ($maxIterations-- > 0)
  37. ) $current->add($frequency);
  38. }
  39. } else {
  40. if (!$vsNow and ($maxIterations-- > 0)) {
  41. $current->sub($frequency);
  42. } else {
  43. while (
  44. ($current->getTimestamp() > $now->getTimestamp())
  45. and ($maxIterations-- > 0)
  46. ) $current->sub($frequency);
  47. }
  48. }
  49. $nextEvent = $current->format('Y-m-d');
  50. } while (
  51. in_array($nextEvent, $excludes)
  52. and ($maxIterations > 0)
  53. );
  54. return $current;
  55. }
  56. function findPrevious($start, $frequency, $excludes = [], $vsNow = true, $maxIterations = 1000) {
  57. return findNext($start, $frequency, $excludes, $vsNow, $maxIterations, -1);
  58. }
  59. define('CONFIG_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'config.php');
  60. define('DATA_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'data.php');
  61. if (file_exists(CONFIG_FILE)) require_once CONFIG_FILE;
  62. if (!isset($config)) $config = [];
  63. $action = (isset($_REQUEST['action']) and preg_match(ACTION_REGEX, $_REQUEST['action'])) ? $_REQUEST['action'] : null;
  64. $supplier = array_key_exists('supplier', $_REQUEST) ? $_REQUEST['supplier'] : $requestSupplier;
  65. $hasSupplier = is_string($supplier) and preg_match(SUPPLIER_REGEX, $supplier);
  66. $excludesFormatter = new \IntlDateFormatter('fr_FR.UTF8', \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE, 'Europe/Paris');
  67. if ($hasSupplier) {
  68. if (!isset($config[$supplier]))
  69. $config[$supplier] = [];
  70. $config[$supplier] = array_merge(
  71. [
  72. 'title' => '',
  73. 'subtitle' => '<small class="text-muted text-nowrap d-block d-sm-inline">%date%</small>',
  74. 'description' => '',
  75. 'choices' => [],
  76. 'start' => 'now 00:00:00',
  77. 'frequency' => '1 day',
  78. 'password' => '',
  79. 'excludes' => [],
  80. ],
  81. $config[$supplier]
  82. );
  83. $hasPassword = !empty($config[$supplier]['password']);
  84. if ($action === 'config') {
  85. if ($hasPassword) {
  86. if (!isset($_SERVER['PHP_AUTH_USER'])) {
  87. header(sprintf('WWW-Authenticate: Basic realm="Configuration de mon panier bio pour %s"', $supplier));
  88. header('HTTP/1.0 401 Unauthorized');
  89. printf('Cette configuration est protégée par mot de passe !');
  90. exit;
  91. } elseif (
  92. ($_SERVER['PHP_AUTH_USER'] !== $supplier)
  93. or ($_SERVER['PHP_AUTH_PW'] !== $config[$supplier]['password'])
  94. ) {
  95. header('HTTP/1.0 403 Forbidden');
  96. printf('Cette configuration est protégée par mot de passe !');
  97. exit;
  98. }
  99. }
  100. foreach (array_keys($config[$supplier]) as $key)
  101. if (isset($_REQUEST[$key]))
  102. $config[$supplier][$key] = (!in_array($key, ['title', 'subtitle', 'description']) ? filter_var($_REQUEST[$key], FILTER_SANITIZE_STRING) : $_REQUEST[$key]);
  103. }
  104. if (empty($config[$supplier]['start']))
  105. $config[$supplier]['start'] = 'now 00:00:00';
  106. foreach (['choices', 'excludes'] as $key) {
  107. if (is_string($config[$supplier][$key]))
  108. $config[$supplier][$key] = explode(PHP_EOL, $config[$supplier][$key]);
  109. if (!is_array($config[$supplier][$key]))
  110. $config[$supplier][$key] = [];
  111. $config[$supplier][$key] = array_filter(
  112. $config[$supplier][$key],
  113. function ($choice) {
  114. return is_string($choice) and !empty(trim($choice));
  115. }
  116. );
  117. $config[$supplier][$key] = array_map('trim', $config[$supplier][$key]);
  118. }
  119. $config[$supplier]['excludes'] = array_filter(
  120. array_map(
  121. function ($value) use ($excludesFormatter) {
  122. if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value))
  123. return $value;
  124. $timestamp = $excludesFormatter->parse($value, $offset);
  125. if ($timestamp !== false)
  126. return (new \DateTimeImmutable('@' . $timestamp, new \DateTimeZone('Europe/Paris')))->format('Y-m-d');
  127. try {
  128. return (new \DateTimeImmutable($value, new \DateTimeZone('Europe/Paris')))->format('Y-m-d');
  129. } catch (\Exception $exception) {
  130. return null;
  131. }
  132. },
  133. $config[$supplier]['excludes']
  134. ),
  135. function ($value) {
  136. return !is_null($value);
  137. }
  138. );
  139. }
  140. $isConfig = false;
  141. if ($action === 'config') {
  142. $output = fopen(CONFIG_FILE, 'w+');
  143. if ($output) {
  144. if (flock($output, LOCK_EX)) {
  145. fwrite($output, '<?php' . PHP_EOL);
  146. fprintf(
  147. $output,
  148. '$config = %s;' . PHP_EOL,
  149. var_export($config, true)
  150. );
  151. flock($output, LOCK_UN);
  152. }
  153. fclose($output);
  154. }
  155. $isConfig = true;
  156. }
  157. $suppliers = array_keys($config);
  158. sort($suppliers);
  159. try {
  160. $event = array_key_exists('event', $_REQUEST) ? $_REQUEST['event'] : $requestEvent;
  161. $hasEvent = (
  162. is_string($event)
  163. and preg_match(EVENT_REGEX, $event)
  164. and ((new \DateTimeImmutable($event)) instanceof \DateTimeImmutable)
  165. );
  166. } catch (\Exception $exception) {
  167. $hasEvent = false;
  168. }
  169. if (!$isConfig and $hasSupplier) {
  170. $start = new \DateTime($config[$supplier]['start']);
  171. if (!$hasEvent) {
  172. $next = findNext($start, $config[$supplier]['frequency'], $config[$supplier]['excludes'], true);
  173. $nextEvent = $next->format('Y-m-d');
  174. header('Location: ' . generateUrl($supplier, $nextEvent));
  175. die();
  176. } else {
  177. $current = new \DateTime($event);
  178. $previous = findPrevious($current, $config[$supplier]['frequency'], $config[$supplier]['excludes'], false);
  179. $previousEvent = $previous->format('Y-m-d');
  180. if (false and !array_key_exists($previousEvent, $data[$supplier]))
  181. unset($previousEvent);
  182. $next = findNext($current, $config[$supplier]['frequency'], $config[$supplier]['excludes'], false);
  183. $nextEvent = $next->format('Y-m-d');
  184. if (false and !array_key_exists($nextEvent, $data[$supplier]))
  185. unset($nextEvent);
  186. }
  187. switch ($action) {
  188. case 'insert' :
  189. case 'delete' :
  190. $isBeginning = (!file_exists(DATA_FILE) or in_array(filesize(DATA_FILE), [ false, 0 ]));
  191. $output = fopen(DATA_FILE, 'a+');
  192. if (!$output) break;
  193. if (!flock($output, LOCK_EX)) break;
  194. if ($isBeginning)
  195. fwrite($output, '<?php' . PHP_EOL);
  196. $item = [];
  197. foreach (['name', 'choice', 'action'] as $field)
  198. $item[$field] = filter_var($_REQUEST[$field], FILTER_SANITIZE_STRING);
  199. $item['timestamp'] = time();
  200. $item['hash'] = md5(implode([ $item['name'], $item['choice'], ]));
  201. fprintf(
  202. $output,
  203. '$data[%s][%s][] = %s;' . PHP_EOL,
  204. var_export($supplier, true),
  205. var_export($event, true),
  206. str_replace(PHP_EOL, '', var_export($item, true))
  207. );
  208. flock($output, LOCK_UN);
  209. fclose($output);
  210. header('Location: ' . generateUrl($supplier, $event));
  211. die();
  212. }
  213. if (!isset($data)) $data = [];
  214. if (file_exists(DATA_FILE)) include DATA_FILE;
  215. $items = [];
  216. $allItems = isset($data[$supplier][$event]) ? $data[$supplier][$event] : [];
  217. usort($allItems, function ($a, $b) {
  218. $a = intval($a['timestamp']);
  219. $b = intval($b['timestamp']);
  220. if ($a === $b)
  221. return 0;
  222. return ($a < $b) ? -1 : 1;
  223. });
  224. foreach ($allItems as $item) {
  225. if ($item['action'] === 'insert') {
  226. $items[] = $item;
  227. } elseif ($item['action'] === 'delete') {
  228. foreach ($items as $index => $prevItem)
  229. if ($prevItem['hash'] === $item['hash'])
  230. unset($items[$index]);
  231. }
  232. }
  233. $date = (new \IntlDateFormatter('fr_FR.UTF8', \IntlDateFormatter::FULL, \IntlDateFormatter::NONE, 'Europe/Paris'))->format(new \DateTime($event));
  234. foreach (['title', 'subtitle', 'description'] as $key) {
  235. while (preg_match('/%([^%]+)%/i', $config[$supplier][$key], $match))
  236. $config[$supplier][$key] = str_replace(
  237. $match[0],
  238. ${$match[1]},
  239. $config[$supplier][$key]
  240. );
  241. }
  242. if (empty($config[$supplier]['title']))
  243. $config[$supplier]['title'] = $supplier;
  244. $stats = [];
  245. foreach ($items as $item)
  246. if (!empty($item['choice']))
  247. $stats[$item['choice']] += 1;
  248. }
  249. ?><!DOCTYPE html>
  250. <html lang="fr">
  251. <head>
  252. <meta charset="UTF-8" />
  253. <meta name="viewport" content="width=device-width, initial-scale=1" />
  254. <title><?php if ($hasSupplier) : ?><?php echo strip_tags($config[$supplier]['title']); ?><?php if (!$isConfig) : ?> — <?php echo strip_tags($config[$supplier]['subtitle']); ?><?php endif; ?><?php else : ?><?php echo DEFAULT_TITLE; ?><?php endif; ?></title>
  255. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  256. </head>
  257. <body>
  258. <header>
  259. <nav class="navbar navbar-dark bg-dark">
  260. <div class="container-fluid">
  261. <a class="navbar-brand" href="<?php echo $hasSupplier ? generateUrl($supplier) : generateUrl(); ?>">
  262. <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="bi bi-basket d-inline-block align-text-top" viewBox="0 0 16 16">
  263. <path d="M5.757 1.071a.5.5 0 0 1 .172.686L3.383 6h9.234L10.07 1.757a.5.5 0 1 1 .858-.514L13.783 6H15a1 1 0 0 1 1 1v1a1 1 0 0 1-1 1v4.5a2.5 2.5 0 0 1-2.5 2.5h-9A2.5 2.5 0 0 1 1 13.5V9a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h1.217L5.07 1.243a.5.5 0 0 1 .686-.172zM2 9v4.5A1.5 1.5 0 0 0 3.5 15h9a1.5 1.5 0 0 0 1.5-1.5V9H2zM1 7v1h14V7H1zm3 3a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3A.5.5 0 0 1 4 10zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3A.5.5 0 0 1 6 10zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3A.5.5 0 0 1 8 10zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3a.5.5 0 0 1 .5-.5zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3a.5.5 0 0 1 .5-.5z"/>
  264. </svg>
  265. <?php echo $hasSupplier ? $supplier : DEFAULT_TITLE; ?>
  266. </a>
  267. <?php if ($hasSupplier) : ?>
  268. <span class="navbar-text text-muted">
  269. <?php if ($isConfig) : ?>
  270. <a class="text-reset" href="<?php echo generateUrl($supplier); ?>">Retour</a>
  271. <?php else : ?>
  272. <?php if ($hasPassword) : ?>
  273. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-lock" viewBox="0 0 16 16">
  274. <path d="M8 1a2 2 0 0 1 2 2v4H6V3a2 2 0 0 1 2-2zm3 6V3a3 3 0 0 0-6 0v4a2 2 0 0 0-2 2v5a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2zM5 8h6a1 1 0 0 1 1 1v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V9a1 1 0 0 1 1-1z"/>
  275. </svg>
  276. <?php else : ?>
  277. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-unlock" viewBox="0 0 16 16">
  278. <path d="M11 1a2 2 0 0 0-2 2v4a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h5V3a3 3 0 0 1 6 0v4a.5.5 0 0 1-1 0V3a2 2 0 0 0-2-2zM3 8a1 1 0 0 0-1 1v5a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V9a1 1 0 0 0-1-1H3z"/>
  279. </svg>
  280. <?php endif; ?>
  281. <a tabindex="-1" class="text-reset" href="<?php printf('%s?action=config', generateUrl($supplier)); ?>">Configuration</a>
  282. <?php endif; ?>
  283. </span>
  284. <?php endif; ?>
  285. </div>
  286. </nav>
  287. </header>
  288. <main>
  289. <?php if (!$hasSupplier) : ?>
  290. <section class="container-fluid">
  291. <div class="row my-3 g-3">
  292. <div class="col-12">
  293. <div class="alert alert-danger" role="alert">
  294. Pas de fournisseur !
  295. </div>
  296. </div>
  297. <div class="col-12">
  298. <form action="<?php echo generateUrl(); ?>" method="get">
  299. <datalist id="supplierList">
  300. <?php foreach ($suppliers as $supplier) : ?>
  301. <option value="<?php echo $supplier; ?>" />
  302. <?php endforeach; ?>
  303. </datalist>
  304. <div class="input-group">
  305. <span class="input-group-text"><?php echo generateUrl(); ?>/</span>
  306. <input type="text" class="form-control" name="supplier" list="supplierList" required />
  307. <button class="btn btn-primary" type="submit">Aller</button>
  308. </div>
  309. </form>
  310. </div>
  311. </div>
  312. </section>
  313. <?php else : ?>
  314. <?php if ($isConfig) : ?>
  315. <section class="container-fluid">
  316. <div class="row my-3 g-3">
  317. <div class="col">
  318. <h1>Configuration</h1>
  319. </div>
  320. </div>
  321. </section>
  322. <section class="container-fluid">
  323. <div class="row g-3">
  324. <form action="<?php echo generateUrl($supplier); ?>" method="post">
  325. <div class="row mb-3">
  326. <label for="title" class="col-sm-2 col-form-label">Titre</label>
  327. <div class="col-sm-10">
  328. <input class="form-control" type="text" name="title" value="<?php echo htmlspecialchars($config[$supplier]['title']); ?>" placeholder="<?php echo $supplier; ?>" />
  329. <div class="form-text">Le titre de la page. Par défaut ce sera le nom du fournisseur </div>
  330. </div>
  331. </div>
  332. <div class="row mb-3">
  333. <label for="description" class="col-sm-2 col-form-label">Description</label>
  334. <div class="col-sm-10">
  335. <textarea class="form-control js-ckeditor" name="description" rows="10"><?php echo $config[$supplier]['description']; ?></textarea>
  336. <div class="form-text">La description affichée sous le titre.</div>
  337. </div>
  338. </div>
  339. <div class="row mb-3">
  340. <label for="choices" class="col-sm-2 col-form-label">Choix</label>
  341. <div class="col-sm-10">
  342. <textarea class="form-control" name="choices" rows="5"><?php echo implode(PHP_EOL, $config[$supplier]['choices']); ?></textarea>
  343. <div class="form-text">Les différents choix possibles. Un par ligne. Ou pas.</div>
  344. </div>
  345. </div>
  346. <div class="row mb-3">
  347. <label for="start" class="col-sm-2 col-form-label">Début</label>
  348. <div class="col-sm-10">
  349. <input class="form-control" type="date" name="start" value="<?php echo $config[$supplier]['start']; ?>" />
  350. <div class="form-text">La date du premier événement, si nécessaire de le préciser.</div>
  351. </div>
  352. </div>
  353. <div class="row mb-3">
  354. <label for="frequency" class="col-sm-2 col-form-label">Fréquence</label>
  355. <div class="col-sm-10">
  356. <input class="form-control" type="text" name="frequency" value="<?php echo $config[$supplier]['frequency']; ?>" />
  357. <div class="form-text">La fréquence des événements dans le format <a class="text-reset" href="https://www.php.net/manual/fr/datetime.formats.relative.php" target="_blank">décrit sur cette page</a>.</div>
  358. </div>
  359. </div>
  360. <div class="row mb-3">
  361. <label for="excludes" class="col-sm-2 col-form-label">Exceptions</label>
  362. <div class="col-sm-10">
  363. <textarea class="form-control" name="excludes" rows="5"><?php echo implode(PHP_EOL, array_map(function ($value) use ($excludesFormatter) { return $excludesFormatter->format(new \DateTimeImmutable($value, new \DateTimeZone('Europe/Paris'))); }, $config[$supplier]['excludes'])); ?></textarea>
  364. <div class="form-text">Les dates à exclure. Une par ligne. Ou pas. En tous cas le format c'est celui de l'<a class="text-reset" href="https://unicode-org.github.io/icu/userguide/format_parse/datetime/" target="_blank">ICU</a> : <kbd><?php echo $excludesFormatter->getPattern(); ?></kbd></div>
  365. </div>
  366. </div>
  367. <div class="row mb-3">
  368. <label for="password" class="col-sm-2 col-form-label">Mot de passe</label>
  369. <div class="col-sm-10">
  370. <input class="form-control" type="text" name="password" value="<?php echo $config[$supplier]['password']; ?>" />
  371. <div class="form-text">Ce mot de passe sera demandé pour accéder à la configuration la prochaine fois. Le nom d'utilisateur est le fournisseur courant (en l'occurrence <kbd><?php echo $supplier; ?></kbd>).</div>
  372. </div>
  373. </div>
  374. <div class="row">
  375. <div class="col mb-3">
  376. <button class="btn btn-primary" type="submit" name="action" value="config">Enregistrer</button>
  377. </div>
  378. </div>
  379. </form>
  380. </div>
  381. </section>
  382. <?php else : ?>
  383. <section class="container-fluid">
  384. <div class="row my-3">
  385. <div class="col">
  386. <h1>
  387. <div class="btn-group float-end" role="group">
  388. <?php if (isset($previousEvent)) : ?>
  389. <a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $previousEvent); ?>" title="Événement précédent">
  390. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-left" viewBox="0 0 16 16">
  391. <path fill-rule="evenodd" d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8z"/>
  392. </svg>
  393. </a>
  394. <?php endif; ?>
  395. <a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $event); ?>" title="Cet événement">
  396. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-link" viewBox="0 0 16 16">
  397. <path d="M6.354 5.5H4a3 3 0 0 0 0 6h3a3 3 0 0 0 2.83-4H9c-.086 0-.17.01-.25.031A2 2 0 0 1 7 10.5H4a2 2 0 1 1 0-4h1.535c.218-.376.495-.714.82-1z"/>
  398. <path d="M9 5.5a3 3 0 0 0-2.83 4h1.098A2 2 0 0 1 9 6.5h3a2 2 0 1 1 0 4h-1.535a4.02 4.02 0 0 1-.82 1H12a3 3 0 1 0 0-6H9z"/>
  399. </svg>
  400. </a>
  401. <?php if (isset($nextEvent)) : ?>
  402. <a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $nextEvent); ?>" title="Événement suivant">
  403. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-right" viewBox="0 0 16 16">
  404. <path fill-rule="evenodd" d="M1 8a.5.5 0 0 1 .5-.5h11.793l-3.147-3.146a.5.5 0 0 1 .708-.708l4 4a.5.5 0 0 1 0 .708l-4 4a.5.5 0 0 1-.708-.708L13.293 8.5H1.5A.5.5 0 0 1 1 8z"/>
  405. </svg>
  406. </a>
  407. <?php endif; ?>
  408. </div>
  409. <?php echo $config[$supplier]['title']; ?>
  410. <?php echo $config[$supplier]['subtitle']; ?>
  411. </h1>
  412. <?php if (!empty($config[$supplier]['description'])) : ?>
  413. <p class="lead"><?php echo $config[$supplier]['description']; ?></p>
  414. <?php endif; ?>
  415. </div>
  416. </div>
  417. </section>
  418. <section class="container-fluid">
  419. <div class="row g-3">
  420. <form class="js-localremember bg-dark text-light" action="<?php echo generateUrl($supplier); ?>" method="post">
  421. <div class="row my-3">
  422. <label for="title" class="col-sm-2 col-form-label">Nom</label>
  423. <div class="col-sm-10">
  424. <input class="form-control" type="text" name="name" required placeholder="Nom" />
  425. </div>
  426. </div>
  427. <?php if (!empty($config[$supplier]['choices'])) : ?>
  428. <div class="row mb-3">
  429. <label for="title" class="col-sm-2 col-form-label">Choix</label>
  430. <div class="col-sm-10">
  431. <div class="btn-group" role="group">
  432. <?php foreach ($config[$supplier]['choices'] as $index => $choice) : ?>
  433. <input type="radio" class="btn-check" id="<?php printf('option%d', $index); ?>" autocomplete="off" name="choice" value="<?php echo $choice; ?>" />
  434. <label class="btn btn-outline-light" for="<?php printf('option%d', $index); ?>"><?php echo $choice; ?></label>
  435. <?php endforeach; ?>
  436. </div>
  437. </div>
  438. </div>
  439. <?php endif; ?>
  440. <div class="row">
  441. <div class="col mb-3">
  442. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  443. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  444. <?php if (empty($config[$supplier]['choices'])) : ?>
  445. <input type="hidden" name="choice" value="" />
  446. <?php endif; ?>
  447. <button class="btn btn-primary" type="submit" name="action" value="insert">Commander</button>
  448. </div>
  449. </div>
  450. </form>
  451. </div>
  452. </section>
  453. <section class="container-fluid">
  454. <div class="row my-3">
  455. <?php if (!empty($items)) : ?>
  456. <div class="col-12">
  457. <div class="table-responsive">
  458. <table class="table table-striped table-hover align-middle">
  459. <thead>
  460. <tr>
  461. <th scope="col">
  462. Nom
  463. </th>
  464. <?php if (!empty($config[$supplier]['choices'])) : ?>
  465. <th scope="col">
  466. Choix
  467. </th>
  468. <?php endif; ?>
  469. <th scope="col">
  470. &nbsp;
  471. </th>
  472. </tr>
  473. </thead>
  474. <tbody>
  475. <?php foreach ($items as $item) : ?>
  476. <tr>
  477. <td>
  478. <?php echo $item['name']; ?>
  479. </td>
  480. <?php if (!empty($config[$supplier]['choices'])) : ?>
  481. <td>
  482. <?php if (!empty($item['choice'])) : ?>
  483. <?php echo $item['choice']; ?>
  484. <?php endif; ?>
  485. </td>
  486. <?php endif; ?>
  487. <td>
  488. <form onsubmit="return confirm('Souhaitez-vous vraiment annuler cette commande ?');">
  489. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  490. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  491. <input type="hidden" name="name" value="<?php echo $item['name']; ?>" />
  492. <input type="hidden" name="choice" value="<?php echo $item['choice']; ?>" />
  493. <button class="btn btn-secondary float-end" type="submit" name="action" value="delete">Annuler</button>
  494. </form>
  495. </td>
  496. </tr>
  497. <?php endforeach; ?>
  498. </tbody>
  499. </table>
  500. </div>
  501. </div>
  502. <?php endif; ?>
  503. <div class="col-12">
  504. <ul class="list-group">
  505. <li class="list-group-item d-flex justify-content-between align-items-center">
  506. Commandes
  507. <span class="badge bg-primary rounded-pill"><?php echo count($items); ?></span>
  508. </li>
  509. <?php foreach ($stats as $choice => $count) : ?>
  510. <li class="list-group-item d-flex justify-content-between align-items-center">
  511. <?php echo $choice; ?>
  512. <span class="badge bg-secondary rounded-pill"><?php echo $count; ?></span>
  513. </li>
  514. <?php endforeach; ?>
  515. </ul>
  516. </div>
  517. </div>
  518. </section>
  519. <?php endif; ?>
  520. <?php endif; ?>
  521. </main>
  522. <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
  523. <?php if ($isConfig) : ?>
  524. <script src="https://cdn.ckeditor.com/ckeditor5/31.0.0/classic/ckeditor.js"></script>
  525. <script>
  526. document.querySelectorAll('.js-ckeditor').forEach(function (element) {
  527. ClassicEditor.create(element).catch(error => { console.error(error); });
  528. });
  529. </script>
  530. <?php endif; ?>
  531. <script>
  532. document.querySelectorAll('.js-localremember').forEach(function (form) {
  533. const fields = [ 'name', 'choice' ];
  534. form.addEventListener('submit', function (event) {
  535. fields.forEach(function (field) {
  536. window.localStorage.setItem('mon_panier_bio_' + field, form.elements[field].value);
  537. });
  538. });
  539. fields.forEach(function (field) {
  540. if (
  541. (form.elements[field].value === '')
  542. && (window.localStorage.getItem('mon_panier_bio_' + field) !== null)
  543. ) {
  544. form.elements[field].value = window.localStorage.getItem('mon_panier_bio_' + field);
  545. }
  546. });
  547. });
  548. </script>
  549. </body>
  550. </html>