<?php

include  './core.php';

if( !isset( $user ) || !$user->can_access( 'settings_admin' ) ) access_error();

$catlist = $MySQL->fetch_complete( "SELECT categoryID, name FROM setting_categories ORDER BY `order`" );
$Smarty->assign( 'catlist', $catlist );

if( isset( $_REQUEST['category'] ) )
	$cat = $MySQL->query_first( sprintf("SELECT categoryID, name FROM setting_categories WHERE categoryID=%d", intval( $_REQUEST['category'] ) ));
else
	$cat = $catlist[0];
$Smarty->assign( 'cat', $cat );

if( isset( $_POST['send_form'] ) )
{
    $values = $MySQL->query( sprintf("SELECT * FROM settings WHERE categoryID=%d ORDER BY `order`", $cat['categoryID'] ));

    while( $row = $MySQL->fetch_array( $values ) )
    {
    	if( stripslashes( $row['value'] ) != $_POST['settings_'.$row['settingID']] )
        {
        	$MySQL->query( sprintf("UPDATE settings SET value='%s' WHERE settingID = %d", me_escape( $_POST['settings_'.$row['settingID']] ), $row['settingID'] ));
        }
    }

    $Smarty->assign( 'saved', true );
}

$values = $MySQL->fetch_complete( sprintf("SELECT * FROM settings WHERE categoryID=%d ORDER BY `order`", $cat['categoryID'] ));
$Smarty->assign( 'values', $values );

$page['content'] = $Smarty->fetch( 'settings_admin.tpl' );

$Smarty->assign( 'page', $page );
$Smarty->display( 'main.tpl' );

?>