[1119] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2297] | 4 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 5 | // | Copyright(C) 2008-2011 Piwigo Team http://piwigo.org | |
---|
[2297] | 6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
| 7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
| 8 | // +-----------------------------------------------------------------------+ |
---|
| 9 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 10 | // | it under the terms of the GNU General Public License as published by | |
---|
| 11 | // | the Free Software Foundation | |
---|
| 12 | // | | |
---|
| 13 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 16 | // | General Public License for more details. | |
---|
| 17 | // | | |
---|
| 18 | // | You should have received a copy of the GNU General Public License | |
---|
| 19 | // | along with this program; if not, write to the Free Software | |
---|
| 20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 21 | // | USA. | |
---|
| 22 | // +-----------------------------------------------------------------------+ |
---|
[1119] | 23 | |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * Tags available. Each return tag is represented as an array with its id, |
---|
| 27 | * its name, its weight (count), its url name. Tags are not sorted. |
---|
| 28 | * |
---|
| 29 | * The returned list can be a subset of all existing tags due to |
---|
| 30 | * permissions, only if a list of forbidden categories is provided |
---|
| 31 | * |
---|
| 32 | * @param array forbidden categories |
---|
| 33 | * @return array |
---|
| 34 | */ |
---|
[1677] | 35 | function get_available_tags() |
---|
[1119] | 36 | { |
---|
| 37 | // we can find top fatter tags among reachable images |
---|
[2087] | 38 | $query = ' |
---|
[6652] | 39 | SELECT tag_id, COUNT(DISTINCT(it.image_id)) AS counter |
---|
[2087] | 40 | FROM '.IMAGE_CATEGORY_TABLE.' ic |
---|
| 41 | INNER JOIN '.IMAGE_TAG_TABLE.' it ON ic.image_id=it.image_id'.get_sql_condition_FandF |
---|
[1677] | 42 | ( |
---|
| 43 | array |
---|
| 44 | ( |
---|
| 45 | 'forbidden_categories' => 'category_id', |
---|
| 46 | 'visible_categories' => 'category_id', |
---|
[2087] | 47 | 'visible_images' => 'ic.image_id' |
---|
[1677] | 48 | ), |
---|
[2087] | 49 | ' |
---|
| 50 | WHERE' |
---|
| 51 | ).' |
---|
[4387] | 52 | GROUP BY tag_id'; |
---|
[2087] | 53 | $tag_counters = simple_hash_from_query($query, 'tag_id', 'counter'); |
---|
[1677] | 54 | |
---|
[2087] | 55 | if ( empty($tag_counters) ) |
---|
[1119] | 56 | { |
---|
[2087] | 57 | return array(); |
---|
[1119] | 58 | } |
---|
[1135] | 59 | |
---|
[2087] | 60 | $query = ' |
---|
[2409] | 61 | SELECT * |
---|
[2087] | 62 | FROM '.TAGS_TABLE; |
---|
| 63 | $result = pwg_query($query); |
---|
[1119] | 64 | $tags = array(); |
---|
[4325] | 65 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[1119] | 66 | { |
---|
[2087] | 67 | $counter = @$tag_counters[ $row['id'] ]; |
---|
| 68 | if ( $counter ) |
---|
| 69 | { |
---|
| 70 | $row['counter'] = $counter; |
---|
| 71 | array_push($tags, $row); |
---|
| 72 | } |
---|
[1119] | 73 | } |
---|
| 74 | return $tags; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * All tags, even tags associated to no image. |
---|
| 79 | * |
---|
| 80 | * @return array |
---|
| 81 | */ |
---|
| 82 | function get_all_tags() |
---|
| 83 | { |
---|
| 84 | $query = ' |
---|
[2409] | 85 | SELECT * |
---|
[1119] | 86 | FROM '.TAGS_TABLE.' |
---|
| 87 | ;'; |
---|
| 88 | $result = pwg_query($query); |
---|
| 89 | $tags = array(); |
---|
[4325] | 90 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[1119] | 91 | { |
---|
| 92 | array_push($tags, $row); |
---|
| 93 | } |
---|
| 94 | |
---|
[2409] | 95 | usort($tags, 'tag_alpha_compare'); |
---|
[1310] | 96 | |
---|
[1119] | 97 | return $tags; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | * Giving a set of tags with a counter for each one, calculate the display |
---|
| 102 | * level of each tag. |
---|
| 103 | * |
---|
| 104 | * The level of each tag depends on the average count of tags. This |
---|
| 105 | * calcylation method avoid having very different levels for tags having |
---|
| 106 | * nearly the same count when set are small. |
---|
| 107 | * |
---|
| 108 | * @param array tags |
---|
| 109 | * @return array |
---|
| 110 | */ |
---|
| 111 | function add_level_to_tags($tags) |
---|
| 112 | { |
---|
| 113 | global $conf; |
---|
| 114 | |
---|
| 115 | if (count($tags) == 0) |
---|
| 116 | { |
---|
| 117 | return $tags; |
---|
| 118 | } |
---|
[1135] | 119 | |
---|
[1119] | 120 | $total_count = 0; |
---|
| 121 | |
---|
| 122 | foreach ($tags as $tag) |
---|
| 123 | { |
---|
| 124 | $total_count+= $tag['counter']; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | // average count of available tags will determine the level of each tag |
---|
| 128 | $tag_average_count = $total_count / count($tags); |
---|
| 129 | |
---|
| 130 | // tag levels threshold calculation: a tag with an average rate must have |
---|
| 131 | // the middle level. |
---|
| 132 | for ($i = 1; $i < $conf['tags_levels']; $i++) |
---|
| 133 | { |
---|
| 134 | $threshold_of_level[$i] = |
---|
| 135 | 2 * $i * $tag_average_count / $conf['tags_levels']; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | // display sorted tags |
---|
| 139 | foreach (array_keys($tags) as $k) |
---|
| 140 | { |
---|
| 141 | $tags[$k]['level'] = 1; |
---|
[1135] | 142 | |
---|
[1119] | 143 | // based on threshold, determine current tag level |
---|
| 144 | for ($i = $conf['tags_levels'] - 1; $i >= 1; $i--) |
---|
| 145 | { |
---|
| 146 | if ($tags[$k]['counter'] > $threshold_of_level[$i]) |
---|
| 147 | { |
---|
| 148 | $tags[$k]['level'] = $i + 1; |
---|
| 149 | break; |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | return $tags; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | /** |
---|
| 158 | * return the list of image ids corresponding to given tags. AND & OR mode |
---|
| 159 | * supported. |
---|
| 160 | * |
---|
| 161 | * @param array tag ids |
---|
| 162 | * @param string mode |
---|
[8726] | 163 | * @param string extra_images_where_sql - optionally apply a sql where filter to retrieved images |
---|
| 164 | * @param string order_by - optionally overwrite default photo order |
---|
[1119] | 165 | * @return array |
---|
| 166 | */ |
---|
[8726] | 167 | function get_image_ids_for_tags($tag_ids, $mode='AND', $extra_images_where_sql='', $order_by='') |
---|
[1119] | 168 | { |
---|
[8726] | 169 | global $conf; |
---|
| 170 | if (empty($tag_ids)) |
---|
[1119] | 171 | { |
---|
[8726] | 172 | return array(); |
---|
| 173 | } |
---|
[1135] | 174 | |
---|
[8726] | 175 | $query = 'SELECT id |
---|
| 176 | FROM '.IMAGES_TABLE.' i |
---|
| 177 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ic ON id=ic.image_id |
---|
| 178 | INNER JOIN '.IMAGE_TAG_TABLE.' it ON id=it.image_id |
---|
| 179 | WHERE tag_id IN ('.implode(',', $tag_ids).')' |
---|
| 180 | .get_sql_condition_FandF |
---|
| 181 | ( |
---|
| 182 | array |
---|
| 183 | ( |
---|
| 184 | 'forbidden_categories' => 'category_id', |
---|
| 185 | 'visible_categories' => 'category_id', |
---|
| 186 | 'visible_images' => 'id' |
---|
| 187 | ), |
---|
| 188 | "\n AND" |
---|
| 189 | ) |
---|
| 190 | .(empty($extra_images_where_sql) ? '' : " \nAND (".$extra_images_where_sql.')') |
---|
| 191 | .' |
---|
| 192 | GROUP BY id'; |
---|
| 193 | |
---|
| 194 | if ($mode=='AND' and count($tag_ids)>1) |
---|
| 195 | { |
---|
| 196 | $query .= ' |
---|
| 197 | HAVING COUNT(DISTINCT tag_id)='.count($tag_ids); |
---|
| 198 | } |
---|
| 199 | $query .= "\n".(empty($order_by) ? $conf['order_by'] : $order_by); |
---|
[1119] | 200 | |
---|
[8726] | 201 | return array_from_query($query, 'id'); |
---|
[1119] | 202 | } |
---|
[1537] | 203 | |
---|
| 204 | /** |
---|
| 205 | * return a list of tags corresponding to given items. |
---|
| 206 | * |
---|
| 207 | * @param array items |
---|
| 208 | * @param array max_tags |
---|
| 209 | * @param array excluded_tag_ids |
---|
| 210 | * @return array |
---|
| 211 | */ |
---|
| 212 | function get_common_tags($items, $max_tags, $excluded_tag_ids=null) |
---|
| 213 | { |
---|
| 214 | if (empty($items)) |
---|
| 215 | { |
---|
| 216 | return array(); |
---|
| 217 | } |
---|
| 218 | $query = ' |
---|
[6652] | 219 | SELECT t.*, count(*) AS counter |
---|
[1537] | 220 | FROM '.IMAGE_TAG_TABLE.' |
---|
[2409] | 221 | INNER JOIN '.TAGS_TABLE.' t ON tag_id = id |
---|
[1537] | 222 | WHERE image_id IN ('.implode(',', $items).')'; |
---|
| 223 | if (!empty($excluded_tag_ids)) |
---|
| 224 | { |
---|
| 225 | $query.=' |
---|
| 226 | AND tag_id NOT IN ('.implode(',', $excluded_tag_ids).')'; |
---|
| 227 | } |
---|
| 228 | $query .=' |
---|
[4387] | 229 | GROUP BY tag_id, t.id, t.name, t.url_name'; |
---|
[1537] | 230 | if ($max_tags>0) |
---|
| 231 | { |
---|
| 232 | $query .= ' |
---|
[1816] | 233 | ORDER BY counter DESC |
---|
[4334] | 234 | LIMIT '.$max_tags; |
---|
[1537] | 235 | } |
---|
| 236 | |
---|
| 237 | $result = pwg_query($query); |
---|
| 238 | $tags = array(); |
---|
[4325] | 239 | while($row = pwg_db_fetch_assoc($result)) |
---|
[1537] | 240 | { |
---|
| 241 | array_push($tags, $row); |
---|
| 242 | } |
---|
[2409] | 243 | usort($tags, 'tag_alpha_compare'); |
---|
[1537] | 244 | return $tags; |
---|
| 245 | } |
---|
[1852] | 246 | |
---|
| 247 | /** |
---|
| 248 | * return a list of tags corresponding to any of ids, url_names, names |
---|
| 249 | * |
---|
| 250 | * @param array ids |
---|
| 251 | * @param array url_names |
---|
| 252 | * @param array names |
---|
| 253 | * @return array |
---|
| 254 | */ |
---|
| 255 | function find_tags($ids, $url_names=array(), $names=array() ) |
---|
| 256 | { |
---|
| 257 | $where_clauses = array(); |
---|
| 258 | if ( !empty($ids) ) |
---|
| 259 | { |
---|
| 260 | $where_clauses[] = 'id IN ('.implode(',', $ids).')'; |
---|
| 261 | } |
---|
| 262 | if ( !empty($url_names) ) |
---|
| 263 | { |
---|
| 264 | $where_clauses[] = |
---|
| 265 | 'url_name IN ('. |
---|
| 266 | implode( |
---|
| 267 | ',', |
---|
| 268 | array_map( |
---|
| 269 | create_function('$s', 'return "\'".$s."\'";'), |
---|
| 270 | $url_names |
---|
| 271 | ) |
---|
| 272 | ) |
---|
| 273 | .')'; |
---|
| 274 | } |
---|
| 275 | if ( !empty($names) ) |
---|
| 276 | { |
---|
| 277 | $where_clauses[] = |
---|
| 278 | 'name IN ('. |
---|
| 279 | implode( |
---|
| 280 | ',', |
---|
| 281 | array_map( |
---|
| 282 | create_function('$s', 'return "\'".$s."\'";'), |
---|
| 283 | $names |
---|
| 284 | ) |
---|
| 285 | ) |
---|
| 286 | .')'; |
---|
| 287 | } |
---|
| 288 | if (empty($where_clauses)) |
---|
| 289 | { |
---|
| 290 | return array(); |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | $query = ' |
---|
[2409] | 294 | SELECT * |
---|
[1852] | 295 | FROM '.TAGS_TABLE.' |
---|
| 296 | WHERE '. implode( ' |
---|
| 297 | OR ', $where_clauses); |
---|
| 298 | |
---|
| 299 | $result = pwg_query($query); |
---|
| 300 | $tags = array(); |
---|
[4325] | 301 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[1852] | 302 | { |
---|
| 303 | array_push($tags, $row); |
---|
| 304 | } |
---|
| 305 | return $tags; |
---|
| 306 | } |
---|
[1119] | 307 | ?> |
---|