<?
class TOOLS
{
public static function getListElements($IBLOCK_ID)
{
$result = array();
$resElements = CIBlockElement::GetList(Array("SORT"=>"ASC"),
array('IBLOCK_ID'=>$IBLOCK_ID, "ACTIVE"=>"Y", "INCLUDE_SUBSECTIONS"=>"Y"), false, false,
array("IBLOCK_ID", "ID", 'NAME', 'SECTION_ID', 'DETAIL_PAGE_URL', 'PROPERTY_*'));
while($obElem = $resElements->GetNextElement()){
$arFields = $obElem->GetFields();
$arProps = $obElem->GetProperties();
$id = $arFields["ID"];
$result[$id] = $arFields;
$result[$id]["PROPERTIES"] = $arProps;
}
return $result;
}
public static function getListElementsBySectionCode($IBLOCK_ID, $SECTION_CODE)
{
$result = array();
$resElements = CIBlockElement::GetList(Array("SORT"=>"ASC"),
array('IBLOCK_ID'=>$IBLOCK_ID, "ACTIVE"=>"Y", "SECTION_CODE"=>$SECTION_CODE), false, false,
array("IBLOCK_ID", "ID", 'NAME', 'SECTION_ID', 'DETAIL_PAGE_URL', 'PROPERTY_*'));
while($obElem = $resElements->GetNextElement()){
$arFields = $obElem->GetFields();
$arProps = $obElem->GetProperties();
$id = $arFields["ID"];
$result[$id] = $arFields;
$result[$id]["PROPERTIES"] = $arProps;
}
return $result;
}
public static function getSectionByCode($IBLOCK_ID, $CODE)
{
$result = array();
$resSections = CIBlockSection::GetList(array("SORT"=>"ASC"),array("IBLOCK_ID"=>$IBLOCK_ID, "ACTIVE"=>"Y", "CODE"=>$CODE),false,
array("IBLOCK_ID","ID","NAME", "SECTION_PAGE_URL", "DESCRIPTION", "UF_*"), false);
$i = 0;
if($obSec = $resSections->GetNext()) {
$result = $obSec;
}
return $result;
}
public static function getSectionByID($IBLOCK_ID, $SECTION_ID)
{
$result = array();
$resSections = CIBlockSection::GetList(array("SORT"=>"ASC"),array("IBLOCK_ID"=>$IBLOCK_ID, "ACTIVE"=>"Y", "ID"=>$SECTION_ID),false,
array("IBLOCK_ID","ID","NAME", "SECTION_PAGE_URL", "DESCRIPTION", "UF_*"), false);
$i = 0;
if($obSec = $resSections->GetNext()) {
$result = $obSec;
}
return $result;
}
public static function getElementByID($IBLOCK_ID, $ID)
{
$result = Array();
$arOrder = Array("SORT"=>"ASC");
$arFilter = array('IBLOCK_ID'=>$IBLOCK_ID, "ACTIVE"=>"Y", "INCLUDE_SUBSECTIONS"=>"Y", "ID"=>$ID);
$arSelect = Array("IBLOCK_ID", "ID", 'NAME', 'SECTION_ID', 'PREVIEW_TEXT', 'DETAIL_TEXT', 'PROPERTY_*');
$res = CIBlockElement::GetList($arOrder, $arFilter, false, false, $arSelect);
if($ob = $res->GetNextElement()){
$arFields = $ob->GetFields();
$arProps = $ob->GetProperties();
$result = $arFields;
$result["PROPERTIES"] = $arProps;
}
return $result;
}
public static function getElementByCode($IBLOCK_ID, $CODE)
{
$result = Array();
$arOrder = Array("SORT"=>"ASC");
$arFilter = array('IBLOCK_ID'=>$IBLOCK_ID, "ACTIVE"=>"Y", "INCLUDE_SUBSECTIONS"=>"Y", "CODE"=>$CODE);
$arSelect = Array("IBLOCK_ID", "ID", 'NAME', 'SECTION_ID', 'PREVIEW_TEXT', 'DETAIL_TEXT', 'PROPERTY_*');
$res = CIBlockElement::GetList($arOrder, $arFilter, false, false, $arSelect);
if($ob = $res->GetNextElement()){
$arFields = $ob->GetFields();
$arProps = $ob->GetProperties();
$result = $arFields;
$result["PROPERTIES"] = $arProps;
}
return $result;
}
public static function pluralWordForm($num, $form_for_1, $form_for_2, $form_for_5)
{
$num = abs($num) % 100;
$num_x = $num % 10;
if ($num > 10 && $num < 20)
return $form_for_5;
if ($num_x > 1 && $num_x < 5)
return $form_for_2;
if ($num_x == 1)
return $form_for_1;
return $form_for_5;
}
public static function my_mb_ucfirst($str)
{
$fc = mb_strtoupper(mb_substr($str, 0, 1));
return $fc.mb_substr($str, 1);
}
public static function includeLangFile()
{
IncludeTemplateLangFile($_SERVER["DOCUMENT_ROOT"].SITE_TEMPLATE_PATH."/main_lang_file.php");
}
// 404 Page
public static function OnEpilogHandler()
{
if (defined('ERROR_404') && ERROR_404 == 'Y') {
global $APPLICATION;
$APPLICATION->RestartBuffer();
CHTTP::SetStatus("404 Not Found");
include $_SERVER['DOCUMENT_ROOT'] . '/local/templates/tc/header.php';
include $_SERVER['DOCUMENT_ROOT'] . '/404.php';
include $_SERVER['DOCUMENT_ROOT'] . '/local/templates/tc/footer.php';
}
}
public static function getExtension($filename) {
return end( explode( '.', $filename ) );
}
public static function validateHTML(&$content)
{
$content = str_replace(' type="text/javascript"', "", $content);
$content = self::addAltTagsToImages($content);
}
public static function addAltTagsToImages($content)
{
global $APPLICATION;
$pageTitle = $APPLICATION->GetTitle(false);
if(empty($pageTitle)){
$pageTitle = $APPLICATION->GetPageProperty("title");
}
preg_match_all('/<img[^>]+>/i',$content, $images);
if(!empty($images[0]))
{
foreach($images[0] as $img)
{
if(!strpos($img, 'alt=')){
$newIMG = str_replace('<img', '<img alt="'.$pageTitle.'"', $img);
$content = str_replace($img, $newIMG, $content);
}
}
}
$content = str_replace('alt=""', 'alt="'.$pageTitle.'"', $content);
$content = str_replace("alt=''", "alt='".$pageTitle."'", $content);
return $content;
}
public static function pre($data)
{
$res = "";
$res.= "<pre>";
if(is_array($data)){
$res.= print_r($data, true);
} else {
$res.= $data;
}
$res.= "</pre>";
echo $res;
}
}