Транслитерация и создание символьного кода свойства

25.10.2012 Bitrix No comments , , ,

AddEventHandler("iblock", "OnBeforeIBlockPropertyAdd", Array("IblockModifer", "OnBeforeIBlockPropertyAddHandler"));
AddEventHandler("iblock", "OnBeforeIBlockPropertyUpdate", Array("IblockModifer", "OnBeforeIBlockPropertyUpdateHandler"));

class IblockModifer
{
    public function OnBeforeIBlockPropertyAddHandler(&$arFields)
    {
        if($arFields['IBLOCK_ID'] == 10 and empty($arFields["CODE"])){
            $arParams = array("replace_space"=>"_","replace_other"=>"_", 'change_case' => 'U', 'max_len' => 20);
            $tempCode = Cutil::translit($arFields['NAME'],"ru",$arParams);
            $arFields['CODE'] = $tempCode;
        }
    }

    public function OnBeforeIBlockPropertyUpdateHandler(&$arFields)
    {
            if($arFields['IBLOCK_ID'] == 10 and empty($arFields["CODE"])){
            $arParams = array("replace_space"=>"_","replace_other"=>"_", 'change_case' => 'U', 'max_len' => 20);
            $tempCode = Cutil::translit($arFields['NAME'],"ru",$arParams);
            $arFields['CODE'] = $tempCode;
        }
    }
}

Вставка баннеров в закешированный контент

25.10.2012 Bitrix 2 комментария , ,

AddEventHandler("main", "OnEndBufferContent", "ShowBannerInsideNews");
function ShowBannerInsideNews(&$content){
	if (CModule::IncludeModule("advertising")){
		if (strpos($content, '#BANNER_INSIDE_INSTS_LIST#') !== false){
			$crop_content = explode('<span>#BANNER_INSIDE_INSTS_LIST#</span>',$content);
			$aEarsAdv = array();
			for($i=0,$j=1; $i<$j; $i++){
				$sAdv = CAdvBanner::GetRandom('insts_list');
				if(!empty($sAdv)){
					$AdvWeightSort[] = $sAdv['WEIGHT'];
					$AdvIdSort[] = $sAdv['ID'];
					$aEarsAdv[] = $sAdv;
					$j++;
				}
				else
					break;
			}

			array_multisort($AdvWeightSort,SORT_ASC,$AdvIdSort,SORT_ASC,$aEarsAdv);

			$content = null;
			foreach($crop_content as $croped_key=>$croped){
				$banner = CAdvBanner::GetHTML($aEarsAdv[$croped_key]);
				$content .= $croped.$banner;
			}
		}
	}
}

Быстрое удаление секций

25.10.2012 Bitrix No comments

require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_admin_before.php'); 

CModule::IncludeModule('iblock'); 
$arFilter=array('IBLOCK_ID'=>ID_инфоблока); 
$rsItems=CIBlockSection::GetList(array(), $arFilter, false, array('ID'));
$rsItems->NavStart(50);
$count=0; 
while($arItem = $rsItems->GetNext(false, false)) 
{ 
   $count++; 
   if(CIBlockSection::Delete($arItem['ID']))
       echo '<div>Удален раздел '.$arItem['ID'].'</div>'; 
   else 
       echo '<div>Ошибка удаления раздела '.$arItem['ID'].'</div>'; 
} 
    
if($count==0) 
    echo '<div>Все!</div>'; 
else 
    die('<div>Удаление разделов... </div> <script>document.location="?delete";</script>'); 

Регулярка для телефона

19.10.2012 php, php скрипты No comments

^((8|\+7)[\- ]?)?(\(?\d{3,5}\)?[\- ]?)?[\d\- ]{5,10}$

Сегодня

14.10.2012 javascript No comments , ,

var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth()
var year = currentDate.getFullYear()
var today = new Date(year, month, day);

BITRIX 404

12.10.2012 Bitrix, php скрипты No comments ,

AddEventHandler("main", "OnEpilog", "Redirect404");
function Redirect404() {
	if(defined('ERROR_404')){
		@define("PATH_TO_404", "/404.php");
	}
	
    if(!defined('ADMIN_SECTION') &&  defined("ERROR_404") &&  defined("PATH_TO_404") &&  file_exists($_SERVER["DOCUMENT_ROOT"].PATH_TO_404))
	{
        //LocalRedirect("/404.php", "404 Not Found");
        global $APPLICATION;
        $APPLICATION->RestartBuffer();
        CHTTP::SetStatus("404 Not Found");
        include($_SERVER["DOCUMENT_ROOT"].SITE_TEMPLATE_PATH."/header.php");
        include($_SERVER["DOCUMENT_ROOT"].PATH_TO_404);
        include($_SERVER["DOCUMENT_ROOT"].SITE_TEMPLATE_PATH."/footer.php");
    }
}

cphpcache bitrix $obCACHE = new CPHPCache; пример 2

03.10.2012 Bitrix No comments ,

$obCACHE = new CPHPCache;
$iCACHE_TIME=3600;
$strCACHE_ID="AreaMaxPlot";
$strCACHE_PATH = "/object.area";
if (isset($_REQUEST['clear_cache']) && $_REQUEST['clear_cache']=="Y"){ $obCACHE->Clean($strCACHE_ID,$strCACHE_PATH); }
if($obCACHE->InitCache($iCACHE_TIME,$strCACHE_ID,$strCACHE_PATH)) 
    {    
        $arrRESULT = $obCACHE->GetVars();
        $arrAreaMaxDat  = $arrRESULT['AreaMaxDat'];
    }else
        {
            $arrAreaMaxDat='test';
        }
 
if($obCACHE->StartDataCache()) {
    $obCACHE->EndDataCache(array("AreaMaxDat"=>$arrAreaMaxDat));
}

bitrix $obCACHE = new CPHPCache; пример

03.10.2012 Bitrix No comments ,

bitrix $obCACHE = new CPHPCache; пример

$obCACHE = new CPHPCache;
$iCACHE_TIME=3600;
$strCACHE_ID="AreaMaxPlot";
$strCACHE_PATH = "/object.area";
if (isset($_REQUEST['clear_cache']) && $_REQUEST['clear_cache']=="Y"){ $obCACHE->Clean($strCACHE_ID,$strCACHE_PATH); }
if($iCACHE_TIME > 0 && $obCACHE->InitCache($iCACHE_TIME,$strCACHE_ID,$strCACHE_PATH)) 
    {    
        $arrRESULT = $obCACHE->GetVars();
        $arrAreaMaxDat  = $arrRESULT['AreaMaxDat'];
    }
if(!is_array($arrAreaMaxDat) || !count($arrAreaMaxDat)){
 $arrAreaMaxDat[] = 'test';
 $obCACHE->StartDataCache($iCACHE_TIME, $strCACHE_ID, $strCACHE_PATH);
 $obCACHE->EndDataCache(array("AreaMaxDat"=>$arrAreaMaxDat));
}	

bitrix jquery

27.09.2012 Bitrix, javascript No comments , , ,

bitrix jquery

CJSCore::Init(Array("jquery"));

Склонение слов с помощью javaScript

27.09.2012 javascript 15 комментариев , ,


function declination(a5, b1, c2, s) { 
 	var words = [a5, b1, c2]; 
	var index = s % 100; 
	if (index >=11 && index <= 14) { index = 0; } 
	else { index = (index %= 10) < 5 ? (index > 2 ? 2 : index): 0; } 
	return(words[index]); 
} 

declination('лет', 'год', 'года', 23)

php

function declination($status= array('','а','ов'), $value){
	$array =array(2,0,1,1,1,2);
	return $status[($value%100>4 && $value%100<20)? 2 : $array[($value%10<5)?$value%10:5]];
	}

Bitrix

use Bitrix\Main\Grid\Declension;
$yearDeclension = new Declension('год', 'года', 'лет');
$yearDeclension->get($year);