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.

615 lines
24 KiB

3 years ago
3 years ago
3 years ago
  1. <?php
  2. define('DEFAULT_TITLE', 'Mon panier bio');
  3. define('SUPPLIER_REGEX', '[A-Za-z]\w{0,31}');
  4. define('EVENT_REGEX', '\d{4}\-[01]\d\-[0123]\d');
  5. define('REQUEST_REGEX', '/^https?:\/\/.+\/(?<supplier>' . SUPPLIER_REGEX . ')\/?(?<event>' . EVENT_REGEX . ')?\/?$/');
  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 pt-3">
  291. <div class="alert alert-danger alert-dismissible mb-3" role="alert">
  292. Pas de fournisseur !
  293. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Fermer"></button>
  294. </div>
  295. <div class="row mb-3 g-3">
  296. <div class="col-12">
  297. <form action="<?php echo generateUrl(); ?>" method="post">
  298. <datalist id="supplierList">
  299. <?php foreach ($suppliers as $supplier) : ?>
  300. <option value="<?php echo $supplier; ?>" />
  301. <?php endforeach; ?>
  302. </datalist>
  303. <div class="input-group">
  304. <span class="input-group-text">
  305. <span class="d-none d-sm-inline"><?php echo generateUrl(); ?></span>
  306. <span class="d-inline d-sm-none" title="<?php echo generateUrl(); ?>">&hellip;</span>
  307. /
  308. </span>
  309. <input type="text" class="form-control js-closealerts" name="supplier" list="supplierList" required />
  310. <button class="btn btn-primary" type="submit">Aller</button>
  311. </div>
  312. </form>
  313. </div>
  314. </div>
  315. </section>
  316. <?php else : ?>
  317. <?php if ($isConfig) : ?>
  318. <section class="container-fluid">
  319. <div class="row my-3 g-3">
  320. <div class="col">
  321. <h1>Configuration</h1>
  322. </div>
  323. </div>
  324. </section>
  325. <section class="container-fluid">
  326. <div class="row g-3">
  327. <form action="<?php echo generateUrl($supplier); ?>" method="post">
  328. <div class="row mb-3">
  329. <label for="title" class="col-sm-2 col-form-label">Titre</label>
  330. <div class="col-sm-10">
  331. <input class="form-control" type="text" name="title" value="<?php echo htmlspecialchars($config[$supplier]['title']); ?>" placeholder="<?php echo $supplier; ?>" />
  332. <div class="form-text">Le titre de la page. Par défaut ce sera le nom du fournisseur </div>
  333. </div>
  334. </div>
  335. <div class="row mb-3">
  336. <label for="description" class="col-sm-2 col-form-label">Description</label>
  337. <div class="col-sm-10">
  338. <textarea class="form-control js-ckeditor" name="description" rows="10"><?php echo $config[$supplier]['description']; ?></textarea>
  339. <div class="form-text">La description affichée sous le titre.</div>
  340. </div>
  341. </div>
  342. <div class="row mb-3">
  343. <label for="choices" class="col-sm-2 col-form-label">Choix</label>
  344. <div class="col-sm-10">
  345. <textarea class="form-control" name="choices" rows="5"><?php echo implode(PHP_EOL, $config[$supplier]['choices']); ?></textarea>
  346. <div class="form-text">Les différents choix possibles. Un par ligne. Ou pas.</div>
  347. </div>
  348. </div>
  349. <div class="row mb-3">
  350. <label for="start" class="col-sm-2 col-form-label">Début</label>
  351. <div class="col-sm-10">
  352. <input class="form-control" type="date" name="start" value="<?php echo $config[$supplier]['start']; ?>" />
  353. <div class="form-text">La date du premier événement, si nécessaire de le préciser.</div>
  354. </div>
  355. </div>
  356. <div class="row mb-3">
  357. <label for="frequency" class="col-sm-2 col-form-label">Fréquence</label>
  358. <div class="col-sm-10">
  359. <input class="form-control" type="text" name="frequency" value="<?php echo $config[$supplier]['frequency']; ?>" />
  360. <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>
  361. </div>
  362. </div>
  363. <div class="row mb-3">
  364. <label for="excludes" class="col-sm-2 col-form-label">Exceptions</label>
  365. <div class="col-sm-10">
  366. <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>
  367. <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>
  368. </div>
  369. </div>
  370. <div class="row mb-3">
  371. <label for="password" class="col-sm-2 col-form-label">Mot de passe</label>
  372. <div class="col-sm-10">
  373. <input class="form-control" type="text" name="password" value="<?php echo $config[$supplier]['password']; ?>" />
  374. <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>
  375. </div>
  376. </div>
  377. <div class="row">
  378. <div class="col mb-3">
  379. <button class="btn btn-primary" type="submit" name="action" value="config">Enregistrer</button>
  380. </div>
  381. </div>
  382. </form>
  383. </div>
  384. </section>
  385. <?php else : ?>
  386. <section class="container-fluid">
  387. <div class="row my-3">
  388. <div class="col">
  389. <h1>
  390. <div class="btn-group float-end" role="group">
  391. <?php if (isset($previousEvent)) : ?>
  392. <a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $previousEvent); ?>" title="Événement précédent">
  393. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-left" viewBox="0 0 16 16">
  394. <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"/>
  395. </svg>
  396. </a>
  397. <?php endif; ?>
  398. <a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $event); ?>" title="Cet événement">
  399. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-link" viewBox="0 0 16 16">
  400. <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"/>
  401. <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"/>
  402. </svg>
  403. </a>
  404. <?php if (isset($nextEvent)) : ?>
  405. <a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $nextEvent); ?>" title="Événement suivant">
  406. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-right" viewBox="0 0 16 16">
  407. <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"/>
  408. </svg>
  409. </a>
  410. <?php endif; ?>
  411. </div>
  412. <?php echo $config[$supplier]['title']; ?>
  413. <?php echo $config[$supplier]['subtitle']; ?>
  414. </h1>
  415. <?php if (!empty($config[$supplier]['description'])) : ?>
  416. <p class="lead"><?php echo $config[$supplier]['description']; ?></p>
  417. <?php endif; ?>
  418. </div>
  419. </div>
  420. </section>
  421. <section class="container-fluid">
  422. <div class="row g-3">
  423. <form class="js-localremember bg-dark text-light" action="<?php echo generateUrl($supplier); ?>" method="post">
  424. <div class="row my-3">
  425. <label for="title" class="col-sm-2 col-form-label">Nom</label>
  426. <div class="col-sm-10">
  427. <input class="form-control" type="text" name="name" required placeholder="Nom" />
  428. </div>
  429. </div>
  430. <?php if (!empty($config[$supplier]['choices'])) : ?>
  431. <div class="row mb-3">
  432. <label for="title" class="col-sm-2 col-form-label">Choix</label>
  433. <div class="col-sm-10">
  434. <div class="btn-group" role="group">
  435. <?php foreach ($config[$supplier]['choices'] as $index => $choice) : ?>
  436. <input type="radio" class="btn-check" id="<?php printf('option%d', $index); ?>" autocomplete="off" name="choice" value="<?php echo $choice; ?>" />
  437. <label class="btn btn-outline-light" for="<?php printf('option%d', $index); ?>"><?php echo $choice; ?></label>
  438. <?php endforeach; ?>
  439. </div>
  440. </div>
  441. </div>
  442. <?php endif; ?>
  443. <div class="row">
  444. <div class="col mb-3">
  445. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  446. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  447. <?php if (empty($config[$supplier]['choices'])) : ?>
  448. <input type="hidden" name="choice" value="" />
  449. <?php endif; ?>
  450. <button class="btn btn-primary" type="submit" name="action" value="insert">Commander</button>
  451. </div>
  452. </div>
  453. </form>
  454. </div>
  455. </section>
  456. <section class="container-fluid">
  457. <div class="row my-3">
  458. <?php if (!empty($items)) : ?>
  459. <div class="col-12">
  460. <div class="table-responsive">
  461. <table class="table table-striped table-hover align-middle">
  462. <thead>
  463. <tr>
  464. <th scope="col">
  465. Nom
  466. </th>
  467. <?php if (!empty($config[$supplier]['choices'])) : ?>
  468. <th scope="col">
  469. Choix
  470. </th>
  471. <?php endif; ?>
  472. <th scope="col">
  473. &nbsp;
  474. </th>
  475. </tr>
  476. </thead>
  477. <tbody>
  478. <?php foreach ($items as $item) : ?>
  479. <tr>
  480. <td>
  481. <?php echo $item['name']; ?>
  482. </td>
  483. <?php if (!empty($config[$supplier]['choices'])) : ?>
  484. <td>
  485. <?php if (!empty($item['choice'])) : ?>
  486. <?php echo $item['choice']; ?>
  487. <?php endif; ?>
  488. </td>
  489. <?php endif; ?>
  490. <td>
  491. <form onsubmit="return confirm('Souhaitez-vous vraiment annuler cette commande ?');">
  492. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  493. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  494. <input type="hidden" name="name" value="<?php echo $item['name']; ?>" />
  495. <input type="hidden" name="choice" value="<?php echo $item['choice']; ?>" />
  496. <button class="btn btn-secondary float-end" type="submit" name="action" value="delete">Annuler</button>
  497. </form>
  498. </td>
  499. </tr>
  500. <?php endforeach; ?>
  501. </tbody>
  502. </table>
  503. </div>
  504. </div>
  505. <?php endif; ?>
  506. <div class="col-12">
  507. <ul class="list-group">
  508. <li class="list-group-item d-flex justify-content-between align-items-center">
  509. Commandes
  510. <span class="badge bg-primary rounded-pill"><?php echo count($items); ?></span>
  511. </li>
  512. <?php foreach ($stats as $choice => $count) : ?>
  513. <li class="list-group-item d-flex justify-content-between align-items-center">
  514. <?php echo $choice; ?>
  515. <span class="badge bg-secondary rounded-pill"><?php echo $count; ?></span>
  516. </li>
  517. <?php endforeach; ?>
  518. </ul>
  519. </div>
  520. </div>
  521. </section>
  522. <?php endif; ?>
  523. <?php endif; ?>
  524. </main>
  525. <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>
  526. <?php if ($isConfig) : ?>
  527. <script src="https://cdn.ckeditor.com/ckeditor5/31.0.0/classic/ckeditor.js"></script>
  528. <script>
  529. document.querySelectorAll('.js-ckeditor').forEach(function (element) {
  530. ClassicEditor.create(element).catch(error => { console.error(error); });
  531. });
  532. </script>
  533. <?php endif; ?>
  534. <script>
  535. document.querySelectorAll('.js-localremember').forEach(function (form) {
  536. const fields = [ 'name', 'choice' ];
  537. form.addEventListener('submit', function (event) {
  538. fields.forEach(function (field) {
  539. window.localStorage.setItem('mon_panier_bio_' + field, form.elements[field].value);
  540. });
  541. });
  542. fields.forEach(function (field) {e
  543. if (
  544. (form.elements[field].value === '')
  545. && (window.localStorage.getItem('mon_panier_bio_' + field) !== null)
  546. ) {
  547. form.elements[field].value = window.localStorage.getItem('mon_panier_bio_' + field);
  548. }
  549. });
  550. });
  551. document.querySelectorAll('.js-closealerts').forEach(function (element) {
  552. element.addEventListener('input', function (event) {
  553. if (event.target.value !== '') {
  554. document.querySelectorAll('.alert').forEach(function (alertElement) {
  555. var alert = bootstrap.Alert.getOrCreateInstance(alertElement)
  556. alert.close();
  557. });
  558. }
  559. });
  560. });
  561. </script>
  562. </body>
  563. </html>