Changeset 244


Ignore:
Timestamp:
Jan 3, 2004, 9:13:42 PM (20 years ago)
Author:
z0rglub
Message:
  • remove get_subcats_id
  • replacing function get_plain_structure by get_user_plain_structure which only retrieve categories shown in the menu
  • replacing function create_structure by create_user_structure
  • remove function search_last_date (now stored in the database)
  • add function count_user_total_images
  • management of the new database info : categories.uppercats (modification in function get_local_dir)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/release-1_3/include/functions_category.inc.php

    r133 r244  
    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 ) )
     
    7451      $query.= ' FROM '.PREFIX_TABLE.'categories';
    7552      $query.= ' WHERE id = '.$cat;
    76       $query. ';';
     53      $query.= ';';
    7754      $result = mysql_query( $query );
    7855      if ( mysql_num_rows( $result ) != 0 )
     
    9269}
    9370
    94 function get_plain_structure()
    95 {
    96   $infos = array( 'name','id','date_last','nb_images','dir','id_uppercat',
    97                   'rank','site_id');
     71function get_user_plain_structure()
     72{
     73  global $page,$user;
     74 
     75  $infos = array( 'name','id','uc.date_last','nb_images','dir','id_uppercat',
     76                  'rank','site_id','nb_sub_categories','uppercats');
    9877 
    9978  $query = 'SELECT ';
     
    10281    $query.= $info;
    10382  }
    104   $query.= ' FROM '.PREFIX_TABLE.'categories';
     83  $query.= ' FROM '.PREFIX_TABLE.'categories AS c';
     84//  $query.= ' ,'.PREFIX_TABLE.'user_category AS uc';
     85  $query.= ' INNER JOIN '.PREFIX_TABLE.'user_category AS uc';
     86  $query.= ' ON c.id = uc.category_id';
     87  $query.= ' WHERE user_id = '.$user['id'];
     88  if ( $page['expand'] != 'all' )
     89  {
     90    $query.= ' AND (id_uppercat is NULL';
     91    if ( count( $page['tab_expand'] ) > 0 )
     92    {
     93      $query.= ' OR id_uppercat IN ('.$page['expand'].')';
     94    }
     95    $query.= ')';
     96  }
     97  if ( $user['forbidden_categories'] != '' )
     98  {
     99    $query.= ' AND id NOT IN ';
     100    $query.= '('.$user['forbidden_categories'].')';
     101  }
     102//  $query.= ' AND c.id = uc.category_id';
    105103  $query.= ' ORDER BY id_uppercat ASC, rank ASC';
    106104  $query.= ';';
     
    125123}
    126124
    127 function create_structure( $id_uppercat, $restrictions )
     125function create_user_structure( $id_uppercat )
    128126{
    129127  global $page;
    130128
    131129  if ( !isset( $page['plain_structure'] ) )
    132     $page['plain_structure'] = get_plain_structure();
     130    $page['plain_structure'] = get_user_plain_structure();
    133131
    134132  $structure = array();
    135   $ids = get_subcat_ids( $id_uppercat );
     133  $ids = get_user_subcat_ids( $id_uppercat );
    136134  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     }
     135    $category = $page['plain_structure'][$id];
     136    $category['subcats'] = create_user_structure( $id );
     137    array_push( $structure, $category );
    143138  }
    144139  return $structure;
    145140}
    146141
    147 function get_subcat_ids( $id_uppercat )
     142function get_user_subcat_ids( $id_uppercat )
    148143{
    149144  global $page;
     
    158153
    159154// 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"
     155// structure :
    162156//
    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 ?
     157// 1. should the category be expanded in the menu ?
    180158// If the category has to be expanded (ie its id is in the
    181159// $page['tab_expand'] or all the categories must be expanded by default),
    182160// $category['expanded'] is set to true.
    183161//
    184 // 3. associated expand string
     162// 2. associated expand string
    185163// in the menu, there is a expand string (used in the URL) to tell which
    186164// categories must be expanded in the menu if this category is chosen
     
    192170
    193171  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;
    197172    // update the "expanded" key
    198173    if ( $user['expand']
     
    206181      $category['expanded'] = false;
    207182    }
    208     // update the  "expand_string" key
     183    // update the "expand_string" key
    209184    if ( $page['expand'] == 'all' )
    210185    {
     
    220195        $tab_expand = array_diff( $page['tab_expand'],array($category['id']) );
    221196      }
    222       else if ( count( $category['subcats'] ) > 0 )
     197      else if ( $category['nb_sub_categories'] > 0 )
    223198      {
    224199        // we have this time to add the $category['id']...
     
    234209
    235210  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;
    254211}
    255212
     
    263220function count_images( $categories )
    264221{
     222  return count_user_total_images();
    265223  $total = 0;
    266224  foreach ( $categories as $category ) {
     
    269227  }
    270228  return $total;
     229}
     230
     231function count_user_total_images()
     232{
     233  global $user;
     234
     235  $query = 'SELECT SUM(nb_images) AS total';
     236  $query.= ' FROM '.PREFIX_TABLE.'categories';
     237  if ( count( $user['restrictions'] ) > 0 )
     238    $query.= ' WHERE id NOT IN ('.$user['forbidden_categories'].')';
     239  $query.= ';';
     240 
     241  $row = mysql_fetch_array( mysql_query( $query ) );
     242
     243  return $row['total'];
    271244}
    272245
     
    290263  $query = 'SELECT nb_images,id_uppercat,comment,site_id,galleries_url,dir';
    291264  $query.= ',date_last,uploadable,status,visible,representative_picture_id';
     265  $query.= ',uppercats';
    292266  $query.= ' FROM '.PREFIX_TABLE.'categories AS a';
    293267  $query.= ', '.PREFIX_TABLE.'sites AS b';
     
    304278  $cat['status']      = $row['status'];
    305279  $cat['visible']     = get_boolean( $row['visible'] );
     280  $cat['uppercats']   = $row['uppercats'];
    306281  $cat['representative_picture_id'] = $row['representative_picture_id'];
    307282
    308283  $cat['name'] = array();
    309284
    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   }
     285  $query = 'SELECT name';
     286  $query.= ' FROM '.PREFIX_TABLE.'categories';
     287  $query.= ' WHERE id IN ('.$cat['uppercats'].')';
     288  $query.= ' ORDER BY id ASC';
     289  $query.= ';';
     290  $result = mysql_query( $query );
     291  while( $row = mysql_fetch_array( $result ) )
     292  {
     293    array_push( $cat['name'], $row['name'] );
     294  }
     295 
    319296  return $cat;
    320297}
     
    338315  global $page;
    339316
    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;
     317  $uppercats = '';
     318  $local_dir = '';
     319
     320  if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) )
     321  {
     322    $uppercats = $page['plain_structure'][$category_id]['uppercats'];
     323  }
     324  else
     325  {
     326    $query = 'SELECT uppercats';
     327    $query.= ' FROM '.PREFIX_TABLE.'categories';
     328    $query.= ' WHERE id = '.$category_id;
     329    $query.= ';';
     330    $row = mysql_fetch_array( mysql_query( $query ) );
     331    $uppercats = $row['uppercats'];
     332  }
     333
     334  $query = 'SELECT dir';
     335  $query.= ' FROM '.PREFIX_TABLE.'categories';
     336  $query.= ' WHERE id IN ('.$uppercats.')';
     337  $query.= ' ORDER BY id ASC';
     338  $query.= ';';
     339  $result = mysql_query( $query );
     340  while( $row = mysql_fetch_array( $result ) )
     341  {
     342    $local_dir.= $row['dir'].'/';
     343  }
     344
     345  return $local_dir;
    351346}
    352347
     
    357352  global $page;
    358353
    359   if ( !isset( $page['plain_structure'] ) )
    360     $page['plain_structure'] = get_plain_structure();
    361 
    362354  $query = 'SELECT galleries_url';
    363   $query.= ' FROM '.PREFIX_TABLE.'sites';
    364   $query.= ' WHERE id = '.$page['plain_structure'][$category_id]['site_id'];
     355  $query.= ' FROM '.PREFIX_TABLE.'sites AS s,'.PREFIX_TABLE.'categories AS c';
     356  $query.= ' WHERE s.id = c.site_id';
     357  $query.= ' AND c.id = '.$category_id;
    365358  $query.= ';';
    366359  $row = mysql_fetch_array( mysql_query( $query ) );
     
    375368//   - $style : the style of the span tag for the lowest category,
    376369//     "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     }
     370function get_cat_display_name( $array_cat_names, $separation,
     371                               $style, $replace_space = true )
     372{
     373  $output = '';
     374  foreach ( $array_cat_names as $i => $name ) {
     375    if ( $i > 0 ) $output.= $separation;
     376    if ( $i < count( $array_cat_names ) - 1 or $style == '')
     377      $output.= $name;
    390378    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 );
     379      $output.= '<span style="'.$style.'">'.$name.'</span>';
     380  }
     381  if ( $replace_space ) return replace_space( $output );
     382  else                  return $output;
    404383}
    405384
     
    419398function initialize_category( $calling_page = 'category' )
    420399{
     400  pwg_debug( 'start initialize_category' );
    421401  global $page,$lang,$user,$conf;
    422402
     
    438418      $page['cat_site_id']    = $result['site_id'];
    439419      $page['cat_uploadable'] = $result['uploadable'];
    440       $page['title'] = get_cat_display_name( $page['cat_name'], ' - ', '' );
     420      $page['uppercats']      = $result['uppercats'];
     421      $page['title'] = get_cat_display_name( $page['cat_name'],' - ','',false);
    441422      $page['where'] = ' WHERE category_id = '.$page['cat'];
    442423    }
     
    447428      {
    448429        // 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 )
     430        if ( $user['forbidden_categories'] != '' )
    451431        {
    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.= ')';
     432          $query.= ' AND category_id NOT IN ';
     433          $query.= '('.$user['forbidden_categories'].')';
    458434        }
    459435      }
     
    574550    $page['title'] = $lang['diapo_default_page_title'];
    575551  }
     552  pwg_debug( 'end initialize_category' );
    576553}
    577554
     
    605582  else                               $query.= '= '.$id_uppercat;
    606583  // we must not show pictures of a forbidden category
    607   foreach ( $user['restrictions'] as $restricted_cat ) {
    608     $query.= ' AND id != '.$restricted_cat;
     584  if ( $user['forbidden_categories'] != '' )
     585  {
     586    $query.= ' AND id NOT IN ('.$user['forbidden_categories'].')';
    609587  }
    610588  $query.= ' ORDER BY rank';
     
    636614  $query.= ' WHERE id_uppercat = '.$id_uppercat;
    637615  // we must not show pictures of a forbidden category
    638   foreach ( $user['restrictions'] as $restricted_cat ) {
    639     $query.= ' AND id != '.$restricted_cat;
     616  if ( $user['forbidden_categories'] != '' )
     617  {
     618    $query.= ' AND id NOT IN ('.$user['forbidden_categories'].')';
    640619  }
    641620  $query.= ' ORDER BY RAND()';
Note: See TracChangeset for help on using the changeset viewer.