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.

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