plegall / Piwigo-community

11 stars 23 forks source link

Upload-Action overwrites synchronized metadata unconditianally #56

Open Louisonix opened 3 years ago

Louisonix commented 3 years ago

Piwigo offers the possibility to synchronize image metadata (exif,iptc) into database fields. One can sync fields from iptc to database fields. It's enabled with the following config example:

// use_iptc: Use IPTC data during database synchronization with files
// metadata
$conf['use_iptc'] = true;

// use_iptc_mapping : in which IPTC fields will Piwigo find image
// information ? This setting is used during metadata synchronisation. It
// associates a piwigo_images column name to a IPTC key
$conf['use_iptc_mapping'] = array(
  'date_creation'   => '2#055',
  'author'          => '2#080',
  'name'            => '2#005',
  'comment'         => '2#120'
  );

When uploading images with the admin interface, this works fine.

With the community-upload plugin mechanism, these fields will end up empty, most of the time. Why? Because the the image-upload form offers 3 text inputs for "name", "author" and "description". These fields are written to the database, after the image was uploaded and saved, therefore it overwrites the values taken from the iptc fields.

The following patch only submits fields containing user input, so the metadata is only overwritten when the user enters text into these fields.

diff --git a/plugins/community/add_photos.tpl b/plugins/community/add_photos.tpl
index 0152d3b..d463fbb 100644
--- a/plugins/community/add_photos.tpl
+++ b/plugins/community/add_photos.tpl
@@ -290,16 +290,28 @@ var limit_storage = {$limit_storage};
         uploadedPhotos.push(parseInt(data.result.image_id));
         uploadCategory = data.result.category;

+        // Improved handling of settings override: 
+        var postParams = {
+            single_value_mode: "replace",
+            image_id: data.result.image_id
+        };
+
+        if(jQuery("input[name=author]").val().length > 0) {
+          postParams.author = jQuery("input[name=author]").val();
+        }
+
+        if(jQuery("input[name=name]").val().length > 0) {
+          postParams.name = jQuery("input[name=name]").val();
+        }
+
+        if(jQuery("textarea[name=description]").val().length > 0) {
+          postParams.comment = jQuery("textarea[name=description]").val();
+        }
+
         jQuery.ajax({
           url: rootUrl + "ws.php?format=json&method=pwg.images.setInfo",
           type:"POST",
-          data: {
-            single_value_mode: "replace",
-            image_id: data.result.image_id,
-            author: jQuery("input[name=author]").val(),
-            name: jQuery("input[name=name]").val(),
-            comment: jQuery("textarea[name=description]").val(),
-          },
+          data: postParams,
           dataType: "json",
           success:function(data) {
             console.log(data);
-- 
2.10.5