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.

397 lines
14 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>%event%</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. while (preg_match('/%([^%]+)%/i', $config[$supplier]['title'], $match))
  139. $config[$supplier]['title'] = str_replace(
  140. $match[0],
  141. ${$match[1]},
  142. $config[$supplier]['title']
  143. );
  144. $stats = [];
  145. foreach ($items as $item)
  146. if (!empty($item['choice']))
  147. $stats[$item['choice']] += 1;
  148. }
  149. ?><!DOCTYPE html>
  150. <html lang="fr">
  151. <head>
  152. <meta charset="UTF-8" />
  153. <meta name="viewport" content="width=device-width, initial-scale=1" />
  154. <title><?php echo strip_tags($config[$supplier]['title']); ?></title>
  155. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  156. </head>
  157. <body>
  158. <header>
  159. <nav class="navbar navbar-dark bg-dark">
  160. <div class="container-fluid">
  161. <a class="navbar-brand" href="<?php echo $requestUrl; ?>">
  162. <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">
  163. <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"/>
  164. </svg>
  165. <?php echo $hasSupplier ? $supplier : 'Mon panier bio'; ?>
  166. </a>
  167. <?php if ($hasSupplier) : ?>
  168. <span class="navbar-text text-muted">
  169. <?php if ($isConfig) : ?>
  170. <a class="text-reset" href="<?php printf('%s?supplier=%s', $requestUrl, $supplier); ?>">Retour</a>
  171. <?php else : ?>
  172. <a tabindex="-1" class="text-reset" href="<?php printf('%s?supplier=%s&action=config', $requestUrl, $supplier); ?>">Configuration</a>
  173. <?php endif; ?>
  174. </span>
  175. <?php endif; ?>
  176. </div>
  177. </nav>
  178. </header>
  179. <main>
  180. <?php if (!$hasSupplier) : ?>
  181. <section class="container-fluid">
  182. <div class="row my-3">
  183. <div class="col">
  184. <div class="alert alert-danger" role="alert">
  185. Pas de fournisseur !
  186. </div>
  187. </div>
  188. </div>
  189. </section>
  190. <?php else : ?>
  191. <?php if ($isConfig) : ?>
  192. <section class="container-fluid">
  193. <div class="row g-3">
  194. <div class="col">
  195. <h1>Configuration</h1>
  196. </div>
  197. </div>
  198. </section>
  199. <section class="container-fluid">
  200. <div class="row g-3">
  201. <form action="<?php printf('%s?supplier=%s', $requestUrl, $supplier); ?>" method="post">
  202. <div class="row mb-3">
  203. <label for="title" class="col-sm-2 col-form-label">Titre</label>
  204. <div class="col-sm-10">
  205. <input class="form-control" type="text" name="title" value="<?php echo $config[$supplier]['title']; ?>" />
  206. <div class="form-text">Le titre de la page. <kbd>%supplier%</kbd> est le fournisseur et <kbd>%event%</kbd> l'événement.</div>
  207. </div>
  208. </div>
  209. <div class="row mb-3">
  210. <label for="description" class="col-sm-2 col-form-label">Description</label>
  211. <div class="col-sm-10">
  212. <textarea class="form-control js-ckeditor" name="description" rows="10"><?php echo $config[$supplier]['description']; ?></textarea>
  213. <div class="form-text">La description affichée sous le titre.</div>
  214. </div>
  215. </div>
  216. <div class="row mb-3">
  217. <label for="choices" class="col-sm-2 col-form-label">Choix</label>
  218. <div class="col-sm-10">
  219. <textarea class="form-control" name="choices" rows="5"><?php echo implode(PHP_EOL, $config[$supplier]['choices']); ?></textarea>
  220. <div class="form-text">Les différents choix possibles. Un par ligne. Ou pas.</div>
  221. </div>
  222. </div>
  223. <div class="row mb-3">
  224. <label for="start" class="col-sm-2 col-form-label">Début</label>
  225. <div class="col-sm-10">
  226. <input class="form-control" type="date" name="start" value="<?php echo $config[$supplier]['start']; ?>" />
  227. <div class="form-text">La date du premier événement, si nécessaire de le préciser.</div>
  228. </div>
  229. </div>
  230. <div class="row mb-3">
  231. <label for="frequency" class="col-sm-2 col-form-label">Fréquence</label>
  232. <div class="col-sm-10">
  233. <input class="form-control" type="text" name="frequency" value="<?php echo $config[$supplier]['frequency']; ?>" />
  234. <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>
  235. </div>
  236. </div>
  237. <div class="row mb-3">
  238. <label for="password" class="col-sm-2 col-form-label">Mot de passe</label>
  239. <div class="col-sm-10">
  240. <input class="form-control" type="text" name="password" value="<?php echo $config[$supplier]['password']; ?>" />
  241. <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>
  242. </div>
  243. </div>
  244. <div class="row">
  245. <div class="col mb-3">
  246. <button class="btn btn-primary" type="submit" name="action" value="config">Enregistrer</button>
  247. </div>
  248. </div>
  249. </form>
  250. </div>
  251. </section>
  252. <?php else : ?>
  253. <section class="container-fluid">
  254. <div class="row my-3">
  255. <div class="col">
  256. <h1><?php echo $config[$supplier]['title']; ?></h1>
  257. <?php if (!empty($config[$supplier]['description'])) : ?>
  258. <p class="lead"><?php echo $config[$supplier]['description']; ?></p>
  259. <?php endif; ?>
  260. </div>
  261. </div>
  262. </section>
  263. <section class="container-fluid">
  264. <div class="row g-3">
  265. <form action="<?php printf('%s?supplier=%s', $requestUrl, $supplier); ?>" method="post">
  266. <div class="row mb-3">
  267. <label for="title" class="col-sm-2 col-form-label">Nom</label>
  268. <div class="col-sm-10">
  269. <input class="form-control" type="text" name="name" required placeholder="Camille Martin" />
  270. </div>
  271. </div>
  272. <?php if (!empty($config[$supplier]['choices'])) : ?>
  273. <div class="row mb-3">
  274. <label for="title" class="col-sm-2 col-form-label">Choix</label>
  275. <div class="col-sm-10">
  276. <select class="form-select" name="choice" required placeholder="choix">
  277. <option/>
  278. <?php foreach ($config[$supplier]['choices'] as $choice) : ?>
  279. <option><?php echo $choice; ?></option>
  280. <?php endforeach; ?>
  281. </select>
  282. </div>
  283. </div>
  284. <?php endif; ?>
  285. <div class="row">
  286. <div class="col mb-3">
  287. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  288. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  289. <?php if (empty($config[$supplier]['choices'])) : ?>
  290. <input type="hidden" name="choice" value="" />
  291. <?php endif; ?>
  292. <button class="btn btn-primary" type="submit" name="action" value="insert">Commander</button>
  293. </div>
  294. </div>
  295. </form>
  296. </div>
  297. </section>
  298. <section class="container-fluid">
  299. <div class="row my-3">
  300. <div class="col">
  301. <div class="table-responsive">
  302. <table class="table table-striped table-hover align-middle">
  303. <thead>
  304. <tr>
  305. <th scope="col">
  306. Nom
  307. </th>
  308. <?php if (!empty($config[$supplier]['choices'])) : ?>
  309. <th scope="col">
  310. Choix
  311. </th>
  312. <?php endif; ?>
  313. <th scope="col">
  314. &nbsp;
  315. </th>
  316. </tr>
  317. </thead>
  318. <tbody>
  319. <?php foreach ($items as $item) : ?>
  320. <tr>
  321. <td>
  322. <?php echo $item['name']; ?>
  323. </td>
  324. <?php if (!empty($config[$supplier]['choices'])) : ?>
  325. <td>
  326. <?php if (!empty($item['choice'])) : ?>
  327. <?php echo $item['choice']; ?>
  328. <?php endif; ?>
  329. </td>
  330. <?php endif; ?>
  331. <td>
  332. <form onsubmit="return confirm('Souhaitez-vous vraiment annuler cette commande ?');">
  333. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  334. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  335. <input type="hidden" name="name" value="<?php echo $item['name']; ?>" />
  336. <input type="hidden" name="choice" value="<?php echo $item['choice']; ?>" />
  337. <button class="btn btn-secondary float-end" type="submit" name="action" value="delete">Annuler</button>
  338. </form>
  339. </td>
  340. </tr>
  341. <?php endforeach; ?>
  342. </tbody>
  343. <caption>
  344. Commandes&nbsp;<span class="badge bg-primary rounded-pill"><?php echo count($items); ?></span>
  345. <?php foreach ($stats as $choice => $count) : ?>
  346. /
  347. <?php echo $choice; ?>&nbsp;<span class="badge bg-secondary rounded-pill"><?php echo $count; ?></span>
  348. <?php endforeach; ?>
  349. </caption>
  350. </table>
  351. </div>
  352. </div>
  353. </div>
  354. </section>
  355. <?php endif; ?>
  356. <?php endif; ?>
  357. </main>
  358. <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>
  359. <script src="https://cdn.ckeditor.com/ckeditor5/31.0.0/classic/ckeditor.js"></script>
  360. <script>
  361. document.querySelectorAll('.js-ckeditor').forEach(function (element) {
  362. ClassicEditor.create(element).catch(error => { console.error(error); });
  363. });
  364. </script>
  365. </body>
  366. </html>