[1119] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2297] | 4 | // +-----------------------------------------------------------------------+ |
---|
[19703] | 5 | // | Copyright(C) 2008-2013 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 | |
---|
[21817] | 25 | /** returns the number of available tags for the connected user */ |
---|
| 26 | function get_nb_available_tags() |
---|
| 27 | { |
---|
| 28 | global $user; |
---|
| 29 | if (!isset($user['nb_available_tags'])) |
---|
| 30 | { |
---|
| 31 | $user['nb_available_tags'] = count(get_available_tags()); |
---|
| 32 | single_update(USER_CACHE_TABLE, |
---|
| 33 | array('nb_available_tags'=>$user['nb_available_tags']), |
---|
| 34 | array('user_id'=>$user['id']) |
---|
| 35 | ); |
---|
| 36 | } |
---|
| 37 | return $user['nb_available_tags']; |
---|
| 38 | } |
---|
| 39 | |
---|
[1119] | 40 | /** |
---|
| 41 | * Tags available. Each return tag is represented as an array with its id, |
---|
| 42 | * its name, its weight (count), its url name. Tags are not sorted. |
---|
| 43 | * |
---|
| 44 | * The returned list can be a subset of all existing tags due to |
---|
| 45 | * permissions, only if a list of forbidden categories is provided |
---|
| 46 | * |
---|
| 47 | * @param array forbidden categories |
---|
| 48 | * @return array |
---|
| 49 | */ |
---|
[1677] | 50 | function get_available_tags() |
---|
[1119] | 51 | { |
---|
| 52 | // we can find top fatter tags among reachable images |
---|
[2087] | 53 | $query = ' |
---|
[6652] | 54 | SELECT tag_id, COUNT(DISTINCT(it.image_id)) AS counter |
---|
[2087] | 55 | FROM '.IMAGE_CATEGORY_TABLE.' ic |
---|
| 56 | INNER JOIN '.IMAGE_TAG_TABLE.' it ON ic.image_id=it.image_id'.get_sql_condition_FandF |
---|
[1677] | 57 | ( |
---|
| 58 | array |
---|
| 59 | ( |
---|
| 60 | 'forbidden_categories' => 'category_id', |
---|
| 61 | 'visible_categories' => 'category_id', |
---|
[2087] | 62 | 'visible_images' => 'ic.image_id' |
---|
[1677] | 63 | ), |
---|
[2087] | 64 | ' |
---|
| 65 | WHERE' |
---|
| 66 | ).' |
---|
[4387] | 67 | GROUP BY tag_id'; |
---|
[2087] | 68 | $tag_counters = simple_hash_from_query($query, 'tag_id', 'counter'); |
---|
[1677] | 69 | |
---|
[2087] | 70 | if ( empty($tag_counters) ) |
---|
[1119] | 71 | { |
---|
[2087] | 72 | return array(); |
---|
[1119] | 73 | } |
---|
[1135] | 74 | |
---|
[2087] | 75 | $query = ' |
---|
[2409] | 76 | SELECT * |
---|
[2087] | 77 | FROM '.TAGS_TABLE; |
---|
| 78 | $result = pwg_query($query); |
---|
[1119] | 79 | $tags = array(); |
---|
[4325] | 80 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[1119] | 81 | { |
---|
[18455] | 82 | $counter = intval(@$tag_counters[ $row['id'] ]); |
---|
[2087] | 83 | if ( $counter ) |
---|
| 84 | { |
---|
| 85 | $row['counter'] = $counter; |
---|
[11317] | 86 | $row['name'] = trigger_event('render_tag_name', $row['name']); |
---|
[18455] | 87 | $tags[] = $row; |
---|
[2087] | 88 | } |
---|
[1119] | 89 | } |
---|
| 90 | return $tags; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | * All tags, even tags associated to no image. |
---|
| 95 | * |
---|
| 96 | * @return array |
---|
| 97 | */ |
---|
| 98 | function get_all_tags() |
---|
| 99 | { |
---|
| 100 | $query = ' |
---|
[2409] | 101 | SELECT * |
---|
[1119] | 102 | FROM '.TAGS_TABLE.' |
---|
| 103 | ;'; |
---|
| 104 | $result = pwg_query($query); |
---|
| 105 | $tags = array(); |
---|
[4325] | 106 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[1119] | 107 | { |
---|
[11317] | 108 | $row['name'] = trigger_event('render_tag_name', $row['name']); |
---|
[18455] | 109 | $tags[] = $row; |
---|
[1119] | 110 | } |
---|
| 111 | |
---|
[2409] | 112 | usort($tags, 'tag_alpha_compare'); |
---|
[1310] | 113 | |
---|
[1119] | 114 | return $tags; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | * Giving a set of tags with a counter for each one, calculate the display |
---|
| 119 | * level of each tag. |
---|
| 120 | * |
---|
| 121 | * The level of each tag depends on the average count of tags. This |
---|
| 122 | * calcylation method avoid having very different levels for tags having |
---|
| 123 | * nearly the same count when set are small. |
---|
| 124 | * |
---|
| 125 | * @param array tags |
---|
| 126 | * @return array |
---|
| 127 | */ |
---|
| 128 | function add_level_to_tags($tags) |
---|
| 129 | { |
---|
| 130 | global $conf; |
---|
| 131 | |
---|
| 132 | if (count($tags) == 0) |
---|
| 133 | { |
---|
| 134 | return $tags; |
---|
| 135 | } |
---|
[1135] | 136 | |
---|
[1119] | 137 | $total_count = 0; |
---|
| 138 | |
---|
| 139 | foreach ($tags as $tag) |
---|
| 140 | { |
---|
| 141 | $total_count+= $tag['counter']; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | // average count of available tags will determine the level of each tag |
---|
| 145 | $tag_average_count = $total_count / count($tags); |
---|
| 146 | |
---|
| 147 | // tag levels threshold calculation: a tag with an average rate must have |
---|
| 148 | // the middle level. |
---|
| 149 | for ($i = 1; $i < $conf['tags_levels']; $i++) |
---|
| 150 | { |
---|
| 151 | $threshold_of_level[$i] = |
---|
| 152 | 2 * $i * $tag_average_count / $conf['tags_levels']; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | // display sorted tags |
---|
[18455] | 156 | foreach ($tags as &$tag) |
---|
[1119] | 157 | { |
---|
[18455] | 158 | $tag['level'] = 1; |
---|
[1135] | 159 | |
---|
[1119] | 160 | // based on threshold, determine current tag level |
---|
| 161 | for ($i = $conf['tags_levels'] - 1; $i >= 1; $i--) |
---|
| 162 | { |
---|
[18455] | 163 | if ($tag['counter'] > $threshold_of_level[$i]) |
---|
[1119] | 164 | { |
---|
[18455] | 165 | $tag['level'] = $i + 1; |
---|
[1119] | 166 | break; |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | } |
---|
[18455] | 170 | unset($tag); |
---|
[1119] | 171 | |
---|
| 172 | return $tags; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | /** |
---|
| 176 | * return the list of image ids corresponding to given tags. AND & OR mode |
---|
| 177 | * supported. |
---|
| 178 | * |
---|
| 179 | * @param array tag ids |
---|
| 180 | * @param string mode |
---|
[8726] | 181 | * @param string extra_images_where_sql - optionally apply a sql where filter to retrieved images |
---|
| 182 | * @param string order_by - optionally overwrite default photo order |
---|
[1119] | 183 | * @return array |
---|
| 184 | */ |
---|
[12630] | 185 | function get_image_ids_for_tags($tag_ids, $mode='AND', $extra_images_where_sql='', $order_by='', $use_permissions=true) |
---|
[1119] | 186 | { |
---|
[8726] | 187 | global $conf; |
---|
| 188 | if (empty($tag_ids)) |
---|
[1119] | 189 | { |
---|
[8726] | 190 | return array(); |
---|
| 191 | } |
---|
[1135] | 192 | |
---|
[8726] | 193 | $query = 'SELECT id |
---|
[12630] | 194 | FROM '.IMAGES_TABLE.' i '; |
---|
| 195 | |
---|
| 196 | if ($use_permissions) |
---|
| 197 | { |
---|
| 198 | $query.= ' |
---|
| 199 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ic ON id=ic.image_id'; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | $query.= ' |
---|
[8726] | 203 | INNER JOIN '.IMAGE_TAG_TABLE.' it ON id=it.image_id |
---|
[12630] | 204 | WHERE tag_id IN ('.implode(',', $tag_ids).')'; |
---|
| 205 | |
---|
| 206 | if ($use_permissions) |
---|
| 207 | { |
---|
| 208 | $query.= get_sql_condition_FandF( |
---|
| 209 | array( |
---|
| 210 | 'forbidden_categories' => 'category_id', |
---|
| 211 | 'visible_categories' => 'category_id', |
---|
| 212 | 'visible_images' => 'id' |
---|
[8726] | 213 | ), |
---|
| 214 | "\n AND" |
---|
[12630] | 215 | ); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | $query.= (empty($extra_images_where_sql) ? '' : " \nAND (".$extra_images_where_sql.')').' |
---|
[8726] | 219 | GROUP BY id'; |
---|
| 220 | |
---|
| 221 | if ($mode=='AND' and count($tag_ids)>1) |
---|
| 222 | { |
---|
| 223 | $query .= ' |
---|
| 224 | HAVING COUNT(DISTINCT tag_id)='.count($tag_ids); |
---|
| 225 | } |
---|
| 226 | $query .= "\n".(empty($order_by) ? $conf['order_by'] : $order_by); |
---|
[1119] | 227 | |
---|
[8726] | 228 | return array_from_query($query, 'id'); |
---|
[1119] | 229 | } |
---|
[1537] | 230 | |
---|
| 231 | /** |
---|
| 232 | * return a list of tags corresponding to given items. |
---|
| 233 | * |
---|
| 234 | * @param array items |
---|
| 235 | * @param array max_tags |
---|
| 236 | * @param array excluded_tag_ids |
---|
| 237 | * @return array |
---|
| 238 | */ |
---|
| 239 | function get_common_tags($items, $max_tags, $excluded_tag_ids=null) |
---|
| 240 | { |
---|
| 241 | if (empty($items)) |
---|
| 242 | { |
---|
| 243 | return array(); |
---|
| 244 | } |
---|
| 245 | $query = ' |
---|
[6652] | 246 | SELECT t.*, count(*) AS counter |
---|
[1537] | 247 | FROM '.IMAGE_TAG_TABLE.' |
---|
[2409] | 248 | INNER JOIN '.TAGS_TABLE.' t ON tag_id = id |
---|
[1537] | 249 | WHERE image_id IN ('.implode(',', $items).')'; |
---|
| 250 | if (!empty($excluded_tag_ids)) |
---|
| 251 | { |
---|
| 252 | $query.=' |
---|
| 253 | AND tag_id NOT IN ('.implode(',', $excluded_tag_ids).')'; |
---|
| 254 | } |
---|
| 255 | $query .=' |
---|
[12118] | 256 | GROUP BY t.id |
---|
| 257 | ORDER BY '; |
---|
[1537] | 258 | if ($max_tags>0) |
---|
| 259 | { |
---|
[12118] | 260 | $query .= 'counter DESC |
---|
[4334] | 261 | LIMIT '.$max_tags; |
---|
[1537] | 262 | } |
---|
[12118] | 263 | else |
---|
| 264 | { |
---|
| 265 | $query .= 'NULL'; |
---|
| 266 | } |
---|
[1537] | 267 | |
---|
| 268 | $result = pwg_query($query); |
---|
| 269 | $tags = array(); |
---|
[4325] | 270 | while($row = pwg_db_fetch_assoc($result)) |
---|
[1537] | 271 | { |
---|
[11317] | 272 | $row['name'] = trigger_event('render_tag_name', $row['name']); |
---|
[18455] | 273 | $tags[] = $row; |
---|
[1537] | 274 | } |
---|
[2409] | 275 | usort($tags, 'tag_alpha_compare'); |
---|
[1537] | 276 | return $tags; |
---|
| 277 | } |
---|
[1852] | 278 | |
---|
| 279 | /** |
---|
| 280 | * return a list of tags corresponding to any of ids, url_names, names |
---|
| 281 | * |
---|
| 282 | * @param array ids |
---|
| 283 | * @param array url_names |
---|
| 284 | * @param array names |
---|
| 285 | * @return array |
---|
| 286 | */ |
---|
| 287 | function find_tags($ids, $url_names=array(), $names=array() ) |
---|
| 288 | { |
---|
| 289 | $where_clauses = array(); |
---|
| 290 | if ( !empty($ids) ) |
---|
| 291 | { |
---|
| 292 | $where_clauses[] = 'id IN ('.implode(',', $ids).')'; |
---|
| 293 | } |
---|
| 294 | if ( !empty($url_names) ) |
---|
| 295 | { |
---|
| 296 | $where_clauses[] = |
---|
| 297 | 'url_name IN ('. |
---|
| 298 | implode( |
---|
| 299 | ',', |
---|
| 300 | array_map( |
---|
| 301 | create_function('$s', 'return "\'".$s."\'";'), |
---|
| 302 | $url_names |
---|
| 303 | ) |
---|
| 304 | ) |
---|
| 305 | .')'; |
---|
| 306 | } |
---|
| 307 | if ( !empty($names) ) |
---|
| 308 | { |
---|
| 309 | $where_clauses[] = |
---|
| 310 | 'name IN ('. |
---|
| 311 | implode( |
---|
| 312 | ',', |
---|
| 313 | array_map( |
---|
| 314 | create_function('$s', 'return "\'".$s."\'";'), |
---|
| 315 | $names |
---|
| 316 | ) |
---|
| 317 | ) |
---|
| 318 | .')'; |
---|
| 319 | } |
---|
| 320 | if (empty($where_clauses)) |
---|
| 321 | { |
---|
| 322 | return array(); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | $query = ' |
---|
[2409] | 326 | SELECT * |
---|
[1852] | 327 | FROM '.TAGS_TABLE.' |
---|
| 328 | WHERE '. implode( ' |
---|
| 329 | OR ', $where_clauses); |
---|
| 330 | |
---|
| 331 | $result = pwg_query($query); |
---|
| 332 | $tags = array(); |
---|
[4325] | 333 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[1852] | 334 | { |
---|
| 335 | array_push($tags, $row); |
---|
| 336 | } |
---|
| 337 | return $tags; |
---|
| 338 | } |
---|
[1119] | 339 | ?> |
---|