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.

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