CAdvType::Set: Bitrix method

      
<?php 
//  CAdvType :: Set()
//  /dist/bitrix/modules/advertising/classes/general/advertising.php:5187

    
public static function Set($arFields$OLD_SID$CHECK_RIGHTS="Y")
    {
        
$err_mess = (CAdvType_all::err_mess())."<br>Function: Set<br>Line: ";
        global 
$DB$USER;
        
$SID false;
        
$OLD_SID trim($OLD_SID);
        if (
CAdvType::CheckFields($arFields$OLD_SID$CHECK_RIGHTS))
        {
            
$arFields_i = array();
            
$arrKeys array_keys($arFields);
            if (
in_array("SID"$arrKeys))
                
$arFields_i["SID"] = "'".$DB->ForSql($arFields["SID"], 255)."'";
            if (
in_array("ACTIVE"$arrKeys) && ($arFields["ACTIVE"]=="Y" || $arFields["ACTIVE"]=="N"))
                
$arFields_i["ACTIVE"] = "'".$arFields["ACTIVE"]."'";
            if (
in_array("SORT"$arrKeys))
                
$arFields_i["SORT"] = "'".intval($arFields["SORT"])."'";
            if (
in_array("NAME"$arrKeys))
                
$arFields_i["NAME"] = "'".$DB->ForSql($arFields["NAME"], 255)."'";
            if (
in_array("DESCRIPTION"$arrKeys))
                
$arFields_i["DESCRIPTION"] = "'".$DB->ForSql($arFields["DESCRIPTION"], 2000)."'";
            if (
count($arFields_i)>0)
            {
                if (
strlen($OLD_SID)>0)
                {
                    if (
in_array("DATE_MODIFY"$arrKeys) && CheckDateTime($arFields["DATE_MODIFY"]))
                        
$arFields_i["DATE_MODIFY"] = $DB->CharToDateFunction($arFields["DATE_MODIFY"]);
                    else
                        
$arFields_i["DATE_MODIFY"] = $DB->GetNowFunction();

                    if (
in_array("MODIFIED_BY"$arrKeys))
                        
$arFields_i["MODIFIED_BY"] = intval($arFields["MODIFIED_BY"]);
                    else
                        
$arFields_i["MODIFIED_BY"] = $USER->GetID();

                    
$str "";
                    while (list(
$field,$value)=each($arFields_i))
                    {
                        if (
strlen($value)<=0$str .= "$field = '', "; else $str .= "$field = $value, ";
                    }
                    
$str TrimEx($str,",");
                    
$strSql "UPDATE b_adv_type SET ".$str." WHERE SID='".$DB->ForSql($OLD_SID255)."'";
                    
$DB->Query($strSqlfalse$err_mess.__LINE__);

                    if (
in_array("SID"$arrKeys))
                    {
                        
$SID $arFields["SID"];
                        
// если SID изменился то
                        
if ($arFields["SID"]!=$OLD_SID)
                        {
                            
// обновим тип у баннеров
                            
$arF = array("TYPE_SID" => "'".$DB->ForSql($arFields["SID"],255)."'");
                            
$DB->Update("b_adv_banner",$arF,"WHERE TYPE_SID='".$DB->ForSql($OLD_SID255)."'",$err_mess.__LINE__);

                            
// обновим тип у баннеров
                            
$arF = array("TYPE_SID" => "'".$DB->ForSql($arFields["SID"],255)."'");
                            
$DB->Update("b_adv_contract_2_type",$arF,"WHERE TYPE_SID='".$DB->ForSql($OLD_SID255)."'",$err_mess.__LINE__);
                        }
                    }
                    else 
$SID $OLD_SID;
                }
                elseif (
strlen($arFields_i["SID"])>0)
                {
                    if (
in_array("DATE_CREATE"$arrKeys) && CheckDateTime($arFields["DATE_CREATE"]))
                        
$arFields_i["DATE_CREATE"] = $DB->CharToDateFunction($arFields["DATE_CREATE"]);
                    else
                        
$arFields_i["DATE_CREATE"] = $DB->GetNowFunction();

                    if (
in_array("CREATED_BY"$arrKeys))
                        
$arFields_i["CREATED_BY"] = intval($arFields["CREATED_BY"]);
                    else
                        
$arFields_i["CREATED_BY"] = $USER->GetID();

                    if (
in_array("DATE_MODIFY"$arrKeys) && CheckDateTime($arFields["DATE_MODIFY"]))
                        
$arFields_i["DATE_MODIFY"] = $DB->CharToDateFunction($arFields["DATE_MODIFY"]);
                    else
                        
$arFields_i["DATE_MODIFY"] = $DB->GetNowFunction();

                    if (
in_array("MODIFIED_BY"$arrKeys))
                        
$arFields_i["MODIFIED_BY"] = intval($arFields["MODIFIED_BY"]);
                    else
                        
$arFields_i["MODIFIED_BY"] = $USER->GetID();

                    
$str1 $str2 "";
                    while (list(
$field,$value)=each($arFields_i))
                    {
                        
$str1 .= $field.", ";
                        if (
strlen($value)<=0$str2 .= "'', ";    else $str2 .= "$value, ";
                    }
                    
$str1 TrimEx($str1,",");
                    
$str2 TrimEx($str2,",");
                    
$strSql "INSERT INTO b_adv_type (".$str1.") VALUES (".$str2.")";
                    
$DB->Query($strSqlfalse$err_mess.__LINE__);
                    
$SID $arFields["SID"];
                }
            }
        }
        else
        {
            
$SID $arFields["SID"];
        }

        return 
$SID;
    }