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.

766 lines
30 KiB

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