| // | branch : BSF (Best So Far) | // +-----------------------------------------------------------------------+ // | file : $RCSfile$ // | last update : $Date$ // | last modifier : $Author$ // | revision : $Revision$ // +-----------------------------------------------------------------------+ // | This program is free software; you can redistribute it and/or modify | // | it under the terms of the GNU General Public License as published by | // | the Free Software Foundation | // | | // | This program is distributed in the hope that it will be useful, but | // | WITHOUT ANY WARRANTY; without even the implied warranty of | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | // | General Public License for more details. | // | | // | You should have received a copy of the GNU General Public License | // | along with this program; if not, write to the Free Software | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | // | USA. | // +-----------------------------------------------------------------------+ include_once( './admin/include/isadmin.inc.php' ); //----------------------------------------------------- template initialization $sub = $vtp->Open( './template/'.$user['template'].'/admin/cat_list.vtp' ); $tpl = array( 'cat_edit','cat_up','cat_down','cat_image_info', 'cat_permission','cat_update','cat_add','cat_parent','submit', 'cat_virtual','delete','cat_first','cat_last','errors_title' ); templatize_array( $tpl, 'lang', $sub ); $vtp->setGlobalVar( $sub, 'user_template', $user['template'] ); //--------------------------------------------------- adding a virtual category $errors = array(); if ( isset( $_POST['submit'] ) ) { // is the given category name only containing blank spaces ? if ( preg_match( '/^\s*$/', $_POST['virtual_name'] ) ) array_push( $errors, $lang['cat_error_name'] ); // does the uppercat id exists in the database ? if ( $_POST['associate'] == '' ) { $_POST['associate'] = -1; } else if ( !is_numeric( $_POST['associate'] ) ) { array_push( $errors, $lang['cat_unknown_id'] ); } else { $query = 'SELECT id'; $query.= ' FROM '.PREFIX_TABLE.'categories'; $query.= ' WHERE id = '.$_POST['associate']; $query.= ';'; if ( mysql_num_rows( mysql_query( $query ) ) == 0 ) array_push( $errors, $lang['cat_unknown_id'] ); } if ( count( $errors ) == 0 ) { // we have then to add the virtual category $query = 'INSERT INTO '.PREFIX_TABLE.'categories'; $query.= ' (name,id_uppercat) VALUES '; if ( $_POST['associate'] == -1 ) { $_POST['associate'] = 'NULL'; } $query.= " ('".$_POST['virtual_name']."',".$_POST['associate'].")"; $query.= ';'; mysql_query( $query ); synchronize_all_users(); } } //--------------------------------------------------------------- rank updates if ( isset( $_GET['up'] ) and is_numeric( $_GET['up'] ) ) { // 1. searching level (id_uppercat) // and rank of the category to move $query = 'SELECT id_uppercat,rank'; $query.= ' FROM '.PREFIX_TABLE.'categories'; $query.= ' WHERE id = '.$_GET['up']; $query.= ';'; $row = mysql_fetch_array( mysql_query( $query ) ); $level = $row['id_uppercat']; $rank = $row['rank']; // 2. searching the id and the rank of the category // just above at the same level $query = 'SELECT id,rank'; $query.= ' FROM '.PREFIX_TABLE.'categories'; $query.= ' WHERE rank < '.$rank; if ( $level == '' ) { $query.= ' AND id_uppercat IS NULL'; } else { $query.= ' AND id_uppercat = '.$level; } $query.= ' ORDER BY rank DESC'; $query.= ' LIMIT 0,1'; $query.= ';'; $row = mysql_fetch_array( mysql_query( $query ) ); $new_rank = $row['rank']; $replaced_cat = $row['id']; // 3. exchanging ranks between the two categories $query = 'UPDATE '.PREFIX_TABLE.'categories'; $query.= ' SET rank = '.$new_rank; $query.= ' WHERE id = '.$_GET['up']; $query.= ';'; mysql_query( $query ); $query = 'UPDATE '.PREFIX_TABLE.'categories'; $query.= ' SET rank = '.$rank; $query.= ' WHERE id = '.$replaced_cat; $query.= ';'; mysql_query( $query ); } if ( isset( $_GET['down'] ) and is_numeric( $_GET['down'] ) ) { // 1. searching level (id_uppercat) // and rank of the category to move $query = 'SELECT id_uppercat,rank'; $query.= ' FROM '.PREFIX_TABLE.'categories'; $query.= ' WHERE id = '.$_GET['down']; $query.= ';'; $row = mysql_fetch_array( mysql_query( $query ) ); $level = $row['id_uppercat']; $rank = $row['rank']; // 2. searching the id and the rank of the category // just below at the same level $query = 'SELECT id,rank'; $query.= ' FROM '.PREFIX_TABLE.'categories'; $query.= ' WHERE rank > '.$rank; if ( $level == '' ) { $query.= ' AND id_uppercat IS NULL'; } else { $query.= ' AND id_uppercat = '.$level; } $query.= ' ORDER BY rank ASC'; $query.= ' LIMIT 0,1'; $query.= ';'; $row = mysql_fetch_array( mysql_query( $query ) ); $new_rank = $row['rank']; $replaced_cat = $row['id']; // 3. exchanging ranks between the two categories $query = 'UPDATE '.PREFIX_TABLE.'categories'; $query.= ' SET rank = '.$new_rank; $query.= ' WHERE id = '.$_GET['down']; $query.= ';'; mysql_query( $query ); $query = 'UPDATE '.PREFIX_TABLE.'categories'; $query.= ' SET rank = '.$rank; $query.= ' WHERE id = '.$replaced_cat; $query.= ';'; mysql_query( $query ); } if ( isset( $_GET['last'] ) and is_numeric( $_GET['last'] ) ) { // 1. searching level (id_uppercat) of the category to move $query = 'SELECT id_uppercat,rank'; $query.= ' FROM '.PREFIX_TABLE.'categories'; $query.= ' WHERE id = '.$_GET['last']; $query.= ';'; $row = mysql_fetch_array( mysql_query( $query ) ); $level = $row['id_uppercat']; // 2. searching the highest rank of the categories of the same parent $query = 'SELECT MAX(rank) AS max_rank'; $query.= ' FROM '.PREFIX_TABLE.'categories'; $query.= ' WHERE id_uppercat'; if ( $level == '' ) $query.= ' IS NULL'; else $query.= ' = '.$level; $query.= ';'; $row = mysql_fetch_array( mysql_query( $query ) ); $max_rank = $row['max_rank']; // 3. updating the rank of our category to be after the previous max rank $query = 'UPDATE '.PREFIX_TABLE.'categories'; $query.= ' SET rank = '.($max_rank + 1); $query.= ' WHERE id = '.$_GET['last']; $query.= ';'; mysql_query( $query ); } if ( isset( $_GET['first'] ) and is_numeric( $_GET['first'] ) ) { // to place our category as first, we simply say that is rank is 0, then // reordering will move category ranks correctly (first rank should be 1 // and not 0) $query = 'UPDATE '.PREFIX_TABLE.'categories'; $query.= ' SET rank = 0'; $query.= ' WHERE id = '.$_GET['first']; $query.= ';'; mysql_query( $query ); } if ( isset( $_GET['delete'] ) and is_numeric( $_GET['delete'] ) ) { delete_category( $_GET['delete'] ); synchronize_all_users(); } //------------------------------------------------------------------ reordering function ordering( $id_uppercat ) { $rank = 1; $query = 'SELECT id'; $query.= ' FROM '.PREFIX_TABLE.'categories'; if ( !is_numeric( $id_uppercat ) ) { $query.= ' WHERE id_uppercat IS NULL'; } else { $query.= ' WHERE id_uppercat = '.$id_uppercat; } $query.= ' ORDER BY rank ASC, dir ASC'; $query.= ';'; $result = mysql_query( $query ); while ( $row = mysql_fetch_array( $result ) ) { $query = 'UPDATE '.PREFIX_TABLE.'categories'; $query.= ' SET rank = '.$rank; $query.= ' WHERE id = '.$row['id']; $query.= ';'; mysql_query( $query ); $rank++; ordering( $row['id'] ); } } ordering( 'NULL' ); //-------------------------------------------------------------- errors display if ( count( $errors ) != 0 ) { $vtp->addSession( $sub, 'errors' ); foreach ( $errors as $error ) { $vtp->addSession( $sub, 'li' ); $vtp->setVar( $sub, 'li.content', $error ); $vtp->closeSession( $sub, 'li' ); } $vtp->closeSession( $sub, 'errors' ); } //---------------------------------------------------------------- page display function display_cat_manager( $id_uppercat, $indent, $uppercat_visible, $level ) { global $lang,$conf,$sub,$vtp,$page; // searching the min_rank and the max_rank of the category $query = 'SELECT MIN(rank) AS min, MAX(rank) AS max'; $query.= ' FROM '.PREFIX_TABLE.'categories'; if ( !is_numeric( $id_uppercat ) ) { $query.= ' WHERE id_uppercat IS NULL'; } else { $query.= ' WHERE id_uppercat = '.$id_uppercat; } $query.= ';'; $result = mysql_query( $query ); $row = mysql_fetch_array( $result ); if ( !isset( $row['min'] ) ) $row['min'] = 0; if ( !isset( $row['max'] ) ) $row['max'] = 0; $min_rank = $row['min']; $max_rank = $row['max']; // will we use