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.

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