site-proximo/admin/convert_json.php

57 lines
1.4 KiB
PHP

<?php
echo 'Ouverture du fichier V1...
';
$file = '../data/stock.json';
$fp = fopen($file, 'r');
$result = json_decode(fread($fp, filesize($file)));
fclose($fp);
function getConvertedTypes($oldTypes) {
$newTypes = [];
$counter = 1;
foreach ($oldTypes as $type) {
array_push($newTypes, (object) ["id" => strval($counter), "name" => $type, "icon" => "file"]);
$counter += 1;
}
return $newTypes;
}
function getConvertedArticles($articles, $newTypes) {
foreach ($articles as $article) {
$article->description = "Pas de description";
foreach ($article->type as $key=>$value) {
$article->type[$key] = getTypeIdFromName($value, $newTypes);
}
}
return $articles;
}
function getTypeIdFromName($name, $types) {
foreach ($types as $type) {
if ($type->name == $name)
return $type->id;
}
}
if ($result) {
echo 'Conversion en cours...
';
$newTypes = getConvertedTypes($result->types);
$newArticles = getConvertedArticles($result->articles, $newTypes);
$v2File = json_encode(["types"=>$newTypes, "articles"=>$newArticles]);
echo 'Conversion réussie...
';
echo 'Sauvegarde dans fichier V2...
';
$fp = fopen('../data/stock-v2.json', 'w');
$result = fwrite($fp, $v2File);
fclose($fp);
echo 'Sauvegarde réussie...
';
} else {
echo 'Echec!
'; // Allows to create a newline
var_dump($_POST);
}