/** * 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); } $order_by = ws_std_image_sql_order($params, 'i.'); if (!empty($order_by)) { $order_by = 'ORDER BY ' . $order_by; } $image_ids = get_image_ids_for_tags($tag_ids, $params['tag_mode_and'] ? 'AND' : 'OR', $where_clauses, $order_by); $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_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 * Returns detailed information for an element * @param mixed[] $params * @option int image_id * @option int comments_page * @option int comments_per_page */ function ws_images_getInfo($params, $service) { global $user, $conf; $query = ' SELECT * FROM ' . IMAGES_TABLE . ' WHERE id=' . $params['image_id'] . get_sql_condition_FandF(array('visible_images' => 'id'), ' AND') . ' LIMIT 1 ;'; $result = pwg_query($query); if (pwg_db_num_rows($result) == 0) { return new PwgError(404, 'image_id not found'); } $image_row = pwg_db_fetch_assoc($result); $image_row = array_merge($image_row, ws_std_get_urls($image_row)); //-------------------------------------------------------- related categories $query = ' SELECT id, name, permalink, uppercats, global_rank, commentable FROM ' . IMAGE_CATEGORY_TABLE . ' INNER JOIN ' . CATEGORIES_TABLE . ' ON category_id = id WHERE image_id = ' . $image_row['id'] . get_sql_condition_FandF(array('forbidden_categories' => 'category_id'), ' AND') . ' ;'; $result = pwg_query($query); $is_commentable = false; $related_categories = array(); while ($row = pwg_db_fetch_assoc($result)) { if ($row['commentable'] == 'true') { $is_commentable = true; } unset($row['commentable']); $row['url'] = make_index_url(array('category' => $row)); $row['page_url'] = make_picture_url(array('image_id' => $image_row['id'], 'image_file' => $image_row['file'], 'category' => $row)); $row['id'] = (int) $row['id']; $related_categories[] = $row; } usort($related_categories, 'global_rank_compare'); if (empty($related_categories)) { return new PwgError(401, 'Access denied'); } //-------------------------------------------------------------- related tags $related_tags = get_common_tags(array($image_row['id']), -1); foreach ($related_tags as $i => $tag) { $tag['url'] = make_index_url(array('tags' => array($tag))); $tag['page_url'] = make_picture_url(array('image_id' => $image_row['id'], 'image_file' => $image_row['file'], 'tags' => array($tag))); unset($tag['counter']); $tag['id'] = (int) $tag['id']; $related_tags[$i] = $tag; } //------------------------------------------------------------- related rates $rating = array('score' => $image_row['rating_score'], 'count' => 0, 'average' => null); if (isset($rating['score'])) { $query = ' SELECT COUNT(rate) AS count, ROUND(AVG(rate),2) AS average FROM ' . RATE_TABLE . ' WHERE element_id = ' . $image_row['id'] . ' ;'; $row = pwg_db_fetch_assoc(pwg_query($query)); $rating['score'] = (double) $rating['score']; $rating['average'] = (double) $row['average']; $rating['count'] = (int) $row['count']; } //---------------------------------------------------------- related comments $related_comments = array(); $where_comments = 'image_id = ' . $image_row['id']; if (!is_admin()) { $where_comments .= ' AND validated="true"'; } $query = ' SELECT COUNT(id) AS nb_comments FROM ' . COMMENTS_TABLE . ' WHERE ' . $where_comments . ' ;'; list($nb_comments) = query2array($query, null, 'nb_comments'); $nb_comments = (int) $nb_comments; if ($nb_comments > 0 and $params['comments_per_page'] > 0) { $query = ' SELECT id, date, author, content FROM ' . COMMENTS_TABLE . ' WHERE ' . $where_comments . ' ORDER BY date LIMIT ' . (int) $params['comments_per_page'] . ' OFFSET ' . (int) ($params['comments_per_page'] * $params['comments_page']) . ' ;'; $result = pwg_query($query); while ($row = pwg_db_fetch_assoc($result)) { $row['id'] = (int) $row['id']; $related_comments[] = $row; } } $comment_post_data = null; if ($is_commentable and (!is_a_guest() or is_a_guest() and $conf['comments_forall'])) { $comment_post_data['author'] = stripslashes($user['username']); $comment_post_data['key'] = get_ephemeral_key(2, $params['image_id']); } $ret = $image_row; foreach (array('id', 'width', 'height', 'hit', 'filesize') as $k) { if (isset($ret[$k])) { $ret[$k] = (int) $ret[$k]; } } foreach (array('path', 'storage_category_id') as $k) { unset($ret[$k]); } $ret['rates'] = array(WS_XML_ATTRIBUTES => $rating); $ret['categories'] = new PwgNamedArray($related_categories, 'category', array('id', 'url', 'page_url')); $ret['tags'] = new PwgNamedArray($related_tags, 'tag', ws_std_get_tag_xml_attributes()); if (isset($comment_post_data)) { $ret['comment_post'] = array(WS_XML_ATTRIBUTES => $comment_post_data); } $ret['comments_paging'] = new PwgNamedStruct(array('page' => $params['comments_page'], 'per_page' => $params['comments_per_page'], 'count' => count($related_comments), 'total_count' => $nb_comments)); $ret['comments'] = new PwgNamedArray($related_comments, 'comment', array('id', 'date')); if ($service->_responseFormat != 'rest') { return $ret; // for backward compatibility only } else { return array('image' => new PwgNamedStruct($ret, null, array('name', 'comment'))); } }