Changeset 491 for trunk


Ignore:
Timestamp:
Aug 21, 2004, 3:20:28 PM (20 years ago)
Author:
z0rglub
Message:
  • deletion functions updated : no needs to call delete_elements if no element must be deleted
  • improvement of function update_category : the function is not recursive anymore, but still works on all sub-categories thanks to a SQL query on uppercats database field. Less queries : for updating N categories, only (1+N) SQL queries compared to (3*N) in the previous revision.
  • function my_error added (debug purpose)
File:
1 edited

Legend:

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

    r467 r491  
    2626// +-----------------------------------------------------------------------+
    2727
     28include(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php');
     29
    2830$tab_ext_create_TN = array ( 'jpg', 'png', 'JPG', 'PNG' );
    2931
     
    203205    array_push($element_ids, $row['id']);
    204206  }
    205   delete_elements($element_ids);
     207  if (count($element_ids) > 0)
     208  {
     209    delete_elements($element_ids);
     210  }
    206211
    207212  // destruction of the links between images and this category
     
    236241    array_push($subcat_ids, $row['id']);
    237242  }
    238   delete_categories($subcat_ids);
     243  if (count($subcat_ids) > 0)
     244  {
     245    delete_categories($subcat_ids);
     246  }
    239247
    240248  // destruction of the category
     
    254262function delete_elements($ids)
    255263{
     264  global $count_deleted;
     265 
    256266  // destruction of the comments on the image
    257267  $query = '
    258268DELETE FROM '.COMMENTS_TABLE.'
    259   WHERE image_id IN ('.implode(',', $ids).')
    260 ;';
    261   echo '<pre>'.$query.'</pre>';
     269  WHERE image_id IN (
     270'.wordwrap(implode(', ', $ids), 80, "\n").')
     271;';
    262272  mysql_query($query);
    263273
     
    265275  $query = '
    266276DELETE FROM '.IMAGE_CATEGORY_TABLE.'
    267   WHERE image_id IN ('.implode(',', $ids).')
     277  WHERE image_id IN (
     278'.wordwrap(implode(', ', $ids), 80, "\n").')
    268279;';
    269280  mysql_query($query);
     
    272283  $query = '
    273284DELETE FROM '.FAVORITES_TABLE.'
    274   WHERE image_id IN ('.implode(',', $ids).')
     285  WHERE image_id IN (
     286'.wordwrap(implode(', ', $ids), 80, "\n").')
    275287;';
    276288  mysql_query($query);
     
    279291  $query = '
    280292DELETE FROM '.IMAGES_TABLE.'
    281   WHERE id IN ('.implode(',', $ids).')
     293  WHERE id IN (
     294'.wordwrap(implode(', ', $ids), 80, "\n").')
    282295;';
    283296  mysql_query($query);
     297
     298  $count_deleted+= count($ids);
    284299}
    285300
     
    402417}
    403418
    404 // update_category updates calculated informations about a category :
    405 // date_last and nb_images. It also verifies that the representative picture
    406 // is really linked to the category.
    407 function update_category( $id = 'all' )
    408 {
    409   if ( $id == 'all' )
    410   {
    411     $query = 'SELECT id FROM '.CATEGORIES_TABLE.';';
     419/**
     420 * updates calculated informations about a category : date_last and
     421 * nb_images. It also verifies that the representative picture is really
     422 * linked to the category. Recursive.
     423 *
     424 * @param mixed category id
     425 * @returns void
     426 */
     427function update_category($id = 'all')
     428{
     429  $cat_ids = array();
     430 
     431  $query = '
     432SELECT category_id, COUNT(image_id) AS count, max(date_available) AS date_last
     433  FROM '.IMAGES_TABLE.'
     434    INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id';
     435  if (is_numeric($id))
     436  {
     437    $query.= '
     438  WHERE uppercats REGEXP \'(^|,)'.$id.'(,|$)\'';
     439  }
     440  $query.= '
     441  GROUP BY category_id
     442;';
     443  $result = mysql_query( $query );
     444  while ( $row = mysql_fetch_array( $result ) )
     445  {
     446    array_push($cat_ids, $row['category_id']);
     447    $query = '
     448UPDATE '.CATEGORIES_TABLE.'
     449  SET date_last = \''.$row['date_last'].'\'
     450    , nb_images = '.$row['count'].'
     451  WHERE id = '.$row['category_id'].'
     452;';
     453    mysql_query($query);
     454  }
     455
     456  if (count($cat_ids) > 0)
     457  {
     458    $query = '
     459SELECT id, representative_picture_id
     460  FROM '.CATEGORIES_TABLE.'
     461  WHERE representative_picture_id IS NOT NULL
     462    AND id IN ('.implode(',', $cat_ids).')
     463;';
    412464    $result = mysql_query( $query );
    413465    while ( $row = mysql_fetch_array( $result ) )
    414466    {
    415       // recursive call
    416       update_category( $row['id'] );
    417     }
    418   }
    419   else if ( is_numeric( $id ) )
    420   {
    421     // updating the number of pictures
    422     $query = 'SELECT COUNT(*) as nb_images';
    423     $query.= ' FROM '.PREFIX_TABLE.'image_category';
    424     $query.= ' WHERE category_id = '.$id;
    425     $query.= ';';
    426     list( $nb_images ) = mysql_fetch_array( mysql_query( $query ) );
    427     // updating the date_last
    428     $query = 'SELECT MAX(date_available) AS date_available';
    429     $query.= ' FROM '.PREFIX_TABLE.'images';
    430     $query.= ' INNER JOIN '.PREFIX_TABLE.'image_category ON id = image_id';
    431     $query.= ' WHERE category_id = '.$id;
    432     $query.= ';';
    433     list( $date_available ) = mysql_fetch_array( mysql_query( $query ) );
    434    
    435     $query = 'UPDATE '.CATEGORIES_TABLE;
    436     $query.= " SET date_last = '".$date_available."'";
    437     $query.= ', nb_images = '.$nb_images;
    438     $query.= ' WHERE id = '.$id;
    439     $query.= ';';
    440     mysql_query( $query );
    441 
    442     // updating the representative_picture_id : if the representative
    443     // picture of the category is not any more linked to the category, we
    444     // have to set representative_picture_id to NULL
    445     $query = 'SELECT representative_picture_id';
    446     $query.= ' FROM '.CATEGORIES_TABLE;
    447     $query.= ' WHERE id = '.$id;
    448     $row = mysql_fetch_array( mysql_query( $query ) );
    449     // if the category has no representative picture (ie
    450     // representative_picture_id == NULL) we don't update anything
    451     if ( isset( $row['representative_picture_id'] ) )
    452     {
    453       $query = 'SELECT image_id';
    454       $query.= ' FROM '.PREFIX_TABLE.'image_category';
    455       $query.= ' WHERE category_id = '.$id;
    456       $query.= ' AND image_id = '.$row['representative_picture_id'];
    457       $query.= ';';
     467      $query = '
     468SELECT image_id
     469  FROM '.IMAGE_CATEGORY_TABLE.'
     470  WHERE category_id = '.$row['id'].'
     471    AND image_id = '.$row['representative_picture_id'].'
     472;';
    458473      $result = mysql_query( $query );
    459       if ( mysql_num_rows( $result ) == 0 )
     474      if (mysql_num_rows($result) == 0)
    460475      {
    461         $query = 'UPDATE '.CATEGORIES_TABLE;
    462         $query.= ' SET representative_picture_id = NULL';
    463         $query.= ' WHERE id = '.$id;
    464         $query.= ';';
     476        $query = '
     477UPDATE '.CATEGORIES_TABLE.'
     478  SET representative_picture_id = NULL
     479  WHERE id = '.$row['id'].'
     480;';
    465481        mysql_query( $query );
    466482      }
     
    882898  return $sub_dirs;
    883899}
     900
     901// my_error returns (or send to standard output) the message concerning the
     902// error occured for the last mysql query.
     903function my_error($header, $echo = true)
     904{
     905  $error = $header.'<span style="font-weight:bold;">N°= '.mysql_errno();
     906  $error.= ' -->> '.mysql_error()."</span><br /><br />\n";
     907  if ($echo)
     908  {
     909    echo $error;
     910  }
     911  else
     912  {
     913    return $error;
     914  }
     915}
    884916?>
Note: See TracChangeset for help on using the changeset viewer.