From ddbdbd93bb2fa7dd833b36c35a361cc504723c81 Mon Sep 17 00:00:00 2001
From: Matthieu Leproux
Date: Wed, 28 Jul 2021 16:52:44 +0200
Subject: [PATCH] fixes #1381 orphans calculation is done only when needed by
ajax call
---
admin/cat_modify.php | 68 +----------
admin/themes/default/template/cat_modify.tpl | 64 ++++++++---
include/ws_functions/pwg.php | 112 +++++++++++++++++++
ws.php | 12 ++
4 files changed, 176 insertions(+), 80 deletions(-)
diff --git a/admin/cat_modify.php b/admin/cat_modify.php
index a819c1973..a4dd56084 100644
--- a/admin/cat_modify.php
+++ b/admin/cat_modify.php
@@ -189,68 +189,6 @@ $subcat_ids = get_subcat_ids(array($category['id']));
$category['nb_subcats'] = count($subcat_ids) - 1;
-// total number of images under this category (including sub-categories)
-$query = '
-SELECT
- DISTINCT(image_id)
- FROM '.IMAGE_CATEGORY_TABLE.'
- WHERE category_id IN ('.implode(',', $subcat_ids).')
- ;';
-$image_ids_recursive = query2array($query, null, 'image_id');
-
-$category['nb_images_recursive'] = count($image_ids_recursive);
-
-// number of images that would become orphan on album deletion
-$category['nb_images_becoming_orphan'] = 0;
-$category['nb_images_associated_outside'] = 0;
-
-if ($category['nb_images_recursive'] > 0)
-{
- // if we don't have "too many" photos, it's faster to compute the orphans with MySQL
- if ($category['nb_images_recursive'] < 1000)
- {
- $query = '
-SELECT
- DISTINCT(image_id)
- FROM '.IMAGE_CATEGORY_TABLE.'
- WHERE category_id NOT IN ('.implode(',', $subcat_ids).')
- AND image_id IN ('.implode(',', $image_ids_recursive).')
-;';
-
- $image_ids_associated_outside = query2array($query, null, 'image_id');
- $category['nb_images_associated_outside'] = count($image_ids_associated_outside);
-
- $image_ids_becoming_orphan = array_diff($image_ids_recursive, $image_ids_associated_outside);
- $category['nb_images_becoming_orphan'] = count($image_ids_becoming_orphan);
- }
- // else it's better to avoid sending a huge SQL request, we compute the orphan list with PHP
- else
- {
- $image_ids_recursive_keys = array_flip($image_ids_recursive);
-
- $query = '
-SELECT
- image_id
- FROM '.IMAGE_CATEGORY_TABLE.'
- WHERE category_id NOT IN ('.implode(',', $subcat_ids).')
-;';
- $image_ids_associated_outside = query2array($query, null, 'image_id');
- $image_ids_not_orphan = array();
-
- foreach ($image_ids_associated_outside as $image_id)
- {
- if (isset($image_ids_recursive_keys[$image_id]))
- {
- $image_ids_not_orphan[] = $image_id;
- }
- }
-
- $category['nb_images_associated_outside'] = count(array_unique($image_ids_not_orphan));
- $image_ids_becoming_orphan = array_diff($image_ids_recursive, $image_ids_not_orphan);
- $category['nb_images_becoming_orphan'] = count($image_ids_becoming_orphan);
- }
-}
-
// Navigation path
$navigation = get_cat_display_name_cache(
$category['uppercats'],
@@ -391,9 +329,9 @@ $template->assign(
array(
'CATEGORY_FULLNAME' => trim(strip_tags($navigation)),
'NB_SUBCATS' => $category['nb_subcats'],
- 'NB_IMAGES_RECURSIVE' => $category['nb_images_recursive'],
- 'NB_IMAGES_BECOMING_ORPHAN' => $category['nb_images_becoming_orphan'],
- 'NB_IMAGES_ASSOCIATED_OUTSIDE' => $category['nb_images_associated_outside'],
+ // 'NB_IMAGES_RECURSIVE' => $category['nb_images_recursive'],
+ // 'NB_IMAGES_BECOMING_ORPHAN' => $category['nb_images_becoming_orphan'],
+ // 'NB_IMAGES_ASSOCIATED_OUTSIDE' => $category['nb_images_associated_outside'],
)
);
diff --git a/admin/themes/default/template/cat_modify.tpl b/admin/themes/default/template/cat_modify.tpl
index 9de57c3af..146f6734f 100644
--- a/admin/themes/default/template/cat_modify.tpl
+++ b/admin/themes/default/template/cat_modify.tpl
@@ -1,11 +1,17 @@
{combine_script id='common' load='footer' path='admin/themes/default/js/common.js'}
{include file='include/colorbox.inc.tpl'}
{combine_script id='LocalStorageCache' load='footer' path='admin/themes/default/js/LocalStorageCache.js'}
+{combine_script id='jquery.confirm' load='footer' require='jquery' path='themes/default/js/plugins/jquery-confirm.min.js'}
+{* {combine_script id='cat_modify' load='footer' path='admin/themes/default/js/cat_modify.js'} *}
{combine_script id='jquery.selectize' load='footer' path='themes/default/js/plugins/selectize.min.js'}
{combine_css id='jquery.selectize' path="themes/default/js/plugins/selectize.{$themeconf.colorscheme}.css"}
{footer_script}
+const has_images_associated_outside = '{"delete album and all %d photos, even the %d associated to other albums"|@translate|escape:javascript}';
+const has_images_becomming_orphans = '{'delete album and the %d orphan photos'|@translate|escape:javascript}';
+const has_images_recursives = '{'delete only album, not photos'|@translate|escape:javascript}';
+
{* *}
var categoriesCache = new CategoriesCache({
serverKey: '{$CACHE_KEYS.categories}',
@@ -98,6 +104,40 @@ jQuery(document).ready(function() {
e.preventDefault();
});
+ $(".deleteAlbum").on("click", function() {
+ $.ajax({
+ url: "ws.php?format=json&method=pwg.images.calculateOrphansOnAlbumDeletion",
+ type: "GET",
+ data: {
+ category_id: {$CAT_ID},
+ },
+ success: function (raw_data) {
+ let data = JSON.parse(raw_data).result[0]
+ console.log(data);
+ if (data.nb_images_recursive == 0) {
+ $(".delete_popin ul").hide();
+ } else {
+ if (data.nb_images_associated_outside == 0) {
+ $("#IMAGES_ASSOCIATED_OUTSIDE").hide();
+ } else {
+ $("#IMAGES_ASSOCIATED_OUTSIDE .innerText").html("");
+ $("#IMAGES_ASSOCIATED_OUTSIDE .innerText").append(has_images_associated_outside.replace('%d', data.nb_images_recursive).replace('%d', data.nb_images_associated_outside));
+ }
+ if (data.nb_images_becoming_orphan == 0) {
+ $("#IMAGES_BECOMING_ORPHAN").hide();
+ } else {
+ $("#IMAGES_BECOMING_ORPHAN .innerText").html("");
+ $("#IMAGES_BECOMING_ORPHAN .innerText").append(has_images_becomming_orphans.replace('%d', data.nb_images_becoming_orphan));
+ }
+
+ }
+ },
+ error: function(message) {
+ console.log(message);
+ }
+ });
+ });
+
jQuery(".deleteAlbum").click(function() {
jQuery.colorbox({
inline:true,
@@ -186,6 +226,10 @@ function cropImage() {
.delete_popin p.popin-actions {
margin-top:30px;
}
+
+#cboxContent {
+ background: none;
+}
{/html_style}
@@ -354,25 +398,15 @@ function cropImage() {
{'Delete album "%s" and its %d sub-albums.'|translate:$CATEGORIES_NAV:$NB_SUBCATS}
{/if}
-
-{if $NB_IMAGES_RECURSIVE > 0}
-
-{/if}
-
+
{'Confirm deletion'|translate}
{'Cancel'|translate}
-
-{* $U_DELETE *}
diff --git a/include/ws_functions/pwg.php b/include/ws_functions/pwg.php
index d05b30b9e..2e953f27b 100644
--- a/include/ws_functions/pwg.php
+++ b/include/ws_functions/pwg.php
@@ -555,4 +555,116 @@ SELECT
fclose($f);
}
+
+/**
+ * API method
+ * Return the number of orphan photos if an album is deleted
+ */
+
+function ws_images_calculateOrphansOnAlbumDeletion($param, &$service)
+{
+ global $conf;
+
+ $category_id = $param['category_id'][0];
+
+ $query = '
+SELECT DISTINCT
+ category_id
+ FROM
+ '.IMAGE_CATEGORY_TABLE.'
+ WHERE
+ category_id = '.$category_id.'
+ LIMIT 1';
+ $result = pwg_query($query);
+ $category['has_images'] = pwg_db_num_rows($result)>0 ? true : false;
+
+ // number of sub-categories
+ $subcat_ids = get_subcat_ids(array($category_id));
+
+ $category['nb_subcats'] = count($subcat_ids) - 1;
+
+ // total number of images under this category (including sub-categories)
+ $query = '
+SELECT DISTINCT
+ (image_id)
+ FROM
+ '.IMAGE_CATEGORY_TABLE.'
+ WHERE
+ category_id IN ('.implode(',', $subcat_ids).')
+ ;';
+ $image_ids_recursive = query2array($query, null, 'image_id');
+
+ $category['nb_images_recursive'] = count($image_ids_recursive);
+
+ // number of images that would become orphan on album deletion
+ $category['nb_images_becoming_orphan'] = 0;
+ $category['nb_images_associated_outside'] = 0;
+
+ if ($category['nb_images_recursive'] > 0)
+ {
+ // if we don't have "too many" photos, it's faster to compute the orphans with MySQL
+ if ($category['nb_images_recursive'] < 1000)
+ {
+ $query = '
+ SELECT DISTINCT
+ (image_id)
+ FROM
+ '.IMAGE_CATEGORY_TABLE.'
+ WHERE
+ category_id
+ NOT IN
+ ('.implode(',', $subcat_ids).')
+ AND
+ image_id
+ IN
+ ('.implode(',', $image_ids_recursive).')
+ ;';
+
+ $image_ids_associated_outside = query2array($query, null, 'image_id');
+ $category['nb_images_associated_outside'] = count($image_ids_associated_outside);
+
+ $image_ids_becoming_orphan = array_diff($image_ids_recursive, $image_ids_associated_outside);
+ $category['nb_images_becoming_orphan'] = count($image_ids_becoming_orphan);
+ }
+ // else it's better to avoid sending a huge SQL request, we compute the orphan list with PHP
+ else
+ {
+ $image_ids_recursive_keys = array_flip($image_ids_recursive);
+
+ $query = '
+ SELECT
+ image_id
+ FROM
+ '.IMAGE_CATEGORY_TABLE.'
+ WHERE
+ category_id
+ NOT IN
+ ('.implode(',', $subcat_ids).')
+ ;';
+ $image_ids_associated_outside = query2array($query, null, 'image_id');
+ $image_ids_not_orphan = array();
+
+ foreach ($image_ids_associated_outside as $image_id)
+ {
+ if (isset($image_ids_recursive_keys[$image_id]))
+ {
+ $image_ids_not_orphan[] = $image_id;
+ }
+ }
+
+ $category['nb_images_associated_outside'] = count(array_unique($image_ids_not_orphan));
+ $image_ids_becoming_orphan = array_diff($image_ids_recursive, $image_ids_not_orphan);
+ $category['nb_images_becoming_orphan'] = count($image_ids_becoming_orphan);
+ }
+}
+
+ $output[] = array(
+ 'nb_images_associated_outside' => $category['nb_images_associated_outside'],
+ 'nb_images_becoming_orphan' => $category['nb_images_becoming_orphan'],
+ 'nb_images_recursive' => $category['nb_images_recursive'],
+ );
+
+ return $output;
+}
+
?>
\ No newline at end of file
diff --git a/ws.php b/ws.php
index 430d83797..da00ffd2c 100644
--- a/ws.php
+++ b/ws.php
@@ -593,6 +593,18 @@ function ws_addDefaultMethods( $arr )
array('admin_only'=>true, 'post_only'=>true)
);
+ $service->addMethod(
+ 'pwg.images.calculateOrphansOnAlbumDeletion',
+ 'ws_images_calculateOrphansOnAlbumDeletion',
+ array(
+ 'category_id' => array('type'=>WS_TYPE_ID,
+ 'flags'=>WS_PARAM_FORCE_ARRAY),
+ ),
+ 'Return the number of orphan photos if an album is deleted.',
+ $ws_functions_root . 'pwg.php',
+ array('admin_only'=>true)
+ );
+
$service->addMethod(
'pwg.categories.getAdminList',
'ws_categories_getAdminList',