From 1b7781c8669ac98d656b57f4b540ef0fa818a94a Mon Sep 17 00:00:00 2001 From: plegall Date: Wed, 19 Jan 2011 13:19:16 +0000 Subject: feature 1289 added: easy "delete orphan tags" function. On the "tags" administration page, a warning message is displayed if you have at least one orphan tag + direct action to delete them. git-svn-id: http://piwigo.org/svn/trunk@8762 68402e56-0260-453c-a942-63ccdbb3a9ee --- admin.php | 6 ++++ admin/include/functions.php | 48 +++++++++++++++++++++++++++ admin/maintenance.php | 8 ++++- admin/tags.php | 38 +++++++++++++++++++++ admin/themes/default/template/admin.tpl | 10 ++++++ admin/themes/default/template/maintenance.tpl | 7 +--- language/en_UK/admin.lang.php | 3 ++ language/fr_FR/admin.lang.php | 3 ++ 8 files changed, 116 insertions(+), 7 deletions(-) diff --git a/admin.php b/admin.php index 19c1661ed..a3d212164 100644 --- a/admin.php +++ b/admin.php @@ -91,6 +91,7 @@ else $page['errors'] = array(); $page['infos'] = array(); +$page['warnings'] = array(); if (isset($_SESSION['page_infos'])) { @@ -176,6 +177,11 @@ if (count($page['infos']) != 0) $template->assign('infos', $page['infos']); } +if (count($page['warnings']) != 0) +{ + $template->assign('warnings', $page['warnings']); +} + // Add the Piwigo Official menu $template->assign( 'pwgmenu', pwg_URL() ); diff --git a/admin/include/functions.php b/admin/include/functions.php index 6ad6bc7fe..91931cf4d 100644 --- a/admin/include/functions.php +++ b/admin/include/functions.php @@ -363,6 +363,54 @@ DELETE FROM '.USERS_TABLE.' trigger_action('delete_user', $user_id); } +/** + * Deletes all tags linked to no photo + */ +function delete_orphan_tags() +{ + $orphan_tags = get_orphan_tags(); + + if (count($orphan_tags) > 0) + { + $orphan_tag_ids = array(); + foreach ($orphan_tags as $tag) + { + array_push($orphan_tag_ids, $tag['id']); + } + + $query = ' +DELETE + FROM '.TAGS_TABLE.' + WHERE id IN ('.implode(',', $orphan_tag_ids).') +;'; + pwg_query($query); + } +} + +/** + * Get all tags (id + name) linked to no photo + */ +function get_orphan_tags() +{ + $orphan_tags = array(); + + $query = ' +SELECT + id, + name + FROM '.TAGS_TABLE.' + LEFT JOIN '.IMAGE_TAG_TABLE.' ON id = tag_id + WHERE tag_id IS NULL +;'; + $result = pwg_query($query); + while ($row = pwg_db_fetch_assoc($result)) + { + array_push($orphan_tags, $row); + } + + return $orphan_tags; +} + /** * Verifies that the representative picture really exists in the db and * picks up a random represantive if possible and based on config. diff --git a/admin/maintenance.php b/admin/maintenance.php index d6aacc843..d9e7b113d 100644 --- a/admin/maintenance.php +++ b/admin/maintenance.php @@ -55,6 +55,11 @@ switch ($action) update_average_rate(); break; } + case 'delete_orphan_tags' : + { + delete_orphan_tags(); + break; + } case 'history_detail' : { $query = ' @@ -133,13 +138,14 @@ $template->assign( array( 'U_MAINT_CATEGORIES' => $start_url.'categories', 'U_MAINT_IMAGES' => $start_url.'images', + 'U_MAINT_ORPHAN_TAGS' => $start_url.'delete_orphan_tags', 'U_MAINT_HISTORY_DETAIL' => $start_url.'history_detail', 'U_MAINT_HISTORY_SUMMARY' => $start_url.'history_summary', 'U_MAINT_SESSIONS' => $start_url.'sessions', 'U_MAINT_FEEDS' => $start_url.'feeds', 'U_MAINT_DATABASE' => $start_url.'database', 'U_MAINT_C13Y' => $start_url.'c13y', - 'U_MAINT_SEARCH' => $start_url.'search', + 'U_MAINT_SEARCH' => $start_url.'search', 'U_MAINT_COMPILED_TEMPLATES' => $start_url.'compiled-templates', 'U_HELP' => get_root_url().'admin/popuphelp.php?page=maintenance', ) diff --git a/admin/tags.php b/admin/tags.php index 412a57b14..16dea3661 100644 --- a/admin/tags.php +++ b/admin/tags.php @@ -137,6 +137,19 @@ DELETE ); } +// +-----------------------------------------------------------------------+ +// | delete orphan tags | +// +-----------------------------------------------------------------------+ + +if (isset($_GET['action']) and 'delete_orphans' == $_GET['action']) +{ + check_pwg_token(); + + delete_orphan_tags(); + $_SESSION['page_infos'] = array(l10n('Orphan tags deleted')); + redirect(get_root_url().'admin.php?page=tags'); +} + // +-----------------------------------------------------------------------+ // | add a tag | // +-----------------------------------------------------------------------+ @@ -199,6 +212,31 @@ $template->assign( ) ); +// +-----------------------------------------------------------------------+ +// | orphan tags | +// +-----------------------------------------------------------------------+ + +$orphan_tags = get_orphan_tags(); + +$orphan_tag_names = array(); +foreach ($orphan_tags as $tag) +{ + array_push($orphan_tag_names, $tag['name']); +} + +if (count($orphan_tag_names) > 0) +{ + array_push( + $page['warnings'], + sprintf( + l10n('You have %d orphan tags: %s.').' '.l10n('Delete orphan tags').'', + count($orphan_tag_names), + implode(', ', $orphan_tag_names), + get_root_url().'admin.php?page=tags&action=delete_orphans&pwg_token='.get_pwg_token() + ) + ); +} + // +-----------------------------------------------------------------------+ // | form creation | // +-----------------------------------------------------------------------+ diff --git a/admin/themes/default/template/admin.tpl b/admin/themes/default/template/admin.tpl index 9528bee79..4ff019b26 100644 --- a/admin/themes/default/template/admin.tpl +++ b/admin/themes/default/template/admin.tpl @@ -126,5 +126,15 @@ jQuery(document).ready(function(){ldelim} {/if} + {if isset($warnings)} +
+ +
+ {/if} + {$ADMIN_CONTENT} diff --git a/admin/themes/default/template/maintenance.tpl b/admin/themes/default/template/maintenance.tpl index 8469decab..dcde866bb 100644 --- a/admin/themes/default/template/maintenance.tpl +++ b/admin/themes/default/template/maintenance.tpl @@ -11,18 +11,13 @@ - - - - diff --git a/language/en_UK/admin.lang.php b/language/en_UK/admin.lang.php index 7d044e56e..b369624bd 100644 --- a/language/en_UK/admin.lang.php +++ b/language/en_UK/admin.lang.php @@ -214,6 +214,7 @@ $lang['Default user does not exist'] = "The default user does not exist"; $lang['default values'] = "default values"; $lang['default'] = "default"; $lang['delete album'] = "delete album"; +$lang['Delete orphan tags'] = 'Delete orphan tags'; $lang['Delete Representant'] = "Delete Representant"; $lang['Delete selected photos'] = "Delete selected photos"; $lang['Delete selected tags'] = "Delete selected tags"; @@ -470,6 +471,7 @@ $lang['Options'] = "Options"; $lang['Options'] = "Options"; $lang['Order of menubar items has been updated successfully.'] = 'Order of menubar items has been updated successfully.'; $lang['Original templates'] = "Original templates"; +$lang['Orphan tags deleted'] = 'Orphan tags deleted'; $lang['Other plugins'] = "Other plugins available"; $lang['Other private albums'] = "Other private albums"; $lang['other'] = "other"; @@ -761,6 +763,7 @@ $lang['You are running on development sources, no check possible.'] = "You are r $lang['You are running the latest version of Piwigo.'] = "You are running Piwigo latest version."; $lang['You cannot delete your account'] = "You cannot delete your account"; $lang['You cannot move an album in its own sub album'] = "You cannot move an album in its own sub album"; +$lang['You have %d orphan tags: %s.'] = 'You have %d orphan tags: %s.'; $lang['You have subscribed to receiving notifications by mail.'] = "You have subscribed to receive notifications by mail."; $lang['You have unsubscribed from receiving notifications by mail.'] = "You have unsubscribed from being notified by mail."; $lang['You might go to plugin list to install and activate it.'] = "Go to the plugins list to install and activate it."; diff --git a/language/fr_FR/admin.lang.php b/language/fr_FR/admin.lang.php index a5341a661..c001c21e5 100644 --- a/language/fr_FR/admin.lang.php +++ b/language/fr_FR/admin.lang.php @@ -559,6 +559,7 @@ $lang['Extend for templates'] = "Etendre les templates"; $lang['Replacement of original templates by customized templates from template-extension subfolder'] = "Remplacement des templates d'origine par vos templates personnalisés issus du dossier template-extension"; $lang['Replacers (customized templates)'] = "Remplaçants (templates modifiés)"; $lang['Original templates'] = "Templates d'origine"; +$lang['Orphan tags deleted'] = 'Tags orphelins supprimés'; $lang['Optional URL keyword'] = "Paramètre facultatif de l'URL"; $lang['Templates configuration has been recorded.'] = "La configuration des templates a été enregistrée."; $lang['All optimizations have been successfully completed.'] = "Toutes les optimisations ont été réalisées avec succès."; @@ -781,4 +782,6 @@ $lang['remove creation date'] = 'supprimer la date de création'; $lang['with no album'] = 'sans album'; $lang['with no tag'] = 'sans tag'; $lang['Week starts on'] = 'La semaine commence le'; +$lang['You have %d orphan tags: %s.'] = 'Vous avez %d tags orphelins: %s.'; +$lang['Delete orphan tags'] = 'Supprimer les tags orphelins'; ?> -- cgit v1.2.3