From bd7f8c0ba2e5686c25152de220f645dc39a72ccd Mon Sep 17 00:00:00 2001 From: plegall Date: Fri, 2 Sep 2011 08:37:21 +0000 Subject: feature 1078 added: ability to merge tags git-svn-id: http://piwigo.org/svn/trunk@12032 68402e56-0260-453c-a942-63ccdbb3a9ee --- admin/tags.php | 128 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 112 insertions(+), 16 deletions(-) (limited to 'admin/tags.php') diff --git a/admin/tags.php b/admin/tags.php index 67cc9bf3c..a879806fb 100644 --- a/admin/tags.php +++ b/admin/tags.php @@ -101,31 +101,121 @@ SELECT id, name } // +-----------------------------------------------------------------------+ -// | delete tags | +// | merge tags | // +-----------------------------------------------------------------------+ -if (isset($_POST['delete']) and isset($_POST['tags'])) +if (isset($_POST['confirm_merge'])) { - $query = ' -SELECT name + if (!isset($_POST['destination_tag'])) + { + array_push( + $page['errors'], + l10n('No destination tag selected') + ); + } + else + { + $destination_tag_id = $_POST['destination_tag']; + $tag_ids = explode(',', $_POST['merge_list']); + + if (is_array($tag_ids) and count($tag_ids) > 1) + { + $name_of_tag = array(); + $query = ' +SELECT + id, + name FROM '.TAGS_TABLE.' - WHERE id IN ('.implode(',', $_POST['tags']).') + WHERE id IN ('.implode(',', $tag_ids).') ;'; - $tag_names = array_from_query($query, 'name'); - - $query = ' -DELETE + $result = pwg_query($query); + while ($row = pwg_db_fetch_assoc($result)) + { + $name_of_tag[ $row['id'] ] = trigger_event('render_tag_name', $row['name']); + } + + $tag_ids_to_delete = array_diff( + $tag_ids, + array($destination_tag_id) + ); + + $query = ' +SELECT + DISTINCT(image_id) FROM '.IMAGE_TAG_TABLE.' - WHERE tag_id IN ('.implode(',', $_POST['tags']).') + WHERE tag_id IN ('.implode(',', $tag_ids_to_delete).') ;'; - pwg_query($query); - + $image_ids = array_from_query($query, 'image_id'); + + delete_tags($tag_ids_to_delete); + + $query = ' +SELECT + image_id + FROM '.IMAGE_TAG_TABLE.' + WHERE tag_id = '.$destination_tag_id.' +;'; + $destination_tag_image_ids = array_from_query($query, 'image_id'); + + $image_ids_to_link = array_diff( + $image_ids, + $destination_tag_image_ids + ); + + $inserts = array(); + foreach ($image_ids_to_link as $image_id) + { + array_push( + $inserts, + array( + 'tag_id' => $destination_tag_id, + 'image_id' => $image_id + ) + ); + } + + if (count($inserts) > 0) + { + mass_inserts( + IMAGE_TAG_TABLE, + array_keys($inserts[0]), + $inserts + ); + } + + $tags_deleted = array(); + foreach ($tag_ids_to_delete as $tag_id) + { + $tags_deleted[] = $name_of_tag[$tag_id]; + } + + array_push( + $page['infos'], + sprintf( + l10n('Tags %s merged into tag %s'), + implode(', ', $tags_deleted), + $name_of_tag[$destination_tag_id] + ) + ); + } + } +} + + +// +-----------------------------------------------------------------------+ +// | delete tags | +// +-----------------------------------------------------------------------+ + +if (isset($_POST['delete']) and isset($_POST['tags'])) +{ $query = ' -DELETE +SELECT name FROM '.TAGS_TABLE.' WHERE id IN ('.implode(',', $_POST['tags']).') ;'; - pwg_query($query); + $tag_names = array_from_query($query, 'name'); + + delete_tags($_POST['tags']); array_push( $page['infos'], @@ -250,11 +340,17 @@ $template->assign( ) ); -if (isset($_POST['edit']) and isset($_POST['tags'])) +if ((isset($_POST['edit']) or isset($_POST['merge'])) and isset($_POST['tags'])) { + $list_name = 'EDIT_TAGS_LIST'; + if (isset($_POST['merge'])) + { + $list_name = 'MERGE_TAGS_LIST'; + } + $template->assign( array( - 'EDIT_TAGS_LIST' => implode(',', $_POST['tags']), + $list_name => implode(',', $_POST['tags']), ) ); -- cgit v1.2.3