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.

772 lines
31 KiB

2 years ago
2 years ago
2 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. $linkUrl = !$hasSupplier ? generateUrl() : (!$hasEvent ? generateUrl($supplier) : generateUrl($supplier, $event));
  280. ?><!DOCTYPE html>
  281. <html lang="fr">
  282. <head>
  283. <meta charset="UTF-8" />
  284. <meta name="viewport" content="width=device-width, initial-scale=1" />
  285. <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>
  286. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  287. </head>
  288. <body>
  289. <header>
  290. <nav class="navbar navbar-dark bg-dark">
  291. <div class="container-fluid">
  292. <a class="navbar-brand" href="<?php echo $hasSupplier ? generateUrl($supplier) : generateUrl(); ?>">
  293. <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">
  294. <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"/>
  295. </svg>
  296. <?php echo $hasSupplier ? $supplier : DEFAULT_TITLE; ?>
  297. </a>
  298. <span class="navbar-text text-muted">
  299. <a class="text-reset me-3" data-bs-toggle="modal" href="#linkModal">Lien</a>
  300. <?php if ($hasSupplier) : ?>
  301. <?php if ($isConfig) : ?>
  302. <a class="text-reset" href="<?php echo generateUrl($supplier); ?>">Retour</a>
  303. <?php else : ?>
  304. <a tabindex="-1" class="text-reset" href="<?php printf('%s?action=config', generateUrl($supplier)); ?>">
  305. <?php if ($hasPassword) : ?>
  306. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-lock" viewBox="0 0 16 16">
  307. <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"/>
  308. </svg>
  309. <?php else : ?>
  310. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-unlock" viewBox="0 0 16 16">
  311. <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"/>
  312. </svg>
  313. <?php endif; ?>
  314. Configuration
  315. </a>
  316. <?php endif; ?>
  317. <?php endif; ?>
  318. </span>
  319. </div>
  320. </nav>
  321. </header>
  322. <main>
  323. <?php if (!$hasSupplier) : ?>
  324. <section class="container-fluid pt-3">
  325. <div class="alert alert-danger alert-dismissible mb-3" role="alert">
  326. Pas de fournisseur !
  327. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Fermer"></button>
  328. </div>
  329. <div class="row mb-3 g-3">
  330. <div class="col-12">
  331. <form action="<?php echo generateUrl(); ?>" method="post">
  332. <datalist id="supplierList">
  333. <?php foreach ($suppliers as $supplier) : ?>
  334. <option value="<?php echo $supplier; ?>" />
  335. <?php endforeach; ?>
  336. </datalist>
  337. <div class="input-group input-group-lg">
  338. <span class="input-group-text">
  339. <span class="d-none d-sm-inline"><?php echo generateUrl(); ?></span>
  340. <span class="d-inline d-sm-none" title="<?php echo generateUrl(); ?>">&hellip;</span>
  341. /
  342. </span>
  343. <input type="text" class="form-control js-closealerts" name="supplier" list="supplierList" required />
  344. <button class="btn btn-primary" type="submit">Aller&nbsp;&rarr;</button>
  345. </div>
  346. </form>
  347. </div>
  348. <div class="col-12">
  349. <details>
  350. <summary>Documentation</summary>
  351. </details>
  352. </div>
  353. </div>
  354. </section>
  355. <?php else : ?>
  356. <?php if ($isConfig) : ?>
  357. <section class="container-fluid">
  358. <div class="row my-3 g-3">
  359. <div class="col">
  360. <h1>Configuration</h1>
  361. </div>
  362. </div>
  363. </section>
  364. <section class="container-fluid">
  365. <div class="row g-3">
  366. <form action="<?php echo generateUrl($supplier); ?>" method="post">
  367. <div class="row mb-3">
  368. <label for="title" class="col-sm-2 col-form-label">Titre</label>
  369. <div class="col-sm-10">
  370. <input class="form-control" type="text" name="title" value="<?php echo htmlspecialchars($config[$supplier]['title']); ?>" placeholder="<?php echo $supplier; ?>" />
  371. <div class="form-text">Le titre de la page. Par défaut ce sera le nom du fournisseur </div>
  372. </div>
  373. </div>
  374. <div class="row mb-3">
  375. <label for="description" class="col-sm-2 col-form-label">Description</label>
  376. <div class="col-sm-10">
  377. <textarea class="form-control js-ckeditor" name="description" rows="20"><?php echo $config[$supplier]['description']; ?></textarea>
  378. <div class="form-text">La description affichée sous le titre.</div>
  379. </div>
  380. </div>
  381. <div class="row mb-3">
  382. <label for="choices" class="col-sm-2 col-form-label">Choix</label>
  383. <div class="col-sm-10">
  384. <textarea class="form-control" name="choices" rows="5"><?php echo implode(PHP_EOL, $config[$supplier]['choices']); ?></textarea>
  385. <div class="form-text">Les différents choix possibles. Un par ligne. Ou pas.</div>
  386. </div>
  387. </div>
  388. <div class="row mb-3">
  389. <label for="start" class="col-sm-2 col-form-label">Début</label>
  390. <div class="col-sm-10">
  391. <input class="form-control" type="date" name="start" value="<?php echo $config[$supplier]['start']; ?>" />
  392. <div class="form-text">La date du premier événement, si nécessaire de le préciser.</div>
  393. </div>
  394. </div>
  395. <div class="row mb-3">
  396. <label for="frequency" class="col-sm-2 col-form-label">Fréquence</label>
  397. <div class="col-sm-10">
  398. <input class="form-control" type="text" name="frequency" value="<?php echo $config[$supplier]['frequency']; ?>" />
  399. <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>
  400. </div>
  401. </div>
  402. <div class="row mb-3">
  403. <label for="excludes" class="col-sm-2 col-form-label">Exceptions</label>
  404. <div class="col-sm-10">
  405. <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>
  406. <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>
  407. </div>
  408. </div>
  409. <div class="row mb-3">
  410. <label for="password" class="col-sm-2 col-form-label">Mot de passe</label>
  411. <div class="col-sm-10">
  412. <input class="form-control" type="text" name="password" value="<?php echo $config[$supplier]['password']; ?>" />
  413. <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>
  414. </div>
  415. </div>
  416. <div class="row">
  417. <div class="col mb-3">
  418. <button class="btn btn-primary" type="submit" name="action" value="config">Enregistrer</button>
  419. </div>
  420. </div>
  421. </form>
  422. </div>
  423. </section>
  424. <?php else /* !$isConfig */ : ?>
  425. <?php if ($supplierIsNew) : ?>
  426. <section class="container-fluid pt-3">
  427. <div class="alert alert-warning alert-dismissible" role="alert">
  428. Ce fournisseur n'existe pas encore !
  429. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Fermer"></button>
  430. </div>
  431. <div class="row g-3">
  432. <div class="col-xs-12 col-sm-6">
  433. <div class="card h-100">
  434. <div class="card-body">
  435. <h2 class="card-title">Oops !</h2>
  436. <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>
  437. <p class="card-text">
  438. Peut-être sagissait-il de
  439. <?php $max = 3; foreach ($closestSuppliers as $index => $item) : ?>
  440. <?php if ($index < $max) : ?>
  441. <?php if ($index > 0) : ?>
  442. <?php if ($index === min($max, count($closestSuppliers) - 1)) : ?>
  443. ou
  444. <?php else : ?>
  445. ,
  446. <?php endif; ?>
  447. <?php endif; ?>
  448. « <tt><a class="card-link" href="<?php echo generateUrl($item['supplier']); ?>"><?php echo $item['supplier']; ?></a></tt> »
  449. <?php endif; ?>
  450. <?php endforeach; ?>
  451. ?
  452. </p>
  453. <a class="btn btn-primary" href="<?php echo generateUrl(); ?>">Recommencer</a>
  454. </div>
  455. </div>
  456. </div>
  457. <div class="col-xs-12 col-sm-6">
  458. <div class="card h-100">
  459. <div class="card-body">
  460. <h2 class="card-title">C'est normal !</h2>
  461. <p class="card-text">On souhaite le créer.</p>
  462. <p class="card_text">Une fois configuré il sera prêt à être utilisé.</p>
  463. <a class="btn btn-primary" href="<?php echo generateUrl($supplier) . '?action=config'; ?>">Configurer</a>
  464. </div>
  465. </div>
  466. </div>
  467. </div>
  468. </section>
  469. <?php else /* !$supplierIsNew */ : ?>
  470. <section class="container-fluid">
  471. <div class="row my-3">
  472. <div class="col">
  473. <h1>
  474. <div class="btn-group float-end" role="group">
  475. <?php if (isset($previousEvent)) : ?>
  476. <a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $previousEvent); ?>" title="Événement précédent">
  477. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-left" viewBox="0 0 16 16">
  478. <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"/>
  479. </svg>
  480. </a>
  481. <?php endif; ?>
  482. <?php /* ?>
  483. <a class="btn btn-outline-primary d-none d-sm-inline" href="<?php echo generateUrl($supplier, $event); ?>" title="Cet événement">
  484. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-link" viewBox="0 0 16 16">
  485. <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"/>
  486. <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"/>
  487. </svg>
  488. </a>
  489. <?php */ ?>
  490. <?php if (isset($nextEvent)) : ?>
  491. <a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $nextEvent); ?>" title="Événement suivant">
  492. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-right" viewBox="0 0 16 16">
  493. <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"/>
  494. </svg>
  495. </a>
  496. <?php endif; ?>
  497. </div>
  498. <?php echo $config[$supplier]['title']; ?>
  499. <?php echo $config[$supplier]['subtitle']; ?>
  500. </h1>
  501. <?php if (!empty($config[$supplier]['description'])) : ?>
  502. <p class="lead"><?php echo $config[$supplier]['description']; ?></p>
  503. <?php endif; ?>
  504. </div>
  505. </div>
  506. </section>
  507. <section class="container-fluid">
  508. <div class="row g-3">
  509. <form class="js-localremember bg-dark text-light" action="<?php echo generateUrl($supplier); ?>" method="post">
  510. <div class="row my-3">
  511. <label for="title" class="col-sm-2 col-form-label">Nom</label>
  512. <div class="col-sm-10">
  513. <input class="form-control" type="text" name="name" required placeholder="Nom" />
  514. </div>
  515. </div>
  516. <?php if (!empty($config[$supplier]['choices'])) : ?>
  517. <div class="row mb-3">
  518. <label for="title" class="col-sm-2 col-form-label">Choix</label>
  519. <div class="col-sm-10">
  520. <div class="btn-group" role="group">
  521. <?php foreach ($config[$supplier]['choices'] as $index => $choice) : ?>
  522. <input type="radio" class="btn-check" id="<?php printf('option%d', $index); ?>" autocomplete="off" name="choice" value="<?php echo $choice; ?>" />
  523. <label class="btn btn-outline-light" for="<?php printf('option%d', $index); ?>"><?php echo $choice; ?></label>
  524. <?php endforeach; ?>
  525. </div>
  526. </div>
  527. </div>
  528. <?php endif; ?>
  529. <div class="row">
  530. <div class="col mb-3">
  531. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  532. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  533. <?php if (empty($config[$supplier]['choices'])) : ?>
  534. <input type="hidden" name="choice" value="" />
  535. <?php endif; ?>
  536. <button class="btn btn-primary" type="submit" name="action" value="insert">Commander</button>
  537. </div>
  538. </div>
  539. </form>
  540. </div>
  541. </section>
  542. <section class="container-fluid">
  543. <div class="row my-3">
  544. <?php if (!empty($items)) : ?>
  545. <div class="col-12">
  546. <div class="table-responsive">
  547. <table class="table table-striped table-hover align-middle">
  548. <thead>
  549. <tr>
  550. <th scope="col">
  551. Nom
  552. </th>
  553. <?php if (!empty($config[$supplier]['choices'])) : ?>
  554. <th scope="col">
  555. Choix
  556. </th>
  557. <?php endif; ?>
  558. <th scope="col">
  559. &nbsp;
  560. </th>
  561. </tr>
  562. </thead>
  563. <tbody>
  564. <?php foreach ($items as $item) : ?>
  565. <tr>
  566. <td>
  567. <?php echo $item['name']; ?>
  568. </td>
  569. <?php if (!empty($config[$supplier]['choices'])) : ?>
  570. <td>
  571. <?php if (!empty($item['choice'])) : ?>
  572. <?php echo $item['choice']; ?>
  573. <?php endif; ?>
  574. </td>
  575. <?php endif; ?>
  576. <td>
  577. <form onsubmit="return confirm('Souhaitez-vous vraiment annuler cette commande ?');">
  578. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  579. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  580. <input type="hidden" name="name" value="<?php echo $item['name']; ?>" />
  581. <input type="hidden" name="choice" value="<?php echo $item['choice']; ?>" />
  582. <button class="btn btn-secondary float-end" type="submit" name="action" value="delete">Annuler</button>
  583. </form>
  584. </td>
  585. </tr>
  586. <?php endforeach; ?>
  587. </tbody>
  588. </table>
  589. </div>
  590. </div>
  591. <?php endif; ?>
  592. <div class="col-12">
  593. <div class="accordion accordion-flush">
  594. <div class="accordion-item">
  595. <h2 class="accordion-header">
  596. <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#accordion1" aria-expanded="false">
  597. Commandes
  598. <span class="badge bg-primary rounded-pill ms-1"><?php echo count($items); ?></span>
  599. </button>
  600. </h2>
  601. <div id="accordion1" class="accordion-collapse collapse">
  602. <div class="accordion-body">
  603. <ul class="list-group">
  604. <?php foreach ($stats as $choice => $count) : ?>
  605. <li class="list-group-item d-flex justify-content-between align-items-center">
  606. <?php echo $choice; ?>
  607. <span class="badge bg-secondary rounded-pill"><?php echo $count; ?></span>
  608. </li>
  609. <?php endforeach; ?>
  610. </ul>
  611. </div>
  612. </div>
  613. </div>
  614. </div>
  615. </div>
  616. </div>
  617. </section>
  618. <?php endif; /* $supplierIsNew */ ?>
  619. <?php endif; /* $isConfig*/ ?>
  620. <?php endif; ?>
  621. </main>
  622. <div class="modal fade" id="linkModal" tabindex="-1" aria-hidden="true">
  623. <div class="modal-dialog">
  624. <div class="modal-content">
  625. <div class="modal-header">
  626. <h5 class="modal-title">Lien</h5>
  627. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Fermer"></button>
  628. </div>
  629. <div class="modal-body">
  630. <div class="container-fluid">
  631. <div class="row g-3">
  632. <div class="col-12">
  633. Adresse web
  634. </div>
  635. <div class="col-12 text-center">
  636. <a href="<?php echo $linkUrl; ?>"><tt id="linkURL"><?php echo $linkUrl; ?></tt></a>
  637. <button class="btn btn-outline-dark js-clipboard" type="button" role="button" data-clipboard-target="#linkURL" data-bs-toggle="tooltip" data-bs-trigger="manual">
  638. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-clipboard" viewBox="0 0 16 16">
  639. <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"/>
  640. <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"/>
  641. </svg>
  642. </button>
  643. </div>
  644. <div class="col-12">
  645. QR Code
  646. </div>
  647. <div class="col-12">
  648. <div id="linkQRCode"></div>
  649. </div>
  650. </div>
  651. </div>
  652. </div>
  653. <div class="modal-footer">
  654. <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Fermer</button>
  655. </div>
  656. </div>
  657. </div>
  658. </div>
  659. <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>
  660. <script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
  661. <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.8/clipboard.min.js"></script>
  662. <?php if ($isConfig) : ?>
  663. <script src="https://cdn.ckeditor.com/ckeditor5/31.0.0/classic/ckeditor.js"></script>
  664. <script>
  665. document.querySelectorAll('.js-ckeditor').forEach(function (element) {
  666. ClassicEditor.create(element).catch(error => { console.error(error); });
  667. });
  668. </script>
  669. <?php endif; ?>
  670. <script>
  671. document.addEventListener('DOMContentLoaded', function () {
  672. document.querySelectorAll('.js-localremember').forEach(function (form) {
  673. const fields = [ 'name', 'choice' ];
  674. form.addEventListener('submit', function (event) {
  675. fields.forEach(function (field) {
  676. window.localStorage.setItem('mon_panier_bio_' + field, form.elements[field].value);
  677. });
  678. });
  679. fields.forEach(function (field) {
  680. if (
  681. (form.elements[field].value === '')
  682. && (window.localStorage.getItem('mon_panier_bio_' + field) !== null)
  683. ) {
  684. form.elements[field].value = window.localStorage.getItem('mon_panier_bio_' + field);
  685. }
  686. });
  687. });
  688. document.querySelectorAll('.js-closealerts').forEach(function (element) {
  689. element.addEventListener('input', function (event) {
  690. if (event.target.value !== '') {
  691. document.querySelectorAll('.alert').forEach(function (alertElement) {
  692. var alert = bootstrap.Alert.getOrCreateInstance(alertElement)
  693. alert.close();
  694. });
  695. }
  696. });
  697. });
  698. var qrcode = new QRCode('linkQRCode', {
  699. text: document.getElementById('linkURL').innerText,
  700. width: 300,
  701. height: 300,
  702. colorDark : '#000000',
  703. colorLight : '#ffffff',
  704. correctLevel : QRCode.CorrectLevel.H,
  705. });
  706. document.querySelector('#linkQRCode img').classList.add('img-fluid', 'mx-auto', 'd-block');
  707. var clipboard = new ClipboardJS('.js-clipboard');
  708. clipboard.on('success', function (event) {
  709. var tooltip = new bootstrap.Tooltip(event.trigger, {
  710. title: 'Copié dans le presse-papier'
  711. });
  712. tooltip.show();
  713. });
  714. }, false);
  715. </script>
  716. </body>
  717. </html>