Ignore:
Timestamp:
Aug 5, 2004, 7:35:43 PM (20 years ago)
Author:
z0rglub
Message:
  • non picture files management
  • refactoring
  • function delete_category and delete_element now runs with an array of ids in parameters instead of a single id.
File:
1 edited

Legend:

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

    r423 r467  
    6464
    6565/**
    66  * returns an array with all picture files according to $conf['picture_ext']
     66 * returns an array with all picture files according to $conf['file_ext']
    6767 *
    6868 * @param string $dir
    6969 * @return array
    7070 */
    71 function get_picture_files( $dir )
     71function get_pwg_files($dir)
    7272{
    7373  global $conf;
    7474
    7575  $pictures = array();
    76   if ( $opendir = opendir( $dir ) )
    77   {
    78     while ( $file = readdir( $opendir ) )
    79     {
    80       if ( in_array( get_extension( $file ), $conf['picture_ext'] ) )
     76  if ($opendir = opendir($dir))
     77  {
     78    while ($file = readdir($opendir))
     79    {
     80      if (in_array(get_extension($file), $conf['file_ext']))
    8181      {
    82         array_push( $pictures, $file );
     82        array_push($pictures, $file);
    8383      }
    8484    }
     
    9494 * @return array
    9595 */
    96 function get_thumb_files( $dir )
     96function get_thumb_files($dir)
    9797{
    9898  global $conf;
    9999
    100   $prefix_length = strlen( $conf['prefix_thumbnail'] );
     100  $prefix_length = strlen($conf['prefix_thumbnail']);
    101101 
    102102  $thumbnails = array();
    103   if ( $opendir = @opendir( $dir ) )
    104   {
    105     while ( $file = readdir( $opendir ) )
    106     {
    107       if ( in_array( get_extension( $file ), $conf['picture_ext'] )
    108            and substr($file,0,$prefix_length) == $conf['prefix_thumbnail'] )
     103  if ($opendir = @opendir($dir.'/thumbnail'))
     104  {
     105    while ($file = readdir($opendir))
     106    {
     107      if (in_array(get_extension($file), $conf['picture_ext'])
     108          and substr($file, 0, $prefix_length) == $conf['prefix_thumbnail'])
    109109      {
    110         array_push( $thumbnails, $file );
     110        array_push($thumbnails, $file);
    111111      }
    112112    }
    113113  }
    114114  return $thumbnails;
     115}
     116
     117/**
     118 * returns an array with representative picture files of a directory
     119 * according to $conf['picture_ext']
     120 *
     121 * @param string $dir
     122 * @return array
     123 */
     124function get_representative_files($dir)
     125{
     126  global $conf;
     127
     128  $pictures = array();
     129  if ($opendir = @opendir($dir.'/representative'))
     130  {
     131    while ($file = readdir($opendir))
     132    {
     133      if (in_array(get_extension($file), $conf['picture_ext']))
     134      {
     135        array_push($pictures, $file);
     136      }
     137    }
     138  }
     139  return $pictures;
    115140}
    116141
     
    131156       
    132157
    133 // The function delete_site deletes a site
    134 // and call the function delete_category for each primary category of the site
     158// The function delete_site deletes a site and call the function
     159// delete_categories for each primary category of the site
    135160function delete_site( $id )
    136161{
    137162  // destruction of the categories of the site
    138   $query = 'SELECT id';
    139   $query.= ' FROM '.CATEGORIES_TABLE;
    140   $query.= ' WHERE site_id = '.$id;
    141   $query.= ';';
    142   $result = mysql_query( $query );
    143   while ( $row = mysql_fetch_array( $result ) )
    144   {
    145     delete_category( $row['id'] );
    146   }
     163  $query = '
     164SELECT id
     165  FROM '.CATEGORIES_TABLE.'
     166  WHERE site_id = '.$id.'
     167;';
     168  $result = mysql_query($query);
     169  $category_ids = array();
     170  while ($row = mysql_fetch_array($result))
     171  {
     172    array_push($category_ids, $row['id']);
     173  }
     174  delete_categories($category_ids);
    147175               
    148176  // destruction of the site
    149   $query = 'DELETE FROM '.PREFIX_TABLE.'sites';
    150   $query.= ' WHERE id = '.$id;
    151   $query.= ';';
    152   mysql_query( $query );
     177  $query = '
     178DELETE FROM '.SITES_TABLE.'
     179  WHERE id = '.$id.'
     180;';
     181  mysql_query($query);
    153182}
    154183       
    155184
    156 // The function delete_category deletes the category identified by the $id
    157 // It also deletes (in the database) :
    158 //    - all the images of the images (thanks to delete_image, see further)
    159 //    - all the links between images and this category
     185// The function delete_categories deletes the categories identified by the
     186// (numeric) key of the array $ids. It also deletes (in the database) :
     187//    - all the elements of the category (delete_elements, see further)
     188//    - all the links between elements and this category
    160189//    - all the restrictions linked to the category
    161190// The function works recursively.
    162 function delete_category( $id )
    163 {
    164   // destruction of all the related images
    165   $query = 'SELECT id';
    166   $query.= ' FROM '.PREFIX_TABLE.'images';
    167   $query.= ' WHERE storage_category_id = '.$id;
    168   $query.= ';';
    169   $result = mysql_query( $query );
    170   while ( $row = mysql_fetch_array( $result ) )
    171   {
    172     delete_image( $row['id'] );
    173   }
     191function delete_categories($ids)
     192{
     193  // destruction of all the related elements
     194  $query = '
     195SELECT id
     196  FROM '.IMAGES_TABLE.'
     197  WHERE storage_category_id IN ('.implode(',', $ids).')
     198;';
     199  $result = mysql_query($query);
     200  $element_ids = array();
     201  while ($row = mysql_fetch_array($result))
     202  {
     203    array_push($element_ids, $row['id']);
     204  }
     205  delete_elements($element_ids);
    174206
    175207  // destruction of the links between images and this category
    176   $query = 'DELETE FROM '.PREFIX_TABLE.'image_category';
    177   $query.= ' WHERE category_id = '.$id;
    178   $query.= ';';
    179   mysql_query( $query );
     208  $query = '
     209DELETE FROM '.IMAGE_CATEGORY_TABLE.'
     210  WHERE category_id IN ('.implode(',', $ids).')
     211;';
     212  mysql_query($query);
    180213
    181214  // destruction of the access linked to the category
    182   $query = 'DELETE FROM '.PREFIX_TABLE.'user_access';
    183   $query.= ' WHERE cat_id = '.$id;
    184   $query.= ';';
    185   mysql_query( $query );
    186   $query = 'DELETE FROM '.PREFIX_TABLE.'group_access';
    187   $query.= ' WHERE cat_id = '.$id;
    188   $query.= ';';
    189   mysql_query( $query );
     215  $query = '
     216DELETE FROM '.USER_ACCESS_TABLE.'
     217  WHERE cat_id IN ('.implode(',', $ids).')
     218;';
     219  mysql_query($query);
     220  $query = '
     221DELETE FROM '.GROUP_ACCESS_TABLE.'
     222  WHERE cat_id IN ('.implode(',', $ids).')
     223;';
     224  mysql_query($query);
    190225
    191226  // destruction of the sub-categories
    192   $query = 'SELECT id';
    193   $query.= ' FROM '.CATEGORIES_TABLE;
    194   $query.= ' WHERE id_uppercat = '.$id;
    195   $query.= ';';
    196   $result = mysql_query( $query );
    197   while( $row = mysql_fetch_array( $result ) )
    198   {
    199     delete_category( $row['id'] );
    200   }
     227  $query = '
     228SELECT id
     229  FROM '.CATEGORIES_TABLE.'
     230  WHERE id_uppercat IN ('.implode(',', $ids).')
     231;';
     232  $result = mysql_query($query);
     233  $subcat_ids = array();
     234  while($row = mysql_fetch_array($result))
     235  {
     236    array_push($subcat_ids, $row['id']);
     237  }
     238  delete_categories($subcat_ids);
    201239
    202240  // destruction of the category
    203   $query = 'DELETE FROM '.CATEGORIES_TABLE;
    204   $query.= ' WHERE id = '.$id;
    205   $query.= ';';
    206   mysql_query( $query );
     241  $query = '
     242DELETE FROM '.CATEGORIES_TABLE.'
     243  WHERE id IN ('.implode(',', $ids).')
     244;';
     245  mysql_query($query);
    207246}
    208247       
    209248
    210 // The function delete_image deletes the image identified by the $id
    211 // It also deletes (in the database) :
    212 //    - all the comments related to the image
    213 //    - all the links between categories and this image
    214 //    - all the favorites associated to the image
    215 function delete_image( $id )
    216 {
    217   global $count_deleted;
    218                
     249// The function delete_elements deletes the elements identified by the
     250// (numeric) values of the array $ids. It also deletes (in the database) :
     251//    - all the comments related to elements
     252//    - all the links between categories and elements
     253//    - all the favorites associated to elements
     254function delete_elements($ids)
     255{
    219256  // destruction of the comments on the image
    220   $query = 'DELETE FROM '.PREFIX_TABLE.'comments';
    221   $query.= ' WHERE image_id = '.$id;
    222   $query.= ';';
    223   mysql_query( $query );
     257  $query = '
     258DELETE FROM '.COMMENTS_TABLE.'
     259  WHERE image_id IN ('.implode(',', $ids).')
     260;';
     261  echo '<pre>'.$query.'</pre>';
     262  mysql_query($query);
    224263
    225264  // destruction of the links between images and this category
    226   $query = 'DELETE FROM '.PREFIX_TABLE.'image_category';
    227   $query.= ' WHERE image_id = '.$id;
    228   $query.= ';';
    229   mysql_query( $query );
     265  $query = '
     266DELETE FROM '.IMAGE_CATEGORY_TABLE.'
     267  WHERE image_id IN ('.implode(',', $ids).')
     268;';
     269  mysql_query($query);
    230270
    231271  // destruction of the favorites associated with the picture
    232   $query = 'DELETE FROM '.PREFIX_TABLE.'favorites';
    233   $query.= ' WHERE image_id = '.$id;
    234   $query.= ';';
    235   mysql_query( $query );
     272  $query = '
     273DELETE FROM '.FAVORITES_TABLE.'
     274  WHERE image_id IN ('.implode(',', $ids).')
     275;';
     276  mysql_query($query);
    236277               
    237278  // destruction of the image
    238   $query = 'DELETE FROM '.PREFIX_TABLE.'images';
    239   $query.= ' WHERE id = '.$id;
    240   $query.= ';';
    241   mysql_query( $query );
    242   $count_deleted++;
     279  $query = '
     280DELETE FROM '.IMAGES_TABLE.'
     281  WHERE id IN ('.implode(',', $ids).')
     282;';
     283  mysql_query($query);
    243284}
    244285
Note: See TracChangeset for help on using the changeset viewer.