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.

156 lines
4.6 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. $supplier = $_REQUEST['supplier'];
  8. if (!isset($config[$supplier]))
  9. $config[$supplier] = [];
  10. $config[$supplier] = array_merge(
  11. [
  12. 'title' => '%supplier% <small>%event%</small>',
  13. 'description' => '',
  14. 'choices' => [],
  15. 'start' => 'now 00:00:00',
  16. 'end' => '+1 year',
  17. 'frequency' => '1 day',
  18. ],
  19. $config[$supplier]
  20. );
  21. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
  22. $hasEvent = isset($_REQUEST['event']);
  23. if (!$hasEvent) {
  24. $now = new \DateTime('now');
  25. $current = new \DateTime($config[$supplier]['start']);
  26. $frequency = \DateInterval::createFromDateString($config[$supplier]['frequency']);
  27. $maxIterations = 1000;
  28. while (
  29. ($current->getTimestamp() < $now->getTimestamp())
  30. and ($maxIterations-- > 0)
  31. ) $current->add($frequency);
  32. $nextEvent = $current->format('Y-m-d');
  33. header(sprintf('Location: %s?supplier=%s&event=%s', $requestUrl, $supplier, $nextEvent));
  34. die();
  35. }
  36. $event = $_REQUEST['event'];
  37. switch ($action) {
  38. case 'insert' :
  39. case 'delete' :
  40. $isBeginning = (!file_exists(DATA_FILE) or in_array(filesize(DATA_FILE), [ false, 0 ]));
  41. $output = fopen(DATA_FILE, 'a+');
  42. if (!$output) break;
  43. if (!flock($output, LOCK_EX)) break;
  44. if ($isBeginning)
  45. fwrite($output, '<?php' . PHP_EOL);
  46. $item = [];
  47. foreach (['name', 'choice', 'action'] as $field)
  48. $item[$field] = $_REQUEST[$field];
  49. $item['timestamp'] = time();
  50. $item['hash'] = md5(implode([ $item['name'], $item['choice'], ]));
  51. fprintf(
  52. $output,
  53. '$data[%s][%s][] = %s;' . PHP_EOL,
  54. var_export($supplier, true),
  55. var_export($event, true),
  56. str_replace(PHP_EOL, '', var_export($item, true))
  57. );
  58. flock($output, LOCK_UN);
  59. fclose($output);
  60. header(sprintf('Location: %s?supplier=%s&event=%s', $requestUrl, $supplier, $event));
  61. die();
  62. }
  63. if (!isset($data)) $data = [];
  64. if (file_exists(DATA_FILE)) include DATA_FILE;
  65. $items = [];
  66. $allItems = isset($data[$supplier][$event]) ? $data[$supplier][$event] : [];
  67. usort($allItems, function ($a, $b) {
  68. $a = intval($a['timestamp']);
  69. $b = intval($b['timestamp']);
  70. if ($a === $b)
  71. return 0;
  72. return ($a < $b) ? -1 : 1;
  73. });
  74. foreach ($allItems as $item) {
  75. if ($item['action'] === 'insert') {
  76. $items[] = $item;
  77. } elseif ($item['action'] === 'delete') {
  78. foreach ($items as $index => $prevItem)
  79. if ($prevItem['hash'] === $item['hash'])
  80. unset($items[$index]);
  81. }
  82. }
  83. while (preg_match('/%([^%]+)%/i', $config[$supplier]['title'], $match))
  84. $config[$supplier]['title'] = str_replace(
  85. $match[0],
  86. ${$match[1]},
  87. $config[$supplier]['title']
  88. );
  89. ?><!DOCTYPE html>
  90. <html lang="fr">
  91. <head>
  92. <meta charset="UTF-8" />
  93. <meta name="viewport" content="width=device-width, initial-scale=1" />
  94. <title><?php echo strip_tags($config[$supplier]['title']); ?></title>
  95. </head>
  96. <body>
  97. <h1><?php echo $config[$supplier]['title']; ?></h1>
  98. <?php if (!empty($config[$supplier]['description'])) : ?>
  99. <p><?php echo $config[$supplier]['description']; ?></p>
  100. <?php endif; ?>
  101. <ul>
  102. <?php foreach ($items as $item) : ?>
  103. <li>
  104. <form>
  105. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  106. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  107. <input type="hidden" name="name" value="<?php echo $item['name']; ?>" />
  108. <input type="hidden" name="choice" value="<?php echo $item['choice']; ?>" />
  109. <?php echo $item['name']; ?>
  110. <?php if (!empty($item['choice'])) : ?>
  111. <?php echo $item['choice']; ?>
  112. <?php endif; ?>
  113. <button type="submit" name="action" value="delete">supprimer</button>
  114. </form>
  115. </li>
  116. <?php endforeach; ?>
  117. <li>
  118. <form>
  119. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  120. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  121. <input type="text" name="name" required placeholder="nom" />
  122. <?php if (empty($config[$supplier]['choices'])) : ?>
  123. <input type="hidden" name="choice" value="<?php echo $item['choice']; ?>" />
  124. <?php else : ?>
  125. <select name="choice" required placeholder="choix">
  126. <option/>
  127. <?php foreach ($config[$supplier]['choices'] as $choice) : ?>
  128. <option><?php echo $choice; ?></option>
  129. <?php endforeach; ?>
  130. </select>
  131. <?php endif; ?>
  132. <button type="submit" name="action" value="insert">ajouter</button>
  133. </form>
  134. </li>
  135. </ul>
  136. </body>
  137. </html>