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.

750 lines
29 KiB

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