Ignore:
Timestamp:
Apr 3, 2006, 12:26:19 AM (18 years ago)
Author:
plg
Message:

improvement: tags replace keywords. Better data model, less
limitations. Each image can be associated to as many tag as needed. Tags can
contain non ASCII characters. Oriented navigation with tags by association.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/include/functions.php

    r1116 r1119  
    270270  pwg_query($query);
    271271
     272  // destruction of the links between images and tags
     273  $query = '
     274DELETE FROM '.IMAGE_TAG_TABLE.'
     275  WHERE image_id IN (
     276'.wordwrap(implode(', ', $ids), 80, "\n").')
     277;';
     278  pwg_query($query);
     279
    272280  // destruction of the favorites associated with the picture
    273281  $query = '
     
    575583    return '';
    576584  }
    577 }
    578 
    579 /**
    580  * returns an array with relevant keywords found in the given string.
    581  *
    582  * Keywords must be separated by comma or space characters.
    583  *
    584  * @param string keywords_string
    585  * @return array
    586  */
    587 function get_keywords($keywords_string)
    588 {
    589   return
    590     array_unique(
    591       preg_split(
    592         '/[\s,]+/',
    593         $keywords_string
    594         )
    595       );
    596585}
    597586
     
    20952084
    20962085/**
     2086 * Set tags to an image. Warning: given tags are all tags associated to the
     2087 * image, not additionnal tags.
     2088 *
     2089 * @param array tag ids
     2090 * @param int image id
     2091 * @return void
     2092 */
     2093function set_tags($tags, $image_id)
     2094{
     2095  $query = '
     2096DELETE
     2097  FROM '.IMAGE_TAG_TABLE.'
     2098  WHERE image_id = '.$image_id.'
     2099;';
     2100  pwg_query($query);
     2101
     2102  if (count($tags) > 0)
     2103  {
     2104    $inserts = array();
     2105    foreach ($tags as $tag_id)
     2106    {
     2107      array_push(
     2108        $inserts,
     2109        array(
     2110          'tag_id' => $tag_id,
     2111          'image_id' => $image_id
     2112          )
     2113        );
     2114    }
     2115    mass_inserts(
     2116      IMAGE_TAG_TABLE,
     2117      array_keys($inserts[0]),
     2118      $inserts
     2119      );
     2120  }
     2121}
     2122
     2123/**
     2124 * Add new tags to a set of images.
     2125 *
     2126 * @param array tag ids
     2127 * @param array image ids
     2128 * @return void
     2129 */
     2130function add_tags($tags, $images)
     2131{
     2132  if (count($tags) == 0 or count($tags) == 0)
     2133  {
     2134    return;
     2135  }
     2136 
     2137  // we can't insert twice the same {image_id,tag_id} so we must first
     2138  // delete lines we'll insert later
     2139  $query = '
     2140DELETE
     2141  FROM '.IMAGE_TAG_TABLE.'
     2142  WHERE image_id IN ('.implode(',', $images).')
     2143    AND tag_id IN ('.implode(',', $tags).')
     2144;';
     2145  pwg_query($query);
     2146
     2147  $inserts = array();
     2148  foreach ($images as $image_id)
     2149  {
     2150    foreach ($tags as $tag_id)
     2151    {
     2152      array_push(
     2153        $inserts,
     2154        array(
     2155          'image_id' => $image_id,
     2156          'tag_id' => $tag_id,
     2157          )
     2158        );
     2159    }
     2160  }
     2161  mass_inserts(
     2162    IMAGE_TAG_TABLE,
     2163    array_keys($inserts[0]),
     2164    $inserts
     2165    );
     2166}
     2167
     2168function tag_id_from_tag_name($tag_name)
     2169{
     2170  global $page;
     2171
     2172  if (isset($page['tag_id_from_tag_name_cache'][$tag_name]))
     2173  {
     2174    return $page['tag_id_from_tag_name_cache'][$tag_name];
     2175  }
     2176 
     2177  if (function_exists('mysql_real_escape_string'))
     2178  {
     2179    $tag_name = mysql_real_escape_string($tag_name);
     2180  }
     2181  else
     2182  {
     2183    $tag_name = mysql_escape_string($tag_name);
     2184  }
     2185
     2186  // does the tag already exist?
     2187  $query = '
     2188SELECT id
     2189  FROM '.TAGS_TABLE.'
     2190  WHERE name = \''.$tag_name.'\'
     2191;';
     2192  $existing_tags = array_from_query($query, 'id');
     2193
     2194  if (count($existing_tags) == 0)
     2195  {
     2196    mass_inserts(
     2197      TAGS_TABLE,
     2198      array('name', 'url_name'),
     2199      array(
     2200        array(
     2201          'name' => $tag_name,
     2202          'url_name' => str2url($tag_name),
     2203          )
     2204        )
     2205      );
     2206
     2207    $page['tag_id_from_tag_name_cache'][$tag_name] = mysql_insert_id();
     2208  }
     2209  else
     2210  {
     2211    $page['tag_id_from_tag_name_cache'][$tag_name] = $existing_tags[0];
     2212  }
     2213
     2214  return $page['tag_id_from_tag_name_cache'][$tag_name];
     2215}
     2216
     2217function set_tags_of($tags_of)
     2218{
     2219  if (count($tags_of) > 0)
     2220  {
     2221    $query = '
     2222DELETE
     2223  FROM '.IMAGE_TAG_TABLE.'
     2224  WHERE image_id IN ('.implode(',', array_keys($tags_of)).')
     2225;';
     2226    pwg_query($query);
     2227
     2228    $inserts = array();
     2229   
     2230    foreach ($tags_of as $image_id => $tag_ids)
     2231    {
     2232      foreach ($tag_ids as $tag_id)
     2233      {
     2234        array_push(
     2235          $inserts,
     2236          array(
     2237            'image_id' => $image_id,
     2238            'tag_id' => $tag_id,
     2239            )
     2240          );
     2241      }
     2242    }
     2243
     2244    mass_inserts(
     2245      IMAGE_TAG_TABLE,
     2246      array_keys($inserts[0]),
     2247      $inserts
     2248      );
     2249  }
     2250}
     2251
     2252/**
    20972253 * Do maintenance on all PWG tables
    20982254 *
     
    21442300
    21452301}
    2146 
    2147 
    21482302?>
Note: See TracChangeset for help on using the changeset viewer.