function CheckFields($ACTION, &$arFields, $ID = 0)
 {
     if ((is_set($arFields, "BLOG_ID") || $ACTION == "ADD") && IntVal($arFields["BLOG_ID"]) <= 0) {
         $GLOBALS["APPLICATION"]->ThrowException(GetMessage("BLG_GUGP_EMPTY_BLOG_ID"), "EMPTY_BLOG_ID");
         return false;
     } elseif (is_set($arFields, "BLOG_ID")) {
         $arResult = CBlog::GetByID($arFields["BLOG_ID"]);
         if (!$arResult) {
             $GLOBALS["APPLICATION"]->ThrowException(str_replace("#ID#", $arFields["BLOG_ID"], GetMessage("BLG_GUGP_ERROR_NO_BLOG")), "ERROR_NO_BLOG");
             return false;
         }
     }
     if ((is_set($arFields, "USER_GROUP_ID") || $ACTION == "ADD") && IntVal($arFields["USER_GROUP_ID"]) <= 0) {
         $GLOBALS["APPLICATION"]->ThrowException(GetMessage("BLG_GUGP_EMPTY_USER_GROUP_ID"), "EMPTY_USER_GROUP_ID");
         return false;
     } elseif (is_set($arFields, "USER_GROUP_ID")) {
         $arResult = CBlogUserGroup::GetByID($arFields["USER_GROUP_ID"]);
         if (!$arResult) {
             $GLOBALS["APPLICATION"]->ThrowException(str_replace("#ID#", $arFields["USER_GROUP_ID"], GetMessage("BLG_GUGP_ERROR_NO_USER_GROUP")), "ERROR_NO_USER_GROUP");
             return false;
         }
     }
     if ((is_set($arFields, "PERMS_TYPE") || $ACTION == "ADD") && $arFields["PERMS_TYPE"] != BLOG_PERMS_POST && $arFields["PERMS_TYPE"] != BLOG_PERMS_COMMENT) {
         $GLOBALS["APPLICATION"]->ThrowException(GetMessage("BLG_GUGP_EMPTY_PERMS_TYPE"), "EMPTY_PERMS_TYPE");
         return false;
     }
     if ((is_set($arFields, "PERMS") || $ACTION == "ADD") && strlen($arFields["PERMS"]) <= 0) {
         $GLOBALS["APPLICATION"]->ThrowException(GetMessage("BLG_GUGP_EMPTY_PERMS"), "EMPTY_PERMS");
         return false;
     } elseif (is_set($arFields, "PERMS")) {
         $arAvailPerms = array_keys($GLOBALS["AR_BLOG_PERMS"]);
         if (!in_array($arFields["PERMS"], $arAvailPerms)) {
             $GLOBALS["APPLICATION"]->ThrowException(str_replace("#ID#", $arFields["PERMS"], GetMessage("BLG_GUGP_ERROR_NO_PERMS")), "ERROR_NO_PERMS");
             return false;
         }
     }
     if ((is_set($arFields, "AUTOSET") || $ACTION == "ADD") && $arFields["AUTOSET"] != "Y" && $arFields["AUTOSET"] != "N") {
         $arFields["AUTOSET"] = "N";
     }
     return True;
 }
 function SetGroupPerms($ID, $blogID, $postID = 0, $permission = BLOG_PERMS_DENY, $permsType = BLOG_PERMS_POST)
 {
     global $DB;
     $ID = IntVal($ID);
     $blogID = IntVal($blogID);
     $postID = IntVal($postID);
     $arAvailPerms = array_keys($GLOBALS["AR_BLOG_PERMS"]);
     if (!in_array($permission, $arAvailPerms)) {
         $permission = $arAvailPerms[0];
     }
     $permsType = $permsType == BLOG_PERMS_COMMENT ? BLOG_PERMS_COMMENT : BLOG_PERMS_POST;
     $bSuccess = True;
     $arUserGroup = CBlogUserGroup::GetByID($ID);
     if (!$arUserGroup) {
         $GLOBALS["APPLICATION"]->ThrowException(str_replace("#ID#", $ID, GetMessage("BLG_GUG_ERROR_NO_USER_GROUP")), "ERROR_NO_USER_GROUP");
         $bSuccess = False;
     }
     if ($bSuccess) {
         $arBlog = CBlog::GetByID($blogID);
         if (!$arBlog) {
             $GLOBALS["APPLICATION"]->ThrowException(str_replace("#ID#", $blogID, GetMessage("BLG_GUG_ERROR_NO_BLOG")), "ERROR_NO_BLOG");
             $bSuccess = False;
         }
     }
     if ($bSuccess) {
         if ($postID > 0) {
             $arPost = CBlogPost::GetByID($postID);
             if (!$arPost) {
                 $GLOBALS["APPLICATION"]->ThrowException(str_replace("#ID#", $postID, GetMessage("BLG_GUG_ERROR_NO_POST")), "ERROR_NO_POST");
                 $bSuccess = False;
             }
         }
     }
     if ($bSuccess) {
         $oldGroupPerms = CBlogUserGroup::GetGroupPerms(1, $blogID, 0, BLOG_PERMS_POST);
         $DB->StartTransaction();
         $currentPerms = CBlogUserGroup::GetGroupPerms($ID, $blogID, $postID, $permsType);
         if ($currentPerms) {
             if ($currentPerms != $permission) {
                 if ($postID > 0) {
                     $DB->Query("UPDATE b_blog_user_group_perms SET " . "\tPERMS = '" . $DB->ForSql($permission) . "' " . "WHERE BLOG_ID = " . $blogID . " " . "\tAND POST_ID = " . $postID . " " . "\tAND USER_GROUP_ID = " . $ID . "" . "\tAND PERMS_TYPE = '" . $DB->ForSql($permsType) . "'");
                 } else {
                     $DB->Query("UPDATE b_blog_user_group_perms SET " . "\tPERMS = '" . $DB->ForSql($permission) . "' " . "WHERE BLOG_ID = " . $blogID . " " . "\tAND USER_GROUP_ID = " . $ID . " " . "\tAND PERMS_TYPE = '" . $DB->ForSql($permsType) . "'" . "\tAND POST_ID IS NULL ");
                 }
             }
         } else {
             if ($postID > 0) {
                 $DB->Query("INSERT INTO b_blog_user_group_perms (BLOG_ID, USER_GROUP_ID, PERMS_TYPE, POST_ID, PERMS) " . "VALUES (" . $blogID . ", " . $ID . ", '" . $DB->ForSql($permsType) . "', " . $postID . ", '" . $DB->ForSql($permission) . "') ");
             } else {
                 $DB->Query("INSERT INTO b_blog_user_group_perms (BLOG_ID, USER_GROUP_ID, PERMS_TYPE, POST_ID, PERMS) " . "VALUES (" . $blogID . ", " . $ID . ", '" . $DB->ForSql($permsType) . "', null, '" . $DB->ForSql($permission) . "') ");
             }
         }
         $DB->Commit();
         unset($GLOBALS["BLOG_USER_GROUP"]["BLOG_GROUP_PERMS_CACHE_" . $blogID . "_" . $postID . "_" . $permsType . "_" . $ID]);
         unset($GLOBALS["BLOG_USER_GROUP"]["BLOG_USER_PERMS_CACHE_" . $blogID . "_" . $postID . "_" . $permsType]);
     }
     if ($bSuccess) {
         if (CModule::IncludeModule("search")) {
             $newGroupPerms = CBlogUserGroup::GetGroupPerms(1, $blogID, 0, BLOG_PERMS_POST);
             if ($oldGroupPerms >= BLOG_PERMS_READ && $newGroupPerms < BLOG_PERMS_READ) {
                 CSearch::DeleteIndex("blog", false, $blogID);
             } elseif ($oldGroupPerms < BLOG_PERMS_READ && $newGroupPerms >= BLOG_PERMS_READ) {
             }
         }
     }
     return $bSuccess;
 }