示例#1
0
/**
 * 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 images per category
 * @param mixed[] $params
 *    @option int[] cat_id (optional)
 *    @option bool recursive
 *    @option int per_page
 *    @option int page
 *    @option string order (optional)
 */
function ws_categories_getImages($params, &$service)
{
    global $user, $conf;
    $images = array();
    //------------------------------------------------- get the related categories
    $where_clauses = array();
    foreach ($params['cat_id'] as $cat_id) {
        if ($params['recursive']) {
            $where_clauses[] = 'uppercats ' . DB_REGEX_OPERATOR . ' \'(^|,)' . $cat_id . '(,|$)\'';
        } else {
            $where_clauses[] = 'id=' . $cat_id;
        }
    }
    if (!empty($where_clauses)) {
        $where_clauses = array('(' . implode("\n    OR ", $where_clauses) . ')');
    }
    $where_clauses[] = get_sql_condition_FandF(array('forbidden_categories' => 'id'), null, true);
    $query = '
SELECT id, name, permalink, image_order
  FROM ' . CATEGORIES_TABLE . '
  WHERE ' . implode("\n    AND ", $where_clauses) . '
;';
    $result = pwg_query($query);
    $cats = array();
    while ($row = pwg_db_fetch_assoc($result)) {
        $row['id'] = (int) $row['id'];
        $cats[$row['id']] = $row;
    }
    //-------------------------------------------------------- get the images
    if (!empty($cats)) {
        $where_clauses = ws_std_image_sql_filter($params, 'i.');
        $where_clauses[] = 'category_id IN (' . implode(',', array_keys($cats)) . ')';
        $where_clauses[] = get_sql_condition_FandF(array('visible_images' => 'i.id'), null, true);
        $order_by = ws_std_image_sql_order($params, 'i.');
        if (empty($order_by) and count($params['cat_id']) == 1 and isset($cats[$params['cat_id'][0]]['image_order'])) {
            $order_by = $cats[$params['cat_id'][0]]['image_order'];
        }
        $order_by = empty($order_by) ? $conf['order_by'] : 'ORDER BY ' . $order_by;
        $query = '
SELECT i.*, GROUP_CONCAT(category_id) AS cat_ids
  FROM ' . IMAGES_TABLE . ' i
    INNER JOIN ' . IMAGE_CATEGORY_TABLE . ' ON i.id=image_id
  WHERE ' . implode("\n    AND ", $where_clauses) . '
  GROUP BY i.id
  ' . $order_by . '
  LIMIT ' . $params['per_page'] . '
  OFFSET ' . $params['per_page'] * $params['page'] . '
;';
        $result = pwg_query($query);
        while ($row = pwg_db_fetch_assoc($result)) {
            $image = array();
            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_cats = array();
            foreach (explode(',', $row['cat_ids']) as $cat_id) {
                $url = make_index_url(array('category' => $cats[$cat_id]));
                $page_url = make_picture_url(array('category' => $cats[$cat_id], 'image_id' => $row['id'], 'image_file' => $row['file']));
                $image_cats[] = array('id' => (int) $cat_id, 'url' => $url, 'page_url' => $page_url);
            }
            $image['categories'] = new PwgNamedArray($image_cats, 'category', array('id', 'url', 'page_url'));
            $images[] = $image;
        }
    }
    return array('paging' => new PwgNamedStruct(array('page' => $params['page'], 'per_page' => $params['per_page'], 'count' => count($images))), 'images' => new PwgNamedArray($images, 'image', ws_std_get_image_xml_attributes()));
}
示例#3
0
/**
 * API method
 * Returns a list of elements corresponding to a query search
 * @param mixed[] $params
 *    @option string query
 *    @option int per_page
 *    @option int page
 *    @option string order (optional)
 */
function ws_images_search($params, $service)
{
    include_once PHPWG_ROOT_PATH . 'include/functions_search.inc.php';
    $images = array();
    $where_clauses = ws_std_image_sql_filter($params, 'i.');
    $order_by = ws_std_image_sql_order($params, 'i.');
    $super_order_by = false;
    if (!empty($order_by)) {
        global $conf;
        $conf['order_by'] = 'ORDER BY ' . $order_by;
        $super_order_by = true;
        // quick_search_result might be faster
    }
    $search_result = get_quick_search_results($params['query'], array('super_order_by' => $super_order_by, 'images_where' => implode(' AND ', $where_clauses)));
    $image_ids = array_slice($search_result['items'], $params['page'] * $params['per_page'], $params['per_page']);
    if (count($image_ids)) {
        $query = '
SELECT *
  FROM ' . IMAGES_TABLE . '
  WHERE id IN (' . implode(',', $image_ids) . ')
;';
        $result = pwg_query($query);
        $image_ids = array_flip($image_ids);
        while ($row = pwg_db_fetch_assoc($result)) {
            $image = array();
            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));
            $images[$image_ids[$image['id']]] = $image;
        }
        ksort($images, SORT_NUMERIC);
        $images = array_values($images);
    }
    return array('paging' => new PwgNamedStruct(array('page' => $params['page'], 'per_page' => $params['per_page'], 'count' => count($images), 'total_count' => count($search_result['items']))), 'images' => new PwgNamedArray($images, 'image', ws_std_get_image_xml_attributes()));
}