From 42abf4c57664d2596872d437f70b95193f9a5d18 Mon Sep 17 00:00:00 2001 From: plegall Date: Sun, 2 Apr 2006 22:26:19 +0000 Subject: improvement: tags replace keywords. Better data model, less limitations. Each image can be associated to as many tag as needed. Tags can contain non ASCII characters. Oriented navigation with tags by association. git-svn-id: http://piwigo.org/svn/trunk@1119 68402e56-0260-453c-a942-63ccdbb3a9ee --- admin/element_set_global.php | 134 ++++++------------ admin/element_set_unit.php | 34 +++-- admin/include/functions.php | 196 +++++++++++++++++++++++--- admin/include/functions_metadata.php | 34 ++++- admin/picture_modify.php | 31 ++-- admin/site_update.php | 53 +++++-- admin/tags.php | 265 +++++++++++++++++++++++++++++++++++ 7 files changed, 593 insertions(+), 154 deletions(-) create mode 100644 admin/tags.php (limited to 'admin') diff --git a/admin/element_set_global.php b/admin/element_set_global.php index 61ef2d6ee..f4b8aae2b 100644 --- a/admin/element_set_global.php +++ b/admin/element_set_global.php @@ -43,40 +43,6 @@ include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); // +-----------------------------------------------------------------------+ check_status(ACCESS_ADMINISTRATOR); -// +-----------------------------------------------------------------------+ -// | functions | -// +-----------------------------------------------------------------------+ - -/** - * returns the list of uniq keywords among given elements - * - * @param array element_ids - */ -function get_elements_keywords($element_ids) -{ - if (0 == count($element_ids)) - { - return array(); - } - - $keywords = array(); - - $query = ' -SELECT keywords - FROM '.IMAGES_TABLE.' - WHERE id IN ('.implode(',', $element_ids).') -;'; - $result = pwg_query($query); - while ($row = mysql_fetch_array($result)) - { - if (isset($row['keywords']) and !empty($row['keywords'])) - { - $keywords = array_merge($keywords, explode(',', $row['keywords'])); - } - } - return array_unique($keywords); -} - // +-----------------------------------------------------------------------+ // | global mode form submission | // +-----------------------------------------------------------------------+ @@ -111,6 +77,22 @@ if (isset($_POST['submit'])) } } + if (isset($_POST['add_tags']) and count($collection) > 0) + { + add_tags($_POST['add_tags'], $collection); + } + + if (isset($_POST['del_tags']) and count($collection) > 0) + { + $query = ' +DELETE + FROM '.IMAGE_TAG_TABLE.' + WHERE image_id IN ('.implode(',', $collection).') + AND tag_id IN ('.implode(',', $_POST['del_tags']).') +;'; + pwg_query($query); + } + if ($_POST['associate'] != 0 and count($collection) > 0) { $datas = array(); @@ -192,11 +174,6 @@ DELETE $datas = array(); $dbfields = array('primary' => array('id'), 'update' => array()); - if (!empty($_POST['add_keywords']) or $_POST['remove_keyword'] != '0') - { - array_push($dbfields['update'], 'keywords'); - } - $formfields = array('author', 'name', 'date_creation'); foreach ($formfields as $formfield) { @@ -210,7 +187,7 @@ DELETE if (count($dbfields['update']) > 0 and count($collection) > 0) { $query = ' -SELECT id, keywords +SELECT id FROM '.IMAGES_TABLE.' WHERE id IN ('.implode(',', $collection).') ;'; @@ -221,44 +198,6 @@ SELECT id, keywords $data = array(); $data['id'] = $row['id']; - if (!empty($_POST['add_keywords'])) - { - $data['keywords'] = - implode( - ',', - array_unique( - array_merge( - get_keywords(empty($row['keywords']) ? '' : $row['keywords']), - get_keywords($_POST['add_keywords']) - ) - ) - ); - } - - if ($_POST['remove_keyword'] != '0') - { - if (!isset($data['keywords'])) - { - $data['keywords'] = empty($row['keywords']) ? '' : $row['keywords']; - } - - $data['keywords'] = - implode( - ',', - array_unique( - array_diff( - get_keywords($data['keywords']), - array($_POST['remove_keyword']) - ) - ) - ); - - if ($data['keywords'] == '') - { - unset($data['keywords']); - } - } - if ('set' == $_POST['author_action']) { $data['author'] = $_POST['author']; @@ -384,25 +323,36 @@ SELECT DISTINCT(category_id) AS id, c.name, uppercats, global_rank display_select_cat_wrapper($query, array(), $blockname, true); } -$blockname = 'remove_keyword_option'; - -$template->assign_block_vars( - $blockname, - array('VALUE'=> 0, - 'OPTION' => '------------' - )); +// add tags +$template->assign_vars( + array( + 'ADD_TAG_SELECTION' => get_html_tag_selection(get_all_tags(), 'add_tags'), + ) + ); -$keywords = get_elements_keywords($page['cat_elements_id']); +// remove tags +$query = ' +SELECT tag_id, name, url_name, count(*) counter + FROM '.IMAGE_TAG_TABLE.' + INNER JOIN '.TAGS_TABLE.' ON tag_id = id + WHERE image_id IN ('.implode(',', $page['cat_elements_id']).') + GROUP BY tag_id + ORDER BY name ASC +;'; +$result = pwg_query($query); -foreach ($keywords as $keyword) +$tags = array(); +while($row = mysql_fetch_array($result)) { - $template->assign_block_vars( - $blockname, - array('VALUE'=> $keyword, - 'OPTION' => $keyword - )); + array_push($tags, $row); } +$template->assign_vars( + array( + 'DEL_TAG_SELECTION' => get_html_tag_selection($tags, 'del_tags'), + ) + ); + // creation date $day = empty($_POST['date_creation_day']) ? date('j') : $_POST['date_creation_day']; diff --git a/admin/element_set_unit.php b/admin/element_set_unit.php index c43522894..f6800f399 100644 --- a/admin/element_set_unit.php +++ b/admin/element_set_unit.php @@ -103,25 +103,21 @@ SELECT id, date_creation { $data{'date_creation'} = $row['date_creation']; } + + array_push($datas, $data); - $keywords = get_keywords($_POST['keywords-'.$row['id']]); - if (count($keywords) > 0) - { - $data{'keywords'} = implode(',', $keywords); - } - else + // tags management + if (isset($_POST[ 'tags-'.$row['id'] ])) { - $data{'keywords'} = ''; + set_tags($_POST[ 'tags-'.$row['id'] ], $row['id']); } - - array_push($datas, $data); } mass_updates( IMAGES_TABLE, array( 'primary' => array('id'), - 'update' => array('name','author','comment','date_creation','keywords') + 'update' => array('name','author','comment','date_creation') ), $datas ); @@ -192,11 +188,13 @@ if (count($page['cat_elements_id']) > 0) ); $template->assign_vars(array('NAV_BAR' => $nav_bar)); + // tags + $all_tags = get_all_tags(); $element_ids = array(); $query = ' -SELECT id,path,tn_ext,name,date_creation,comment,keywords,author,file +SELECT id,path,tn_ext,name,date_creation,comment,author,file FROM '.IMAGES_TABLE.' WHERE id IN ('.implode(',', $page['cat_elements_id']).') '.$conf['order_by'].' @@ -210,6 +208,13 @@ SELECT id,path,tn_ext,name,date_creation,comment,keywords,author,file array_push($element_ids, $row['id']); $src = get_thumbnail_src($row['path'], @$row['tn_ext']); + + $query = ' +SELECT tag_id + FROM '.IMAGE_TAG_TABLE.' + WHERE image_id = '.$row['id'].' +;'; + $selected_tags = array_from_query($query, 'tag_id'); // creation date if (!empty($row['date_creation'])) @@ -237,7 +242,12 @@ SELECT id,path,tn_ext,name,date_creation,comment,keywords,author,file 'AUTHOR' => @$row['author'], 'DESCRIPTION' => @$row['comment'], 'DATE_CREATION_YEAR' => $year, - 'KEYWORDS' => @$row['keywords'] + + 'TAG_SELECTION' => get_html_tag_selection( + $all_tags, + 'tags-'.$row['id'], + $selected_tags + ), ) ); diff --git a/admin/include/functions.php b/admin/include/functions.php index dc477f6d1..3a8c6b506 100644 --- a/admin/include/functions.php +++ b/admin/include/functions.php @@ -269,6 +269,14 @@ DELETE FROM '.IMAGE_CATEGORY_TABLE.' ;'; pwg_query($query); + // destruction of the links between images and tags + $query = ' +DELETE FROM '.IMAGE_TAG_TABLE.' + WHERE image_id IN ( +'.wordwrap(implode(', ', $ids), 80, "\n").') +;'; + pwg_query($query); + // destruction of the favorites associated with the picture $query = ' DELETE FROM '.FAVORITES_TABLE.' @@ -576,25 +584,6 @@ function date_convert_back( $date ) } } -/** - * returns an array with relevant keywords found in the given string. - * - * Keywords must be separated by comma or space characters. - * - * @param string keywords_string - * @return array - */ -function get_keywords($keywords_string) -{ - return - array_unique( - preg_split( - '/[\s,]+/', - $keywords_string - ) - ); -} - /** * returns an array containing sub-directories which can be a category, * recursive by default @@ -2093,6 +2082,173 @@ UPDATE ); } +/** + * Set tags to an image. Warning: given tags are all tags associated to the + * image, not additionnal tags. + * + * @param array tag ids + * @param int image id + * @return void + */ +function set_tags($tags, $image_id) +{ + $query = ' +DELETE + FROM '.IMAGE_TAG_TABLE.' + WHERE image_id = '.$image_id.' +;'; + pwg_query($query); + + if (count($tags) > 0) + { + $inserts = array(); + foreach ($tags as $tag_id) + { + array_push( + $inserts, + array( + 'tag_id' => $tag_id, + 'image_id' => $image_id + ) + ); + } + mass_inserts( + IMAGE_TAG_TABLE, + array_keys($inserts[0]), + $inserts + ); + } +} + +/** + * Add new tags to a set of images. + * + * @param array tag ids + * @param array image ids + * @return void + */ +function add_tags($tags, $images) +{ + if (count($tags) == 0 or count($tags) == 0) + { + return; + } + + // we can't insert twice the same {image_id,tag_id} so we must first + // delete lines we'll insert later + $query = ' +DELETE + FROM '.IMAGE_TAG_TABLE.' + WHERE image_id IN ('.implode(',', $images).') + AND tag_id IN ('.implode(',', $tags).') +;'; + pwg_query($query); + + $inserts = array(); + foreach ($images as $image_id) + { + foreach ($tags as $tag_id) + { + array_push( + $inserts, + array( + 'image_id' => $image_id, + 'tag_id' => $tag_id, + ) + ); + } + } + mass_inserts( + IMAGE_TAG_TABLE, + array_keys($inserts[0]), + $inserts + ); +} + +function tag_id_from_tag_name($tag_name) +{ + global $page; + + if (isset($page['tag_id_from_tag_name_cache'][$tag_name])) + { + return $page['tag_id_from_tag_name_cache'][$tag_name]; + } + + if (function_exists('mysql_real_escape_string')) + { + $tag_name = mysql_real_escape_string($tag_name); + } + else + { + $tag_name = mysql_escape_string($tag_name); + } + + // does the tag already exist? + $query = ' +SELECT id + FROM '.TAGS_TABLE.' + WHERE name = \''.$tag_name.'\' +;'; + $existing_tags = array_from_query($query, 'id'); + + if (count($existing_tags) == 0) + { + mass_inserts( + TAGS_TABLE, + array('name', 'url_name'), + array( + array( + 'name' => $tag_name, + 'url_name' => str2url($tag_name), + ) + ) + ); + + $page['tag_id_from_tag_name_cache'][$tag_name] = mysql_insert_id(); + } + else + { + $page['tag_id_from_tag_name_cache'][$tag_name] = $existing_tags[0]; + } + + return $page['tag_id_from_tag_name_cache'][$tag_name]; +} + +function set_tags_of($tags_of) +{ + if (count($tags_of) > 0) + { + $query = ' +DELETE + FROM '.IMAGE_TAG_TABLE.' + WHERE image_id IN ('.implode(',', array_keys($tags_of)).') +;'; + pwg_query($query); + + $inserts = array(); + + foreach ($tags_of as $image_id => $tag_ids) + { + foreach ($tag_ids as $tag_id) + { + array_push( + $inserts, + array( + 'image_id' => $image_id, + 'tag_id' => $tag_id, + ) + ); + } + } + + mass_inserts( + IMAGE_TAG_TABLE, + array_keys($inserts[0]), + $inserts + ); + } +} + /** * Do maintenance on all PWG tables * @@ -2143,6 +2299,4 @@ function do_maintenance_all_tables() pwg_query($query); } - - ?> diff --git a/admin/include/functions_metadata.php b/admin/include/functions_metadata.php index 337019b2e..21897c2cd 100644 --- a/admin/include/functions_metadata.php +++ b/admin/include/functions_metadata.php @@ -50,10 +50,7 @@ function get_sync_iptc_data($file) if (isset($iptc['keywords'])) { - // keywords separator is the comma, nothing else. Allowed characters in - // keywords : [A-Za-z0-9], "-" and "_". All other characters will be - // considered as separators - $iptc['keywords'] = preg_replace('/[^\w-]+/', ',', $iptc['keywords']); + // keywords separator is the comma $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']); } @@ -90,6 +87,7 @@ function update_metadata($files) } $datas = array(); + $tags_of = array(); foreach ($files as $id => $file) { @@ -123,7 +121,25 @@ function update_metadata($files) { foreach (array_keys($iptc) as $key) { - $data[$key] = addslashes($iptc[$key]); + if ($key == 'keywords' or $key == 'tags') + { + if (!isset($tags_of[$id])) + { + $tags_of[$id] = array(); + } + + foreach (explode(',', $iptc[$key]) as $tag_name) + { + array_push( + $tags_of[$id], + tag_id_from_tag_name($tag_name) + ); + } + } + else + { + $data[$key] = addslashes($iptc[$key]); + } } } } @@ -157,7 +173,10 @@ function update_metadata($files) $update_fields = array_merge( $update_fields, - array_keys($conf['use_iptc_mapping']) + array_diff( + array_keys($conf['use_iptc_mapping']), + array('tags', 'keywords') + ) ); } @@ -166,8 +185,11 @@ function update_metadata($files) 'primary' => array('id'), 'update' => array_unique($update_fields) ); + echo '
'; print_r($datas); echo '
'; mass_updates(IMAGES_TABLE, $fields, $datas); } + + set_tags_of(tags_of); } /** diff --git a/admin/picture_modify.php b/admin/picture_modify.php index be7497a07..83bf09a23 100644 --- a/admin/picture_modify.php +++ b/admin/picture_modify.php @@ -100,16 +100,6 @@ if (isset($_POST['submit']) and count($page['errors']) == 0) } } - $keywords = get_keywords($_POST['keywords']); - if (count($keywords) > 0) - { - $data{'keywords'} = implode(',', $keywords); - } - else - { - $data{'keywords'} = ''; - } - mass_updates( IMAGES_TABLE, array( @@ -119,6 +109,11 @@ if (isset($_POST['submit']) and count($page['errors']) == 0) array($data) ); + set_tags( + isset($_POST['tags']) ? $_POST['tags'] : array(), + $_GET['image_id'] + ); + array_push($page['infos'], l10n('Picture informations updated')); } // associate the element to other categories than its storage category @@ -215,6 +210,14 @@ $row = mysql_fetch_array(pwg_query($query)); $storage_category_id = $row['category_id']; $image_file = $row['file']; +// tags +$query = ' +SELECT tag_id + FROM '.IMAGE_TAG_TABLE.' + WHERE image_id = '.$_GET['image_id'].' +;'; +$selected_tags = array_from_query($query, 'tag_id'); + // Navigation path $date = isset($_POST['date_creation']) && empty($page['errors']) @@ -257,9 +260,11 @@ $template->assign_vars( 'CREATION_DATE' => $date, - 'KEYWORDS' => - isset($_POST['keywords']) ? - stripslashes($_POST['keywords']) : @$row['keywords'], + 'TAG_SELECTION' => get_html_tag_selection( + get_all_tags(), + 'tags', + $selected_tags + ), 'DESCRIPTION' => isset($_POST['description']) ? diff --git a/admin/site_update.php b/admin/site_update.php index b68ce2b28..c7c31106c 100644 --- a/admin/site_update.php +++ b/admin/site_update.php @@ -715,6 +715,7 @@ if (isset($_POST['submit']) and preg_match('/^metadata/', $_POST['sync']) $start = get_moment(); $datas = array(); + $tags_of = array(); foreach ( $files as $id=>$file ) { $data = $site_reader->get_element_update_attributes($file); @@ -723,23 +724,55 @@ if (isset($_POST['submit']) and preg_match('/^metadata/', $_POST['sync']) $data['date_metadata_update'] = CURRENT_DATE; $data['id']=$id; array_push($datas, $data); + + foreach (array('keywords', 'tags') as $key) + { + if (isset($data[$key])) + { + if (!isset($tags_of[$id])) + { + $tags_of[$id] = array(); + } + + foreach (explode(',', $data[$key]) as $tag_name) + { + array_push( + $tags_of[$id], + tag_id_from_tag_name($tag_name) + ); + } + } + } } else { array_push($errors, array('path' => $file, 'type' => 'PWG-ERROR-NO-FS')); } } - $update_fields = $site_reader->get_update_attributes(); - $update_fields = array_merge($update_fields, array('date_metadata_update')); - $fields = - array( - 'primary' => array('id'), - 'update' => array_unique($update_fields) - ); - //print_r($datas); - if (!$simulate and count($datas)>0 ) + + if (!$simulate) { - mass_updates(IMAGES_TABLE, $fields, $datas); + if (count($datas) > 0) + { + mass_updates( + IMAGES_TABLE, + // fields + array( + 'primary' => array('id'), + 'update' => array_unique( + array_merge( + array_diff( + $site_reader->get_update_attributes(), + // keywords and tags fields are managed separately + array('keywords', 'tags') + ), + array('date_metadata_update')) + ) + ), + $datas + ); + } + set_tags_of($tags_of); } echo '