Ignore:
Timestamp:
Nov 23, 2004, 11:31:24 PM (19 years ago)
Author:
plg
Message:
  • global categories' options : instead of N horizontal tabs on a single page, N options in the left menu (but the same backend)
  • categories.global_rank : new calculated field. It gives a global rank of the category among all others (updated during ordering)
  • category.php page : menu is generated faster thanks to categories.global_rank, recursivity becomes useless :-)
  • new function to display select box with a set of categories : display_select_cat_wrapper
  • cat_options : instead of using 1 multiselect for true/false items, using 1 multiselect for true, and another one for false. The form provides buttons with arrows to switch categories from one multiselect to another
  • deletion of obsolete function display_categories (working with the old template system)
  • deletion of obsolete functions get_user_plain_structure, create_user_structure, get_user_subcat_ids, update_structure, count_images : useless thanks to global_rank
File:
1 edited

Legend:

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

    r610 r614  
    117117}
    118118
    119 function get_user_plain_structure()
     119function get_categories_menu()
    120120{
    121121  global $page,$user;
    122122 
    123   $infos = array('name','id','date_last','nb_images','dir','id_uppercat',
    124                   'rank','site_id','uppercats');
     123  $infos = array('');
    125124 
    126125  $query = '
    127 SELECT '.implode(',', $infos).'
     126SELECT name,id,date_last,nb_images,global_rank
    128127  FROM '.CATEGORIES_TABLE.'
    129128  WHERE 1 = 1'; // stupid but permit using AND after it !
     
    144143  }
    145144  $query.= '
    146   ORDER BY id_uppercat ASC, rank ASC
    147145;';
    148146
    149   $plain_structure = array();
    150147  $result = pwg_query($query);
     148  $cats = array();
    151149  while ($row = mysql_fetch_array($result))
    152150  {
    153     $category = array();
    154     foreach ($infos as $info)
    155     {
    156       if ($info == 'uc.date_last')
    157       {
    158         if (empty($row['date_last']))
    159         {
    160           $category['date_last'] = 0;
    161         }
    162         else
    163         {
    164           list($year,$month,$day) = explode('-', $row['date_last']);
    165           $category['date_last'] = mktime(0,0,0,$month,$day,$year);
    166         }
    167       }
    168       else if (isset($row[$info]))
    169       {
    170         $category[$info] = $row[$info];
    171       }
    172       else
    173       {
    174         $category[$info] = '';
    175       }
    176     }
    177     $plain_structure[$row['id']] = $category;
    178   }
    179 
    180   return $plain_structure;
    181 }
    182 
    183 function create_user_structure( $id_uppercat )
    184 {
    185   global $page;
    186 
    187   if ( !isset( $page['plain_structure'] ) )
    188     $page['plain_structure'] = get_user_plain_structure();
    189 
    190   $structure = array();
    191   $ids = get_user_subcat_ids( $id_uppercat );
    192   foreach ( $ids as $id ) {
    193     $category = $page['plain_structure'][$id];
    194     $category['subcats'] = create_user_structure( $id );
    195     array_push( $structure, $category );
    196   }
    197   return $structure;
    198 }
    199 
    200 function get_user_subcat_ids( $id_uppercat )
    201 {
    202   global $page;
    203 
    204   $ids = array();
    205   foreach ( $page['plain_structure'] as $id => $category ) {
    206     if ( $category['id_uppercat'] == $id_uppercat ) array_push( $ids, $id );
    207     else if ( count( $ids ) > 0 )                   return $ids;
    208   }
    209   return $ids;
    210 }
    211 
    212 // update_structure updates or add informations about each node of the
    213 // structure :
    214 //
    215 // 1. should the category be expanded in the menu ?
    216 // If the category has to be expanded (ie its id is in the
    217 // $page['tab_expand'] or all the categories must be expanded by default),
    218 // $category['expanded'] is set to true.
    219 //
    220 // 2. associated expand string
    221 // in the menu, there is a expand string (used in the URL) to tell which
    222 // categories must be expanded in the menu if this category is chosen
    223 function update_structure( $categories )
    224 {
    225   global $page, $user;
    226 
    227   $updated_categories = array();
    228 
    229   foreach ( $categories as $category ) {
    230     // update the "expanded" key
    231     if ( $user['expand']
    232          or in_array( $category['id'], $page['tab_expand'] ) )
    233     {
    234       $category['expanded'] = true;
    235     }
    236     else
    237     {
    238       $category['expanded'] = false;
    239     }
    240     // recursive call
    241     $category['subcats'] = update_structure( $category['subcats'] );
    242     // adding the updated category
    243     array_push( $updated_categories, $category );
    244   }
    245 
    246   return $updated_categories;
    247 }
    248 
    249 // count_images returns the number of pictures contained in the given
    250 // category represented by an array, in this array, we have (among other
    251 // things) :
    252 // $category['nb_images'] -> number of pictures in this category
    253 // $category['subcats'] -> array of sub-categories
    254 // count_images goes to the deepest sub-category to find the total number of
    255 // pictures contained in the given given category
    256 function count_images( $categories )
    257 {
    258   return count_user_total_images();
    259   $total = 0;
    260   foreach ( $categories as $category ) {
    261     $total+= $category['nb_images'];
    262     $total+= count_images( $category['subcats'] );
    263   }
    264   return $total;
     151    array_push($cats, $row);
     152  }
     153  usort($cats, 'global_rank_compare');
     154
     155  return get_html_menu_category($cats);
    265156}
    266157
     
    830721
    831722function display_select_categories($categories,
    832                                    $indent,
    833723                                   $selecteds,
    834724                                   $blockname,
    835                                    $CSS_classes)
    836 {
    837   global $template,$user;
     725                                   $fullname = true)
     726{
     727  global $template;
    838728
    839729  foreach ($categories as $category)
    840730  {
    841     if (!in_array($category['id'], $user['restrictions']))
    842     {
    843       $selected = '';
    844       if (in_array($category['id'], $selecteds))
    845       {
    846         $selected = ' selected="selected"';
    847       }
    848 
    849       $class = '';
    850       foreach (array_keys($CSS_classes) as $CSS_class)
    851       {
    852         if (in_array($category['id'], $CSS_classes[$CSS_class]))
    853         {
    854           $class = $CSS_class;
    855         }
    856       }
    857 
    858       $template->assign_block_vars(
    859         $blockname,
    860         array('SELECTED'=>$selected,
    861               'VALUE'=>$category['id'],
    862               'CLASS'=>$class,
    863               'OPTION'=>$indent.'- '.$category['name']
    864               ));
    865      
    866       display_select_categories($category['subcats'],
    867                                 $indent.str_repeat(' ',3),
    868                                 $selecteds,
    869                                 $blockname,
    870                                 $CSS_classes);
    871     }
    872   }
     731    $selected = '';
     732    if (in_array($category['id'], $selecteds))
     733    {
     734      $selected = ' selected="selected"';
     735    }
     736
     737    if ($fullname)
     738    {
     739      $option = get_cat_display_name_cache($category['uppercats'],
     740                                           ' → ',
     741                                           '',
     742                                           false);
     743    }
     744    else
     745    {
     746      $option = str_repeat(' ',
     747                           (3 * substr_count($category['global_rank'], '.')));
     748      $option.= '- '.$category['name'];
     749    }
     750   
     751    $template->assign_block_vars(
     752      $blockname,
     753      array('SELECTED'=>$selected,
     754            'VALUE'=>$category['id'],
     755            'OPTION'=>$option
     756        ));
     757  }
     758}
     759
     760function display_select_cat_wrapper($query, $selecteds, $blockname,
     761                                    $fullname = true)
     762{
     763  $result = pwg_query($query);
     764  $categories = array();
     765  while ($row = mysql_fetch_array($result))
     766  {
     767    array_push($categories, $row);
     768  }
     769  usort($categories, 'global_rank_compare');
     770  display_select_categories($categories, $selecteds, $blockname, $fullname);
    873771}
    874772
     
    905803  return $subcats;
    906804}
     805
     806function global_rank_compare($a, $b)
     807{
     808  return strnatcasecmp($a['global_rank'], $b['global_rank']);
     809}
    907810?>
Note: See TracChangeset for help on using the changeset viewer.