<?php

if ( !defined( 'SMARTY_DIR' ) ) {
	include_once( '../init.php' );
}

include ( 'sessioninc.php' );

define( 'PAGE_ID', 'profie_approval' );

if ( !checkAdminPermission( PAGE_ID ) ) {

	header( 'location: not_authorize.php' );
	exit;
}

$sort = findSortBy('username');

if ( $_POST['groupaction'] != '' && count($_POST['txtchk']) > 0 && $_POST['groupaction'] != get_lang('status_act','cancel') ){

	foreach ($lang['status_act'] as $key => $val ) {
		if ($val == $_POST['groupaction']) {
			$action = $key;
		}
	}

	$sql = 'UPDATE ! SET status =?, active = 1 WHERE 0 ';

	foreach( $_POST['txtchk'] as $item) {

		$sql .= ' OR id = \'' . $item .'\'';

		if ($action == 'active') {

			$activedays = $osDB->getOne('select distinct mem.activedays from ! as mem, ! as usr where usr.id = ? and mem.roleid = usr.level', array(MEMBERSHIP_TABLE, USER_TABLE, $item) );

			$levelend = strtotime("+$activedays day",time());

			$osDB->query('update ! set levelend = ? where id = ?',array( USER_TABLE, $levelend, $item) );

		}
	}
	if ($action == 'rejected') { $errid = PROFILES_REJECTED; }
	elseif ($action == 'active') { 	$errid = PROFILES_ACTIVATED; }
 	else { $errid = PROFILES_SUSPENDED; }

	$rs = $osDB->query( $sql, array( USER_TABLE, $action ) );

	header( 'location: ?errid='.$errid );

	exit;
}

$t->assign( 'sort_type', checkSortType( $_GET['type'] ) );

$t->assign ( 'data' , $osDB->getAll ( 'SELECT * FROM ! WHERE status in (?, ?) ORDER BY ' . $sort, array( USER_TABLE, get_lang('status_enum','approval'), 'approval' ) ) );

$t->assign ( 'errid' , $_GET['errid'] );

$t->assign("error_message", get_lang('errormsgs', $_GET['errid']) );

$t->assign ( 'lang', $lang );

$t->assign('rendered_page', $t->fetch('admin/unapprovedusers.tpl'));

$t->display ( 'admin/index.tpl' );

?>
