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.

446 lines
18 KiB

  1. <?php
  2. $requestUrl = trim(str_replace($_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']), '?');
  3. define('CONFIG_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'config.php');
  4. define('DATA_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'data.php');
  5. if (file_exists(CONFIG_FILE)) require_once CONFIG_FILE;
  6. if (!isset($config)) $config = [];
  7. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
  8. $hasSupplier = isset($_REQUEST['supplier']) and !empty($_REQUEST['supplier']);
  9. $supplier = $_REQUEST['supplier'];
  10. if ($hasSupplier) {
  11. if (!isset($config[$supplier]))
  12. $config[$supplier] = [];
  13. $config[$supplier] = array_merge(
  14. [
  15. 'title' => '%supplier% <small>%date%</small>',
  16. 'description' => '',
  17. 'choices' => [],
  18. 'start' => 'now 00:00:00',
  19. 'frequency' => '1 day',
  20. 'password' => '',
  21. ],
  22. $config[$supplier]
  23. );
  24. $hasPassword = !empty($config[$supplier]['password']);
  25. if ($action === 'config') {
  26. if ($hasPassword) {
  27. if (!isset($_SERVER['PHP_AUTH_USER'])) {
  28. header(sprintf('WWW-Authenticate: Basic realm="mon-panier-bio config %s"', $supplier));
  29. header('HTTP/1.0 401 Unauthorized');
  30. printf('Cette config est protégée par mot de passe !');
  31. exit;
  32. } elseif (
  33. ($_SERVER['PHP_AUTH_USER'] !== $supplier)
  34. or ($_SERVER['PHP_AUTH_PW'] !== $config[$supplier]['password'])
  35. ) {
  36. header('HTTP/1.0 403 Forbidden');
  37. printf('Cette config est protégée par mot de passe !');
  38. exit;
  39. }
  40. }
  41. foreach (array_keys($config[$supplier]) as $key)
  42. if (isset($_REQUEST[$key]))
  43. $config[$supplier][$key] = $_REQUEST[$key];
  44. }
  45. if (empty($config[$supplier]['start']))
  46. $config[$supplier]['start'] = 'now 00:00:00';
  47. if (is_string($config[$supplier]['choices']))
  48. $config[$supplier]['choices'] = explode(PHP_EOL, $config[$supplier]['choices']);
  49. if (!is_array($config[$supplier]['choices']))
  50. $config[$supplier]['choices'] = [];
  51. $config[$supplier]['choices'] = array_filter(
  52. $config[$supplier]['choices'],
  53. function ($choice) {
  54. return is_string($choice) and !empty(trim($choice));
  55. }
  56. );
  57. $config[$supplier]['choices'] = array_map('trim', $config[$supplier]['choices']);
  58. }
  59. $isConfig = false;
  60. if ($action === 'config') {
  61. $output = fopen(CONFIG_FILE, 'w+');
  62. if ($output) {
  63. if (flock($output, LOCK_EX)) {
  64. fwrite($output, '<?php' . PHP_EOL);
  65. fprintf(
  66. $output,
  67. '$config = %s;' . PHP_EOL,
  68. var_export($config, true)
  69. );
  70. flock($output, LOCK_UN);
  71. }
  72. fclose($output);
  73. }
  74. $isConfig = true;
  75. }
  76. $hasEvent = isset($_REQUEST['event']);
  77. if (!$isConfig and $hasSupplier) {
  78. $start = new \DateTime($config[$supplier]['start']);
  79. if (!$hasEvent) {
  80. $now = new \DateTime('now');
  81. $current = clone $start;
  82. $frequency = \DateInterval::createFromDateString($config[$supplier]['frequency']);
  83. $maxIterations = 1000;
  84. while (
  85. ($current->getTimestamp() < $now->getTimestamp())
  86. and ($maxIterations-- > 0)
  87. ) $current->add($frequency);
  88. $nextEvent = $current->format('Y-m-d');
  89. header(sprintf('Location: %s?supplier=%s&event=%s', $requestUrl, $supplier, $nextEvent));
  90. die();
  91. } else {
  92. $event = $_REQUEST['event'];
  93. $current = new \DateTimeImmutable($event);
  94. $frequency = \DateInterval::createFromDateString($config[$supplier]['frequency']);
  95. $previous = $current->sub($frequency);
  96. $previousEvent = $previous->format('Y-m-d');
  97. if (false and !array_key_exists($previousEvent, $data[$supplier]))
  98. unset($previousEvent);
  99. $next = $current->add($frequency);
  100. $nextEvent = $next->format('Y-m-d');
  101. if (false and !array_key_exists($nextEvent, $data[$supplier]))
  102. unset($nextEvent);
  103. }
  104. switch ($action) {
  105. case 'insert' :
  106. case 'delete' :
  107. $isBeginning = (!file_exists(DATA_FILE) or in_array(filesize(DATA_FILE), [ false, 0 ]));
  108. $output = fopen(DATA_FILE, 'a+');
  109. if (!$output) break;
  110. if (!flock($output, LOCK_EX)) break;
  111. if ($isBeginning)
  112. fwrite($output, '<?php' . PHP_EOL);
  113. $item = [];
  114. foreach (['name', 'choice', 'action'] as $field)
  115. $item[$field] = $_REQUEST[$field];
  116. $item['timestamp'] = time();
  117. $item['hash'] = md5(implode([ $item['name'], $item['choice'], ]));
  118. fprintf(
  119. $output,
  120. '$data[%s][%s][] = %s;' . PHP_EOL,
  121. var_export($supplier, true),
  122. var_export($event, true),
  123. str_replace(PHP_EOL, '', var_export($item, true))
  124. );
  125. flock($output, LOCK_UN);
  126. fclose($output);
  127. header(sprintf('Location: %s?supplier=%s&event=%s', $requestUrl, $supplier, $event));
  128. die();
  129. }
  130. if (!isset($data)) $data = [];
  131. if (file_exists(DATA_FILE)) include DATA_FILE;
  132. $items = [];
  133. $allItems = isset($data[$supplier][$event]) ? $data[$supplier][$event] : [];
  134. usort($allItems, function ($a, $b) {
  135. $a = intval($a['timestamp']);
  136. $b = intval($b['timestamp']);
  137. if ($a === $b)
  138. return 0;
  139. return ($a < $b) ? -1 : 1;
  140. });
  141. foreach ($allItems as $item) {
  142. if ($item['action'] === 'insert') {
  143. $items[] = $item;
  144. } elseif ($item['action'] === 'delete') {
  145. foreach ($items as $index => $prevItem)
  146. if ($prevItem['hash'] === $item['hash'])
  147. unset($items[$index]);
  148. }
  149. }
  150. $date = (new \IntlDateFormatter('fr_FR.UTF8', \IntlDateFormatter::FULL, \IntlDateFormatter::NONE, 'Europe/Paris'))->format(new \DateTime($event));
  151. while (preg_match('/%([^%]+)%/i', $config[$supplier]['title'], $match))
  152. $config[$supplier]['title'] = str_replace(
  153. $match[0],
  154. ${$match[1]},
  155. $config[$supplier]['title']
  156. );
  157. $stats = [];
  158. foreach ($items as $item)
  159. if (!empty($item['choice']))
  160. $stats[$item['choice']] += 1;
  161. }
  162. ?><!DOCTYPE html>
  163. <html lang="fr">
  164. <head>
  165. <meta charset="UTF-8" />
  166. <meta name="viewport" content="width=device-width, initial-scale=1" />
  167. <title><?php echo strip_tags($config[$supplier]['title']); ?></title>
  168. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  169. </head>
  170. <body>
  171. <header>
  172. <nav class="navbar navbar-dark bg-dark">
  173. <div class="container-fluid">
  174. <a class="navbar-brand" href="<?php echo $hasSupplier ? sprintf('%s?supplier=%s', $requestUrl, $supplier) : $requestUrl; ?>">
  175. <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">
  176. <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"/>
  177. </svg>
  178. <?php echo $hasSupplier ? $supplier : 'Mon panier bio'; ?>
  179. </a>
  180. <?php if ($hasSupplier) : ?>
  181. <span class="navbar-text text-muted">
  182. <?php if ($isConfig) : ?>
  183. <a class="text-reset" href="<?php printf('%s?supplier=%s', $requestUrl, $supplier); ?>">Retour</a>
  184. <?php else : ?>
  185. <?php if ($hasPassword) : ?>
  186. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-lock" viewBox="0 0 16 16">
  187. <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"/>
  188. </svg>
  189. <?php else : ?>
  190. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-unlock" viewBox="0 0 16 16">
  191. <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"/>
  192. </svg>
  193. <?php endif; ?>
  194. <a tabindex="-1" class="text-reset" href="<?php printf('%s?supplier=%s&action=config', $requestUrl, $supplier); ?>">Configuration</a>
  195. <?php endif; ?>
  196. </span>
  197. <?php endif; ?>
  198. </div>
  199. </nav>
  200. </header>
  201. <main>
  202. <?php if (!$hasSupplier) : ?>
  203. <section class="container-fluid">
  204. <div class="row my-3">
  205. <div class="col">
  206. <div class="alert alert-danger" role="alert">
  207. Pas de fournisseur !
  208. </div>
  209. </div>
  210. </div>
  211. </section>
  212. <?php else : ?>
  213. <?php if ($isConfig) : ?>
  214. <section class="container-fluid">
  215. <div class="row g-3">
  216. <div class="col">
  217. <h1>Configuration</h1>
  218. </div>
  219. </div>
  220. </section>
  221. <section class="container-fluid">
  222. <div class="row g-3">
  223. <form action="<?php printf('%s?supplier=%s', $requestUrl, $supplier); ?>" method="post">
  224. <div class="row mb-3">
  225. <label for="title" class="col-sm-2 col-form-label">Titre</label>
  226. <div class="col-sm-10">
  227. <input class="form-control" type="text" name="title" value="<?php echo htmlspecialchars($config[$supplier]['title']); ?>" />
  228. <div class="form-text">Le titre de la page. <kbd>%supplier%</kbd> est le fournisseur et <kbd>%date%</kbd> la date de l'événement.</div>
  229. </div>
  230. </div>
  231. <div class="row mb-3">
  232. <label for="description" class="col-sm-2 col-form-label">Description</label>
  233. <div class="col-sm-10">
  234. <textarea class="form-control js-ckeditor" name="description" rows="10"><?php echo $config[$supplier]['description']; ?></textarea>
  235. <div class="form-text">La description affichée sous le titre.</div>
  236. </div>
  237. </div>
  238. <div class="row mb-3">
  239. <label for="choices" class="col-sm-2 col-form-label">Choix</label>
  240. <div class="col-sm-10">
  241. <textarea class="form-control" name="choices" rows="5"><?php echo implode(PHP_EOL, $config[$supplier]['choices']); ?></textarea>
  242. <div class="form-text">Les différents choix possibles. Un par ligne. Ou pas.</div>
  243. </div>
  244. </div>
  245. <div class="row mb-3">
  246. <label for="start" class="col-sm-2 col-form-label">Début</label>
  247. <div class="col-sm-10">
  248. <input class="form-control" type="date" name="start" value="<?php echo $config[$supplier]['start']; ?>" />
  249. <div class="form-text">La date du premier événement, si nécessaire de le préciser.</div>
  250. </div>
  251. </div>
  252. <div class="row mb-3">
  253. <label for="frequency" class="col-sm-2 col-form-label">Fréquence</label>
  254. <div class="col-sm-10">
  255. <input class="form-control" type="text" name="frequency" value="<?php echo $config[$supplier]['frequency']; ?>" />
  256. <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>
  257. </div>
  258. </div>
  259. <div class="row mb-3">
  260. <label for="password" class="col-sm-2 col-form-label">Mot de passe</label>
  261. <div class="col-sm-10">
  262. <input class="form-control" type="text" name="password" value="<?php echo $config[$supplier]['password']; ?>" />
  263. <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>
  264. </div>
  265. </div>
  266. <div class="row">
  267. <div class="col mb-3">
  268. <button class="btn btn-primary" type="submit" name="action" value="config">Enregistrer</button>
  269. </div>
  270. </div>
  271. </form>
  272. </div>
  273. </section>
  274. <?php else : ?>
  275. <section class="container-fluid">
  276. <div class="row my-3">
  277. <div class="col">
  278. <h1>
  279. <?php echo $config[$supplier]['title']; ?>
  280. <div class="btn-group float-end" role="group">
  281. <?php if (isset($previousEvent)) : ?>
  282. <a class="btn btn-outline-primary" href="<?php printf('%s?supplier=%s&event=%s', $requestUrl, $supplier, $previousEvent); ?>" title="Événement précédent">
  283. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-left" viewBox="0 0 16 16">
  284. <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"/>
  285. </svg>
  286. </a>
  287. <?php endif; ?>
  288. <a class="btn btn-outline-primary" href="<?php printf('%s?supplier=%s&event=%s', $requestUrl, $supplier, $event); ?>" title="Cet événement">
  289. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-link" viewBox="0 0 16 16">
  290. <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"/>
  291. <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"/>
  292. </svg>
  293. </a>
  294. <?php if (isset($nextEvent)) : ?>
  295. <a class="btn btn-outline-primary" href="<?php printf('%s?supplier=%s&event=%s', $requestUrl, $supplier, $nextEvent); ?>" title="Événement suivant">
  296. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-right" viewBox="0 0 16 16">
  297. <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"/>
  298. </svg>
  299. </a>
  300. <?php endif; ?>
  301. </div>
  302. </h1>
  303. <?php if (!empty($config[$supplier]['description'])) : ?>
  304. <p class="lead"><?php echo $config[$supplier]['description']; ?></p>
  305. <?php endif; ?>
  306. </div>
  307. </div>
  308. </section>
  309. <section class="container-fluid">
  310. <div class="row g-3">
  311. <form action="<?php printf('%s?supplier=%s', $requestUrl, $supplier); ?>" method="post">
  312. <div class="row mb-3">
  313. <label for="title" class="col-sm-2 col-form-label">Nom</label>
  314. <div class="col-sm-10">
  315. <input class="form-control" type="text" name="name" required placeholder="Nom" />
  316. </div>
  317. </div>
  318. <?php if (!empty($config[$supplier]['choices'])) : ?>
  319. <div class="row mb-3">
  320. <label for="title" class="col-sm-2 col-form-label">Choix</label>
  321. <div class="col-sm-10">
  322. <select class="form-select" name="choice" required>
  323. <option/>
  324. <?php foreach ($config[$supplier]['choices'] as $choice) : ?>
  325. <option><?php echo $choice; ?></option>
  326. <?php endforeach; ?>
  327. </select>
  328. </div>
  329. </div>
  330. <?php endif; ?>
  331. <div class="row">
  332. <div class="col mb-3">
  333. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  334. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  335. <?php if (empty($config[$supplier]['choices'])) : ?>
  336. <input type="hidden" name="choice" value="" />
  337. <?php endif; ?>
  338. <button class="btn btn-primary" type="submit" name="action" value="insert">Commander</button>
  339. </div>
  340. </div>
  341. </form>
  342. </div>
  343. </section>
  344. <section class="container-fluid">
  345. <div class="row my-3">
  346. <div class="col">
  347. <div class="table-responsive">
  348. <table class="table table-striped table-hover align-middle">
  349. <thead>
  350. <tr>
  351. <th scope="col">
  352. Nom
  353. </th>
  354. <?php if (!empty($config[$supplier]['choices'])) : ?>
  355. <th scope="col">
  356. Choix
  357. </th>
  358. <?php endif; ?>
  359. <th scope="col">
  360. &nbsp;
  361. </th>
  362. </tr>
  363. </thead>
  364. <tbody>
  365. <?php foreach ($items as $item) : ?>
  366. <tr>
  367. <td>
  368. <?php echo $item['name']; ?>
  369. </td>
  370. <?php if (!empty($config[$supplier]['choices'])) : ?>
  371. <td>
  372. <?php if (!empty($item['choice'])) : ?>
  373. <?php echo $item['choice']; ?>
  374. <?php endif; ?>
  375. </td>
  376. <?php endif; ?>
  377. <td>
  378. <form onsubmit="return confirm('Souhaitez-vous vraiment annuler cette commande ?');">
  379. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  380. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  381. <input type="hidden" name="name" value="<?php echo $item['name']; ?>" />
  382. <input type="hidden" name="choice" value="<?php echo $item['choice']; ?>" />
  383. <button class="btn btn-secondary float-end" type="submit" name="action" value="delete">Annuler</button>
  384. </form>
  385. </td>
  386. </tr>
  387. <?php endforeach; ?>
  388. </tbody>
  389. <caption>
  390. Commandes&nbsp;<span class="badge bg-primary rounded-pill"><?php echo count($items); ?></span>
  391. <?php foreach ($stats as $choice => $count) : ?>
  392. /
  393. <?php echo $choice; ?>&nbsp;<span class="badge bg-secondary rounded-pill"><?php echo $count; ?></span>
  394. <?php endforeach; ?>
  395. </caption>
  396. </table>
  397. </div>
  398. </div>
  399. </div>
  400. </section>
  401. <?php endif; ?>
  402. <?php endif; ?>
  403. </main>
  404. <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>
  405. <?php if ($isConfig) : ?>
  406. <script src="https://cdn.ckeditor.com/ckeditor5/31.0.0/classic/ckeditor.js"></script>
  407. <script>
  408. document.querySelectorAll('.js-ckeditor').forEach(function (element) {
  409. ClassicEditor.create(element).catch(error => { console.error(error); });
  410. });
  411. </script>
  412. <?php endif; ?>
  413. </body>
  414. </html>