<?php
if ( !eregi("admin.php", $PHP_SELF) ) { 
	die ("Access Denied"); 
}
if ( $xoopsUser->isAdmin($xoopsModule->mid()) ) {

/*********************************************************/
/* Users Functions                                       */
/*********************************************************/
include_once(XOOPS_ROOT_PATH."/class/xoopslists.php");
include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");

function displayUsers() {
	global $xoopsDB, $xoopsConfig, $xoopsModule;
        xoops_cp_header();
	OpenTable();
	$editform = new XoopsThemeForm(_AM_EDEUSER, "edituser", "admin.php");
	$user_select = new XoopsFormSelect(_AM_NICKNAME, "uid");
	$user_select->addOptionArray(XoopsUser::getAllUsersList(array(), $orderby="uname ASC"));
	$op_select = new XoopsFormSelect("", "op");
	$op_select->addOptionArray(array("modifyUser"=>_AM_MODIFYUSER, "delUser"=>_AM_DELUSER));
	$submit_button = new XoopsFormButton("", "submit", _AM_GO, "submit");
	$fct_hidden = new XoopsFormHidden("fct", "users");
	$editform->addElement($user_select);
	$editform->addElement($op_select);
	$editform->addElement($submit_button);
	$editform->addElement($fct_hidden);
	$editform->display();
	echo "<br />\n";
	$uid_value = "";
	$uname_value = "";
	$name_value = "";
	$email_value = "";
	$email_cbox_value = 0;
	$url_value = "";
	$avatar_value = "blank.gif";
	$theme_value = $xoopsConfig['default_theme'];
	$timezone_value = $xoopsConfig['default_TZ'];
	$icq_value = "";
	$aim_value = "";
	$yim_value = "";
	$msnm_value = "";
	$location_value = "";
	$occ_value = "";
	$interest_value = "";
	$sig_value = "";
	$sig_cbox_value = 0;
	$umode_value = $xoopsConfig['com_mode'];
	$uorder_value = $xoopsConfig['com_order'];
	$bio_value = "";
	$rank_value = 0;
	$op_value = "addUser";
	$form_title = _AM_ADDUSER;
	include(XOOPS_ROOT_PATH."/modules/system/admin/users/userform.php");
	CloseTable();
        xoops_cp_footer();
}

function modifyUser($user) {
	global $xoopsDB, $xoopsConfig, $xoopsModule;
        xoops_cp_header();
	OpenTable();
	$user = new XoopsUser($user);
	if ( !$user->isActive() ) {
		echo "<h4 style='text-align:left;'>"._AM_NOTACTIVE."</h4>";
		echo "<table><tr><td>\n";
		echo myTextForm("admin.php?fct=users&amp;op=reactivate&amp;uid=".$user->getVar("uid"), _AM_YES);
		echo "</td><td>\n";
		echo myTextForm("admin.php?fct=users", _AM_NO);
		echo "</td></tr></table>\n";
		CloseTable();
		xoops_cp_footer();
		exit();
	}
        if ( $user ) {
                $uid_value = $user->getVar("uid");
		$uname_value = $user->getVar("uname", "E");
		$name_value = $user->getVar("name", "E");
		$email_value = $user->getVar("email", "E");
		$email_cbox_value = $user->getVar("user_viewemail") ? 1 : 0;
		$url_value = $user->getVar("url", "E");
		$avatar_value = $user->getVar("user_avatar");
		$temp = $user->getVar("theme");
		$theme_value = empty($temp) ? $xoopsConfig['default_theme'] : $temp;
		$timezone_value = $user->getVar("timezone_offset");
		$icq_value = $user->getVar("user_icq", "E");
		$aim_value = $user->getVar("user_aim", "E");
		$yim_value = $user->getVar("user_yim", "E");
		$msnm_value = $user->getVar("user_msnm", "E");
		$location_value = $user->getVar("user_from", "E");
		$occ_value = $user->getVar("user_occ", "E");
		$interest_value = $user->getVar("user_intrest", "E");
		$sig_value = $user->getVar("user_sig", "E");
		$sig_cbox_value = ($user->getVar("attachsig") == 1) ? 1 : 0;
		$umode_value = $user->getVar("umode");
		$uorder_value = $user->getVar("uorder");
		$bio_value = $user->getVar("bio", "E");
		$rank_value = $user->rank(false);
		$op_value = "updateUser";
		$form_title = _AM_UPDATEUSER.": ".$user->getVar("uname");
		include(XOOPS_ROOT_PATH."/modules/system/admin/users/userform.php");
		echo "<br /><b>"._AM_USERPOST."</b><br /><br />\n";
		echo "<table border=\"0\">\n";
		echo "<tr><td>"._AM_COMMENTS."</td><td>".$user->getVar("posts")."</td></tr>\n";
		echo "</table>\n";
		echo "<br />"._AM_PTBBTSDIYT."<br />\n";
		echo "<form action=\"admin.php\" method=\"post\">\n";
		echo "<input type=\"hidden\" name=\"id\" value=\"".$user->getVar("uid")."\">";
		echo "<input type=\"hidden\" name=\"type\" value=\"user\">\n";
		echo "<input type=\"hidden\" name=\"fct\" value=\"users\">\n";
		echo "<input type=\"hidden\" name=\"op\" value=\"synchronize\">\n";
		echo "<input type=\"submit\" value=\""._AM_SYNCHRONIZE."\">\n";
		echo "</form>\n";
		CloseTable();
        } else {
		echo "<h4 style='text-align:left;'>";
		echo _AM_USERDONEXIT;
		echo "</h4>";
		CloseTable();
        }
        xoops_cp_footer();
}

function updateUser($uid, $uname, $name, $url, $email, $user_icq, $user_aim, $user_yim, $user_msnm, $user_from, $user_occ, $user_intrest, $user_viewemail, $user_avatar, $user_sig, $attachsig, $theme, $pass, $pass2, $rank, $bio, $uorder, $umode, $timezone_offset) {

	global $xoopsConfig, $xoopsDB, $xoopsModule;
	$edituser = new XoopsUser($uid);
	$edituser->setVar("name", $name);
	$edituser->setVar("uname", $uname);
	$edituser->setVar("email", $email);
	if (isset($url) && $url != "") {
		$edituser->setVar("url", formatURL($url));
	}
	$edituser->setVar("user_avatar", $user_avatar);
	$edituser->setVar("user_icq", $user_icq);
	$edituser->setVar("user_from", $user_from);
	$edituser->setVar("user_sig", $user_sig);
	$edituser->setVar("user_viewemail", $user_viewemail);
	$edituser->setVar("user_aim", $user_aim);
	$edituser->setVar("user_yim", $user_yim);
	$edituser->setVar("user_msnm", $user_msnm);
	$edituser->setVar("attachsig", $attachsig);
	$edituser->setVar("timezone_offset", $timezone_offset);
	$edituser->setVar("theme", $theme);
	$edituser->setVar("uorder", $uorder);
	$edituser->setVar("umode", $umode);
	$edituser->setVar("bio", $bio);
	$edituser->setVar("rank", $rank);
	$edituser->setVar("user_occ", $user_occ);
	$edituser->setVar("user_intrest", $user_intrest);
	if ($pass2 != "") {
                if ( $pass != $pass2 ) {
			xoops_cp_header();
			echo "
			<center>"._AM_STNPDNM."</center>";
			xoops_cp_footer();
			exit;
                }
		$edituser->setVar("pass", md5($pass));
        }
	if ( !$edituser->store() ) {
		xoops_cp_header();
		echo $editusers->getErrors();
		xoops_cp_footer();
	} else {
		redirect_header("admin.php?fct=users",1,_AM_DBUPDATED);
	}
	exit();
}


function synchronize($id, $type) {
	global $xoopsDB;
   	switch($type) {
   		case 'user':
			$tables = array("comments","xoopspollcomments");
			$total_posts = 0;
			$sql = "SELECT COUNT(*) AS total FROM ".$xoopsDB->prefix("bb_posts")." WHERE uid = $id";
   			if ($result = $xoopsDB->query($sql)) {
				if($row = $xoopsDB->fetchArray($result)){
					$total_posts = $total_posts + $row['total'];
   				}
			}
			foreach ($tables as $table) {
   				$sql = "SELECT COUNT(*) AS total FROM ".$xoopsDB->prefix($table)." WHERE user_id = $id";
   				if ( $result = $xoopsDB->query($sql) ) {
					if ($row = $xoopsDB->fetchArray($result)) {
						$total_posts = $total_posts + $row['total'];
   					}
   				}
			}
			$sql = "UPDATE ".$xoopsDB->prefix("users")." SET posts = $total_posts WHERE uid = $id";
   			if (!$result = $xoopsDB->query($sql)) {
   				die(sprintf(_AM_CNUUSER %s ,$id));
   			}
   			break;

   		case 'all users':
   			$sql = "SELECT uid FROM ".$xoopsDB->prefix("users")."";
   			if (!$result = $xoopsDB->query($sql)) {
   				die(_AM_CNGUSERID);
   			}
   			while ($row = $xoopsDB->fetchArray($result)) {
   				$id = $row['uid'];
   				synchronize($id, "user");
   			}
   			break;
   		default:
			break;
   	}
   	redirect_header("admin.php?fct=users&op=modifyUser&uid=".$id."",1,_AM_DBUPDATED);
	exit();
}

} else {
    	echo "Access Denied";
}

?>