Example #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()));
}
/**
 * the reverse of make_section_in_url
 * returns the 'section' (categories/tags/...) and the data associated with it
 *
 * Depending on section, other parameters are returned (category/tags/list/...)
 *
 * @param array of url tokens to parse
 * @param int the index in the array of url tokens; in/out
 * @return array
 */
function parse_section_url($tokens, &$next_token)
{
    $page = array();
    if (strncmp(@$tokens[$next_token], 'categor', 7) == 0) {
        $page['section'] = 'categories';
        $next_token++;
        if (isset($tokens[$next_token])) {
            if (preg_match('/^(\\d+)(?:-(.+))?$/', $tokens[$next_token], $matches)) {
                if (isset($matches[2])) {
                    $page['hit_by']['cat_url_name'] = $matches[2];
                }
                $page['category'] = $matches[1];
                $next_token++;
            } else {
                // try a permalink
                $maybe_permalinks = array();
                $current_token = $next_token;
                while (isset($tokens[$current_token]) and strpos($tokens[$current_token], 'created-') !== 0 and strpos($tokens[$current_token], 'posted-') !== 0 and strpos($tokens[$next_token], 'start-') !== 0 and strpos($tokens[$next_token], 'startcat-') !== 0 and $tokens[$current_token] != 'flat') {
                    if (empty($maybe_permalinks)) {
                        $maybe_permalinks[] = $tokens[$current_token];
                    } else {
                        $maybe_permalinks[] = $maybe_permalinks[count($maybe_permalinks) - 1] . '/' . $tokens[$current_token];
                    }
                    $current_token++;
                }
                if (count($maybe_permalinks)) {
                    $cat_id = get_cat_id_from_permalinks($maybe_permalinks, $perma_index);
                    if (isset($cat_id)) {
                        $next_token += $perma_index + 1;
                        $page['category'] = $cat_id;
                        $page['hit_by']['cat_permalink'] = $maybe_permalinks[$perma_index];
                    } else {
                        page_not_found(l10n('Permalink for album not found'));
                    }
                }
            }
        }
        if (isset($page['category'])) {
            $result = get_cat_info($page['category']);
            if (empty($result)) {
                page_not_found(l10n('Requested album does not exist'));
            }
            $page['category'] = $result;
        }
    } elseif ('tags' == @$tokens[$next_token]) {
        global $conf;
        $page['section'] = 'tags';
        $page['tags'] = array();
        $next_token++;
        $i = $next_token;
        $requested_tag_ids = array();
        $requested_tag_url_names = array();
        while (isset($tokens[$i])) {
            if (strpos($tokens[$i], 'created-') === 0 or strpos($tokens[$i], 'posted-') === 0 or strpos($tokens[$i], 'start-') === 0) {
                break;
            }
            if ($conf['tag_url_style'] != 'tag' and preg_match('/^(\\d+)(?:-(.*)|)$/', $tokens[$i], $matches)) {
                $requested_tag_ids[] = $matches[1];
            } else {
                $requested_tag_url_names[] = $tokens[$i];
            }
            $i++;
        }
        $next_token = $i;
        if (empty($requested_tag_ids) && empty($requested_tag_url_names)) {
            bad_request('at least one tag required');
        }
        $page['tags'] = find_tags($requested_tag_ids, $requested_tag_url_names);
        if (empty($page['tags'])) {
            page_not_found(l10n('Requested tag does not exist'), get_root_url() . 'tags.php');
        }
    } elseif ('favorites' == @$tokens[$next_token]) {
        $page['section'] = 'favorites';
        $next_token++;
    } elseif ('most_visited' == @$tokens[$next_token]) {
        $page['section'] = 'most_visited';
        $next_token++;
    } elseif ('best_rated' == @$tokens[$next_token]) {
        $page['section'] = 'best_rated';
        $next_token++;
    } elseif ('recent_pics' == @$tokens[$next_token]) {
        $page['section'] = 'recent_pics';
        $next_token++;
    } elseif ('recent_cats' == @$tokens[$next_token]) {
        $page['section'] = 'recent_cats';
        $next_token++;
    } elseif ('search' == @$tokens[$next_token]) {
        $page['section'] = 'search';
        $next_token++;
        preg_match('/(\\d+)/', @$tokens[$next_token], $matches);
        if (!isset($matches[1])) {
            bad_request('search identifier is missing');
        }
        $page['search'] = $matches[1];
        $next_token++;
    } elseif ('list' == @$tokens[$next_token]) {
        $page['section'] = 'list';
        $next_token++;
        $page['list'] = array();
        // No pictures
        if (empty($tokens[$next_token])) {
            // Add dummy element list
            $page['list'][] = -1;
        } else {
            if (!preg_match('/^\\d+(,\\d+)*$/', $tokens[$next_token])) {
                bad_request('wrong format on list GET parameter');
            }
            foreach (explode(',', $tokens[$next_token]) as $image_id) {
                $page['list'][] = $image_id;
            }
        }
        $next_token++;
    }
    return $page;
}
Example #3
0
File: meta.php Project: philum/cms
function slctag($idart, $cat, $curtag)
{
    if (!tag_auth($cat)) {
        return;
    }
    //$curtag=utf8_decode($curtag);//$ret.=eco($curtag,1);
    $ra = find_tags($cat, $curtag);
    //possibles
    if ($ra) {
        if (in_array($curtag, $ra)) {
            $curtag = '';
        }
    }
    $rb = read_tags($idart, $cat);
    //existing
    if ($rb) {
        if (in_array($curtag, $rb)) {
            $curtag = '';
        }
    }
    if ($ra && $rb) {
        $r = array_diff($ra, $rb);
    } elseif ($ra) {
        $r = $ra;
    }
    $ret = add_tag_btn($r, $idart, $cat, $curtag);
    if (!$ret) {
        $ret = ' ';
    }
    return $ret;
}
Example #4
0
function get_tag_ids($tag_names)
{
    $tags = find_tags();
    $ret = array();
    foreach ($tag_names as $key => $tag_name) {
        $idx = array_search($tag_name, $tags);
        if ($idx !== false) {
            $ret[] = $idx;
        }
    }
    return $ret;
}