From 2eae3907a7e94f82c43cd755fe0319d8bf4dac4e Mon Sep 17 00:00:00 2001 From: mistic100 Date: Fri, 1 Nov 2013 11:03:10 +0000 Subject: splits ws_functions.inc.php in 8 files + comments + code cleaning git-svn-id: http://piwigo.org/svn/trunk@25281 68402e56-0260-453c-a942-63ccdbb3a9ee --- include/ws_functions/pwg.tags.php | 244 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 244 insertions(+) create mode 100644 include/ws_functions/pwg.tags.php (limited to 'include/ws_functions/pwg.tags.php') diff --git a/include/ws_functions/pwg.tags.php b/include/ws_functions/pwg.tags.php new file mode 100644 index 000000000..003993e62 --- /dev/null +++ b/include/ws_functions/pwg.tags.php @@ -0,0 +1,244 @@ +'tags', + 'tags'=>array($tags[$i]) + ) + ); + } + + return array( + 'tags' => new PwgNamedArray( + $tags, + 'tag', + ws_std_get_tag_xml_attributes() + ) + ); +} + +/** + * API method + * Returns the list of tags as you can see them in administration + * @param mixed[] $params + * + * Only admin can run this method and permissions are not taken into + * account. + */ +function ws_tags_getAdminList($params, &$service) +{ + return array( + 'tags' => new PwgNamedArray( + get_all_tags(), + 'tag', + ws_std_get_tag_xml_attributes() + ) + ); +} + +/** + * API method + * Returns a list of images for tags + * @param mixed[] $params + * @option int[] tag_id (optional) + * @option string[] tag_url_name (optional) + * @option string[] tag_name (optional) + * @option bool tag_mode_and + * @option int per_page + * @option int page + * @option string order + */ +function ws_tags_getImages($params, &$service) +{ + // first build all the tag_ids we are interested in + $tags = find_tags($params['tag_id'], $params['tag_url_name'], $params['tag_name']); + $tags_by_id = array(); + foreach ($tags as $tag) + { + $tags['id'] = (int)$tag['id']; + $tags_by_id[ $tag['id'] ] = $tag; + } + unset($tags); + $tag_ids = array_keys($tags_by_id); + + $where_clauses = ws_std_image_sql_filter($params); + if (!empty($where_clauses)) + { + $where_clauses = implode(' AND ', $where_clauses); + } + + $image_ids = get_image_ids_for_tags( + $tag_ids, + $params['tag_mode_and'] ? 'AND' : 'OR', + $where_clauses, + ws_std_image_sql_order($params) + ); + + $count_set = count($image_ids); + $image_ids = array_slice($image_ids, $params['per_page']*$params['page'], $params['per_page'] ); + + $image_tag_map = array(); + // build list of image ids with associated tags per image + if (!empty($image_ids) and !$params['tag_mode_and']) + { + $query = ' +SELECT image_id, GROUP_CONCAT(tag_id) AS tag_ids + FROM '. IMAGE_TAG_TABLE .' + WHERE tag_id IN ('. implode(',', $tag_ids) .') + AND image_id IN ('. implode(',', $image_ids) .') + GROUP BY image_id +;'; + $result = pwg_query($query); + + while ($row = pwg_db_fetch_assoc($result)) + { + $row['image_id'] = (int)$row['image_id']; + $image_ids[] = $row['image_id']; + $image_tag_map[ $row['image_id'] ] = explode(',', $row['tag_ids']); + } + } + + $images = array(); + if (!empty($image_ids)) + { + $rank_of = array_flip($image_ids); + + $query = ' +SELECT * + FROM '. IMAGES_TABLE .' + WHERE id IN ('. implode(',',$image_ids) .') +;'; + $result = pwg_query($query); + + while ($row = pwg_db_fetch_assoc($result)) + { + $image = array(); + $image['rank'] = $rank_of[ $row['id'] ]; + + foreach (array('id', 'width', 'height', 'hit') as $k) + { + if (isset($row[$k])) + { + $image[$k] = (int)$row[$k]; + } + } + foreach (array('file', 'name', 'comment', 'date_creation', 'date_available') as $k) + { + $image[$k] = $row[$k]; + } + $image = array_merge( $image, ws_std_get_urls($row) ); + + $image_tag_ids = ($params['tag_mode_and']) ? $tag_ids : $image_tag_map[$image['id']]; + $image_tags = array(); + foreach ($image_tag_ids as $tag_id) + { + $url = make_index_url( + array( + 'section'=>'tags', + 'tags'=> array($tags_by_id[$tag_id]) + ) + ); + $page_url = make_picture_url( + array( + 'section'=>'tags', + 'tags'=> array($tags_by_id[$tag_id]), + 'image_id' => $row['id'], + 'image_file' => $row['file'], + ) + ); + $image_tags[] = array( + 'id' => (int)$tag_id, + 'url' => $url, + 'page_url' => $page_url, + ); + } + + $image['tags'] = new PwgNamedArray($image_tags, 'tag', ws_std_get_tag_xml_attributes() ); + $images[] = $image; + } + + usort($images, 'rank_compare'); + unset($rank_of); + } + + return array( + 'paging' => new PwgNamedStruct( + array( + 'page' => $params['page'], + 'per_page' => $params['per_page'], + 'count' => count($images), + 'total_count' => $count_set, + ) + ), + 'images' => new PwgNamedArray( + $images, + 'image', + ws_std_get_image_xml_attributes() + ) + ); +} + +/** + * API method + * Adds a tag + * @param mixed[] $params + * @option string name + */ +function ws_tags_add($params, &$service) +{ + include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); + + $creation_output = create_tag($params['name']); + + if (isset($creation_output['error'])) + { + return new PwgError(500, $creation_output['error']); + } + + return $creation_output; +} + +?> \ No newline at end of file -- cgit v1.2.3