Changeset 580 for trunk/admin


Ignore:
Timestamp:
Oct 24, 2004, 8:56:09 PM (20 years ago)
Author:
z0rglub
Message:
  • complete refactoring
  • possibility to update metadata for a category
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/cat_list.php

    r496 r580  
    2626// +-----------------------------------------------------------------------+
    2727
    28 if( !defined("PHPWG_ROOT_PATH") )
    29 {
    30         die ("Hacking attempt!");
    31 }
    32 include_once( PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php' );
    33 
     28if (!defined('PHPWG_ROOT_PATH'))
     29{
     30  die('Hacking attempt!');
     31}
     32include_once(PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php');
     33// +-----------------------------------------------------------------------+
     34// |                            initialization                             |
     35// +-----------------------------------------------------------------------+
    3436$errors = array();
    35 $categories=array();
    36 $navigation=$lang['gallery_index'];
    37 
    38 //---------------------------------------------------  virtual categories
    39 if ( isset( $_GET['delete'] ) && is_numeric( $_GET['delete'] ) )
     37$infos = array();
     38$categories = array();
     39$navigation = $lang['gallery_index'];
     40// +-----------------------------------------------------------------------+
     41// |                    virtual categories management                      |
     42// +-----------------------------------------------------------------------+
     43// request to add a virtual category
     44if (isset($_GET['delete']) and is_numeric($_GET['delete']))
    4045{
    4146  $to_delete_categories = array();
    4247  array_push($to_delete_categories,$_GET['delete']);
    43   delete_categories( $to_delete_categories );
    44 }
    45 elseif ( isset( $_POST['submit'] ) )
     48  delete_categories($to_delete_categories);
     49  array_push($infos, $lang['cat_list_virtual_category_deleted']);
     50}
     51// request to delete a virtual category
     52else if (isset($_POST['submit']))
    4653{
    4754  // is the given category name only containing blank spaces ?
    48   if ( preg_match( '/^\s*$/', $_POST['virtual_name'] ) )
    49     array_push( $errors, $lang['cat_error_name'] );
     55  if (preg_match('/^\s*$/', $_POST['virtual_name']))
     56  {
     57    array_push($errors, $lang['cat_error_name']);
     58  }
    5059       
    51   if ( !count( $errors ))
     60  if (!count($errors))
    5261  {
    5362    $parent_id = !empty($_GET['parent_id'])?$_GET['parent_id']:'NULL';
    54     // As we don't create a virtual category every day, let's do (far) too much queries
    55     if ($parent_id!='NULL')
    56     {
    57           $query = 'SELECT uppercats FROM '.CATEGORIES_TABLE;
    58       $query.= ' WHERE id="'.$parent_id.'";';
    59           $parent_uppercats = array_pop(mysql_fetch_array( mysql_query( $query )));
    60         }
     63    // As we don't create a virtual category every day, let's do (far) too
     64    // much queries
     65    if ($parent_id != 'NULL')
     66    {
     67      $query = '
     68SELECT uppercats
     69  FROM '.CATEGORIES_TABLE.'
     70  WHERE id = '.$parent_id.'
     71;';
     72      $parent_uppercats = array_pop(mysql_fetch_array(mysql_query($query)));
     73    }
    6174       
    62         // we have then to add the virtual category
    63     $query = 'INSERT INTO '.CATEGORIES_TABLE;
    64     $query.= ' (name,id_uppercat,rank) VALUES ';
    65     $query.= " ('".$_POST['virtual_name']."',".$parent_id.",".$_POST['rank'].");";
    66         mysql_query( $query );
     75    // we have then to add the virtual category
     76    $query = '
     77INSERT INTO '.CATEGORIES_TABLE.'
     78  (name,id_uppercat,rank)
     79  VALUES
     80  (\''.$_POST['virtual_name'].'\','.$parent_id.','.$_POST['rank'].')
     81;';
     82    mysql_query($query);
    6783       
    68         // And last we update the uppercats
    69         $query = 'SELECT MAX(id) FROM '.CATEGORIES_TABLE.';';
    70         $my_id = array_pop(mysql_fetch_array( mysql_query( $query )));
    71         $query = 'UPDATE '.CATEGORIES_TABLE.' SET uppercats = "';
    72         if (!empty($parent_uppercats))
     84    // And last we update the uppercats
     85    $query = '
     86SELECT MAX(id)
     87  FROM '.CATEGORIES_TABLE.'
     88;';
     89    $my_id = array_pop(mysql_fetch_array(mysql_query($query)));
     90
     91    $query = '
     92UPDATE '.CATEGORIES_TABLE.'
     93  SET uppercats = \'';
     94    if (!empty($parent_uppercats))
    7395    {
    7496      $query.= $parent_uppercats.',';
    7597    }
    76         $query.= $my_id;
    77         $query.= '" WHERE id = '.$my_id.';';
    78         mysql_query( $query );   
    79   }
    80 }
    81 
    82 // Cache management
    83 
    84 $query = 'SELECT * FROM '.CATEGORIES_TABLE;
    85 if ( !isset($_GET['parent_id']))
    86 {
    87   $query.= ' WHERE id_uppercat IS NULL';
     98    $query.= $my_id;
     99    $query.= '\'
     100  WHERE id = '.$my_id.'
     101;';
     102    mysql_query($query);
     103    array_push($infos, $lang['cat_list_virtual_category_added']);
     104  }
     105}
     106// +-----------------------------------------------------------------------+
     107// |                           Cache management                            |
     108// +-----------------------------------------------------------------------+
     109$query = '
     110SELECT *
     111  FROM '.CATEGORIES_TABLE;
     112if (!isset($_GET['parent_id']))
     113{
     114  $query.= '
     115  WHERE id_uppercat IS NULL';
    88116}
    89117else
    90118{
    91   $query.= ' WHERE id_uppercat = '.$_GET['parent_id'];
    92 }
    93 $query.= ' ORDER BY rank ASC';
    94 $query.= ';';
    95 $result = mysql_query( $query );
    96 while ( $row = mysql_fetch_assoc( $result ) )
    97 {
    98   $categories[$row['rank']]=$row;
    99 }
    100 
    101 // Navigation path
     119  $query.= '
     120  WHERE id_uppercat = '.$_GET['parent_id'];
     121}
     122$query.= '
     123  ORDER BY rank ASC
     124;';
     125$result = mysql_query($query);
     126while ($row = mysql_fetch_assoc($result))
     127{
     128  $categories[$row['rank']] = $row;
     129}
     130// +-----------------------------------------------------------------------+
     131// |                            Navigation path                            |
     132// +-----------------------------------------------------------------------+
    102133if (isset($_GET['parent_id']))
    103134{
     135  $separator = ' -> ';
     136  $base_url = PHPWG_ROOT_PATH.'admin.php?page=cat_list';
     137 
     138  $navigation = '<a class="" href="'.add_session_id($base_url).'">';
     139  $navigation.= $lang['gallery_index'];
     140  $navigation.= '</a>';
     141  $navigation.= $separator;
     142
    104143  $current_category = get_cat_info($_GET['parent_id']);
    105   $url = PHPWG_ROOT_PATH.'admin.php?page=cat_list&amp;parent_id=';
    106   $navigation = '<a class="" href="'.add_session_id(PHPWG_ROOT_PATH.'admin.php?page=cat_list').'">';
    107   $navigation.= $lang['gallery_index'].'</a>-&gt;';
    108   $navigation.= get_cat_display_name($current_category['name'], '-&gt;', $url);
    109 }
    110 
    111 //---------------------------------------------------------------  rank updates
    112 $current_rank=0;
    113 if ( isset( $_GET['up'] ) && is_numeric( $_GET['up'] ))
     144  $navigation.= get_cat_display_name($current_category['name'],
     145                                     $separator,
     146                                     $base_url.'&amp;parent_id=',
     147                                     false);
     148}
     149// +-----------------------------------------------------------------------+
     150// |                               rank updates                            |
     151// +-----------------------------------------------------------------------+
     152$current_rank = 0;
     153if (isset($_GET['up']) and is_numeric($_GET['up']))
    114154{
    115155  // 1. searching the id of the category just above at the same level
     
    117157  {
    118158    if ($current['id'] == $_GET['up'])
    119         {
    120           $current_rank = $current['rank'];
    121           break;
    122     }
    123   }
    124   if ($current_rank>1)
     159    {
     160      $current_rank = $current['rank'];
     161      break;
     162    }
     163  }
     164  if ($current_rank > 1)
    125165  {
    126166    // 2. Exchanging ranks between the two categories
    127     $query = 'UPDATE '.CATEGORIES_TABLE;
    128     $query.= ' SET rank = '.($current_rank-1);
    129     $query.= ' WHERE id = '.$_GET['up'];
    130     $query.= ';';
    131     mysql_query( $query );
    132     $query = 'UPDATE '.CATEGORIES_TABLE;
    133     $query.= ' SET rank = '.$current_rank;
    134     $query.= ' WHERE id = '.$categories[($current_rank-1)]['id'];
    135     $query.= ';';
    136     mysql_query( $query );
    137         // 3. Updating the cache array
    138         $categories[$current_rank]=$categories[($current_rank-1)];
    139         $categories[($current_rank-1)] = $current;
     167    $query = '
     168UPDATE '.CATEGORIES_TABLE.'
     169  SET rank = '.($current_rank-1).'
     170  WHERE id = '.$_GET['up'].'
     171;';
     172    mysql_query($query);
     173    $query = '
     174UPDATE '.CATEGORIES_TABLE.'
     175  SET rank = '.$current_rank.'
     176  WHERE id = '.$categories[($current_rank-1)]['id'].'
     177;';
     178    mysql_query($query);
     179    // 3. Updating the cache array
     180    $categories[$current_rank] = $categories[($current_rank-1)];
     181    $categories[($current_rank-1)] = $current;
    140182  }
    141183  else
    142184  {
    143185    // 2. Updating the rank of our category to be after the previous max rank
    144     $query = 'UPDATE '.CATEGORIES_TABLE;
    145     $query.= ' SET rank = '.(count($categories) + 1);
    146     $query.= ' WHERE id = '.$_GET['up'];
    147     $query.= ';';
    148     mysql_query( $query );
    149     $query = 'UPDATE '.CATEGORIES_TABLE;
    150     $query.= ' SET rank = rank-1';
    151         $query.= ' WHERE id_uppercat ';
    152         $query.= empty($_GET['parent_id'])?'IS NULL':('= '.$_GET['parent_id']);
    153         $query.= ';';
    154     mysql_query( $query );
    155         // 3. Updating the cache array
    156         array_push($categories, $current);
    157         array_shift($categories);
    158   }
    159 }
    160 elseif ( isset( $_GET['down'] ) && is_numeric( $_GET['down'] ) )
     186    $query = '
     187UPDATE '.CATEGORIES_TABLE.'
     188  SET rank = '.(count($categories) + 1).'
     189  WHERE id = '.$_GET['up'].'
     190;';
     191    mysql_query($query);
     192    $query = '
     193UPDATE '.CATEGORIES_TABLE.'
     194  SET rank = rank-1
     195  WHERE id_uppercat ';
     196    if (empty($_GET['parent_id']))
     197    {
     198      $query.= 'IS NULL';
     199    }
     200    else
     201    {
     202      $query.= '= '.$_GET['parent_id'];
     203    }
     204    $query.= '
     205;';
     206    mysql_query($query);
     207    // 3. Updating the cache array
     208    array_push($categories, $current);
     209    array_shift($categories);
     210  }
     211}
     212else if (isset($_GET['down']) and is_numeric($_GET['down']))
    161213{
    162214  // 1. searching the id of the category just above at the same level
     
    164216  {
    165217    if ($current['id'] == $_GET['down'])
    166         {
    167           $current_rank = $current['rank'];
    168           break;
    169         }
     218    {
     219      $current_rank = $current['rank'];
     220      break;
     221    }
    170222  }
    171223  if ($current_rank < count($categories))
    172224  {
    173225    // 2. Exchanging ranks between the two categories
    174     $query = 'UPDATE '.CATEGORIES_TABLE;
    175     $query.= ' SET rank = '.($current_rank+1);
    176     $query.= ' WHERE id = '.$_GET['down'];
    177     $query.= ';';
    178     mysql_query( $query );
    179     $query = 'UPDATE '.CATEGORIES_TABLE;
    180     $query.= ' SET rank = '.$current_rank;
    181     $query.= ' WHERE id = '.$categories[($current_rank+1)]['id'];
    182     $query.= ';';
    183     mysql_query( $query );
    184         // 3. Updating the cache array
    185         $categories[$current_rank]=$categories[($current_rank+1)];
    186         $categories[($current_rank+1)] = $current;
     226    $query = '
     227UPDATE '.CATEGORIES_TABLE.'
     228  SET rank = '.($current_rank+1).'
     229  WHERE id = '.$_GET['down'].'
     230;';
     231    mysql_query($query);
     232    $query = '
     233UPDATE '.CATEGORIES_TABLE.'
     234  SET rank = '.$current_rank.'
     235  WHERE id = '.$categories[($current_rank+1)]['id'].'
     236;';
     237    mysql_query($query);
     238    // 3. Updating the cache array
     239    $categories[$current_rank]=$categories[($current_rank+1)];
     240    $categories[($current_rank+1)] = $current;
    187241  }
    188242  else
    189243  {
    190244    // 2. updating the rank of our category to be the first one
    191     $query = 'UPDATE '.CATEGORIES_TABLE;
    192     $query.= ' SET rank = 0';
    193     $query.= ' WHERE id = '.$_GET['down'];
    194     $query.= ';';
    195     mysql_query( $query );
    196     $query = 'UPDATE '.CATEGORIES_TABLE;
    197     $query.= ' SET rank = (rank+1)';
    198         $query.= ' WHERE id_uppercat ';
    199         $query.= empty($_GET['parent_id'])?'IS NULL':('= '.$_GET['parent_id']);
    200         $query.= ';';
    201     mysql_query( $query );
    202         // 3. Updating the cache array
    203         array_unshift($categories, $current);
    204         array_pop($categories);
     245    $query = '
     246UPDATE '.CATEGORIES_TABLE.'
     247  SET rank = 0
     248  WHERE id = '.$_GET['down'].'
     249;';
     250    mysql_query($query);
     251    $query = '
     252UPDATE '.CATEGORIES_TABLE.'
     253  SET rank = rank+1
     254  WHERE id_uppercat ';
     255    if (empty($_GET['parent_id']))
     256    {
     257      $query.= 'IS NULL';
     258    }
     259    else
     260    {
     261      $query.= '= '.$_GET['parent_id'];
     262    }
     263    $query.= '
     264;';
     265    mysql_query($query);
     266    // 3. Updating the cache array
     267    array_unshift($categories, $current);
     268    array_pop($categories);
    205269  }
    206270}
    207271reset($categories);
    208 
    209 //----------------------------------------------------- template initialization
    210 $template->set_filenames( array('categories'=>'admin/cat_list.tpl') );
     272// +-----------------------------------------------------------------------+
     273// |                           metadata update                             |
     274// +-----------------------------------------------------------------------+
     275if (isset($_GET['metadata']) and is_numeric($_GET['metadata']))
     276{
     277  $files = get_filelist($_GET['metadata'], true, false);
     278  update_metadata($files);
     279  array_push($infos,
     280             count($files).' '.$lang['cat_list_update_metadata_confirmation']);
     281}
     282// +-----------------------------------------------------------------------+
     283// |                       template initialization                         |
     284// +-----------------------------------------------------------------------+
     285$template->set_filenames(array('categories'=>'admin/cat_list.tpl'));
    211286
    212287$template->assign_vars(array(
     
    222297  'L_EDIT'=>$lang['edit'],
    223298  'L_INFO_IMG'=>$lang['cat_image_info'],
    224   'L_DELETE'=>$lang['delete']
    225   ));
    226  
    227 $tpl = array( 'cat_first','cat_last');
    228                          
    229 //-------------------------------------------------------------- errors display
    230 if ( sizeof( $errors ) != 0 )
     299  'L_DELETE'=>$lang['delete'],
     300  'L_UPDATE_METADATA'=>$lang['cat_list_update_metadata']
     301 ));
     302 
     303$tpl = array('cat_first','cat_last');
     304// +-----------------------------------------------------------------------+
     305// |                            errors & infos                             |
     306// +-----------------------------------------------------------------------+
     307if (count($errors) != 0)
    231308{
    232309  $template->assign_block_vars('errors',array());
    233   for ( $i = 0; $i < sizeof( $errors ); $i++ )
    234   {
    235     $template->assign_block_vars('errors.error',array('ERROR'=>$errors[$i]));
    236   }
    237 }
    238 //----------------------------------------------------------- Categories display
    239 while (list ($id,$category) = each($categories))
    240 {
    241 
     310  foreach ($errors as $error)
     311  {
     312    $template->assign_block_vars('errors.error',array('ERROR'=>$error));
     313  }
     314}
     315if (count($infos) != 0)
     316{
     317  $template->assign_block_vars('infos',array());
     318  foreach ($infos as $info)
     319  {
     320    $template->assign_block_vars('infos.info',array('INFO'=>$info));
     321  }
     322}
     323// +-----------------------------------------------------------------------+
     324// |                          Categories display                           |
     325// +-----------------------------------------------------------------------+
     326while (list($id,$category) = each($categories))
     327{
     328  $images_folder = PHPWG_ROOT_PATH.'template/';
     329  $images_folder.= $user['template'].'/admin/images';
     330 
    242331  if ($category['visible'] == 'false')
    243332  {
    244     $category_image = '<img src="'.PHPWG_ROOT_PATH.'template/'.$user['template'].'/admin/images/icon_folder_lock.gif"
    245           alt="'.$lang['cat_private'].'" title="'.$lang['cat_private'].'"/>';
    246   }
    247   elseif (empty($category['dir']))
    248   {
    249     $category_image = '<img src="'.PHPWG_ROOT_PATH.'template/'.$user['template'].'/admin/images/icon_folder_link.gif"
    250           alt="'.$lang['cat_virtual'].'" title="'.$lang['cat_virtual'].'"/>';
     333    $image_src = $images_folder.'/icon_folder_lock.gif';
     334    $image_alt = $lang['cat_private'];
     335    $image_title = $lang['cat_private'];
     336  }
     337  else if (empty($category['dir']))
     338  {
     339    $image_src = $images_folder.'/icon_folder_link.gif';
     340    $image_alt = $lang['cat_virtual'];
     341    $image_title = $lang['cat_virtual'];
    251342  }
    252343  else
    253344  {
    254         // May be should we have to introduce a computed field in the table to avoid this query
    255     $query = 'SELECT COUNT(id) as sub_cats FROM ' . CATEGORIES_TABLE . ' WHERE id_uppercat = '.$category['id'];
    256     $result = mysql_fetch_array(mysql_query( $query ));
    257         $category_image = ($result['sub_cats']) ?
    258           '<img src="'.PHPWG_ROOT_PATH.'template/'.$user['template'].'/admin/images/icon_subfolder.gif" alt="" />' :
    259           '<img src="'.PHPWG_ROOT_PATH.'template/'.$user['template'].'/admin/images/icon_folder.gif" alt="" />';
    260   }
    261  
    262   if ( !isset( $category['dir'] ) ) $category['dir'] = '';
    263   $simple_url = PHPWG_ROOT_PATH.'admin.php?page=cat_list&amp;';
    264   $url = $simple_url;
     345    // (Gweltas) May be should we have to introduce a computed field in the
     346    // table to avoid this query -> (z0rglub) no because the number of
     347    // sub-categories depends on permissions
     348    $query = '
     349SELECT COUNT(id) AS nb_sub_cats
     350  FROM '. CATEGORIES_TABLE.'
     351  WHERE id_uppercat = '.$category['id'].'
     352;';
     353    $row = mysql_fetch_array(mysql_query($query));
     354
     355    if ($row['nb_sub_cats'] > 0)
     356    {
     357      $image_src = $images_folder.'/icon_subfolder.gif';
     358    }
     359    else
     360    {
     361      $image_src = $images_folder.'/icon_folder.gif';
     362    }
     363    $image_alt = '';
     364    $image_title = '';
     365  }
     366
     367  $base_url = PHPWG_ROOT_PATH.'admin.php?page=';
     368  $cat_list_url = $base_url.'cat_list';
     369 
     370  $self_url = $cat_list_url;
    265371  if (isset($_GET['parent_id']))
    266     $url = $simple_url.'parent_id='.$_GET['parent_id'].'&amp;';
    267 
    268   $template->assign_block_vars('category' ,array(
    269     'CATEGORY_IMG'=>$category_image,
    270     'CATEGORY_NAME'=>$category['name'],
    271         'CATEGORY_DIR'=>$category['dir'],
    272         'CATEGORY_NB_IMG'=>$category['nb_images'],
    273        
    274         'U_CATEGORY'=>add_session_id( $simple_url.'parent_id='.$category['id']),
    275         'U_MOVE_UP'=>add_session_id( $url.'up='.$category['id'] ),
    276         'U_MOVE_DOWN'=>add_session_id( $url.'down='.$category['id'] ),
    277         'U_CAT_EDIT'=>add_session_id( PHPWG_ROOT_PATH.'admin.php?page=cat_modify&amp;cat_id='.$category['id'] ),
    278         'U_CAT_DELETE'=>add_session_id( $url.'delete='.$category['id'] ),
    279         'U_INFO_IMG'=>add_session_id( PHPWG_ROOT_PATH.'admin.php?page=infos_images&amp;cat_id='.$category['id'] ),
    280         'U_CAT_UPDATE'=>add_session_id( PHPWG_ROOT_PATH.'admin.php?page=update&amp;update='.$category['id'] )
    281         ));
    282        
    283   if ( !empty($category['dir']))
     372  {
     373    $self_url.= '&amp;parent_id='.$_GET['parent_id'];
     374  }
     375
     376  $template->assign_block_vars(
     377    'category',
     378    array(
     379      'CATEGORY_IMG_SRC'=>$image_src,
     380      'CATEGORY_IMG_ALT'=>$image_alt,
     381      'CATEGORY_IMG_TITLE'=>$image_title,
     382      'CATEGORY_NAME'=>$category['name'],
     383      'CATEGORY_DIR'=>@$category['dir'],
     384      'CATEGORY_NB_IMG'=>$category['nb_images'],
     385     
     386      'U_CATEGORY'=>
     387      add_session_id($cat_list_url.'&amp;parent_id='.$category['id']),
     388     
     389      'U_MOVE_UP'=>add_session_id($self_url.'&amp;up='.$category['id']),
     390     
     391      'U_MOVE_DOWN'=>add_session_id($self_url.'&amp;down='.$category['id']),
     392     
     393      'U_CAT_EDIT'=>
     394      add_session_id($base_url.'cat_modify&amp;cat_id='.$category['id']),
     395     
     396      'U_CAT_DELETE'=>add_session_id($self_url.'&amp;delete='.$category['id']),
     397     
     398      'U_INFO_IMG'
     399      => add_session_id($base_url.'infos_images&amp;cat_id='.$category['id']),
     400     
     401      'U_CAT_UPDATE'=>
     402      add_session_id($base_url.'update&amp;update='.$category['id']),
     403     
     404      'U_CAT_METADATA'=>
     405      add_session_id($cat_list_url.'&amp;metadata='.$category['id'])
     406      ));
     407 
     408  if (!empty($category['dir']))
    284409  {
    285410    $template->assign_block_vars('category.storage' ,array());
     
    287412  else
    288413  {
    289         $template->assign_block_vars('category.virtual' ,array());
    290   }
    291   $url = add_session_id( PHPWG_ROOT_PATH.'admin.php?page=cat_modify&amp;cat='.$row['id'] );
    292   if ( $category['nb_images'] > 0 )
     414    $template->assign_block_vars('category.virtual' ,array());
     415  }
     416
     417  if ($category['site_id'] == 1 and !empty($category['dir']))
     418  {
     419    $template->assign_block_vars('category.update' ,array());
     420    $template->assign_block_vars('category.metadata' ,array());
     421  }
     422  else
     423  {
     424    $template->assign_block_vars('category.no_update' ,array());
     425    $template->assign_block_vars('category.no_metadata' ,array());
     426  }
     427 
     428  if ($category['nb_images'] > 0)
    293429  {
    294430    $template->assign_block_vars('category.image_info' ,array());
     
    299435  }
    300436}
    301 
    302 //----------------------------------------------------------- sending html code
     437// +-----------------------------------------------------------------------+
     438// |                          sending html code                            |
     439// +-----------------------------------------------------------------------+
    303440$template->assign_var_from_handle('ADMIN_CONTENT', 'categories');
    304441?>
Note: See TracChangeset for help on using the changeset viewer.