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.

410 lines
15 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. if (!$hasEvent) {
  79. $now = new \DateTime('now');
  80. $current = new \DateTime($config[$supplier]['start']);
  81. $frequency = \DateInterval::createFromDateString($config[$supplier]['frequency']);
  82. $maxIterations = 1000;
  83. while (
  84. ($current->getTimestamp() < $now->getTimestamp())
  85. and ($maxIterations-- > 0)
  86. ) $current->add($frequency);
  87. $nextEvent = $current->format('Y-m-d');
  88. header(sprintf('Location: %s?supplier=%s&event=%s', $requestUrl, $supplier, $nextEvent));
  89. die();
  90. }
  91. $event = $_REQUEST['event'];
  92. switch ($action) {
  93. case 'insert' :
  94. case 'delete' :
  95. $isBeginning = (!file_exists(DATA_FILE) or in_array(filesize(DATA_FILE), [ false, 0 ]));
  96. $output = fopen(DATA_FILE, 'a+');
  97. if (!$output) break;
  98. if (!flock($output, LOCK_EX)) break;
  99. if ($isBeginning)
  100. fwrite($output, '<?php' . PHP_EOL);
  101. $item = [];
  102. foreach (['name', 'choice', 'action'] as $field)
  103. $item[$field] = $_REQUEST[$field];
  104. $item['timestamp'] = time();
  105. $item['hash'] = md5(implode([ $item['name'], $item['choice'], ]));
  106. fprintf(
  107. $output,
  108. '$data[%s][%s][] = %s;' . PHP_EOL,
  109. var_export($supplier, true),
  110. var_export($event, true),
  111. str_replace(PHP_EOL, '', var_export($item, true))
  112. );
  113. flock($output, LOCK_UN);
  114. fclose($output);
  115. header(sprintf('Location: %s?supplier=%s&event=%s', $requestUrl, $supplier, $event));
  116. die();
  117. }
  118. if (!isset($data)) $data = [];
  119. if (file_exists(DATA_FILE)) include DATA_FILE;
  120. $items = [];
  121. $allItems = isset($data[$supplier][$event]) ? $data[$supplier][$event] : [];
  122. usort($allItems, function ($a, $b) {
  123. $a = intval($a['timestamp']);
  124. $b = intval($b['timestamp']);
  125. if ($a === $b)
  126. return 0;
  127. return ($a < $b) ? -1 : 1;
  128. });
  129. foreach ($allItems as $item) {
  130. if ($item['action'] === 'insert') {
  131. $items[] = $item;
  132. } elseif ($item['action'] === 'delete') {
  133. foreach ($items as $index => $prevItem)
  134. if ($prevItem['hash'] === $item['hash'])
  135. unset($items[$index]);
  136. }
  137. }
  138. $date = (new \IntlDateFormatter('fr_FR.UTF8', \IntlDateFormatter::FULL, \IntlDateFormatter::NONE, 'Europe/Paris'))->format(new \DateTime($event));
  139. while (preg_match('/%([^%]+)%/i', $config[$supplier]['title'], $match))
  140. $config[$supplier]['title'] = str_replace(
  141. $match[0],
  142. ${$match[1]},
  143. $config[$supplier]['title']
  144. );
  145. $stats = [];
  146. foreach ($items as $item)
  147. if (!empty($item['choice']))
  148. $stats[$item['choice']] += 1;
  149. }
  150. ?><!DOCTYPE html>
  151. <html lang="fr">
  152. <head>
  153. <meta charset="UTF-8" />
  154. <meta name="viewport" content="width=device-width, initial-scale=1" />
  155. <title><?php echo strip_tags($config[$supplier]['title']); ?></title>
  156. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  157. </head>
  158. <body>
  159. <header>
  160. <nav class="navbar navbar-dark bg-dark">
  161. <div class="container-fluid">
  162. <a class="navbar-brand" href="<?php echo $hasSupplier ? sprintf('%s?supplier=%s', $requestUrl, $supplier) : $requestUrl; ?>">
  163. <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">
  164. <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"/>
  165. </svg>
  166. <?php echo $hasSupplier ? $supplier : 'Mon panier bio'; ?>
  167. </a>
  168. <?php if ($hasSupplier) : ?>
  169. <span class="navbar-text text-muted">
  170. <?php if ($isConfig) : ?>
  171. <a class="text-reset" href="<?php printf('%s?supplier=%s', $requestUrl, $supplier); ?>">Retour</a>
  172. <?php else : ?>
  173. <?php if ($hasPassword) : ?>
  174. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-lock" viewBox="0 0 16 16">
  175. <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"/>
  176. </svg>
  177. <?php else : ?>
  178. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-unlock" viewBox="0 0 16 16">
  179. <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"/>
  180. </svg>
  181. <?php endif; ?>
  182. <a tabindex="-1" class="text-reset" href="<?php printf('%s?supplier=%s&action=config', $requestUrl, $supplier); ?>">Configuration</a>
  183. <?php endif; ?>
  184. </span>
  185. <?php endif; ?>
  186. </div>
  187. </nav>
  188. </header>
  189. <main>
  190. <?php if (!$hasSupplier) : ?>
  191. <section class="container-fluid">
  192. <div class="row my-3">
  193. <div class="col">
  194. <div class="alert alert-danger" role="alert">
  195. Pas de fournisseur !
  196. </div>
  197. </div>
  198. </div>
  199. </section>
  200. <?php else : ?>
  201. <?php if ($isConfig) : ?>
  202. <section class="container-fluid">
  203. <div class="row g-3">
  204. <div class="col">
  205. <h1>Configuration</h1>
  206. </div>
  207. </div>
  208. </section>
  209. <section class="container-fluid">
  210. <div class="row g-3">
  211. <form action="<?php printf('%s?supplier=%s', $requestUrl, $supplier); ?>" method="post">
  212. <div class="row mb-3">
  213. <label for="title" class="col-sm-2 col-form-label">Titre</label>
  214. <div class="col-sm-10">
  215. <input class="form-control" type="text" name="title" value="<?php echo htmlspecialchars($config[$supplier]['title']); ?>" />
  216. <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>
  217. </div>
  218. </div>
  219. <div class="row mb-3">
  220. <label for="description" class="col-sm-2 col-form-label">Description</label>
  221. <div class="col-sm-10">
  222. <textarea class="form-control js-ckeditor" name="description" rows="10"><?php echo $config[$supplier]['description']; ?></textarea>
  223. <div class="form-text">La description affichée sous le titre.</div>
  224. </div>
  225. </div>
  226. <div class="row mb-3">
  227. <label for="choices" class="col-sm-2 col-form-label">Choix</label>
  228. <div class="col-sm-10">
  229. <textarea class="form-control" name="choices" rows="5"><?php echo implode(PHP_EOL, $config[$supplier]['choices']); ?></textarea>
  230. <div class="form-text">Les différents choix possibles. Un par ligne. Ou pas.</div>
  231. </div>
  232. </div>
  233. <div class="row mb-3">
  234. <label for="start" class="col-sm-2 col-form-label">Début</label>
  235. <div class="col-sm-10">
  236. <input class="form-control" type="date" name="start" value="<?php echo $config[$supplier]['start']; ?>" />
  237. <div class="form-text">La date du premier événement, si nécessaire de le préciser.</div>
  238. </div>
  239. </div>
  240. <div class="row mb-3">
  241. <label for="frequency" class="col-sm-2 col-form-label">Fréquence</label>
  242. <div class="col-sm-10">
  243. <input class="form-control" type="text" name="frequency" value="<?php echo $config[$supplier]['frequency']; ?>" />
  244. <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>
  245. </div>
  246. </div>
  247. <div class="row mb-3">
  248. <label for="password" class="col-sm-2 col-form-label">Mot de passe</label>
  249. <div class="col-sm-10">
  250. <input class="form-control" type="text" name="password" value="<?php echo $config[$supplier]['password']; ?>" />
  251. <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>
  252. </div>
  253. </div>
  254. <div class="row">
  255. <div class="col mb-3">
  256. <button class="btn btn-primary" type="submit" name="action" value="config">Enregistrer</button>
  257. </div>
  258. </div>
  259. </form>
  260. </div>
  261. </section>
  262. <?php else : ?>
  263. <section class="container-fluid">
  264. <div class="row my-3">
  265. <div class="col">
  266. <h1><?php echo $config[$supplier]['title']; ?></h1>
  267. <?php if (!empty($config[$supplier]['description'])) : ?>
  268. <p class="lead"><?php echo $config[$supplier]['description']; ?></p>
  269. <?php endif; ?>
  270. </div>
  271. </div>
  272. </section>
  273. <section class="container-fluid">
  274. <div class="row g-3">
  275. <form action="<?php printf('%s?supplier=%s', $requestUrl, $supplier); ?>" method="post">
  276. <div class="row mb-3">
  277. <label for="title" class="col-sm-2 col-form-label">Nom</label>
  278. <div class="col-sm-10">
  279. <input class="form-control" type="text" name="name" required placeholder="Nom" />
  280. </div>
  281. </div>
  282. <?php if (!empty($config[$supplier]['choices'])) : ?>
  283. <div class="row mb-3">
  284. <label for="title" class="col-sm-2 col-form-label">Choix</label>
  285. <div class="col-sm-10">
  286. <select class="form-select" name="choice" required>
  287. <option/>
  288. <?php foreach ($config[$supplier]['choices'] as $choice) : ?>
  289. <option><?php echo $choice; ?></option>
  290. <?php endforeach; ?>
  291. </select>
  292. </div>
  293. </div>
  294. <?php endif; ?>
  295. <div class="row">
  296. <div class="col mb-3">
  297. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  298. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  299. <?php if (empty($config[$supplier]['choices'])) : ?>
  300. <input type="hidden" name="choice" value="" />
  301. <?php endif; ?>
  302. <button class="btn btn-primary" type="submit" name="action" value="insert">Commander</button>
  303. </div>
  304. </div>
  305. </form>
  306. </div>
  307. </section>
  308. <section class="container-fluid">
  309. <div class="row my-3">
  310. <div class="col">
  311. <div class="table-responsive">
  312. <table class="table table-striped table-hover align-middle">
  313. <thead>
  314. <tr>
  315. <th scope="col">
  316. Nom
  317. </th>
  318. <?php if (!empty($config[$supplier]['choices'])) : ?>
  319. <th scope="col">
  320. Choix
  321. </th>
  322. <?php endif; ?>
  323. <th scope="col">
  324. &nbsp;
  325. </th>
  326. </tr>
  327. </thead>
  328. <tbody>
  329. <?php foreach ($items as $item) : ?>
  330. <tr>
  331. <td>
  332. <?php echo $item['name']; ?>
  333. </td>
  334. <?php if (!empty($config[$supplier]['choices'])) : ?>
  335. <td>
  336. <?php if (!empty($item['choice'])) : ?>
  337. <?php echo $item['choice']; ?>
  338. <?php endif; ?>
  339. </td>
  340. <?php endif; ?>
  341. <td>
  342. <form onsubmit="return confirm('Souhaitez-vous vraiment annuler cette commande ?');">
  343. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  344. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  345. <input type="hidden" name="name" value="<?php echo $item['name']; ?>" />
  346. <input type="hidden" name="choice" value="<?php echo $item['choice']; ?>" />
  347. <button class="btn btn-secondary float-end" type="submit" name="action" value="delete">Annuler</button>
  348. </form>
  349. </td>
  350. </tr>
  351. <?php endforeach; ?>
  352. </tbody>
  353. <caption>
  354. Commandes&nbsp;<span class="badge bg-primary rounded-pill"><?php echo count($items); ?></span>
  355. <?php foreach ($stats as $choice => $count) : ?>
  356. /
  357. <?php echo $choice; ?>&nbsp;<span class="badge bg-secondary rounded-pill"><?php echo $count; ?></span>
  358. <?php endforeach; ?>
  359. </caption>
  360. </table>
  361. </div>
  362. </div>
  363. </div>
  364. </section>
  365. <?php endif; ?>
  366. <?php endif; ?>
  367. </main>
  368. <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>
  369. <?php if ($isConfig) : ?>
  370. <script src="https://cdn.ckeditor.com/ckeditor5/31.0.0/classic/ckeditor.js"></script>
  371. <script>
  372. document.querySelectorAll('.js-ckeditor').forEach(function (element) {
  373. ClassicEditor.create(element).catch(error => { console.error(error); });
  374. });
  375. </script>
  376. <?php endif; ?>
  377. </body>
  378. </html>