Ignore:
Timestamp:
Jul 21, 2003, 9:47:14 PM (21 years ago)
Author:
z0rglub
Message:

* empty log message *

File:
1 edited

Legend:

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

    r21 r26  
    6060  if ( isset( $cat ) )
    6161  {
    62     if ( is_numeric( $cat ) )
    63     {
    64       $query = 'select id';
    65       $query.= ' from '.PREFIX_TABLE.'categories';
    66       $query.= ' where id = '.$cat;
     62    if ( isset( $page['plain_structure'] ) )
     63    {
     64      if ( isset( $page['plain_structure'][$cat] ) )
     65      {
     66        $page['cat'] = $cat;
     67      }
     68    }
     69    else if ( is_numeric( $cat ) )
     70    {
     71      $query = 'SELECT id';
     72      $query.= ' FROM '.PREFIX_TABLE.'categories';
     73      $query.= ' WHERE id = '.$cat;
    6774      $query. ';';
    6875      $result = mysql_query( $query );
     
    7279      }
    7380    }
    74     if ( $cat == 'fav' or $cat == 'search' or $cat == 'most_visited'
    75          or $cat == 'best_rated' or $cat == 'recent' )
     81    if ( $cat == 'fav'
     82         or $cat == 'search'
     83         or $cat == 'most_visited'
     84         or $cat == 'best_rated'
     85         or $cat == 'recent' )
    7686    {
    7787      $page['cat'] = $cat;
     
    8090}
    8191
    82 function display_cat( $id_uppercat, $indent, $restriction, $tab_expand )
    83 {
    84   global $user,$lang,$conf,$page,$vtp,$handle;
     92function get_plain_structure()
     93{
     94  $infos = array( 'name','id','date_last','nb_images','dir','id_uppercat',
     95                  'rank');
    8596 
    86   $query = 'select name,id,date_dernier,nb_images,dir';
    87   $query.= ' from '.PREFIX_TABLE.'categories';
    88   $query.= ' where id_uppercat';
    89   if ( $id_uppercat == "" )
    90   {
    91     $query.= ' is NULL';
    92   }
    93   else
    94   {
    95     $query.= ' = '.$id_uppercat;
    96   }
    97   $query.= ' order by rank asc;';
     97  $query = 'SELECT ';
     98  foreach ( $infos as $i => $info ) {
     99    if ( $i > 0 ) $query.= ',';
     100    $query.= $info;
     101  }
     102  $query.= ' FROM '.PREFIX_TABLE.'categories';
     103  $query.= ' ORDER BY id_uppercat ASC, rank ASC';
     104  $query.= ';';
     105
     106  $plain_structure = array();
    98107  $result = mysql_query( $query );
    99108  while ( $row = mysql_fetch_array( $result ) )
    100109  {
    101     if ( !in_array( $row['id'], $restriction ) )
    102     {
    103       $nb_subcats = get_nb_subcats( $row['id'] );
    104                                
    105       $expand = "";
    106       // si la catégorie n'a pas de sous catégorie
    107       // ou que l'on doit développer toutes les catégories par défaut
    108       // alors on utilise l'expand par défaut
    109       if ( $nb_subcats == 0 or $user['expand'] == "true" )
    110       {
    111         $expand = $page['expand'];
    112       }
    113       // si la catégorie n'est pas dans les catégories à développer
    114       // alors on l'ajoute aux catégories à développer
    115       else if ( !in_array( $row['id'], $tab_expand ) )
    116       {
    117         $expand = implode( ",", $tab_expand );
    118         if ( strlen( $expand ) > 0 )
    119         {
    120           $expand.= ",";
    121         }
    122         $expand.= $row['id'];
    123       }
    124       // si la catégorie est déjà dans les catégories à développer
    125       // alors on la retire des catégories à développer
    126       else
    127       {
    128         $expand = array_remove( $tab_expand, $row['id'] );
    129       }
    130       $url = "./category.php?cat=".$page['cat']."&expand=$expand";
    131       if ( $page['cat'] == 'search' )
    132       {
    133         $url.= "&search=".$_GET['search'].'&mode='.$_GET['mode'];
    134       }
    135       $lien_cat = add_session_id( $url );
    136       if ( $row['name'] == "" )
    137       {
    138         $name = str_replace( "_", " ", $row['dir'] );
    139       }
    140       else
    141       {
    142         $name = $row['name'];
    143       }
    144 
    145       $vtp->addSession( $handle, 'category' );
    146       $vtp->setVar( $handle, 'category.indent', $indent );
    147 
    148       if ( $user['expand'] == "true" or $nb_subcats == 0 )
    149       {
    150         $vtp->addSession( $handle, 'bullet_wo_link' );
    151         $vtp->setVar( $handle, 'bullet_wo_link.bullet_url',
    152                       $user['lien_collapsed'] );
    153         $vtp->setVar( $handle, 'bullet_wo_link.bullet_alt', '>' );
    154         $vtp->closeSession( $handle, 'bullet_wo_link' );
    155       }
    156       else
    157       {
    158         $vtp->addSession( $handle, 'bullet_w_link' );
    159         $vtp->setVar( $handle, 'bullet_w_link.bullet_link', $lien_cat );
    160         $vtp->setVar( $handle, 'bullet_w_link.bullet_alt', '>' );
    161         if ( in_array( $row['id'], $tab_expand ) )
    162         {
    163           $vtp->setVar( $handle, 'bullet_w_link.bullet_url',
    164                         $user['lien_expanded'] );
    165         }
    166         else
    167         {
    168           $vtp->setVar( $handle, 'bullet_w_link.bullet_url',
    169                         $user['lien_collapsed'] );
    170         }
    171         $vtp->closeSession( $handle, 'bullet_w_link' );
    172       }
    173       $vtp->setVar( $handle, 'category.link_url',
    174                     add_session_id( './category.php?cat='.
    175                                     $row['id'].'&expand='.$expand ) );
    176       $vtp->setVar( $handle, 'category.link_name', $name );
    177       if ( $id_uppercat == "" )
    178       {
    179         $vtp->setVar( $handle, 'category.name_style', 'font-weight:bold;' );
    180       }
    181       if ( $nb_subcats > 0 )
    182       {
    183         $vtp->addSession( $handle, 'subcat' );
    184         $vtp->setVar( $handle, 'subcat.nb_subcats', $nb_subcats );
    185         $vtp->closeSession( $handle, 'subcat' );
    186       }
    187       $vtp->setVar( $handle, 'category.total_cat', $row['nb_images'] );
    188       $date_dispo = explode( "-", $row['date_dernier'] );
    189       $date_cat = mktime( 0, 0, 0, $date_dispo[1], $date_dispo[2],
    190                           $date_dispo[0] );
    191       $vtp->setVar( $handle, 'category.cat_icon', get_icon( $date_cat ) );
    192       $vtp->closeSession( $handle, 'category' );
    193 
    194       if ( in_array( $row['id'], $tab_expand ) or $user['expand'] == "true" )
    195       {
    196         display_cat( $row['id'], $indent.'    ',
    197                      $restriction, $tab_expand );
    198       }
    199     }
    200   }
    201 }
    202        
    203 function get_nb_subcats( $id )
    204 {
    205   global $user;
    206                
    207   $query = 'select count(*) as count';
    208   $query.= ' from '.PREFIX_TABLE.'categories';
    209   $query.= ' where id_uppercat = '.$id;
    210   for ( $i = 0; $i < sizeof( $user['restrictions'] ); $i++ )
    211   {
    212     $query.= " and id != ".$user['restrictions'][$i];
    213   }
    214   $query.= ';';
    215   $result = mysql_query( $query );
    216   $row = mysql_fetch_array( $result );
    217   return $row['count'];
    218 }
    219        
    220 function get_total_image( $id, $restriction )
     110    $category = array();
     111    foreach ( $infos as $info ) {
     112      $category[$info] = $row[$info];
     113      if ( $info == 'date_last' )
     114      {
     115        list($year,$month,$day) = explode( '-', $row[$info] );
     116        $category[$info] = mktime(0,0,0,$month,$day,$year);
     117      }
     118    }
     119    $plain_structure[$row['id']] = $category;
     120  }
     121
     122  return $plain_structure;
     123}
     124
     125function create_structure( $id_uppercat, $restrictions )
     126{
     127  global $page;
     128
     129  $structure = array();
     130  $ids = get_subcat_ids( $id_uppercat );
     131  foreach ( $ids as $id ) {
     132    if ( !in_array( $id, $restrictions ) )
     133    {
     134      $category = $page['plain_structure'][$id];
     135      $category['subcats'] = create_structure( $id, $restrictions );
     136      array_push( $structure, $category );
     137    }
     138  }
     139  return $structure;
     140}
     141
     142function get_subcat_ids( $id_uppercat )
     143{
     144  global $page;
     145
     146  $ids = array();
     147  foreach ( $page['plain_structure'] as $id => $category ) {
     148    if ( $category['id_uppercat'] == $id_uppercat ) array_push( $ids, $id );
     149    else if ( count( $ids ) > 0 )                   return $ids;
     150  }
     151  return $ids;
     152}
     153
     154// update_structure updates or add informations about each node of the
     155// structure : the last date, should the category be expanded in the menu ?,
     156// the associated expand string "48,14,54"
     157//
     158// 1. last date
     159// for each category of the structure, we have to find the most recent
     160// subcat so that the parent cat has the same last_date info.
     161// For example : we have :
     162// > pets       (2003.02.15)
     163//    > dogs    (2003.06.14)
     164//       > rex  (2003.06.18)
     165//       > toby (2003.06.13)
     166//    > kitten  (2003.07.05)
     167// We finally want to have :
     168// > pets       (2003.07.05) <- changed to pets > kitten last date
     169//    > dogs    (2003.06.18) <- changed to pets > dogs > rex last date
     170//       > rex  (2003.06.18)
     171//       > toby (2003.06.13)
     172//    > kitten  (2003.07.05)
     173//
     174// 2. should the category be expanded in the menu ?
     175// If the category has to be expanded (ie its id is in the
     176// $page['tab_expand'] or all the categories must be expanded by default),
     177// $category['expanded'] is set to true.
     178//
     179// 3. associated expand string
     180// in the menu, there is a expand string (used in the URL) to tell which
     181// categories must be expanded in the menu if this category is chosen
     182function update_structure( $categories )
     183{
     184  global $page, $user;
     185
     186  $updated_categories = array();
     187
     188  foreach ( $categories as $category ) {
     189    // update the last date of the category
     190    $last_date = search_last_date( $category );
     191    $category['date_last'] = $last_date;
     192    // update the "expanded" key
     193    if ( $user['expand']
     194         or $page['expand'] == 'all'
     195         or in_array( $category['id'], $page['tab_expand'] ) )
     196    {
     197      $category['expanded'] = true;
     198    }
     199    else
     200    {
     201      $category['expanded'] = false;
     202    }
     203    // update the  "expand_string" key
     204    if ( $page['expand'] == 'all' )
     205    {
     206      $category['expand_string'] = 'all';
     207    }
     208    else
     209    {
     210      $tab_expand = $page['tab_expand'];
     211      if ( in_array( $category['id'], $page['tab_expand'] ) )
     212      {
     213        // the expand string corresponds to the $page['tab_expand'] without
     214        // the $category['id']
     215        $tab_expand = array_diff( $page['tab_expand'],array($category['id']) );
     216      }
     217      else if ( count( $category['subcats'] ) > 0 )
     218      {
     219        // we have this time to add the $category['id']...
     220        $tab_expand = array_merge($page['tab_expand'],array($category['id']));
     221      }
     222      $category['expand_string'] = implode( ',', $tab_expand );
     223    }
     224    // recursive call
     225    $category['subcats'] = update_structure( $category['subcats'] );
     226    // adding the updated category
     227    array_push( $updated_categories, $category );
     228  }
     229
     230  return $updated_categories;
     231}
     232
     233// search_last_date searchs the last date for a given category. If we take
     234// back the example given for update_last_dates, we should have :
     235// search_last_date( pets )        --> 2003.07.05
     236// search_last_date( pets > dogs ) --> 2003.06.18
     237// and so on
     238function search_last_date( $category )
     239{
     240  $date_last = $category['date_last'];
     241  foreach ( $category['subcats'] as $subcat ) {
     242    $subcat_date_last = search_last_date( $subcat );
     243    if ( $subcat_date_last > $date_last )
     244    {
     245      $date_last = $subcat_date_last;
     246    }
     247  }
     248  return $date_last;
     249}
     250
     251// count_images returns the number of pictures contained in the given
     252// category represented by an array, in this array, we have (among other
     253// things) :
     254// $category['nb_images'] -> number of pictures in this category
     255// $category['subcats'] -> array of sub-categories
     256// count_images goes to the deepest sub-category to find the total number of
     257// pictures contained in the given given category
     258function count_images( $categories )
    221259{
    222260  $total = 0;
    223                
    224   $query = 'select id,nb_images';
    225   $query.= ' from '.PREFIX_TABLE.'categories';
    226   $query.= ' where id_uppercat';
    227   if ( !is_numeric( $id ) )
    228   {
    229     $query.= ' is NULL';
    230   }
    231   else
    232   {
    233     $query.= ' = '.$id;
    234   }
    235   $query.= ";";
    236   $result = mysql_query( $query );
    237   while ( $row = mysql_fetch_array( $result ) )
    238   {
    239     if ( !in_array( $row['id'], $restriction ) )
    240     {
    241       $total+= $row['nb_images'];
    242       $total+= get_total_image( $row['id'], $restriction );
    243     }
     261  foreach ( $categories as $category ) {
     262    $total+= $category['nb_images'];
     263    $total+= count_images( $category['subcats'] );
    244264  }
    245265  return $total;
     
    262282  $cat['name'] = array();
    263283               
    264   $query = 'select nb_images,id_uppercat,comment,site_id,galleries_url,dir';
    265   $query.= ' from '.PREFIX_TABLE.'categories as a';
    266   $query.= ', '.PREFIX_TABLE.'sites as b';
    267   $query.= ' where a.id = '.$id;
    268   $query.= ' and a.site_id = b.id;';
     284  $query = 'SELECT nb_images,id_uppercat,comment,site_id,galleries_url,dir';
     285  $query.= ',date_last';
     286  $query.= ' FROM '.PREFIX_TABLE.'categories AS a';
     287  $query.= ', '.PREFIX_TABLE.'sites AS b';
     288  $query.= ' WHERE a.id = '.$id;
     289  $query.= ' AND a.site_id = b.id;';
    269290  $row = mysql_fetch_array( mysql_query( $query ) );
    270291  $cat['site_id']     = $row['site_id'];
     
    273294  $cat['nb_images']   = $row['nb_images'];
    274295  $cat['last_dir']    = $row['dir'];
     296  $cat['date_last']   = $row['date_last'];
    275297  $galleries_url = $row['galleries_url'];
    276298               
     
    281303  while ( !$is_root )
    282304  {
    283     $query = 'select name,dir,id_uppercat';
    284     $query.= ' from '.PREFIX_TABLE.'categories';
    285     $query.= ' where id = '.$row['id_uppercat'].';';
     305    $query = 'SELECT name,dir,id_uppercat';
     306    $query.= ' FROM '.PREFIX_TABLE.'categories';
     307    $query.= ' WHERE id = '.$row['id_uppercat'].';';
    286308    $row = mysql_fetch_array( mysql_query( $query ) );
    287     $cat['dir'] = $row['dir']."/".$cat['dir'];
     309    $cat['dir'] = $row['dir'].'/'.$cat['dir'];
    288310    if ( $row['name'] == "" )
    289311    {
     
    503525}
    504526
    505 // get_non_empty_sub_cat_ids returns an array composing of the infos of the
    506 // direct sub-categories of the given uppercat id. Each of these infos is
    507 // associated to the first found non empty category id. eg :
     527// get_non_empty_subcat_ids returns an array with sub-categories id
     528// associated with their first non empty category id.
     529//
     530//                          example :
    508531//
    509532// - catname [cat_id]
    510533// - cat1 [1] -> given uppercat
    511 //   - cat1.1 [2] (empty)
     534//   - cat1.1 [12] (empty)
    512535//     - cat1.1.1 [5] (empty)
    513536//     - cat1.1.2 [6]
     
    516539//
    517540// get_non_empty_sub_cat_ids will return :
    518 //   $cats[0]['id']            = 2;
    519 //   $cats[0]['name']          = '';
    520 //   $cats[0]['dir']           = 'cat1';
    521 //   $cats[0]['date_dernier']  = '2003-05-17';
    522 //   $cats[0]['non_empty_cat'] = 6;
    523 //
    524 //   $cats[1]['id']            = 3;
    525 //   $cats[1]['non_empty_cat'] = 3;
    526 //
    527 //   $cats[1]['id']            = 4;
    528 //   $cats[1]['non_empty_cat'] = 4;
    529 function get_non_empty_sub_cat_ids( $id_uppercat )
     541//   $ids[12] = 6;
     542//   $ids[3]  = 3;
     543//   $ids[4]  = 4;
     544function get_non_empty_subcat_ids( $id_uppercat )
    530545{
    531546  global $user;
    532547
    533   $cats = array();
    534 
    535   $query = 'SELECT id,name,dir,date_dernier,nb_images';
     548  $ids = array();
     549
     550  $query = 'SELECT id,nb_images';
    536551  $query.= ' FROM '.PREFIX_TABLE.'categories';
    537   $query.= ' WHERE id_uppercat = '.$id_uppercat;
     552  $query.= ' WHERE id_uppercat ';
     553  if ( !is_numeric( $id_uppercat ) ) $query.= 'is NULL';
     554  else                               $query.= '= '.$id_uppercat;
    538555  // we must not show pictures of a forbidden category
    539   $restricted_cats = get_all_restrictions( $user['id'],$user['status'] );
    540   foreach ( $restricted_cats as $restricted_cat ) {
     556  foreach ( $user['restrictions'] as $restricted_cat ) {
    541557    $query.= ' AND id != '.$restricted_cat;
    542558  }
     
    552568         or $non_empty_cat = get_first_non_empty_cat_id( $row['id'] ) )
    553569    {
    554       $temp_cat = array(
    555         'id'            => $row['id'],
    556         'name'          => $row['name'],
    557         'dir'           => $row['dir'],
    558         'date_dernier'  => $row['date_dernier'],
    559         'non_empty_cat' => $non_empty_cat );
    560       array_push( $cats, $temp_cat );
    561     }
    562   }
    563   return $cats;
     570      $ids[$row['id']] = $non_empty_cat;
     571    }
     572  }
     573  return $ids;
    564574}
    565575
     
    575585  $query.= ' WHERE id_uppercat = '.$id_uppercat;
    576586  // we must not show pictures of a forbidden category
    577   $restricted_cats = get_all_restrictions( $user['id'],$user['status'] );
    578   foreach ( $restricted_cats as $restricted_cat ) {
     587  foreach ( $user['restrictions'] as $restricted_cat ) {
    579588    $query.= ' AND id != '.$restricted_cat;
    580589  }
Note: See TracChangeset for help on using the changeset viewer.