Ignore:
Timestamp:
Feb 2, 2004, 1:55:18 AM (20 years ago)
Author:
gweltas
Message:

Merge of the 1.3.1 release
Creation of an unique include file (common.php)
Creation of an unique define file (include/constants.php)
Modification of the installation procedure

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions_category.inc.php

    r133 r345  
    1818 ***************************************************************************/
    1919
    20 function get_subcats_id( $cat_id )
    21 {
    22   $restricted_cats = array();
    23                
    24   $query = 'SELECT id';
    25   $query.= ' FROM '.PREFIX_TABLE.'categories';
    26   $query.= ' WHERE id_uppercat = '.$cat_id;
    27   $query.= ';';
    28   $result = mysql_query( $query );
    29   while ( $row = mysql_fetch_array( $result ) )
    30   {
    31     array_push( $restricted_cats, $row['id'] );
    32     $sub_restricted_cats = get_subcats_id( $row['id'] );
    33     foreach ( $sub_restricted_cats as $sub_restricted_cat ) {
    34       array_push( $restricted_cats, $sub_restricted_cat );
    35     }
    36   }
    37   return $restricted_cats;
    38 }
    39 
    4020function check_restrictions( $category_id )
    4121{
    4222  global $user,$lang;
    4323
    44   if ( is_user_allowed( $category_id, $user['restrictions'] ) > 0 )
     24  if ( in_array( $category_id, $user['restrictions'] ) )
    4525  {
    4626    echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />';
     
    5030  }
    5131}
    52        
     32
    5333// the check_cat_id function check whether the $cat is a right parameter :
    5434//  - $cat is numeric and corresponds to a category in the database
     
    6242  if ( isset( $cat ) )
    6343  {
    64     if ( isset( $page['plain_structure'] ) )
    65     {
    66       if ( isset( $page['plain_structure'][$cat] ) )
    67       {
    68         $page['cat'] = $cat;
    69       }
     44    if ( isset( $page['plain_structure'][$cat] ) )
     45    {
     46      $page['cat'] = $cat;
    7047    }
    7148    else if ( is_numeric( $cat ) )
    7249    {
    7350      $query = 'SELECT id';
    74       $query.= ' FROM '.PREFIX_TABLE.'categories';
    75       $query.= ' WHERE id = '.$cat;
    76       $query. ';';
     51      $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$cat.';';
    7752      $result = mysql_query( $query );
    7853      if ( mysql_num_rows( $result ) != 0 )
     
    9267}
    9368
    94 function get_plain_structure()
    95 {
    96   $infos = array( 'name','id','date_last','nb_images','dir','id_uppercat',
    97                   'rank','site_id');
     69function get_user_plain_structure()
     70{
     71  global $page,$user;
    9872 
    99   $query = 'SELECT ';
    100   foreach ( $infos as $i => $info ) {
    101     if ( $i > 0 ) $query.= ',';
    102     $query.= $info;
    103   }
    104   $query.= ' FROM '.PREFIX_TABLE.'categories';
     73  $infos = array( 'name','id','uc.date_last','nb_images','dir','id_uppercat',
     74                  'rank','site_id','nb_sub_categories','uppercats');
     75 
     76  $query = 'SELECT '.implode( ',', $infos );
     77  $query.= ' FROM '.CATEGORIES_TABLE.' AS c';
     78//  $query.= ' ,'.PREFIX_TABLE.'user_category AS uc';
     79  $query.= ' INNER JOIN '.USER_CATEGORY_TABLE.' AS uc';
     80  $query.= ' ON c.id = uc.category_id';
     81  $query.= ' WHERE user_id = '.$user['id'];
     82  if ( $page['expand'] != 'all' )
     83  {
     84    $query.= ' AND (id_uppercat is NULL';
     85    if ( count( $page['tab_expand'] ) > 0 )
     86    {
     87      $query.= ' OR id_uppercat IN ('.$page['expand'].')';
     88    }
     89    $query.= ')';
     90  }
     91  if ( $user['forbidden_categories'] != '' )
     92  {
     93    $query.= ' AND id NOT IN ';
     94    $query.= '('.$user['forbidden_categories'].')';
     95  }
     96//  $query.= ' AND c.id = uc.category_id';
    10597  $query.= ' ORDER BY id_uppercat ASC, rank ASC';
    10698  $query.= ';';
     
    112104    $category = array();
    113105    foreach ( $infos as $info ) {
    114       $category[$info] = $row[$info];
    115       if ( $info == 'date_last' )
    116       {
    117         list($year,$month,$day) = explode( '-', $row[$info] );
    118         $category[$info] = mktime(0,0,0,$month,$day,$year);
    119       }
     106      if ( $info == 'uc.date_last' )
     107      {
     108        list($year,$month,$day) = explode( '-', $row['date_last'] );
     109        $category['date_last'] = mktime(0,0,0,$month,$day,$year);
     110      }
     111      else if ( isset( $row[$info] ) ) $category[$info] = $row[$info];
     112      else                             $category[$info] = '';
    120113    }
    121114    $plain_structure[$row['id']] = $category;
     
    125118}
    126119
    127 function create_structure( $id_uppercat, $restrictions )
     120function create_user_structure( $id_uppercat )
    128121{
    129122  global $page;
    130123
    131124  if ( !isset( $page['plain_structure'] ) )
    132     $page['plain_structure'] = get_plain_structure();
     125    $page['plain_structure'] = get_user_plain_structure();
    133126
    134127  $structure = array();
    135   $ids = get_subcat_ids( $id_uppercat );
     128  $ids = get_user_subcat_ids( $id_uppercat );
    136129  foreach ( $ids as $id ) {
    137     if ( !in_array( $id, $restrictions ) )
    138     {
    139       $category = $page['plain_structure'][$id];
    140       $category['subcats'] = create_structure( $id, $restrictions );
    141       array_push( $structure, $category );
    142     }
     130    $category = $page['plain_structure'][$id];
     131    $category['subcats'] = create_user_structure( $id );
     132    array_push( $structure, $category );
    143133  }
    144134  return $structure;
    145135}
    146136
    147 function get_subcat_ids( $id_uppercat )
     137function get_user_subcat_ids( $id_uppercat )
    148138{
    149139  global $page;
     
    158148
    159149// update_structure updates or add informations about each node of the
    160 // structure : the last date, should the category be expanded in the menu ?,
    161 // the associated expand string "48,14,54"
     150// structure :
    162151//
    163 // 1. last date
    164 // for each category of the structure, we have to find the most recent
    165 // subcat so that the parent cat has the same last_date info.
    166 // For example : we have :
    167 // > pets       (2003.02.15)
    168 //    > dogs    (2003.06.14)
    169 //       > rex  (2003.06.18)
    170 //       > toby (2003.06.13)
    171 //    > kitten  (2003.07.05)
    172 // We finally want to have :
    173 // > pets       (2003.07.05) <- changed to pets > kitten last date
    174 //    > dogs    (2003.06.18) <- changed to pets > dogs > rex last date
    175 //       > rex  (2003.06.18)
    176 //       > toby (2003.06.13)
    177 //    > kitten  (2003.07.05)
    178 //
    179 // 2. should the category be expanded in the menu ?
     152// 1. should the category be expanded in the menu ?
    180153// If the category has to be expanded (ie its id is in the
    181154// $page['tab_expand'] or all the categories must be expanded by default),
    182155// $category['expanded'] is set to true.
    183156//
    184 // 3. associated expand string
     157// 2. associated expand string
    185158// in the menu, there is a expand string (used in the URL) to tell which
    186159// categories must be expanded in the menu if this category is chosen
     
    192165
    193166  foreach ( $categories as $category ) {
    194     // update the last date of the category
    195     $last_date = search_last_date( $category );
    196     $category['date_last'] = $last_date;
    197167    // update the "expanded" key
    198168    if ( $user['expand']
     
    206176      $category['expanded'] = false;
    207177    }
    208     // update the  "expand_string" key
     178    // update the "expand_string" key
    209179    if ( $page['expand'] == 'all' )
    210180    {
     
    220190        $tab_expand = array_diff( $page['tab_expand'],array($category['id']) );
    221191      }
    222       else if ( count( $category['subcats'] ) > 0 )
     192      else if ( $category['nb_sub_categories'] > 0 )
    223193      {
    224194        // we have this time to add the $category['id']...
     
    234204
    235205  return $updated_categories;
    236 }
    237 
    238 // search_last_date searchs the last date for a given category. If we take
    239 // back the example given for update_last_dates, we should have :
    240 // search_last_date( pets )        --> 2003.07.05
    241 // search_last_date( pets > dogs ) --> 2003.06.18
    242 // and so on
    243 function search_last_date( $category )
    244 {
    245   $date_last = $category['date_last'];
    246   foreach ( $category['subcats'] as $subcat ) {
    247     $subcat_date_last = search_last_date( $subcat );
    248     if ( $subcat_date_last > $date_last )
    249     {
    250       $date_last = $subcat_date_last;
    251     }
    252   }
    253   return $date_last;
    254206}
    255207
     
    263215function count_images( $categories )
    264216{
     217  return count_user_total_images();
    265218  $total = 0;
    266219  foreach ( $categories as $category ) {
     
    269222  }
    270223  return $total;
     224}
     225
     226function count_user_total_images()
     227{
     228  global $user;
     229
     230  $query = 'SELECT SUM(nb_images) AS total';
     231  $query.= ' FROM '.CATEGORIES_TABLE;
     232  if ( count( $user['restrictions'] ) > 0 )
     233    $query.= ' WHERE id NOT IN ('.$user['forbidden_categories'].')';
     234  $query.= ';';
     235 
     236  $row = mysql_fetch_array( mysql_query( $query ) );
     237
     238  if ( !isset( $row['total'] ) ) $row['total'] = 0;
     239
     240  return $row['total'];
    271241}
    272242
     
    286256  global $page;
    287257
     258  $infos = array( 'nb_images','id_uppercat','comment','site_id','galleries_url'
     259                  ,'dir','date_last','uploadable','status','visible'
     260                  ,'representative_picture_id','uppercats' );
     261
     262  $query = 'SELECT '.implode( ',', $infos );
     263  $query.= ' FROM '.CATEGORIES_TABLE.' AS a';
     264  $query.= ', '.SITES_TABLE.' AS b';
     265  $query.= ' WHERE a.id = '.$id;
     266  $query.= ' AND a.site_id = b.id';
     267  $query.= ';';
     268  $row = mysql_fetch_array( mysql_query( $query ) );
     269
    288270  $cat = array();
    289                
    290   $query = 'SELECT nb_images,id_uppercat,comment,site_id,galleries_url,dir';
    291   $query.= ',date_last,uploadable,status,visible,representative_picture_id';
    292   $query.= ' FROM '.PREFIX_TABLE.'categories AS a';
    293   $query.= ', '.PREFIX_TABLE.'sites AS b';
    294   $query.= ' WHERE a.id = '.$id;
    295   $query.= ' AND a.site_id = b.id;';
    296   $row = mysql_fetch_array( mysql_query( $query ) );
    297   $cat['site_id']     = $row['site_id'];
    298   $cat['id_uppercat'] = $row['id_uppercat'];
    299   $cat['comment']     = nl2br( $row['comment'] );
    300   $cat['nb_images']   = $row['nb_images'];
    301   $cat['dir']         = $row['dir'];
    302   $cat['date_last']   = $row['date_last'];
    303   $cat['uploadable']  = get_boolean( $row['uploadable'] );
    304   $cat['status']      = $row['status'];
    305   $cat['visible']     = get_boolean( $row['visible'] );
    306   $cat['representative_picture_id'] = $row['representative_picture_id'];
     271  // affectation of each field of the table "config" to an information of the
     272  // array $cat.
     273  foreach ( $infos as $info ) {
     274    if ( isset( $row[$info] ) ) $cat[$info] = $row[$info];
     275    else                        $cat[$info] = '';
     276    // If the field is true or false, the variable is transformed into a
     277    // boolean value.
     278    if ( $cat[$info] == 'true' or $cat[$info] == 'false' )
     279    {
     280      $cat[$info] = get_boolean( $cat[$info] );
     281    }
     282  }
     283  $cat['comment'] = nl2br( $cat['comment'] );
    307284
    308285  $cat['name'] = array();
    309286
    310   if ( !isset( $page['plain_structure'] ) )
    311     $page['plain_structure'] = get_plain_structure();
    312 
    313   array_push( $cat['name'], $page['plain_structure'][$id]['name'] );
    314   while ( $page['plain_structure'][$id]['id_uppercat'] != '' )
    315   {
    316     $id = $page['plain_structure'][$id]['id_uppercat'];
    317     array_push( $cat['name'], $page['plain_structure'][$id]['name'] );
    318   }
     287  $query = 'SELECT name FROM '.CATEGORIES_TABLE;
     288  $query.= ' WHERE id IN ('.$cat['uppercats'].')';
     289  $query.= ' ORDER BY id ASC';
     290  $query.= ';';
     291  $result = mysql_query( $query );
     292  while( $row = mysql_fetch_array( $result ) )
     293  {
     294    array_push( $cat['name'], $row['name'] );
     295  }
     296 
    319297  return $cat;
    320298}
     
    338316  global $page;
    339317
    340   if ( !isset( $page['plain_structure'] ) )
    341     $page['plain_structure'] = get_plain_structure();
    342  
    343   // creating the local path : "root_cat/sub_cat/sub_sub_cat/"
    344   $dir = $page['plain_structure'][$category_id]['dir'].'/';
    345   while ( $page['plain_structure'][$category_id]['id_uppercat'] != '' )
    346   {
    347     $category_id = $page['plain_structure'][$category_id]['id_uppercat'];
    348     $dir = $page['plain_structure'][$category_id]['dir'].'/'.$dir;
    349   }
    350   return $dir;
     318  $uppercats = '';
     319  $local_dir = '';
     320
     321  if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) )
     322  {
     323    $uppercats = $page['plain_structure'][$category_id]['uppercats'];
     324  }
     325  else
     326  {
     327    $query = 'SELECT uppercats';
     328    $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id;
     329    $query.= ';';
     330    $row = mysql_fetch_array( mysql_query( $query ) );
     331    $uppercats = $row['uppercats'];
     332  }
     333
     334  $upper_array = explode( ',', $uppercats );
     335
     336  $database_dirs = array();
     337  $query = 'SELECT id,dir';
     338  $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')';
     339  $query.= ';';
     340  $result = mysql_query( $query );
     341  while( $row = mysql_fetch_array( $result ) )
     342  {
     343    $database_dirs[$row['id']] = $row['dir'];
     344  }
     345  foreach ( $upper_array as $id ) {
     346    $local_dir.= $database_dirs[$id].'/';
     347  }
     348
     349  return $local_dir;
    351350}
    352351
     
    357356  global $page;
    358357
    359   if ( !isset( $page['plain_structure'] ) )
    360     $page['plain_structure'] = get_plain_structure();
    361 
    362358  $query = 'SELECT galleries_url';
    363   $query.= ' FROM '.PREFIX_TABLE.'sites';
    364   $query.= ' WHERE id = '.$page['plain_structure'][$category_id]['site_id'];
     359  $query.= ' FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c';
     360  $query.= ' WHERE s.id = c.site_id';
     361  $query.= ' AND c.id = '.$category_id;
    365362  $query.= ';';
    366363  $row = mysql_fetch_array( mysql_query( $query ) );
     
    375372//   - $style : the style of the span tag for the lowest category,
    376373//     "font-style:italic;" for example
    377 function get_cat_display_name( $array_cat_names, $separation, $style )
    378 {
    379   $output = "";
    380   for ( $i = sizeof( $array_cat_names ) - 1; $i >= 0; $i-- )
    381   {
    382     if ( $i != sizeof( $array_cat_names ) - 1 )
    383     {
    384       $output.= $separation;
    385     }
    386     if ( $i != 0 )
    387     {
    388       $output.= $array_cat_names[$i];
    389     }
     374function get_cat_display_name( $array_cat_names, $separation,
     375                               $style, $replace_space = true )
     376{
     377  $output = '';
     378  foreach ( $array_cat_names as $i => $name ) {
     379    if ( $i > 0 ) $output.= $separation;
     380    if ( $i < count( $array_cat_names ) - 1 or $style == '')
     381      $output.= $name;
    390382    else
    391     {
    392       if ( $style != "" )
    393       {
    394         $output.= '<span style="'.$style.'">';
    395       }
    396       $output.= $array_cat_names[$i];
    397       if ( $style != "" )
    398       {
    399         $output.= "</span>";
    400       }
    401     }
    402   }
    403   return replace_space( $output );
     383      $output.= '<span style="'.$style.'">'.$name.'</span>';
     384  }
     385  if ( $replace_space ) return replace_space( $output );
     386  else                  return $output;
    404387}
    405388
     
    419402function initialize_category( $calling_page = 'category' )
    420403{
     404  pwg_debug( 'start initialize_category' );
    421405  global $page,$lang,$user,$conf;
    422406
     
    427411    $page['nb_image_page'] = $user['nb_image_page'];
    428412    // $url is used to create the navigation bar
    429     $url = './category.php?cat='.$page['cat'].'&amp;expand='.$page['expand'];
     413    $url = './category.php?cat='.$page['cat'];
     414    if ( isset($page['expand']) ) $url.= '&amp;expand='.$page['expand'];
    430415    // simple category
    431416    if ( is_numeric( $page['cat'] ) )
     
    438423      $page['cat_site_id']    = $result['site_id'];
    439424      $page['cat_uploadable'] = $result['uploadable'];
    440       $page['title'] = get_cat_display_name( $page['cat_name'], ' - ', '' );
     425      $page['uppercats']      = $result['uppercats'];
     426      $page['title'] = get_cat_display_name( $page['cat_name'],' - ','',false);
    441427      $page['where'] = ' WHERE category_id = '.$page['cat'];
    442428    }
     
    447433      {
    448434        // we must not show pictures of a forbidden category
    449         $restricted_cats = get_all_restrictions( $user['id'],$user['status'] );
    450         if ( count( $restricted_cats ) > 0 )
     435        if ( $user['forbidden_categories'] != '' )
    451436        {
    452           $where_append.= ' AND category_id NOT IN (';
    453           foreach ( $restricted_cats as $i => $restricted_cat ) {
    454             if ( $i > 0 ) $where_append.= ',';
    455             $where_append.= $restricted_cat;
    456           }
    457           $where_append.= ')';
     437          $forbidden = ' category_id NOT IN ';
     438          $forbidden.= '('.$user['forbidden_categories'].')';
    458439        }
    459440      }
     
    500481        }
    501482        $page['where'].= ' )';
    502         $page['where'].= $where_append;
     483        if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden;
    503484
    504485        $query = 'SELECT COUNT(DISTINCT(id)) AS nb_total_images';
    505         $query.= ' FROM '.PREFIX_TABLE.'images';
    506         $query.= ' LEFT JOIN '.PREFIX_TABLE.'image_category AS ic';
     486        $query.= ' FROM '.IMAGES_TABLE;
     487        $query.= ' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic';
    507488        $query.= ' ON id = ic.image_id';
    508489        $query.= $page['where'];
     
    516497        $page['title'] = $lang['favorites'];
    517498
    518         $page['where'] = ', '.PREFIX_TABLE.'favorites AS fav';
     499        $page['where'] = ', '.FAVORITES_TABLE.' AS fav';
    519500        $page['where'].= ' WHERE user_id = '.$user['id'];
    520501        $page['where'].= ' AND fav.image_id = id';
    521502     
    522503        $query = 'SELECT COUNT(*) AS nb_total_images';
    523         $query.= ' FROM '.PREFIX_TABLE.'favorites';
     504        $query.= ' FROM '.FAVORITES_TABLE;
    524505        $query.= ' WHERE user_id = '.$user['id'];
    525506        $query.= ';';
     
    534515        $page['where'] = " WHERE date_available > '";
    535516        $page['where'].= date( 'Y-m-d', $date )."'";
    536         $page['where'].= $where_append;
     517        if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden;
    537518
    538519        $query = 'SELECT COUNT(DISTINCT(id)) AS nb_total_images';
    539         $query.= ' FROM '.PREFIX_TABLE.'images';
    540         $query.= ' LEFT JOIN '.PREFIX_TABLE.'image_category AS ic';
     520        $query.= ' FROM '.IMAGES_TABLE;
     521        $query.= ' INNER JOIN '.PREFIX_TABLE.'image_category AS ic';
    541522        $query.= ' ON id = ic.image_id';
    542523        $query.= $page['where'];
     
    547528      {
    548529        $page['title'] = $conf['top_number'].' '.$lang['most_visited_cat'];
    549         $page['where'] = ' WHERE category_id != -1'.$where_append;
     530       
     531        if ( isset( $forbidden ) ) $page['where'] = ' WHERE '.$forbidden;
     532        else                       $page['where'] = '';
    550533        $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
    551534        $page['cat_nb_images'] = $conf['top_number'];
    552         if ( $page['start'] + $user['nb_image_page'] >= $conf['top_number'] )
     535        if ( isset( $page['start'] )
     536             and ($page['start']+$user['nb_image_page']>=$conf['top_number']))
    553537        {
    554538          $page['nb_image_page'] = $conf['top_number'] - $page['start'];
     
    556540      }
    557541
    558       if ( $query != '' )
     542      if ( isset($query))
    559543      {
    560544        $result = mysql_query( $query );
     
    574558    $page['title'] = $lang['diapo_default_page_title'];
    575559  }
     560  pwg_debug( 'end initialize_category' );
    576561}
    577562
     
    600585
    601586  $query = 'SELECT id,nb_images';
    602   $query.= ' FROM '.PREFIX_TABLE.'categories';
     587  $query.= ' FROM '.CATEGORIES_TABLE;
    603588  $query.= ' WHERE id_uppercat ';
    604589  if ( !is_numeric( $id_uppercat ) ) $query.= 'is NULL';
    605590  else                               $query.= '= '.$id_uppercat;
    606591  // we must not show pictures of a forbidden category
    607   foreach ( $user['restrictions'] as $restricted_cat ) {
    608     $query.= ' AND id != '.$restricted_cat;
     592  if ( $user['forbidden_categories'] != '' )
     593  {
     594    $query.= ' AND id NOT IN ('.$user['forbidden_categories'].')';
    609595  }
    610596  $query.= ' ORDER BY rank';
     
    633619
    634620  $query = 'SELECT id,nb_images';
    635   $query.= ' FROM '.PREFIX_TABLE.'categories';
     621  $query.= ' FROM '.CATEGORIES_TABLE;
    636622  $query.= ' WHERE id_uppercat = '.$id_uppercat;
    637623  // we must not show pictures of a forbidden category
    638   foreach ( $user['restrictions'] as $restricted_cat ) {
    639     $query.= ' AND id != '.$restricted_cat;
     624  if ( $user['forbidden_categories'] != '' )
     625  {
     626    $query.= ' AND id NOT IN ('.$user['forbidden_categories'].')';
    640627  }
    641628  $query.= ' ORDER BY RAND()';
Note: See TracChangeset for help on using the changeset viewer.