Ignore:
Timestamp:
Feb 12, 2006, 10:52:16 PM (18 years ago)
Author:
plg
Message:

improvement: $pagewhere string replaced by $pageitems.
$pagewhere was an SQL clause used to retrieve pictures in #images
table. $pageitems is the list of picture ids of the current section.

improvement: function initialize_category replaced by dedicated included PHP
script include/section_init.inc.php. Code was refactored to improve
readibility and maintenability. $pagenavigation_bar is now build in
category.php instead of initialize_category function. Function check_cat_id
was also replaced by a piece of code in the new file. The file to include to
display thumbnails from category.php is now set in section_init.inc.php
instead of calculated in category.php.

bug fix: the test for rel="up" link for standard HTML navigation links in
category menu was not working with non numeric categories, such as
"favorites".

improvement: function check_login_authorization removed because useless but
in profile.php.

File:
1 edited

Legend:

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

    r1031 r1036  
    5454}
    5555
    56 /**
    57  * Checks whether the argument is a right parameter category id
    58  *
    59  * The argument is a right parameter if corresponds to one of these :
    60  *
    61  *  - is numeric and corresponds to a category in the database
    62  *  - equals 'fav' (for favorites)
    63  *  - equals 'search' (when the result of a search is displayed)
    64  *  - equals 'most_visited'
    65  *  - equals 'best_rated'
    66  *  - equals 'recent_pics'
    67  *  - equals 'recent_cats'
    68  *  - equals 'calendar'
    69  *  - equals 'list'
    70  *
    71  * The function fills the global var $page['cat'] and returns nothing
    72  *
    73  * @param mixed category id or special category name
    74  * @return void
    75  */
    76 function check_cat_id( $cat )
    77 {
    78   global $page;
    79 
    80   unset( $page['cat'] );
    81   if ( isset( $cat ) )
    82   {
    83     if ( isset( $page['plain_structure'][$cat] ) )
    84     {
    85       $page['cat'] = $cat;
    86     }
    87     else if ( is_numeric( $cat ) )
    88     {
    89       $query = 'SELECT id';
    90       $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$cat.';';
    91       $result = pwg_query( $query );
    92       if ( mysql_num_rows( $result ) != 0 )
    93       {
    94         $page['cat'] = $cat;
    95       }
    96     }
    97     if ( $cat == 'fav'
    98          or $cat == 'most_visited'
    99          or $cat == 'best_rated'
    100          or $cat == 'recent_pics'
    101          or $cat == 'recent_cats'
    102          or $cat == 'calendar' )
    103     {
    104       $page['cat'] = $cat;
    105     }
    106     if ($cat == 'search'
    107         and isset($_GET['search'])
    108         and is_numeric($_GET['search']))
    109     {
    110       $page['cat'] = $cat;
    111     }
    112     if ($cat == 'list'
    113         and isset($_GET['list'])
    114         and preg_match('/^\d+(,\d+)*$/', $_GET['list']))
    115     {
    116       $page['cat'] = 'list';
    117     }
    118   }
    119 }
    120 
    12156function get_categories_menu()
    12257{
     
    333268}
    334269
    335 
    336 // initialize_category initializes ;-) the variables in relation
    337 // with category :
    338 // 1. calculation of the number of pictures in the category
    339 // 2. determination of the SQL query part to ask to find the right category
    340 //    $page['where'] is not the same if we are in
    341 //       - simple category
    342 //       - search result
    343 //       - favorites displaying
    344 //       - most visited pictures
    345 //       - best rated pictures
    346 //       - recent pictures
    347 //       - defined list (used for random)
    348 // 3. determination of the title of the page
    349 // 4. creation of the navigation bar
    350 function initialize_category( $calling_page = 'category' )
    351 {
    352   pwg_debug( 'start initialize_category' );
    353   global $page,$lang,$user,$conf;
    354 
    355   if ( isset( $page['cat'] ) )
    356   {
    357     // $page['nb_image_page'] is the number of picture to display on this page
    358     // By default, it is the same as the $user['nb_image_page']
    359     $page['nb_image_page'] = $user['nb_image_page'];
    360     // $url is used to create the navigation bar
    361     $url = PHPWG_ROOT_PATH.'category.php?cat='.$page['cat'];
    362     if ( isset($page['expand']) ) $url.= '&expand='.$page['expand'];
    363     // simple category
    364     if ( is_numeric( $page['cat'] ) )
    365     {
    366       $result = get_cat_info( $page['cat'] );
    367       $page['comment']        = $result['comment'];
    368       $page['cat_dir']        = $result['dir'];
    369       $page['cat_name']       = $result['name'];
    370       $page['cat_nb_images']  = $result['nb_images'];
    371       $page['cat_site_id']    = $result['site_id'];
    372       $page['cat_uploadable'] = $result['uploadable'];
    373       $page['cat_commentable'] = $result['commentable'];
    374       $page['cat_id_uppercat'] = $result['id_uppercat'];
    375       $page['uppercats']      = $result['uppercats'];
    376       $page['title'] =
    377         get_cat_display_name($page['cat_name'],
    378                              '',
    379                              false);
    380       $page['where'] = ' WHERE category_id = '.$page['cat'];
    381     }
    382     else
    383     {
    384       if ($page['cat'] == 'search'
    385           or $page['cat'] == 'most_visited'
    386           or $page['cat'] == 'recent_pics'
    387           or $page['cat'] == 'recent_cats'
    388           or $page['cat'] == 'best_rated'
    389           or $page['cat'] == 'calendar'
    390           or $page['cat'] == 'list')
    391       {
    392         // we must not show pictures of a forbidden category
    393         if ( $user['forbidden_categories'] != '' )
    394         {
    395           $forbidden = ' category_id NOT IN ';
    396           $forbidden.= '('.$user['forbidden_categories'].')';
    397         }
    398       }
    399       // search result
    400       if ( $page['cat'] == 'search' )
    401       {
    402         $page['title'] = $lang['search_result'];
    403         if ( $calling_page == 'picture' )
    404         {
    405           $page['title'].= ' : <span style="font-style:italic;">';
    406           $page['title'].= $_GET['search']."</span>";
    407         }
    408 
    409         $page['where'] = 'WHERE '.get_sql_search_clause($_GET['search']);
    410        
    411         if (isset($forbidden))
    412         {
    413           $page['where'].= "\n    AND ".$forbidden;
    414         }
    415 
    416         $query = '
    417 SELECT COUNT(DISTINCT(id)) AS nb_total_images
    418   FROM '.IMAGES_TABLE.'
    419     INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
    420   '.$page['where'].'
    421 ;';
    422         $url.= '&amp;search='.$_GET['search'];
    423       }
    424       // favorites displaying
    425       else if ( $page['cat'] == 'fav' )
    426       {
    427         check_user_favorites();
    428        
    429         $page['title'] = $lang['favorites'];
    430 
    431         $page['where'] = ', '.FAVORITES_TABLE.' AS fav';
    432         $page['where'].= ' WHERE user_id = '.$user['id'];
    433         $page['where'].= ' AND fav.image_id = id';
    434      
    435         $query = 'SELECT COUNT(*) AS nb_total_images';
    436         $query.= ' FROM '.FAVORITES_TABLE;
    437         $query.= ' WHERE user_id = '.$user['id'];
    438         $query.= ';';
    439       }
    440       // pictures within the short period
    441       else if ( $page['cat'] == 'recent_pics' )
    442       {
    443         $page['title'] = $lang['recent_pics_cat'];
    444         // We must find the date corresponding to :
    445         // today - $conf['periode_courte']
    446         $date = time() - 60*60*24*$user['recent_period'];
    447         $page['where'] = " WHERE date_available > '";
    448         $page['where'].= date( 'Y-m-d', $date )."'";
    449         if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden;
    450 
    451         $query = '
    452 SELECT COUNT(DISTINCT(id)) AS nb_total_images
    453   FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic
    454     ON id = ic.image_id
    455   '.$page['where'].'
    456 ;';
    457       }
    458       // categories containing recent pictures
    459       else if ( $page['cat'] == 'recent_cats' )
    460       {
    461         $page['title'] = $lang['recent_cats_cat'];
    462         $page['cat_nb_images'] = 0;
    463       }
    464       // most visited pictures
    465       else if ( $page['cat'] == 'most_visited' )
    466       {
    467         $page['title'] = $conf['top_number'].' '.$lang['most_visited_cat'];
    468 
    469         $page['where'] = 'WHERE hit > 0';
    470         if (isset($forbidden))
    471         {
    472           $page['where'] .= "\n".'    AND '.$forbidden;
    473         }
    474 
    475         $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
    476 
    477         // $page['cat_nb_images'] equals $conf['top_number'] unless there
    478         // are less visited items
    479         $query ='
    480 SELECT COUNT(DISTINCT(id)) AS count
    481   FROM '.IMAGES_TABLE.'
    482     INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
    483   '.$page['where'].'
    484 ;';
    485         $row = mysql_fetch_array(pwg_query($query));
    486         if ($row['count'] < $conf['top_number'])
    487         {
    488           $page['cat_nb_images'] = $row['count'];
    489         }
    490         else
    491         {
    492           $page['cat_nb_images'] = $conf['top_number'];
    493         }
    494         unset($query);
    495        
    496         if ( isset( $page['start'] )
    497              and ($page['start']+$user['nb_image_page']>=$conf['top_number']))
    498         {
    499           $page['nb_image_page'] = $conf['top_number'] - $page['start'];
    500         }
    501       }
    502       else if ( $page['cat'] == 'calendar' )
    503       {
    504         $page['cat_nb_images'] = 0;
    505         $page['title'] = $lang['calendar'];
    506         if (isset($_GET['year'])
    507             and preg_match('/^\d+$/', $_GET['year']))
    508         {
    509           $page['calendar_year'] = (int)$_GET['year'];
    510         }
    511         if (isset($_GET['month'])
    512             and preg_match('/^(\d+)\.(\d{2})$/', $_GET['month'], $matches))
    513         {
    514           $page['calendar_year'] = (int)$matches[1];
    515           $page['calendar_month'] = (int)$matches[2];
    516         }
    517         if (isset($_GET['day'])
    518             and preg_match('/^(\d+)\.(\d{2})\.(\d{2})$/',
    519                            $_GET['day'],
    520                            $matches))
    521         {
    522           $page['calendar_year'] = (int)$matches[1];
    523           $page['calendar_month'] = (int)$matches[2];
    524           $page['calendar_day'] = (int)$matches[3];
    525         }
    526         if (isset($page['calendar_year']))
    527         {
    528           $page['title'] .= ' (';
    529           if (isset($page['calendar_day']))
    530           {
    531             if ($page['calendar_year'] >= 1970)
    532             {
    533               $unixdate = mktime(0,0,0,
    534                                  $page['calendar_month'],
    535                                  $page['calendar_day'],
    536                                  $page['calendar_year']);
    537               $page['title'].= $lang['day'][date("w", $unixdate)];
    538             }
    539             $page['title'].= ' '.$page['calendar_day'].', ';
    540           }
    541           if (isset($page['calendar_month']))
    542           {
    543             $page['title'] .= $lang['month'][$page['calendar_month']].' ';
    544           }
    545           $page['title'] .= $page['calendar_year'];
    546           $page['title'] .= ')';
    547         }
    548        
    549         $page['where'] = 'WHERE '.$conf['calendar_datefield'].' IS NOT NULL';
    550         if (isset($forbidden))
    551         {
    552           $page['where'].= ' AND '.$forbidden;
    553         }
    554       }
    555       else if ($page['cat'] == 'best_rated')
    556       {
    557         $page['title'] = $conf['top_number'].' '.$lang['best_rated_cat'];
    558 
    559         $page['where'] = ' WHERE average_rate IS NOT NULL';
    560        
    561         if (isset($forbidden))
    562         {
    563           $page['where'].= ' AND '.$forbidden;
    564         }
    565 
    566         $conf['order_by'] = ' ORDER BY average_rate DESC, id ASC';
    567 
    568         // $page['cat_nb_images'] equals $conf['top_number'] unless there
    569         // are less rated items
    570         $query ='
    571 SELECT COUNT(DISTINCT(id)) AS count
    572   FROM '.IMAGES_TABLE.'
    573     INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
    574   '.$page['where'].'
    575 ;';
    576         $row = mysql_fetch_array(pwg_query($query));
    577         if ($row['count'] < $conf['top_number'])
    578         {
    579           $page['cat_nb_images'] = $row['count'];
    580         }
    581         else
    582         {
    583           $page['cat_nb_images'] = $conf['top_number'];
    584         }
    585         unset($query);
    586          
    587 
    588         if (isset($page['start'])
    589             and ($page['start']+$user['nb_image_page']>=$conf['top_number']))
    590         {
    591           $page['nb_image_page'] = $conf['top_number'] - $page['start'];
    592         }
    593       }
    594       else if ($page['cat'] == 'list')
    595       {
    596         $page['title'] = $lang['random_cat'];
    597          
    598         $page['where'] = 'WHERE 1=1';
    599         if (isset($forbidden))
    600         {
    601           $page['where'].= ' AND '.$forbidden;
    602         }
    603         $page['where'].= ' AND image_id IN ('.$_GET['list'].')';
    604         $page['cat_nb_images'] = count(explode(',', $_GET['list']));
    605 
    606         $url.= '&amp;list='.$_GET['list'];
    607       }
    608 
    609       if (isset($query))
    610       {
    611         $result = pwg_query( $query );
    612         $row = mysql_fetch_array( $result );
    613         $page['cat_nb_images'] = $row['nb_total_images'];
    614       }
    615     }
    616     if ( $calling_page == 'category' )
    617     {
    618       $page['navigation_bar'] =
    619         create_navigation_bar( $url, $page['cat_nb_images'], $page['start'],
    620                                $user['nb_image_page'], 'back' );
    621     }
    622 
    623     if ($page['cat'] != 'most_visited' and $page['cat'] != 'best_rated')
    624     {
    625       $available_image_orders = get_category_preferred_image_orders();
    626 
    627       $order_idx=0;
    628       if ( isset($_COOKIE['pwg_image_order']) )
    629       {
    630         $order_idx = $_COOKIE['pwg_image_order'];
    631       }
    632 
    633       if ( $order_idx > 0 )
    634       {
    635         $order = $available_image_orders[$order_idx][1];
    636         $conf['order_by'] = str_replace('ORDER BY ', 'ORDER BY '.$order.',',
    637                                           $conf['order_by'] );
    638       }
    639     }
    640   }
    641   else
    642   {
    643     $page['title'] = $lang['no_category'];
    644   }
    645   pwg_debug( 'end initialize_category' );
    646 }
    647 
    648270function display_select_categories($categories,
    649271                                   $selecteds,
     
    736358  return strnatcasecmp($a['global_rank'], $b['global_rank']);
    737359}
     360
     361function rank_compare($a, $b)
     362{
     363  if ($a['rank'] == $b['rank'])
     364  {
     365    return 0;
     366  }
     367
     368  return ($a['rank'] < $b['rank']) ? -1 : 1;
     369}
    738370?>
Note: See TracChangeset for help on using the changeset viewer.