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