<?
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_before.php");
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/forum/include.php");

$forumModulePermissions = $APPLICATION->GetGroupRight("forum");
if ($forumModulePermissions == "D")
	$APPLICATION->AuthForm(GetMessage("ACCESS_DENIED"));

IncludeModuleLangFile(__FILE__);
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/forum/prolog.php");

$sTableID = "tbl_forum_group";

$oSort = new CAdminSorting($sTableID, "ID", "asc");
$lAdmin = new CAdminList($sTableID, $oSort);

$arFilterFields = array();

$lAdmin->InitFilter($arFilterFields);

$arFilter = array();

if ($lAdmin->EditAction() && $forumModulePermissions >= "W")
{
	foreach ($FIELDS as $ID => $arFields)
	{
		if ($ID <= 0)
			continue;
		if (!$lAdmin->IsUpdated($ID))
			continue;
		if (CForumGroup::CanUserUpdateGroup($ID, $USER->GetUserGroupArray()))
		{
			if (CForumGroup::Update($ID, $arFields))
				BXClearCache(true, "bitrix/forum/group/");
		}
	}
}

if (($arID = $lAdmin->GroupAction()) && $forumModulePermissions >= "W")
{
	if ($_REQUEST['action_target']=='selected')
	{
		$arID = array();
		$dbResultList = CForumGroup::GetList(
			array($by => $order),
			$arFilter
		);
		while ($arResult = $dbResultList->Fetch())
			$arID[] = $arResult['ID'];
	}

	foreach ($arID as $ID)
	{
		if (strlen($ID) <= 0)
			continue;

		switch ($_REQUEST['action'])
		{
			case "delete":

				@set_time_limit(0);

				$DB->StartTransaction();

				if (!CForumGroup::Delete($ID))
				{
					$DB->Rollback();

					if ($ex = $APPLICATION->GetException())
						$lAdmin->AddGroupError($ex->GetString(), $ID);
					else
						$lAdmin->AddGroupError(GetMessage("ERROR_DEL_GROUP"), $ID);
				}

				$DB->Commit();

				break;
		}
		
	}
	BXClearCache(true, "/".LANG."/forum/group/");
}

$dbResultList = CForumGroup::GetList(
	array($by => $order),
	$arFilter
);

$dbResultList = new CAdminResult($dbResultList, $sTableID);
$dbResultList->NavStart();

$lAdmin->NavText($dbResultList->GetNavPrint(GetMessage("GROUP_NAV")));

$lAdmin->AddHeaders(array(
	array("id"=>"ID", "content"=>GetMessage("GROUP_ID"), "sort"=>"ID", "default"=>true),
	array("id"=>"NAME", "content"=>GetMessage('FORUM_NAME'),	"sort"=>"", "default"=>true),
	array("id"=>"SORT","content"=>GetMessage("GROUP_SORT"), "sort"=>"SORT", "default"=>true, "align"=>"right"),
));

$arVisibleColumns = $lAdmin->GetVisibleHeaderColumns();

/*******************************************************************/
while ($arGroup = $dbResultList->NavNext(true, "f_"))
{
	$row =& $lAdmin->AddRow($f_ID, $arGroup);

	$row->AddField("ID", $f_ID);
	$row->AddInputField("SORT", array("size" => 5));

	if (in_array("NAME", $arVisibleColumns))
	{
		$arGroupLang = CForumGroup::GetLangByID($f_ID, LANG);
		$fieldShow = htmlspecialcharsbx($arGroupLang["NAME"]);
		$row->AddViewField("NAME", '<a title="'.GetMessage("FORUM_EDIT_DESCR").'" href="'."forum_group_edit.php?ID=".$f_ID."&lang=".LANG."&".GetFilterParams("filter_").'">'.$fieldShow.'</a>');
	}

	$arActions = Array();
	if (($forumModulePermissions>="R") && CForumGroup::CanUserUpdateGroup(0, $USER->GetUserGroupArray()))
	{
		$arActions[] = array("ICON"=>"edit", "TEXT"=>GetMessage("FORUM_EDIT_DESCR"), "ACTION"=>$lAdmin->ActionRedirect("forum_group_edit.php?ID=".$f_ID."&lang=".LANG."&".GetFilterParams("filter_", false)), "DEFAULT"=>true);
	}
	if (($forumModulePermissions >= "W") && CForumGroup::CanUserDeleteGroup(0, $USER->GetUserGroupArray()))
	{
		$arActions[] = array("SEPARATOR" => true);
		$arActions[] = array("ICON"=>"delete", "TEXT"=>GetMessage("FORUM_DELETE_DESCR"), "ACTION"=>"if(confirm('".GetMessage('GROUP_DEL_CONF')."')) ".$lAdmin->ActionDoGroup($f_ID, "delete"));
	}

	$row->AddActions($arActions);
}

/*******************************************************************/
$lAdmin->AddFooter(
	array(
		array(
			"title" => GetMessage("MAIN_ADMIN_LIST_SELECTED"),
			"value" => $dbResultList->SelectedRowsCount()
		),
		array(
			"counter" => true,
			"title" => GetMessage("MAIN_ADMIN_LIST_CHECKED"),
			"value" => "0"
		),
	)
);

/*******************************************************************/
$lAdmin->AddGroupActionTable(
	array(
		"delete" => GetMessage("MAIN_ADMIN_LIST_DELETE"),
	)
);

if (($forumModulePermissions >= "W") && CForumGroup::CanUserAddGroup($USER->GetUserGroupArray()))
{
	$aContext = array(
		array(
			"TEXT" => GetMessage("FGAN_ADD_NEW"),
			"LINK" => "forum_group_edit.php?lang=".LANG,
			"TITLE" => GetMessage("FGAN_ADD_NEW_ALT"),
			"ICON" => "btn_new",
		),
	);
	$lAdmin->AddAdminContextMenu($aContext);
}

/*******************************************************************/
$lAdmin->CheckListMode();


/****************************************************************************/
/***********  MAIN PAGE  ****************************************************/
/****************************************************************************/
$APPLICATION->SetTitle(GetMessage("GROUP_TITLE"));
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_after.php");

$lAdmin->DisplayList();

require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/epilog_admin.php");
?>