CBlogUser :: AddToUserGroup: Bitrix method

      
<?php 
//  CBlogUser :: AddToUserGroup()
//  /dist/bitrix/modules/blog/general/blog_user.php:230

    
public static function AddToUserGroup($ID$blogID$arGroups = array(), $joinStatus "Y"$selectType BLOG_BY_BLOG_USER_ID$action BLOG_CHANGE)
    {
        global 
$DB;

        
$ID IntVal($ID);
        
$blogID IntVal($blogID);
        if (!
is_array($arGroups))
            
$arGroups = array($arGroups);
        
$joinStatus = (($joinStatus == "Y") ? "Y" "N");
        
$selectType = (($selectType == BLOG_BY_USER_ID) ? BLOG_BY_USER_ID BLOG_BY_BLOG_USER_ID);
        
$action = (($action == BLOG_ADD) ? BLOG_ADD BLOG_CHANGE);

        
$bSuccess True;

        
$arResult CBlog::GetByID($blogID);
        if (!
$arResult)
        {
            
$GLOBALS["APPLICATION"]->ThrowException(str_replace("#ID#"$blogIDGetMessage("BLG_GU_ERROR_NO_BLOG")), "ERROR_NO_BLOG");
            
$bSuccess False;
        }

        if (
$bSuccess)
        {
            
$arUser CBlogUser::GetByID($ID$selectType);

            
$dbResult CUser::GetByID($arUser["USER_ID"]);
            if (!
$dbResult->Fetch())
            {
                
$GLOBALS["APPLICATION"]->ThrowException(GetMessage("BLG_GU_ERROR_NO_USER_ID"), "ERROR_NO_USER_ID");
                
$bSuccess False;
            }
        }

        if (
$bSuccess)
        {
            if (
$action == BLOG_CHANGE)
                
$DB->Query(
                    
"DELETE FROM b_blog_user2user_group ".
                    
"WHERE USER_ID = ".IntVal($arUser["USER_ID"])." ".
                    
"    AND BLOG_ID = ".$blogID." "
                
);

            if (
count($arGroups) > 0)
            {
                
array_walk($arGroupscreate_function("&\$item""\$item=IntVal(\$item);"));

                
$dbUserGroups CBlogUserGroup::GetList(
                    array(),
                    array(
"ID" => $arGroups"BLOG_ID" => $blogID),
                    
false,
                    
false,
                    array(
"ID")
                );
                
$arGroups = array();
                while (
$arUserGroup $dbUserGroups->Fetch())
                    
$arGroups[] = IntVal($arUserGroup["ID"]);

                if (
$action == BLOG_ADD)
                    
$arCurrentGroups CBlogUser::GetUserGroups($ID$blogID""$selectType);

                foreach(
$arGroups as $val)
                {
                    if (
$val != && $val != 2)
                    {
                        if (
$action == BLOG_CHANGE
                            
|| $action == BLOG_ADD && !in_array($val$arCurrentGroups))
                        {
                            
$DB->Query(
                                
"INSERT INTO b_blog_user2user_group (USER_ID, BLOG_ID, USER_GROUP_ID) ".
                                
"VALUES (".IntVal($arUser["USER_ID"]).", ".$blogID.", ".IntVal($val).")"
                            
);
                        }
                    }
                }
            }

            unset(
$GLOBALS["BLOG_USER"]["BLOG_USER2GROUP_CACHE_".$arUser["ID"]]);
            unset(
$GLOBALS["BLOG_USER"]["BLOG_USER2GROUP1_CACHE_".$arUser["USER_ID"]]);
        }

        return 
$bSuccess;
    }