fix: insertion image identique dans document

pull/7/head
rija.propitech 4 months ago
parent b3a4064dc0
commit a32197bb08

@ -43,28 +43,46 @@ function action_ajouter_imagecci_dist($arg = null)
'data' => '',
);
include_spip('action/ajouter_documents');
include_spip('inc/rechercher');
$ajouter_un_document = charger_fonction('ajouter_un_document', 'action');
$default_conf = lire_config('conf_modele_cci/conf_modele_cci', 'doc');
$align = lire_config('conf_modele_cci/conf_alignement_modele_cci', 'left');
foreach ($_FILES['fichiers']['name'] as $index => $name) {
// reconstruction d'un fichier singulier de type $_FILE
$file = array();
$file['name'] = $name;
$file['type'] = $_FILES['fichiers']['type'][$index];
$file['tmp_name'] = $_FILES['fichiers']['tmp_name'][$index];
$file['error'] = $_FILES['fichiers']['error'][$index];
$file['size'] = $_FILES['fichiers']['size'][$index];
// surcharge pour spip
$file['titrer'] = false;
$file['mode'] = 'image';
// insertion du document
$res = $ajouter_un_document('new', $file, $arg[0], $arg[1], "image");
if (is_numeric($res)) {
$res = "<" . $default_conf . "$res|$align>";
// on remplace le nom par le md5 du fichier
$md5name = md5_file($_FILES['fichiers']['tmp_name'][$index]);
$name = preg_replace('/^(.+)(\.\w+)$/', $md5name . '$2', $name);
$retour = '';
$idDoc = '';
// vérifier si le document n'existe pas déjà et utiliser l'identifiant
// du document existant au lieu de créer un nouveau document
$resDocumentExistant = recherche_en_base($name, 'document');
if (count($resDocumentExistant['document']) == 1) {
$idDoc = array_keys($resDocumentExistant['document'])[0];
}
// document à ajouter
if (!is_numeric($idDoc)) {
// reconstruction d'un fichier singulier de type $_FILE
$file = array();
$file['name'] = $name;
$file['type'] = $_FILES['fichiers']['type'][$index];
$file['tmp_name'] = $_FILES['fichiers']['tmp_name'][$index];
$file['error'] = $_FILES['fichiers']['error'][$index];
$file['size'] = $_FILES['fichiers']['size'][$index];
// surcharge pour spip
$file['titrer'] = false;
$file['mode'] = 'image';
// insertion du document
$idDoc = $ajouter_un_document('new', $file, $arg[0], $arg[1], "image");
}
if (is_numeric($idDoc)) {
$retour = "<" . $default_conf . "$idDoc|$align>";
} else {
$res = "[cci] Erreur $res";
$retour = "[cci] Erreur $idDoc";
}
$resultat['data'] .= $res;
$resultat['data'] .= $retour;
}
echo json_encode($resultat);

Loading…
Cancel
Save