Site du proximo, utilisé pour gérer le stock.
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.

convert_json.php 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. echo 'Ouverture du fichier V1...
  3. ';
  4. $file = '../data/stock.json';
  5. $fp = fopen($file, 'r');
  6. $result = json_decode(fread($fp, filesize($file)));
  7. fclose($fp);
  8. function getConvertedTypes($oldTypes) {
  9. $newTypes = [];
  10. $counter = 1;
  11. foreach ($oldTypes as $type) {
  12. array_push($newTypes, (object) ["id" => strval($counter), "name" => $type, "icon" => "file"]);
  13. $counter += 1;
  14. }
  15. return $newTypes;
  16. }
  17. function getConvertedArticles($articles, $newTypes) {
  18. foreach ($articles as $article) {
  19. $article->description = "Pas de description";
  20. foreach ($article->type as $key=>$value) {
  21. $article->type[$key] = getTypeIdFromName($value, $newTypes);
  22. }
  23. }
  24. return $articles;
  25. }
  26. function getTypeIdFromName($name, $types) {
  27. foreach ($types as $type) {
  28. if ($type->name == $name)
  29. return $type->id;
  30. }
  31. }
  32. if ($result) {
  33. echo 'Conversion en cours...
  34. ';
  35. $newTypes = getConvertedTypes($result->types);
  36. $newArticles = getConvertedArticles($result->articles, $newTypes);
  37. $v2File = json_encode(["types"=>$newTypes, "articles"=>$newArticles]);
  38. echo 'Conversion réussie...
  39. ';
  40. echo 'Sauvegarde dans fichier V2...
  41. ';
  42. $fp = fopen('../data/stock-v2.json', 'w');
  43. $result = fwrite($fp, $v2File);
  44. fclose($fp);
  45. echo 'Sauvegarde réussie...
  46. ';
  47. } else {
  48. echo 'Echec!
  49. '; // Allows to create a newline
  50. var_dump($_POST);
  51. }