Ignore:
Timestamp:
Aug 30, 2003, 5:54:37 PM (21 years ago)
Author:
z0rglub
Message:

Multi categories for the same picture

File:
1 edited

Legend:

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

    r57 r61  
    9595// It also deletes (in the database) :
    9696//    - all the images of the images (thanks to delete_image, see further)
     97//    - all the links between images and this category
    9798//    - all the restrictions linked to the category
    9899// The function works recursively.
     
    102103  $query = 'SELECT id';
    103104  $query.= ' FROM '.PREFIX_TABLE.'images';
    104   $query.= ' WHERE cat_id = '.$id;
     105  $query.= ' WHERE storage_category_id = '.$id;
    105106  $query.= ';';
    106107  $result = mysql_query( $query );
     
    109110    delete_image( $row['id'] );
    110111  }
     112
     113  // destruction of the links between images and this category
     114  $query = 'DELETE FROM '.PREFIX_TABLE.'image_category';
     115  $query.= ' WHERE category_id = '.$id;
     116  $query.= ';';
     117  mysql_query( $query );
    111118
    112119  // destruction of the access linked to the category
     
    141148// It also deletes (in the database) :
    142149//    - all the comments related to the image
     150//    - all the links between categories and this image
    143151//    - all the favorites associated to the image
    144152function delete_image( $id )
     
    151159  $query.= ';';
    152160  mysql_query( $query );
    153                
     161
     162  // destruction of the links between images and this category
     163  $query = 'DELETE FROM '.PREFIX_TABLE.'image_category';
     164  $query.= ' WHERE image_id = '.$id;
     165  $query.= ';';
     166  mysql_query( $query );
     167
    154168  // destruction of the favorites associated with the picture
    155169  $query = 'DELETE FROM '.PREFIX_TABLE.'favorites';
     
    245259  // retrieving all the favorites for this user and comparing their
    246260  // categories to the restricted categories
    247   $query = 'SELECT image_id, cat_id';
    248   $query.= ' FROM '.PREFIX_TABLE.'favorites, '.PREFIX_TABLE.'images';
     261  $query = 'SELECT image_id';
     262  $query.= ' FROM '.PREFIX_TABLE.'favorites';
    249263  $query.= ' WHERE user_id = '.$user_id;
    250   $query.= ' AND id = image_id';
    251264  $query.= ';';
    252265  $result = mysql_query ( $query );
    253266  while ( $row = mysql_fetch_array( $result ) )
    254267  {
    255     if ( in_array( $row['cat_id'], $restricted_cat ) )
     268    // for each picture, we have to check all the categories it belongs
     269    // to. Indeed if a picture belongs to category_1 and category_2 and that
     270    // category_2 is not restricted to the user, he can have the picture as
     271    // favorite.
     272    $query = 'SELECT DISTINCT(category_id) as category_id';
     273    $query.= ' FROM '.PREFIX_TABLE.'image_category';
     274    $query.= ' WHERE image_id = '.$row['image_id'];
     275    $query.= ';';
     276    $picture_result = mysql_query( $query );
     277    $picture_cat = array();
     278    while ( $picture_row = mysql_fetch_array( $picture_result ) )
     279    {
     280      array_push( $picture_cat, $picture_row['category_id'] );
     281    }
     282    if ( count( array_diff( $picture_cat, $restricted_cat ) ) > 0 )
    256283    {
    257284      $query = 'DELETE FROM '.PREFIX_TABLE.'favorites';
     
    263290  }
    264291}
     292
     293// update_category updates calculated informations about a category :
     294// date_last and nb_images
     295function update_category( $id = 'all' )
     296{
     297  if ( $id == 'all' )
     298  {
     299    $query = 'SELECT id';
     300    $query.= ' FROM '.PREFIX_TABLE.'categories';
     301    $query.= ';';
     302    $result = mysql_query( $query );
     303    while ( $row = mysql_fetch_array( $result ) )
     304    {
     305      // recursive call
     306      update_category( $row['id'] );
     307    }
     308  }
     309  else if ( is_numeric( $id ) )
     310  {
     311    // updating the number of pictures
     312    $query = 'SELECT COUNT(*) as nb_images';
     313    $query.= ' FROM '.PREFIX_TABLE.'image_category';
     314    $query.= ' WHERE category_id = '.$id;
     315    $query.= ';';
     316    $row = mysql_fetch_array( mysql_query( $query ) );
     317    $query = 'UPDATE '.PREFIX_TABLE.'categories';
     318    $query.= ' SET nb_images = '.$row['nb_images'];
     319    $query.= ' WHERE id = '.$id;
     320    $query.= ';';
     321    mysql_query( $query );
     322    // updating the date_last
     323    $query = 'SELECT date_available';
     324    $query.= ' FROM '.PREFIX_TABLE.'images';
     325    $query.= ' LEFT JOIN '.PREFIX_TABLE.'image_category ON id = image_id';
     326    $query.= ' WHERE category_id = '.$id;
     327    $query.= ' ORDER BY date_available DESC';
     328    $query.= ' LIMIT 0,1';
     329    $query.= ';';
     330    $row = mysql_fetch_array( mysql_query( $query ) );
     331    $query = 'UPDATE '.PREFIX_TABLE.'categories';
     332    $query.= " SET date_last = '".$row['date_available']."'";
     333    $query.= ' WHERE id = '.$id;
     334    $query.= ';';
     335    mysql_query( $query );
     336  }
     337}
     338
     339function check_date_format( $date )
     340{
     341  // date arrives at this format : DD/MM/YYYY
     342  list($day,$month,$year) = explode( '/', $date );
     343  return checkdate ( $month, $day, $year );
     344}
     345
     346function date_convert( $date )
     347{
     348  // date arrives at this format : DD/MM/YYYY
     349  // It must be transformed in YYYY-MM-DD
     350  list($day,$month,$year) = explode( '/', $date );
     351  return $year.'-'.$month.'-'.$day;
     352}
     353
     354function date_convert_back( $date )
     355{
     356  // date arrives at this format : YYYY-MM-DD
     357  // It must be transformed in DD/MM/YYYY
     358  if ( $date != '' )
     359  {
     360    list($year,$month,$day) = explode( '-', $date );
     361    return $day.'/'.$month.'/'.$year;
     362  }
     363  else
     364  {
     365    return '';
     366  }
     367}
     368
     369// get_keywords returns an array with relevant keywords found in the string
     370// given in argument. Keywords must be separated by comma in this string.
     371// keywords must :
     372//   - be longer or equal to 3 characters
     373//   - not contain ', " or blank characters
     374//   - unique in the string ("test,test" -> "test")
     375function get_keywords( $keywords_string )
     376{
     377  $keywords = array();
     378
     379  $candidates = explode( ',', $keywords_string );
     380  foreach ( $candidates as $candidate ) {
     381    if ( strlen($candidate) >= 3 and !preg_match( '/(\'|"|\s)/', $candidate ) )
     382      array_push( $keywords, $candidate );
     383  }
     384
     385  return array_unique( $keywords );
     386}
     387
     388function display_categories( $categories, $indent )
     389{
     390  global $vtp,$sub;
     391
     392  foreach ( $categories as $category ) {
     393    $vtp->addSession( $sub, 'associate_cat' );
     394    $vtp->setVar( $sub, 'associate_cat.value',   $category['id'] );
     395    $content = $indent.'- '.$category['name'];
     396    $vtp->setVar( $sub, 'associate_cat.content', $content );
     397    $vtp->closeSession( $sub, 'associate_cat' );
     398    display_categories( $category['subcats'], $indent.str_repeat(' ',3) );
     399  }
     400}
    265401?>
Note: See TracChangeset for help on using the changeset viewer.