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.

768 lines
30 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. if (($pos = strpos($baseUrl, '?')) !== false)
  9. $baseUrl = substr($baseUrl, 0, $pos);
  10. $requestUrl = trim(array_key_exists('QUERY_STRING', $_SERVER) ? str_replace($_SERVER['QUERY_STRING'], '', $baseUrl) : $baseUrl, '?');
  11. if (preg_match(REQUEST_REGEX, $requestUrl, $match)) {
  12. $requestSupplier = array_key_exists('supplier', $match) ? $match['supplier'] : null;
  13. $requestEvent = array_key_exists('event', $match) ? $match['event'] : null;
  14. if (!is_null($requestEvent))
  15. $requestUrl = rtrim(str_replace($requestEvent, '', $requestUrl), '/');
  16. if (!is_null($requestSupplier))
  17. $requestUrl = rtrim(str_replace($requestSupplier, '', $requestUrl), '/');
  18. } else {
  19. $requestSupplier = null;
  20. $requestEvent = null;
  21. }
  22. function generateUrl($supplier = null, $event = null) {
  23. global $requestUrl;
  24. if (is_null($supplier))
  25. return $requestUrl;
  26. if (is_null($event))
  27. return sprintf('%s/%s', $requestUrl, $supplier);
  28. return sprintf('%s/%s/%s', $requestUrl, $supplier, $event);
  29. }
  30. function findNext($start, $frequency, $excludes = [], $vsNow = true, $maxIterations = 1000, $direction = +1) {
  31. $now = new \DateTime('now');
  32. $current = clone $start;
  33. $frequency = \DateInterval::createFromDateString($frequency);
  34. do {
  35. if ($direction === abs($direction)) {
  36. if (!$vsNow and ($maxIterations-- > 0)) {
  37. $current->add($frequency);
  38. } else {
  39. while (
  40. ($current->getTimestamp() < $now->getTimestamp())
  41. and ($maxIterations-- > 0)
  42. ) $current->add($frequency);
  43. }
  44. } else {
  45. if (!$vsNow and ($maxIterations-- > 0)) {
  46. $current->sub($frequency);
  47. } else {
  48. while (
  49. ($current->getTimestamp() > $now->getTimestamp())
  50. and ($maxIterations-- > 0)
  51. ) $current->sub($frequency);
  52. }
  53. }
  54. $nextEvent = $current->format('Y-m-d');
  55. } while (
  56. in_array($nextEvent, $excludes)
  57. and ($maxIterations > 0)
  58. );
  59. return $current;
  60. }
  61. function findPrevious($start, $frequency, $excludes = [], $vsNow = true, $maxIterations = 1000) {
  62. return findNext($start, $frequency, $excludes, $vsNow, $maxIterations, -1);
  63. }
  64. define('CONFIG_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'config.php');
  65. define('DATA_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'data.php');
  66. if (file_exists(CONFIG_FILE)) require_once CONFIG_FILE;
  67. if (!isset($config)) $config = [];
  68. $action = (isset($_REQUEST['action']) and preg_match(ACTION_REGEX, $_REQUEST['action'])) ? $_REQUEST['action'] : null;
  69. $supplier = array_key_exists('supplier', $_REQUEST) ? $_REQUEST['supplier'] : $requestSupplier;
  70. $hasSupplier = is_string($supplier) and preg_match('/^' . SUPPLIER_REGEX . '$/', $supplier);
  71. $excludesFormatter = new \IntlDateFormatter('fr_FR.UTF8', \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE, 'Europe/Paris');
  72. $supplierIsNew = false;
  73. if ($hasSupplier) {
  74. if (!isset($config[$supplier])) {
  75. $config[$supplier] = [];
  76. $supplierIsNew = true;
  77. }
  78. $config[$supplier] = array_merge(
  79. [
  80. 'title' => '',
  81. 'subtitle' => '<small class="text-muted text-nowrap d-block d-sm-inline">%date%</small>',
  82. 'description' => '',
  83. 'choices' => [],
  84. 'start' => 'now 00:00:00',
  85. 'frequency' => '1 day',
  86. 'password' => '',
  87. 'excludes' => [],
  88. ],
  89. $config[$supplier]
  90. );
  91. $hasPassword = !empty($config[$supplier]['password']);
  92. if ($action === 'config') {
  93. if ($hasPassword) {
  94. if (!isset($_SERVER['PHP_AUTH_USER'])) {
  95. header(sprintf('WWW-Authenticate: Basic realm="Configuration de mon panier bio pour %s"', $supplier));
  96. header('HTTP/1.0 401 Unauthorized');
  97. printf('Cette configuration est protégée par mot de passe !');
  98. exit;
  99. } elseif (
  100. ($_SERVER['PHP_AUTH_USER'] !== $supplier)
  101. or ($_SERVER['PHP_AUTH_PW'] !== $config[$supplier]['password'])
  102. ) {
  103. header('HTTP/1.0 403 Forbidden');
  104. printf('Cette configuration est protégée par mot de passe !');
  105. exit;
  106. }
  107. }
  108. foreach (array_keys($config[$supplier]) as $key)
  109. if (isset($_REQUEST[$key]))
  110. $config[$supplier][$key] = (!in_array($key, ['title', 'subtitle', 'description']) ? filter_var($_REQUEST[$key], FILTER_SANITIZE_STRING) : $_REQUEST[$key]);
  111. }
  112. if (empty($config[$supplier]['start']))
  113. $config[$supplier]['start'] = 'now 00:00:00';
  114. foreach (['choices', 'excludes'] as $key) {
  115. if (is_string($config[$supplier][$key]))
  116. $config[$supplier][$key] = explode(PHP_EOL, $config[$supplier][$key]);
  117. if (!is_array($config[$supplier][$key]))
  118. $config[$supplier][$key] = [];
  119. $config[$supplier][$key] = array_filter(
  120. $config[$supplier][$key],
  121. function ($choice) {
  122. return is_string($choice) and !empty(trim($choice));
  123. }
  124. );
  125. $config[$supplier][$key] = array_map('trim', $config[$supplier][$key]);
  126. }
  127. $config[$supplier]['excludes'] = array_filter(
  128. array_map(
  129. function ($value) use ($excludesFormatter) {
  130. if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value))
  131. return $value;
  132. $timestamp = $excludesFormatter->parse($value, $offset);
  133. if ($timestamp !== false)
  134. return (new \DateTimeImmutable('@' . $timestamp, new \DateTimeZone('Europe/Paris')))->format('Y-m-d');
  135. try {
  136. return (new \DateTimeImmutable($value, new \DateTimeZone('Europe/Paris')))->format('Y-m-d');
  137. } catch (\Exception $exception) {
  138. return null;
  139. }
  140. },
  141. $config[$supplier]['excludes']
  142. ),
  143. function ($value) {
  144. return !is_null($value);
  145. }
  146. );
  147. }
  148. $isConfig = false;
  149. if ($action === 'config') {
  150. $output = fopen(CONFIG_FILE, 'w+');
  151. if ($output) {
  152. if (flock($output, LOCK_EX)) {
  153. fwrite($output, '<?php' . PHP_EOL);
  154. fprintf(
  155. $output,
  156. '$config = %s;' . PHP_EOL,
  157. var_export($config, true)
  158. );
  159. flock($output, LOCK_UN);
  160. }
  161. fclose($output);
  162. }
  163. $isConfig = true;
  164. }
  165. $suppliers = array_keys($config);
  166. sort($suppliers);
  167. try {
  168. $event = array_key_exists('event', $_REQUEST) ? $_REQUEST['event'] : $requestEvent;
  169. $hasEvent = (
  170. is_string($event)
  171. and preg_match('/^' . EVENT_REGEX . '$/', $event)
  172. and ((new \DateTimeImmutable($event)) instanceof \DateTimeImmutable)
  173. );
  174. } catch (\Exception $exception) {
  175. $hasEvent = false;
  176. }
  177. if (!$isConfig and !$supplierIsNew and $hasSupplier) {
  178. $start = new \DateTime($config[$supplier]['start']);
  179. if (!$hasEvent) {
  180. $next = findNext($start, $config[$supplier]['frequency'], $config[$supplier]['excludes'], true);
  181. $nextEvent = $next->format('Y-m-d');
  182. header('Location: ' . generateUrl($supplier, $nextEvent));
  183. die();
  184. } else {
  185. $current = new \DateTime($event);
  186. $previous = findPrevious($current, $config[$supplier]['frequency'], $config[$supplier]['excludes'], false);
  187. $previousEvent = $previous->format('Y-m-d');
  188. if (false and !array_key_exists($previousEvent, $data[$supplier]))
  189. unset($previousEvent);
  190. $next = findNext($current, $config[$supplier]['frequency'], $config[$supplier]['excludes'], false);
  191. $nextEvent = $next->format('Y-m-d');
  192. if (false and !array_key_exists($nextEvent, $data[$supplier]))
  193. unset($nextEvent);
  194. }
  195. switch ($action) {
  196. case 'insert' :
  197. case 'delete' :
  198. $isBeginning = (!file_exists(DATA_FILE) or in_array(filesize(DATA_FILE), [ false, 0 ]));
  199. $output = fopen(DATA_FILE, 'a+');
  200. if (!$output) break;
  201. if (!flock($output, LOCK_EX)) break;
  202. if ($isBeginning)
  203. fwrite($output, '<?php' . PHP_EOL);
  204. $item = [];
  205. foreach (['name', 'choice', 'action'] as $field)
  206. $item[$field] = filter_var($_REQUEST[$field], FILTER_SANITIZE_STRING);
  207. $item['timestamp'] = time();
  208. $item['hash'] = md5(implode([ $item['name'], $item['choice'], ]));
  209. fprintf(
  210. $output,
  211. '$data[%s][%s][] = %s;' . PHP_EOL,
  212. var_export($supplier, true),
  213. var_export($event, true),
  214. str_replace(PHP_EOL, '', var_export($item, true))
  215. );
  216. flock($output, LOCK_UN);
  217. fclose($output);
  218. header('Location: ' . generateUrl($supplier, $event));
  219. die();
  220. }
  221. if (!isset($data)) $data = [];
  222. if (file_exists(DATA_FILE)) include DATA_FILE;
  223. $items = [];
  224. $allItems = isset($data[$supplier][$event]) ? $data[$supplier][$event] : [];
  225. usort($allItems, function ($a, $b) {
  226. $a = intval($a['timestamp']);
  227. $b = intval($b['timestamp']);
  228. if ($a === $b)
  229. return 0;
  230. return ($a < $b) ? -1 : 1;
  231. });
  232. foreach ($allItems as $item) {
  233. if ($item['action'] === 'insert') {
  234. $items[] = $item;
  235. } elseif ($item['action'] === 'delete') {
  236. foreach ($items as $index => $prevItem)
  237. if ($prevItem['hash'] === $item['hash'])
  238. unset($items[$index]);
  239. }
  240. }
  241. $date = (new \IntlDateFormatter('fr_FR.UTF8', \IntlDateFormatter::FULL, \IntlDateFormatter::NONE, 'Europe/Paris'))->format(new \DateTime($event));
  242. foreach (['title', 'subtitle', 'description'] as $key) {
  243. while (preg_match('/%([^%]+)%/i', $config[$supplier][$key], $match))
  244. $config[$supplier][$key] = str_replace(
  245. $match[0],
  246. ${$match[1]},
  247. $config[$supplier][$key]
  248. );
  249. }
  250. if (empty($config[$supplier]['title']))
  251. $config[$supplier]['title'] = $supplier;
  252. $stats = [];
  253. foreach ($items as $item)
  254. if (!empty($item['choice']))
  255. $stats[$item['choice']] += 1;
  256. }
  257. if ($supplierIsNew and !empty($suppliers)) {
  258. $closestSuppliers = array_filter(
  259. array_map(
  260. function ($other) use ($supplier) {
  261. return [
  262. 'supplier' => $other,
  263. 'score' => levenshtein($supplier, $other),
  264. ];
  265. },
  266. $suppliers
  267. ),
  268. function ($item) {
  269. return $item['score'] > 0;
  270. }
  271. );
  272. usort($closestSuppliers, function ($a, $b) {
  273. if ($a['score'] == $b['score']) {
  274. return 0;
  275. }
  276. return ($a['score'] < $b['score']) ? -1 : 1;
  277. });
  278. }
  279. ?><!DOCTYPE html>
  280. <html lang="fr">
  281. <head>
  282. <meta charset="UTF-8" />
  283. <meta name="viewport" content="width=device-width, initial-scale=1" />
  284. <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>
  285. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  286. </head>
  287. <body>
  288. <header>
  289. <nav class="navbar navbar-dark bg-dark">
  290. <div class="container-fluid">
  291. <a class="navbar-brand" href="<?php echo $hasSupplier ? generateUrl($supplier) : generateUrl(); ?>">
  292. <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">
  293. <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"/>
  294. </svg>
  295. <?php echo $hasSupplier ? $supplier : DEFAULT_TITLE; ?>
  296. </a>
  297. <span class="navbar-text text-muted">
  298. <a class="text-reset me-3" data-bs-toggle="modal" href="#linkModal">Lien</a>
  299. <?php if ($hasSupplier) : ?>
  300. <?php if ($isConfig) : ?>
  301. <a class="text-reset" href="<?php echo generateUrl($supplier); ?>">Retour</a>
  302. <?php else : ?>
  303. <a tabindex="-1" class="text-reset" href="<?php printf('%s?action=config', generateUrl($supplier)); ?>">
  304. <?php if ($hasPassword) : ?>
  305. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-lock" viewBox="0 0 16 16">
  306. <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"/>
  307. </svg>
  308. <?php else : ?>
  309. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-unlock" viewBox="0 0 16 16">
  310. <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"/>
  311. </svg>
  312. <?php endif; ?>
  313. Configuration
  314. </a>
  315. <?php endif; ?>
  316. <?php endif; ?>
  317. </span>
  318. </div>
  319. </nav>
  320. </header>
  321. <main>
  322. <?php if (!$hasSupplier) : ?>
  323. <section class="container-fluid pt-3">
  324. <div class="alert alert-danger alert-dismissible mb-3" role="alert">
  325. Pas de fournisseur !
  326. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Fermer"></button>
  327. </div>
  328. <div class="row mb-3 g-3">
  329. <div class="col-12">
  330. <form action="<?php echo generateUrl(); ?>" method="post">
  331. <datalist id="supplierList">
  332. <?php foreach ($suppliers as $supplier) : ?>
  333. <option value="<?php echo $supplier; ?>" />
  334. <?php endforeach; ?>
  335. </datalist>
  336. <div class="input-group input-group-lg">
  337. <span class="input-group-text">
  338. <span class="d-none d-sm-inline"><?php echo generateUrl(); ?></span>
  339. <span class="d-inline d-sm-none" title="<?php echo generateUrl(); ?>">&hellip;</span>
  340. /
  341. </span>
  342. <input type="text" class="form-control js-closealerts" name="supplier" list="supplierList" required />
  343. <button class="btn btn-primary" type="submit">Aller&nbsp;&rarr;</button>
  344. </div>
  345. </form>
  346. </div>
  347. <div class="col-12">
  348. <details>
  349. <summary>Documentation</summary>
  350. </details>
  351. </div>
  352. </div>
  353. </section>
  354. <?php else : ?>
  355. <?php if ($isConfig) : ?>
  356. <section class="container-fluid">
  357. <div class="row my-3 g-3">
  358. <div class="col">
  359. <h1>Configuration</h1>
  360. </div>
  361. </div>
  362. </section>
  363. <section class="container-fluid">
  364. <div class="row g-3">
  365. <form action="<?php echo generateUrl($supplier); ?>" method="post">
  366. <div class="row mb-3">
  367. <label for="title" class="col-sm-2 col-form-label">Titre</label>
  368. <div class="col-sm-10">
  369. <input class="form-control" type="text" name="title" value="<?php echo htmlspecialchars($config[$supplier]['title']); ?>" placeholder="<?php echo $supplier; ?>" />
  370. <div class="form-text">Le titre de la page. Par défaut ce sera le nom du fournisseur </div>
  371. </div>
  372. </div>
  373. <div class="row mb-3">
  374. <label for="description" class="col-sm-2 col-form-label">Description</label>
  375. <div class="col-sm-10">
  376. <textarea class="form-control js-ckeditor" name="description" rows="20"><?php echo $config[$supplier]['description']; ?></textarea>
  377. <div class="form-text">La description affichée sous le titre.</div>
  378. </div>
  379. </div>
  380. <div class="row mb-3">
  381. <label for="choices" class="col-sm-2 col-form-label">Choix</label>
  382. <div class="col-sm-10">
  383. <textarea class="form-control" name="choices" rows="5"><?php echo implode(PHP_EOL, $config[$supplier]['choices']); ?></textarea>
  384. <div class="form-text">Les différents choix possibles. Un par ligne. Ou pas.</div>
  385. </div>
  386. </div>
  387. <div class="row mb-3">
  388. <label for="start" class="col-sm-2 col-form-label">Début</label>
  389. <div class="col-sm-10">
  390. <input class="form-control" type="date" name="start" value="<?php echo $config[$supplier]['start']; ?>" />
  391. <div class="form-text">La date du premier événement, si nécessaire de le préciser.</div>
  392. </div>
  393. </div>
  394. <div class="row mb-3">
  395. <label for="frequency" class="col-sm-2 col-form-label">Fréquence</label>
  396. <div class="col-sm-10">
  397. <input class="form-control" type="text" name="frequency" value="<?php echo $config[$supplier]['frequency']; ?>" />
  398. <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>
  399. </div>
  400. </div>
  401. <div class="row mb-3">
  402. <label for="excludes" class="col-sm-2 col-form-label">Exceptions</label>
  403. <div class="col-sm-10">
  404. <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>
  405. <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>
  406. </div>
  407. </div>
  408. <div class="row mb-3">
  409. <label for="password" class="col-sm-2 col-form-label">Mot de passe</label>
  410. <div class="col-sm-10">
  411. <input class="form-control" type="text" name="password" value="<?php echo $config[$supplier]['password']; ?>" />
  412. <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>
  413. </div>
  414. </div>
  415. <div class="row">
  416. <div class="col mb-3">
  417. <button class="btn btn-primary" type="submit" name="action" value="config">Enregistrer</button>
  418. </div>
  419. </div>
  420. </form>
  421. </div>
  422. </section>
  423. <?php else /* !$isConfig */ : ?>
  424. <?php if ($supplierIsNew) : ?>
  425. <section class="container-fluid pt-3">
  426. <div class="alert alert-warning alert-dismissible" role="alert">
  427. Ce fournisseur n'existe pas encore !
  428. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Fermer"></button>
  429. </div>
  430. <div class="row g-3">
  431. <div class="col-xs-12 col-sm-6">
  432. <div class="card h-100">
  433. <div class="card-body">
  434. <h2 class="card-title">Oops !</h2>
  435. <p class="card-text">Le nom du fournisseur « <tt><?php echo $supplier; ?></tt> » est probablement mal orthographié, c'est pour ça qu'il n'existe pas.</p>
  436. <p class="card-text">
  437. Peut-être sagissait-il de
  438. <?php $max = 3; foreach ($closestSuppliers as $index => $item) : ?>
  439. <?php if ($index < $max) : ?>
  440. <?php if ($index > 0) : ?>
  441. <?php if ($index === min($max, count($closestSuppliers) - 1)) : ?>
  442. ou
  443. <?php else : ?>
  444. ,
  445. <?php endif; ?>
  446. <?php endif; ?>
  447. « <tt><a class="card-link" href="<?php echo generateUrl($item['supplier']); ?>"><?php echo $item['supplier']; ?></a></tt> »
  448. <?php endif; ?>
  449. <?php endforeach; ?>
  450. ?
  451. </p>
  452. <a class="btn btn-primary" href="<?php echo generateUrl(); ?>">Recommencer</a>
  453. </div>
  454. </div>
  455. </div>
  456. <div class="col-xs-12 col-sm-6">
  457. <div class="card h-100">
  458. <div class="card-body">
  459. <h2 class="card-title">C'est normal !</h2>
  460. <p class="card-text">On souhaite le créer.</p>
  461. <p class="card_text">Une fois configuré il sera prêt à être utilisé.</p>
  462. <a class="btn btn-primary" href="<?php echo generateUrl($supplier) . '?action=config'; ?>">Configurer</a>
  463. </div>
  464. </div>
  465. </div>
  466. </div>
  467. </section>
  468. <?php else /* !$supplierIsNew */ : ?>
  469. <section class="container-fluid">
  470. <div class="row my-3">
  471. <div class="col">
  472. <h1>
  473. <div class="btn-group float-end" role="group">
  474. <?php if (isset($previousEvent)) : ?>
  475. <a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $previousEvent); ?>" title="Événement précédent">
  476. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-left" viewBox="0 0 16 16">
  477. <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"/>
  478. </svg>
  479. </a>
  480. <?php endif; ?>
  481. <a class="btn btn-outline-primary d-none d-sm-inline" href="<?php echo generateUrl($supplier, $event); ?>" title="Cet événement">
  482. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-link" viewBox="0 0 16 16">
  483. <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"/>
  484. <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"/>
  485. </svg>
  486. </a>
  487. <?php if (isset($nextEvent)) : ?>
  488. <a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $nextEvent); ?>" title="Événement suivant">
  489. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-right" viewBox="0 0 16 16">
  490. <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"/>
  491. </svg>
  492. </a>
  493. <?php endif; ?>
  494. </div>
  495. <?php echo $config[$supplier]['title']; ?>
  496. <?php echo $config[$supplier]['subtitle']; ?>
  497. </h1>
  498. <?php if (!empty($config[$supplier]['description'])) : ?>
  499. <p class="lead"><?php echo $config[$supplier]['description']; ?></p>
  500. <?php endif; ?>
  501. </div>
  502. </div>
  503. </section>
  504. <section class="container-fluid">
  505. <div class="row g-3">
  506. <form class="js-localremember bg-dark text-light" action="<?php echo generateUrl($supplier); ?>" method="post">
  507. <div class="row my-3">
  508. <label for="title" class="col-sm-2 col-form-label">Nom</label>
  509. <div class="col-sm-10">
  510. <input class="form-control" type="text" name="name" required placeholder="Nom" />
  511. </div>
  512. </div>
  513. <?php if (!empty($config[$supplier]['choices'])) : ?>
  514. <div class="row mb-3">
  515. <label for="title" class="col-sm-2 col-form-label">Choix</label>
  516. <div class="col-sm-10">
  517. <div class="btn-group" role="group">
  518. <?php foreach ($config[$supplier]['choices'] as $index => $choice) : ?>
  519. <input type="radio" class="btn-check" id="<?php printf('option%d', $index); ?>" autocomplete="off" name="choice" value="<?php echo $choice; ?>" />
  520. <label class="btn btn-outline-light" for="<?php printf('option%d', $index); ?>"><?php echo $choice; ?></label>
  521. <?php endforeach; ?>
  522. </div>
  523. </div>
  524. </div>
  525. <?php endif; ?>
  526. <div class="row">
  527. <div class="col mb-3">
  528. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  529. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  530. <?php if (empty($config[$supplier]['choices'])) : ?>
  531. <input type="hidden" name="choice" value="" />
  532. <?php endif; ?>
  533. <button class="btn btn-primary" type="submit" name="action" value="insert">Commander</button>
  534. </div>
  535. </div>
  536. </form>
  537. </div>
  538. </section>
  539. <section class="container-fluid">
  540. <div class="row my-3">
  541. <?php if (!empty($items)) : ?>
  542. <div class="col-12">
  543. <div class="table-responsive">
  544. <table class="table table-striped table-hover align-middle">
  545. <thead>
  546. <tr>
  547. <th scope="col">
  548. Nom
  549. </th>
  550. <?php if (!empty($config[$supplier]['choices'])) : ?>
  551. <th scope="col">
  552. Choix
  553. </th>
  554. <?php endif; ?>
  555. <th scope="col">
  556. &nbsp;
  557. </th>
  558. </tr>
  559. </thead>
  560. <tbody>
  561. <?php foreach ($items as $item) : ?>
  562. <tr>
  563. <td>
  564. <?php echo $item['name']; ?>
  565. </td>
  566. <?php if (!empty($config[$supplier]['choices'])) : ?>
  567. <td>
  568. <?php if (!empty($item['choice'])) : ?>
  569. <?php echo $item['choice']; ?>
  570. <?php endif; ?>
  571. </td>
  572. <?php endif; ?>
  573. <td>
  574. <form onsubmit="return confirm('Souhaitez-vous vraiment annuler cette commande ?');">
  575. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  576. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  577. <input type="hidden" name="name" value="<?php echo $item['name']; ?>" />
  578. <input type="hidden" name="choice" value="<?php echo $item['choice']; ?>" />
  579. <button class="btn btn-secondary float-end" type="submit" name="action" value="delete">Annuler</button>
  580. </form>
  581. </td>
  582. </tr>
  583. <?php endforeach; ?>
  584. </tbody>
  585. </table>
  586. </div>
  587. </div>
  588. <?php endif; ?>
  589. <div class="col-12">
  590. <ul class="list-group">
  591. <li class="list-group-item d-flex justify-content-between align-items-center">
  592. Commandes
  593. <span class="badge bg-primary rounded-pill"><?php echo count($items); ?></span>
  594. </li>
  595. <?php foreach ($stats as $choice => $count) : ?>
  596. <li class="list-group-item d-flex justify-content-between align-items-center">
  597. <?php echo $choice; ?>
  598. <span class="badge bg-secondary rounded-pill"><?php echo $count; ?></span>
  599. </li>
  600. <?php endforeach; ?>
  601. </ul>
  602. </div>
  603. </div>
  604. </section>
  605. <?php endif; /* $supplierIsNew */ ?>
  606. <?php endif; /* $isConfig*/ ?>
  607. <?php endif; ?>
  608. </main>
  609. <div class="modal fade" id="linkModal" tabindex="-1" aria-hidden="true">
  610. <div class="modal-dialog">
  611. <div class="modal-content">
  612. <div class="modal-header">
  613. <h5 class="modal-title">Lien</h5>
  614. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Fermer"></button>
  615. </div>
  616. <div class="modal-body">
  617. <div class="container-fluid">
  618. <div class="row g-3">
  619. <div class="col-12">
  620. Adresse web
  621. </div>
  622. <div class="col-12 text-center">
  623. <tt id="linkURL">
  624. <?php if (!$hasSupplier) : ?>
  625. <?php echo generateUrl(); ?>
  626. <?php else : ?>
  627. <?php if (!$hasEvent) : ?>
  628. <?php echo generateUrl($supplier); ?>
  629. <?php else : ?>
  630. <?php echo generateUrl($supplier, $event); ?>
  631. <?php endif; ?>
  632. <?php endif; ?>
  633. </tt>
  634. <button class="btn btn-outline-dark js-clipboard" type="button" role="button" data-clipboard-target="#linkURL" data-bs-toggle="tooltip" data-bs-trigger="manual">
  635. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-clipboard" viewBox="0 0 16 16">
  636. <path d="M4 1.5H3a2 2 0 0 0-2 2V14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V3.5a2 2 0 0 0-2-2h-1v1h1a1 1 0 0 1 1 1V14a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3.5a1 1 0 0 1 1-1h1v-1z"/>
  637. <path d="M9.5 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1-.5-.5v-1a.5.5 0 0 1 .5-.5h3zm-3-1A1.5 1.5 0 0 0 5 1.5v1A1.5 1.5 0 0 0 6.5 4h3A1.5 1.5 0 0 0 11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3z"/>
  638. </svg>
  639. </button>
  640. </div>
  641. <div class="col-12">
  642. QR Code
  643. </div>
  644. <div class="col-12">
  645. <div id="linkQRCode"></div>
  646. </div>
  647. </div>
  648. </div>
  649. </div>
  650. <div class="modal-footer">
  651. <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Fermer</button>
  652. </div>
  653. </div>
  654. </div>
  655. </div>
  656. <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>
  657. <script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
  658. <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.8/clipboard.min.js"></script>
  659. <?php if ($isConfig) : ?>
  660. <script src="https://cdn.ckeditor.com/ckeditor5/31.0.0/classic/ckeditor.js"></script>
  661. <script>
  662. document.querySelectorAll('.js-ckeditor').forEach(function (element) {
  663. ClassicEditor.create(element).catch(error => { console.error(error); });
  664. });
  665. </script>
  666. <?php endif; ?>
  667. <script>
  668. document.addEventListener('DOMContentLoaded', function () {
  669. document.querySelectorAll('.js-localremember').forEach(function (form) {
  670. const fields = [ 'name', 'choice' ];
  671. form.addEventListener('submit', function (event) {
  672. fields.forEach(function (field) {
  673. window.localStorage.setItem('mon_panier_bio_' + field, form.elements[field].value);
  674. });
  675. });
  676. fields.forEach(function (field) {
  677. if (
  678. (form.elements[field].value === '')
  679. && (window.localStorage.getItem('mon_panier_bio_' + field) !== null)
  680. ) {
  681. form.elements[field].value = window.localStorage.getItem('mon_panier_bio_' + field);
  682. }
  683. });
  684. });
  685. document.querySelectorAll('.js-closealerts').forEach(function (element) {
  686. element.addEventListener('input', function (event) {
  687. if (event.target.value !== '') {
  688. document.querySelectorAll('.alert').forEach(function (alertElement) {
  689. var alert = bootstrap.Alert.getOrCreateInstance(alertElement)
  690. alert.close();
  691. });
  692. }
  693. });
  694. });
  695. var qrcode = new QRCode('linkQRCode', {
  696. text: document.getElementById('linkURL').innerText,
  697. width: 300,
  698. height: 300,
  699. colorDark : '#000000',
  700. colorLight : '#ffffff',
  701. correctLevel : QRCode.CorrectLevel.H,
  702. });
  703. document.querySelector('#linkQRCode img').classList.add('img-fluid', 'mx-auto', 'd-block');
  704. var clipboard = new ClipboardJS('.js-clipboard');
  705. clipboard.on('success', function (event) {
  706. var tooltip = new bootstrap.Tooltip(event.trigger, {
  707. title: 'Copié dans le presse-papier'
  708. });
  709. tooltip.show();
  710. });
  711. }, false);
  712. </script>
  713. </body>
  714. </html>