CAdvBanner :: FixShowAll: Bitrix method

      
<?php 
//  CAdvBanner :: FixShowAll()
//  /dist/bitrix/modules/advertising/classes/general/advertising.php:4207

    
public static function FixShowAll()
    {
        global 
$DB$CACHE_ADVERTISING$arrADV_VIEWED_BANNERS$APPLICATION;
        
$err_mess = (CAdvBanner::err_mess())."<br>Function: FixShowAll<br>Line: ";

        if (
is_array($CACHE_ADVERTISING) &&
            
array_key_exists("BANNERS_ALL"$CACHE_ADVERTISING) &&
            
is_array($CACHE_ADVERTISING["BANNERS_ALL"]) &&
            !empty(
$CACHE_ADVERTISING["BANNERS_ALL"]) &&
            
array_key_exists("BANNERS_CNT"$CACHE_ADVERTISING) &&
            
is_array($CACHE_ADVERTISING["BANNERS_CNT"]))
        {

            if( 
array_key_exists"ALL_DATE_SHOW_FIRST"$CACHE_ADVERTISING ) && is_array$CACHE_ADVERTISING["ALL_DATE_SHOW_FIRST"] ) )
            {
                foreach( 
$CACHE_ADVERTISING["ALL_DATE_SHOW_FIRST"] as $key => $value )
                {
                    
$DB->Update"b_adv_banner", Array( "DATE_SHOW_FIRST" => $value ), "WHERE ID='" $key "'"$err_mess __LINE__ );
                }
            }

            
$bEqualBanID = ($CACHE_ADVERTISING["BANNERS_ALL"] == $CACHE_ADVERTISING["BANNERS_CNT"]);

            
//Update баннеров
            
$arFields = Array(
                
"SHOW_COUNT"        => "SHOW_COUNT + 1",
                
"DATE_LAST_SHOW"    => $DB->GetNowFunction(),
            );
            if (
$bEqualBanID)
                
$arFields["VISITOR_COUNT"] = "VISITOR_COUNT + 1";

            
$group_all '';
            foreach(
$CACHE_ADVERTISING["BANNERS_ALL"] as $b)
                
$group_all .= ($group_all <> ''',':'').intval($b);

            
$DB->Update("b_adv_banner",$arFields,"WHERE ID IN(".$group_all.")",$err_mess.__LINE__);

            if (!
$bEqualBanID && !empty($CACHE_ADVERTISING["BANNERS_CNT"]))
            {
                
$arFields = Array("VISITOR_COUNT" => "VISITOR_COUNT + 1");
                
$group_inc "";
                foreach(
$CACHE_ADVERTISING["BANNERS_CNT"] as $BANNERS_CNT)
                    
$group_inc .= ($group_inc <> """,":"").intval($BANNERS_CNT);
                
$DB->Update("b_adv_banner",$arFields,"WHERE ID IN(".$group_inc.")",$err_mess.__LINE__);
            }

            
//Баннеры по дням
            
$strSql "SELECT BANNER_ID FROM b_adv_banner_2_day WHERE BANNER_ID IN (".$group_all.") and DATE_STAT = ".$DB->GetNowDate();
            
$res $DB->Query($strSqlfalse$err_mess.__LINE__);
            
$arExist $arInsert = Array();
            while (
$ar $res->Fetch())
                
$arExist[] = $ar["BANNER_ID"];

            
$arInsert array_diff($CACHE_ADVERTISING["BANNERS_ALL"], $arExist);

            foreach (
$arInsert as $BANNER_ID)
            {
                
$strSql "INSERT INTO b_adv_banner_2_day (DATE_STAT, BANNER_ID, SHOW_COUNT, VISITOR_COUNT) VALUES (".$DB->GetNowDate().", $BANNER_ID,1,1)";
                
$DB->Query($strSqltrue$err_mess.__LINE__);
            }

            if (!empty(
$arExist))
            {
                
$arExistInc array_intersect($arExist,$CACHE_ADVERTISING["BANNERS_CNT"]);
                if (!empty(
$arExistInc))
                {
                    
$sExistInc '';
                    foreach(
$arExistInc as $b)
                        
$sExistInc .= ($sExistInc <> ''',':'').intval($b);

                    
$arFields = Array("SHOW_COUNT" => "SHOW_COUNT + 1""VISITOR_COUNT" => "VISITOR_COUNT + 1");
                    
$DB->Update("b_adv_banner_2_day",$arFields,"WHERE BANNER_ID IN(".$sExistInc.") and DATE_STAT = ".$DB->GetNowDate(),$err_mess.__LINE__);
                }

                
$arExistInc array_diff($arExist$arExistInc);

                if (!empty(
$arExistInc))
                {
                    
$sExistInc '';
                    foreach(
$arExistInc as $b)
                        
$sExistInc .= ($sExistInc <> ''',':'').intval($b);

                    
$arFields = Array("SHOW_COUNT" => "SHOW_COUNT + 1");
                    
$DB->Update("b_adv_banner_2_day",$arFields,"WHERE BANNER_ID IN(".$sExistInc.") and DATE_STAT = ".$DB->GetNowDate(),$err_mess.__LINE__);
                }
            }

            
//Контракты
            
$DONT_USE_CONTRACT COption::GetOptionString("advertising""DONT_USE_CONTRACT""N");
            if (
$DONT_USE_CONTRACT == "N" &&
                
array_key_exists("CONTRACTS_ALL"$CACHE_ADVERTISING) &&
                
is_array($CACHE_ADVERTISING["CONTRACTS_ALL"]) &&
                !empty(
$CACHE_ADVERTISING["CONTRACTS_ALL"]) &&
                
array_key_exists("CONTRACTS_CNT"$CACHE_ADVERTISING) &&
                
is_array($CACHE_ADVERTISING["CONTRACTS_CNT"]))
            {

                
$arCount array_count_values($CACHE_ADVERTISING["CONTRACTS_ALL"]);

                
$arUpdate = Array();
                foreach(
$arCount as $CONTRACT_ID => $value)
                    
$arUpdate[$value][] = $CONTRACT_ID;

                foreach (
$arUpdate as $count => $arContact)
                {
                    
$arFields = Array("SHOW_COUNT" => "SHOW_COUNT + ".$count);
                    if (
$arContact == (array_intersect($arContact$CACHE_ADVERTISING["CONTRACTS_CNT"])))
                    {
                        
$arFields["VISITOR_COUNT"] = "VISITOR_COUNT + 1";
                        
$CACHE_ADVERTISING["CONTRACTS_CNT"] = array_diff($CACHE_ADVERTISING["CONTRACTS_CNT"], $arContact);
                    }
                    
$sContact '';
                    foreach(
$arContact as $c)
                        
$sContact .= ($sContact <> ''',':'').intval($c);

                    
$DB->Update("b_adv_contract",$arFields,"WHERE ID IN(".$sContact.")",$err_mess.__LINE__);
                }

                if (!empty(
$CACHE_ADVERTISING["CONTRACTS_CNT"]))
                {
                    
$sContrCnt '';
                    foreach(
$CACHE_ADVERTISING["CONTRACTS_CNT"] as $c)
                        
$sContrCnt .= ($sContrCnt <> ''',':'').intval($c);

                    
$arFields = Array("VISITOR_COUNT" => "VISITOR_COUNT + 1");
                    
$DB->Update("b_adv_banner",$arFields,"WHERE ID IN(".$sContrCnt.")",$err_mess.__LINE__);
                }
            }
            
// сформируем значение cookie
            
if(is_array($arrADV_VIEWED_BANNERS) && count($arrADV_VIEWED_BANNERS) > 0)
            {
                
$cookie_value "";
                
$arrCookie $arrADV_VIEWED_BANNERS;
                foreach(
$arrCookie as $key => $arr)
                    if (
intval($key)>0)
                        
$cookie_value .= intval($arr["CONTRACT_ID"])."_".$key."_".intval($arr["COUNTER"]). "_".trim($arr["EXPIRATION_DATE"]).",";

                
// длина cookie не может превышать 4Кб
                
$max_length 4*1024;
                
$j 0;
                while (
strlen($cookie_value)>$max_length && $j<200)
                {
                    
$j++;
                    
$arrCookie_temp $arrCookie;
                    
$arrCookie = array();
                    
$i=0;
                    foreach(
$arrCookie_temp as $key => $arrValue)
                    {
                        
$i++;
                        if (
$i>1)
                            
$arrCookie[$key] = $arrValue;
                    }
                    
$cookie_value "";
                    foreach(
$arrCookie as $key => $arr)
                        if (
intval($key)>0)
                            
$cookie_value .= intval($arr["CONTRACT_ID"])."_".$key."_".intval($arr["COUNTER"]). "_".trim($arr["EXPIRATION_DATE"]).",";
                }
                
$cookie_value trim($cookie_value,",");
                
$cookie_name "BANNERS";
                
$secure = (COption::GetOptionString("main""use_secure_password_cookies""N") == "Y" && CMain::IsHTTPS());
                
$APPLICATION->set_cookie($cookie_name$cookie_valuefalse"/"false$secure);
            }

            
CAdvBanner::BeforeRestartBuffer();
        }
    }