<?php
/**
 * Edit page
 *
 * Edit members permissions
 *
 * @package PHP Competition System
 * @author  Eric GALLENE <okrome@free.fr>
 * @link	http://www.phpcompet.com
 * @version $Id: prem_members.php,v 1.00 2006/10/26 19:47:13
 */

require_once './core/functions.php';


// random view
if (empty($id)) 
{
	$count  = 0;
    $all    = strtoupper($lang['radio_all']);
    $WHERES = '';
    
    // find a random id
    $SELECT = 'SELECT id, REVERSE(RAND(NOW())) AS rnd
                 FROM '.TBL_USERS.'
                '.$WHERES.'
                ORDER BY rnd
                LIMIT 1';

	while (!$id)
	{
		$result = runSQL($SELECT);
        
        // prevent endless loop
        if (!count($result) || ($count++ > 50)) break;
        
		$id = $result[0]['id'];
	}
    
	// id still empty? go back to index.
	if (empty($id)) redirect('members.php');
}

//Edit member permission
if (!empty($id) && !empty($edit))
{
$perm=0;
if ($adminflag) {$perm += 1;}
if ($readflag) {$perm += 2;}
if ($mkcommentflag) {$perm += 4;}
if ($galleryflag) {$perm += 8;}
if ($mkbetflag) {$perm += 16;}
if ($addgalleryflag) {$perm += 32;}
if ($permnewsflag) {$perm += 64;}
if ($permwriteflag) {$perm += 128;}

runSQL("UPDATE ".TBL_USERS." SET permissions = $perm WHERE id = $id");
redirect('perm_members.php?id='.$id);

}

// get data (id may be empty on a empty database)
if (!empty($id))
{

    // XML export?
    if (($export == 'xml') && $config['xml'])
    {
        require_once './core/'.$export.'.php';
        xmlexport('members.id = '.$id);
        exit;
    }

$SELECT = 'SELECT '.TBL_USERS.'.id, '.TBL_USERS.'.name 
			FROM '.TBL_USERS.'
			WHERE '.TBL_USERS.'.id = \''.$id.'\';';
$memberid   = runSQL($SELECT);


$SELECT = 'SELECT * 
			FROM '.TBL_USERCONFIG.'
			WHERE '.TBL_USERCONFIG.'.user_id = \''.$memberid[0]['id'].'\';';
$memberconfig   = runSQL($SELECT);

for ($x=0; $x<count($memberconfig); $x++) {
$member[$memberconfig[$x]['opt']] = $memberconfig[$x]['value'];
}

    // is it editable/ copyable?
    $memberid[0]['editable'] = localnet();
    
    // multi-user permissions
    if ($config['multiuser'])
    {
        $memberid[0]['editable'] = check_permission(PERM_ADMIN, $_COOKIE['PCSuserid']);
		if ($memberid[0]['editable']) {
			if($activedit) {$memberid[0]['activedit'] = 1;}
		}
    }
	$memberid[0]['adminflag']      = check_permission(PERM_ADMIN, $memberid[0]['id']);
	$memberid[0]['readflag']       = check_permission(PERM_READ, $memberid[0]['id']);
	$memberid[0]['mkcommentflag']  = check_permission(PERM_MKCOMMENT, $memberid[0]['id']);
	$memberid[0]['galleryflag']    = check_permission(PERM_GALLERY, $memberid[0]['id']);
	$memberid[0]['mkbetflag']      = check_permission(PERM_MKBET, $memberid[0]['id']);
	$memberid[0]['addgalleryflag'] = check_permission(PERM_ADDGALLERY, $memberid[0]['id']);
	$memberid[0]['permnewsflag']   = check_permission(PERM_NEWS, $memberid[0]['id']);
	$memberid[0]['permwriteflag']  = check_permission(PERM_WRITE, $memberid[0]['id']);
		


    // previous/next buttons
    $ids = $_SESSION['pcs']['query_result'];
    if (is_array($ids))
    {
        if (($key = array_search($id, $ids)) !== false)
        {
            $memberid[0]['prev_id'] = ($key > 0) ? $ids[$key-1] : 0;
            $memberid[0]['next_id'] = ($key < count($ids)-1) ? $ids[$key+1] : 0;
        }
    }
}

$smarty->assign('memberid', $memberid[0]);
// prepare templates
tpl_page('detailview', $member['name']);
if (!empty($id)) tpl_perm_members($member);

// caching enabled?
if ($config['http_caching'])
{
    require_once('./core/httpcache.php');
    httpCacheCaptureStart();
}

// display templates
smarty_display('header.tpl');
if (!$config['http_caching']) flush();
if (!empty($id)) smarty_display('perm_members.tpl', $id);
smarty_display('footer.tpl');

// caching enabled?
if ($config['http_caching'])
{
    httpCacheOutput('perm_members'.$id, httpCacheCaptureEnd());
}

?>
