240240-- *************************************************
241241local function buildCatHierarchy (allCats )
242242 -- convert flat list of categories to hierarchical table with children tables
243- log :info (" buildCatHierarchy - allCats\n " .. utils .serialiseVar (allCats ))
243+ -- log:info("buildCatHierarchy - allCats\n" .. utils.serialiseVar(allCats))
244244 -- 1. Create all nodes (no hierarchy yet)
245245 local nodes = {}
246246 local roots = {}
@@ -273,7 +273,7 @@ local function buildCatHierarchy(allCats)
273273 end
274274 end
275275 end
276- log :info (" buildCatHierarchy - hierarchy\n " .. utils .serialiseVar (roots ))
276+ -- log:info("buildCatHierarchy - hierarchy\n" .. utils.serialiseVar(roots))
277277 return roots
278278end
279279
@@ -1058,6 +1058,11 @@ function PiwigoAPI.ConnectionChange(propertyTable)
10581058 propertyTable .cookieHeader = nil
10591059 propertyTable .userStatus = " "
10601060 propertyTable .token = " "
1061+
1062+ if (propertyTable .savedHost ~= propertyTable .host ) or (propertyTable .savedUsername ~= propertyTable .userName ) then
1063+ propertyTable .unsavedConnectionChanges = true
1064+ end
1065+
10611066end
10621067
10631068-- *************************************************
@@ -1247,6 +1252,7 @@ function PiwigoAPI.login(propertyTable)
12471252 " Error: Piwigo server URL, username or password is empty." )
12481253 return false
12491254 end
1255+ -- set default connectionname if blank
12501256 if not (propertyTable .LR_publish_connectionName ) or propertyTable .LR_publish_connectionName == " " then
12511257 propertyTable .LR_publish_connectionName = propertyTable .userName .. " @" .. propertyTable .host
12521258 end
@@ -1519,7 +1525,7 @@ function PiwigoAPI.importAlbums(propertyTable)
15191525 -- log:info("PiwigoAPI:importAlbums - allCats\n" .. utils.serialiseVar(allCats) )
15201526 -- hierarchical table of categories
15211527 local catHierarchy = buildCatHierarchy (allCats )
1522- log :info (" PiwigoAPI:importAlbums - catHierarchy\n " .. utils .serialiseVar (catHierarchy ))
1528+ -- log:info("PiwigoAPI:importAlbums - catHierarchy\n" .. utils.serialiseVar(catHierarchy))
15231529
15241530 local statusData = {
15251531 existing = 0 ,
@@ -2026,11 +2032,12 @@ end
20262032-- *************************************************
20272033function PiwigoAPI .associateImageToCategory (propertyTable , imageId , categoryId )
20282034 -- Associates an existing Piwigo image to an additional category without re-uploading
2029- log :info (" PiwigoAPI.associateImageToCategory - imageId: " .. tostring (imageId ) .. " , categoryId: " .. tostring (categoryId ))
2030-
2035+ log :info (" PiwigoAPI.associateImageToCategory - imageId: " ..
2036+ tostring (imageId ) .. " , categoryId: " .. tostring (categoryId ))
2037+
20312038 local callStatus = { status = false }
20322039 local rv
2033-
2040+
20342041 -- Check connection
20352042 if not propertyTable .Connected then
20362043 rv = PiwigoAPI .login (propertyTable )
@@ -2039,37 +2046,38 @@ function PiwigoAPI.associateImageToCategory(propertyTable, imageId, categoryId)
20392046 return callStatus
20402047 end
20412048 end
2042-
2049+
20432050 local params = {
2044- { name = " method" , value = " pwg.images.setInfo" },
2045- { name = " image_id" , value = tostring (imageId ) },
2046- { name = " categories" , value = tostring (categoryId ) },
2051+ { name = " method" , value = " pwg.images.setInfo" },
2052+ { name = " image_id" , value = tostring (imageId ) },
2053+ { name = " categories" , value = tostring (categoryId ) },
20472054 { name = " multiple_value_mode" , value = " append" },
2048- { name = " pwg_token" , value = propertyTable .token }
2055+ { name = " pwg_token" , value = propertyTable .token }
20492056 }
2050-
2057+
20512058 local postResponse = PiwigoAPI .httpPostMultiPart (propertyTable , params )
2052-
2059+
20532060 if postResponse .status then
20542061 callStatus .status = true
20552062 callStatus .remoteid = imageId
20562063 callStatus .remoteurl = string.format (" %s/picture.php?/%s/category/%s" , propertyTable .host , imageId , categoryId )
20572064 else
20582065 callStatus .statusMsg = postResponse .statusMsg or " Association failed"
20592066 end
2060-
2067+
20612068 return callStatus
20622069end
20632070
20642071-- *************************************************
20652072function PiwigoAPI .dissociateImageFromCategory (propertyTable , imageId , categoryId )
20662073 -- Removes an image from a specific category WITHOUT deleting the image
20672074 -- Uses pwg.images.setInfo to update categories list (excluding the target category)
2068- log :info (" PiwigoAPI.dissociateImageFromCategory - imageId: " .. tostring (imageId ) .. " , categoryId: " .. tostring (categoryId ))
2069-
2075+ log :info (" PiwigoAPI.dissociateImageFromCategory - imageId: " ..
2076+ tostring (imageId ) .. " , categoryId: " .. tostring (categoryId ))
2077+
20702078 local callStatus = { status = false }
20712079 local rv
2072-
2080+
20732081 -- Check connection
20742082 if not propertyTable .Connected then
20752083 rv = PiwigoAPI .login (propertyTable )
@@ -2078,58 +2086,58 @@ function PiwigoAPI.dissociateImageFromCategory(propertyTable, imageId, categoryI
20782086 return callStatus
20792087 end
20802088 end
2081-
2089+
20822090 -- First, get current categories for this image
20832091 local checkStatus = PiwigoAPI .checkPhoto (propertyTable , imageId )
20842092 if not checkStatus .status then
20852093 callStatus .statusMsg = " Cannot find image " .. tostring (imageId ) .. " on Piwigo"
20862094 return callStatus
20872095 end
2088-
2096+
20892097 local imageDets = checkStatus .imageDets
20902098 local currentCategories = imageDets .categories or {}
2091-
2099+
20922100 log :info (" PiwigoAPI.dissociateImageFromCategory - image currently in " .. # currentCategories .. " categories" )
2093-
2101+
20942102 -- Build new categories list excluding the target category
20952103 local newCategoryIds = {}
20962104 for _ , cat in ipairs (currentCategories ) do
20972105 if tostring (cat .id ) ~= tostring (categoryId ) then
20982106 table.insert (newCategoryIds , tostring (cat .id ))
20992107 end
21002108 end
2101-
2109+
21022110 log :info (" PiwigoAPI.dissociateImageFromCategory - remaining categories: " .. # newCategoryIds )
2103-
2111+
21042112 -- If image would be orphaned (no remaining categories), delete it entirely
21052113 if # newCategoryIds == 0 then
21062114 log :info (" PiwigoAPI.dissociateImageFromCategory - image would be orphaned, deleting entirely" )
21072115 return PiwigoAPI .deletePhoto (propertyTable , categoryId , imageId , callStatus )
21082116 end
2109-
2117+
21102118 -- Update image with new categories list (replaces all associations)
21112119 local categoriesStr = table.concat (newCategoryIds , " ;" )
2112-
2120+
21132121 local params = {
2114- { name = " method" , value = " pwg.images.setInfo" },
2115- { name = " image_id" , value = tostring (imageId ) },
2116- { name = " categories" , value = categoriesStr },
2122+ { name = " method" , value = " pwg.images.setInfo" },
2123+ { name = " image_id" , value = tostring (imageId ) },
2124+ { name = " categories" , value = categoriesStr },
21172125 { name = " multiple_value_mode" , value = " replace" },
2118- { name = " pwg_token" , value = propertyTable .token }
2126+ { name = " pwg_token" , value = propertyTable .token }
21192127 }
2120-
2128+
21212129 log :info (" PiwigoAPI.dissociateImageFromCategory - new categories string: " .. categoriesStr )
2122-
2130+
21232131 local postResponse = PiwigoAPI .httpPostMultiPart (propertyTable , params )
2124-
2132+
21252133 if postResponse .status then
21262134 callStatus .status = true
21272135 log :info (" PiwigoAPI.dissociateImageFromCategory - success" )
21282136 else
21292137 callStatus .statusMsg = postResponse .statusMsg or " Dissociation failed"
21302138 log :info (" PiwigoAPI.dissociateImageFromCategory - failed: " .. callStatus .statusMsg )
21312139 end
2132-
2140+
21332141 return callStatus
21342142end
21352143
0 commit comments