Changeset 1036 for trunk/picture.php


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/picture.php

    r1029 r1036  
    3131include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
    3232//-------------------------------------------------- access authorization check
    33 check_cat_id( $_GET['cat'] );
    34 
    35 if (!isset($page['cat']))
    36 {
    37   die($lang['access_forbiden']);
    38 }
    39 
    40 check_login_authorization();
    41 if ( isset( $page['cat'] ) and is_numeric( $page['cat'] ) )
    42 {
    43   check_restrictions( $page['cat'] );
    44 }
     33if (isset($page['cat']) and is_numeric($page['cat']))
     34{
     35  check_restrictions($page['cat']);
     36}
     37//-------------------------------------------------------------- initialization
     38include(PHPWG_ROOT_PATH.'include/section_init.inc.php');
     39
     40// if this image_id doesn't correspond to this category, an error message is
     41// displayed, and execution is stopped
     42if (!in_array($_GET['image_id'], $page['items']))
     43{
     44  echo '
     45<div style="text-align:center;">'.$lang['access_forbiden'].'<br />
     46  <a href="'.PHPWG_ROOT_PATH.'category.php'.'">'.$lang['thumbnails'].'</a>
     47</div>';
     48  exit();
     49}
     50
     51$page['rank_of'] = array_flip($page['items']);
     52
     53// caching first_rank, last_rank, current_rank in the displayed
     54// section. This should also help in readability.
     55$page['first_rank']   = 0;
     56$page['last_rank']    = count($page['items']) - 1;
     57$page['current_rank'] = $page['rank_of'][ $_GET['image_id'] ];
     58
     59// caching current item : readability purpose
     60$page['current_item'] = $_GET['image_id'];
     61
     62if ($page['current_rank'] != $page['first_rank'])
     63{
     64  // "go to first picture of this section" link is displayed only if the
     65  // displayed item is not the first.
     66  $template->assign_block_vars(
     67    'first',
     68    array(
     69      'U_IMG' =>
     70        PHPWG_ROOT_PATH.'picture.php'.
     71        get_query_string_diff(
     72          array('image_id', 'add_fav', 'slideshow', 'rate')
     73          ).
     74        '&amp;image_id='.$page['items'][ $page['first_rank'] ],
     75      )
     76    );
     77
     78  // caching previous item : readability purpose
     79  $page['previous_item'] = $page['items'][ $page['current_rank'] - 1 ];
     80}
     81
     82if ($page['current_rank'] != $page['last_rank'])
     83{
     84  // "go to last picture of this section" link is displayed only if the
     85  // displayed item is not the last.
     86  $template->assign_block_vars(
     87    'last',
     88    array(
     89      'U_IMG' =>
     90        PHPWG_ROOT_PATH.'picture.php'.
     91        get_query_string_diff(
     92          array('image_id', 'add_fav', 'slideshow', 'rate')
     93          ).
     94        '&amp;image_id='.$page['items'][ $page['last_rank'] ],
     95      )
     96    );
     97
     98  // caching next item : readability purpose
     99  $page['next_item'] = $page['items'][ $page['current_rank'] + 1 ];
     100}
     101
    45102//---------------------------------------- incrementation of the number of hits
    46103if ( count(array_intersect(
     
    56113  @pwg_query( $query );
    57114}
    58 //-------------------------------------------------------------- initialization
    59 initialize_category( 'picture' );
    60 // retrieving the number of the picture in its category (in order)
    61 $query = '
    62 SELECT DISTINCT(id)
    63   FROM '.IMAGES_TABLE.'
    64     INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
    65   '.$page['where'].'
    66   '.$conf['order_by'].'
    67 ;';
    68 $result = pwg_query( $query );
    69 $page['num'] = 0;
    70 $belongs = false;
    71 while ($row = mysql_fetch_array($result))
    72 {
    73   if ($row['id'] == $_GET['image_id'])
    74   {
    75     $belongs = true;
    76     break;
    77   }
    78   if ($page['num']==0)
    79   {
    80     $url_first_last = PHPWG_ROOT_PATH.'picture.php';
    81     $url_first_last.= get_query_string_diff(array('image_id','add_fav',
    82                                                     'slideshow','rate'));
    83     $url_first_last.= '&amp;image_id=';
    84     $template->assign_block_vars(
    85       'first',
    86       array(
    87         'U_IMG' => $url_first_last . $row['id'],
    88         ));
    89   }
    90   $page['num']++;
    91 }
    92 if ($page['cat_nb_images']>0 and $page['num'] < $page['cat_nb_images'] - 1)
    93 {
    94   mysql_data_seek($result, $page['cat_nb_images'] - 1);
    95   $row = mysql_fetch_array($result);
    96   $url_first_last = PHPWG_ROOT_PATH.'picture.php';
    97   $url_first_last.= get_query_string_diff(array('image_id','add_fav',
    98                                                   'slideshow','rate'));
    99   $url_first_last.= '&amp;image_id=';
    100   $template->assign_block_vars(
    101     'last',
    102     array(
    103       'U_IMG' => $url_first_last . $row['id'],
    104       ));
    105 }
    106 
    107 // if this image_id doesn't correspond to this category, an error message is
    108 // displayed, and execution is stopped
    109 if (!$belongs)
    110 {
    111   echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />';
    112   echo '<a href="'.PHPWG_ROOT_PATH.'category.php'.'">';
    113   echo $lang['thumbnails'].'</a></div>';
    114   exit();
    115 }
     115
    116116//-------------------------------------------------------------- representative
    117117if ('admin' == $user['status'] and isset($_GET['representative']))
     
    162162$picture = array();
    163163
    164 if ($page['num'] == 0)
    165 {
    166   $has_prev = false;
    167 }
    168 else
    169 {
    170   $has_prev = true;
    171 }
    172 
    173 if ($page['num'] == $page['cat_nb_images'] - 1)
    174 {
    175   $has_next = false;
    176 }
    177 else
    178 {
    179   $has_next = true;
     164$ids = array($_GET['image_id']);
     165if (isset($page['previous_item']))
     166{
     167  array_push($ids, $page['previous_item']);
     168}
     169if (isset($page['next_item']))
     170{
     171  array_push($ids, $page['next_item']);
    180172}
    181173
    182174$query = '
    183 SELECT DISTINCT(i.id), i.*
    184   FROM '.IMAGES_TABLE.' AS i
    185     INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON i.id = ic.image_id
    186   '.$page['where'].'
    187   '.$conf['order_by'].'
    188   ';
    189 
    190 if ( !$has_prev )
    191 {
    192   $query.= ' LIMIT 0,2';
    193 }
    194 else
    195 {
    196   $query.= ' LIMIT '.($page['num'] - 1).',3';
    197 }
    198 $query.= ';';
    199 
    200 $result = pwg_query( $query );
    201 $indexes = array('prev', 'current', 'next');
    202 
    203 foreach (array('prev', 'current', 'next') as $i)
    204 {
    205   if ($i == 'prev' and !$has_prev)
    206   {
    207     continue;
    208   }
    209   if ($i == 'next' and !$has_next)
    210   {
    211     break;
    212   }
    213 
    214   $row = mysql_fetch_array($result);
     175SELECT *
     176  FROM '.IMAGES_TABLE.'
     177  WHERE id IN ('.implode(',', $ids).')
     178;';
     179
     180$result = pwg_query($query);
     181
     182while ($row = mysql_fetch_array($result))
     183{
     184  if (isset($page['previous_item']) and $row['id'] == $page['previous_item'])
     185  {
     186    $i = 'prev';
     187  }
     188  else if (isset($page['next_item']) and $row['id'] == $page['next_item'])
     189  {
     190    $i = 'next';
     191  }
     192  else
     193  {
     194    $i = 'current';
     195  }
     196 
    215197  foreach (array_keys($row) as $key)
    216198  {
     
    235217  if (isset($row['representative_ext']) and $row['representative_ext'] != '')
    236218  {
    237     $picture[$i]['src'] = $cat_directory.'/pwg_representative/';
    238     $picture[$i]['src'].= $file_wo_ext.'.'.$row['representative_ext'];
     219    $picture[$i]['src'] =
     220      $cat_directory.'/pwg_representative/'
     221      .$file_wo_ext.'.'.$row['representative_ext'];
    239222  }
    240223  else
     
    275258  }
    276259
    277   $picture[$i]['url'] = PHPWG_ROOT_PATH.'picture.php';
    278   $picture[$i]['url'].= get_query_string_diff(array('image_id','add_fav',
    279                                                     'slideshow','rate'));
    280   $picture[$i]['url'].= '&amp;image_id='.$row['id'];
     260  $picture[$i]['url'] =
     261    PHPWG_ROOT_PATH.'picture.php'
     262    .get_query_string_diff(array('image_id', 'add_fav', 'slideshow', 'rate'))
     263    .'&amp;image_id='.$row['id'];
    281264}
    282265
    283266$url_up = PHPWG_ROOT_PATH.'category.php?cat='.$page['cat'];
    284 $url_up_start = floor( $page['num'] / $user['nb_image_page'] );
     267
     268$url_up_start = floor( $page['current_rank'] / $user['nb_image_page'] );
    285269$url_up_start *= $user['nb_image_page'];
    286270if ($url_up_start>0)
     
    298282}
    299283
    300 $url_admin = PHPWG_ROOT_PATH.'admin.php?page=picture_modify';
    301 $url_admin.= '&amp;cat_id='.$page['cat'];
    302 $url_admin.= '&amp;image_id='.$_GET['image_id'];
    303 
    304 $url_slide = $picture['current']['url'];
    305 $url_slide.= '&amp;slideshow='.$conf['slideshow_period'];
     284$url_admin =
     285  PHPWG_ROOT_PATH.'admin.php?page=picture_modify'
     286  .'&amp;cat_id='.$page['cat']
     287  .'&amp;image_id='.$_GET['image_id'];
     288
     289$url_slide =
     290  $picture['current']['url'].'&amp;slideshow='.$conf['slideshow_period'];
    306291
    307292//----------------------------------------------------------- rate registration
     
    485470{
    486471  $title_img = replace_space(get_cat_display_name($page['cat_name']));
    487   $n = $page['num'] + 1;
     472  $n = $page['current_rank'] + 1;
    488473  $title_nb = $n.'/'.$page['cat_nb_images'];
    489474}
     
    527512
    528513$page['body_id'] = 'thePicturePage';
    529 //-------------------------------------------------------- navigation management
    530 if ($has_prev)
     514//------------------------------------------------------- navigation management
     515if (isset($page['previous_item']))
    531516{
    532517  $template->assign_block_vars(
     
    537522      'U_IMG' => $picture['prev']['url'],
    538523      'U_IMG_SRC' => $picture['prev']['src']
    539       ));
    540 }
    541 
    542 if ($has_next)
     524      )
     525    );
     526}
     527
     528if (isset($page['next_item']))
    543529{
    544530  $template->assign_block_vars(
     
    549535      'U_IMG' => $picture['next']['url'],
    550536      'U_IMG_SRC' => $picture['next']['src'] // allow navigator to preload
    551       ));
     537      )
     538    );
    552539}
    553540
Note: See TracChangeset for help on using the changeset viewer.