Ignore:
Timestamp:
Feb 28, 2007, 4:07:12 AM (17 years ago)
Author:
rvelices
Message:

feature 657: permalinks for categories

File:
1 edited

Legend:

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

    r1861 r1866  
    6060  // From CATEGORIES_TABLE
    6161  $query.= '
    62   name, id, nb_images, global_rank,';
     62  id, name, permalink, nb_images, global_rank,';
    6363  // From USER_CACHE_CATEGORIES_TABLE
    6464  $query.= '
     
    160160  }
    161161
    162   $names = array();
    163   $query = '
    164 SELECT id, name
    165   FROM '.CATEGORIES_TABLE.'
    166   WHERE id IN ('.$cat['uppercats'].')
    167 ;';
    168   $result = pwg_query($query);
    169   while($row = mysql_fetch_assoc($result))
    170   {
    171     $names[$row['id']] = $row;
    172   }
    173 
    174   // category names must be in the same order than uppercats list
    175   $cat['upper_names'] = array();
    176   foreach (explode(',', $cat['uppercats']) as $cat_id)
    177   {
    178     $cat['upper_names'][$cat_id] = $names[$cat_id];
    179   }
    180 
     162  $upper_ids = explode(',', $cat['uppercats']);
     163  if ( count($upper_ids)==1 )
     164  {// no need to make a query for level 1
     165    $cat['upper_names'] = array(
     166        array(
     167          'id' => $cat['id'],
     168          'name' => $cat['name'],
     169          'permalink' => $cat['permalink'],
     170          )
     171      );
     172  }
     173  else
     174  {
     175    $names = array();
     176    $query = '
     177  SELECT id, name, permalink
     178    FROM '.CATEGORIES_TABLE.'
     179    WHERE id IN ('.$cat['uppercats'].')
     180  ;';
     181    $names = hash_from_query($query, 'id');
     182
     183    // category names must be in the same order than uppercats list
     184    $cat['upper_names'] = array();
     185    foreach ($upper_ids as $cat_id)
     186    {
     187      array_push( $cat['upper_names'], $names[$cat_id]);
     188    }
     189  }
    181190  return $cat;
    182191}
     
    309318  if (!empty($result))
    310319  {
    311     while ($row = mysql_fetch_array($result))
     320    while ($row = mysql_fetch_assoc($result))
    312321    {
    313322      array_push($categories, $row);
     
    356365}
    357366
     367/** returns a category id that corresponds to the given permalink (or null)
     368 * @param string permalink
     369 */
     370function get_cat_id_from_permalink( $permalink )
     371{
     372  $query ='
     373SELECT id FROM '.CATEGORIES_TABLE.'
     374  WHERE permalink="'.$permalink.'"';
     375  $ids = array_from_query($query, 'id');
     376  if (!empty($ids))
     377  {
     378    return $ids[0];
     379  }
     380  return null;
     381}
     382
     383/** returns a category id that has used before this permalink (or null)
     384 * @param string permalink
     385 * @param boolean is_hit if true update the usage counters on the old permalinks
     386 */
     387function get_cat_id_from_old_permalink($permalink, $is_hit)
     388{
     389  $query='
     390SELECT c.id
     391  FROM '.OLD_PERMALINKS_TABLE.' op INNER JOIN '.CATEGORIES_TABLE.' c
     392    ON op.cat_id=c.id
     393  WHERE op.permalink="'.$permalink.'"
     394  LIMIT 1';
     395  $result = pwg_query($query);
     396  $cat_id = null;
     397  if ( mysql_num_rows($result) )
     398    list( $cat_id ) = mysql_fetch_array($result);
     399
     400  if ( isset($cat_id) and $is_hit )
     401  {
     402    $query='
     403UPDATE '.OLD_PERMALINKS_TABLE.' SET last_hit=NOW(), hit=hit+1
     404  WHERE permalink="'.$permalink.'" AND cat_id='.$cat_id.'
     405  LIMIT 1';
     406    pwg_query($query);
     407  }
     408  return $cat_id;
     409}
     410
    358411function global_rank_compare($a, $b)
    359412{
Note: See TracChangeset for help on using the changeset viewer.