KiyoshiSaundo
2/21/2018 - 8:02 AM

createShowCase

createShowCase

<?
// функция находит в тексте все подстроки формата ###<...>###
// и заменяет на витрины. Если для ###<...>### не добавлена проверка,
// то просто удаляет этот код из текста
function CreateShowcase($text, $arImages) {
	// обрабатываем массив картинок для показа
	if ($arImages) {
		foreach ($arImages as $key => $photo) {
			$arImagesUrl[$key+1] = CFile::GetFileArray($photo);
		}
	}
	// находим в тексте все витрины
	preg_match_all("/###([^#]*)###/", $text, $arCases);
	foreach ($arCases[1] as $i => $fun) {
		$strCase = '';
		if (preg_match("#ShowSimpleCase\(([^\)]+)\)#", $fun, $args)) {
			$args = explode(',', $args[1]);
			$strCase = ShowSimpleCase($args, true);
		}
		if (preg_match("#ShowSimpleCase2\(([^\)]+)\)#", $fun, $args)) {
			$args = explode(',', $args[1]);
			$strCase = ShowSimpleCase($args);
		}
		if (preg_match("#ShowImageCase\(([^\)]+)\)#", $fun, $args)) {
			$args = explode(',', $args[1]);
			$strCase = ShowImageCase($args, $arImagesUrl);
		}
		if (preg_match("#ShowLeftImage\(([^\)]+)\)#", $fun, $args)) {
			$args = explode(',', $args[1]);
			$strCase = ShowSingleImage($arImagesUrl[$args[0]], 'left');
		}
		if (preg_match("#ShowRightImage\(([^\)]+)\)#", $fun, $args)) {
			$args = explode(',', $args[1]);
			$strCase = ShowSingleImage($arImagesUrl[$args[0]], 'right');
		}
		if (preg_match("#ShowCenterImage\(([^\)]+)\)#", $fun, $args)) {
			$args = explode(',', $args[1]);
			$strCase = ShowSingleImage($arImagesUrl[$args[0]], 'center');
		}
		if (preg_match("#ShowFullImage\(([^\)]+)\)#", $fun, $args)) {
			$args = explode(',', $args[1]);
			$strCase = ShowSingleImage($arImagesUrl[$args[0]], 'full');
		}
		if (preg_match("#ShowCollectionSlider\(([^\)]+)\)#", $fun, $args)) {
			$args = explode(',', $args[1]);
			$strCase = ShowCollectionSlider($args[0]);
		}
		if (preg_match("#ShowCollectionSlider2\(([^\)]+)\)#", $fun, $args)) {
			$args = explode(',', $args[1]);
			$strCase = ShowCollectionSlider2($args[0], trim($args[1]));
		}
		if (preg_match("#ShowInteriorSlider\(([^\)]+)\)#", $fun, $args)) {
			$args = explode(',', $args[1]);
			$strCase = ShowInteriorSlider($args, false);
		}
		$text = preg_replace(
			"/" . preg_quote($arCases[0][$i]) . "/",
			$strCase,
			$text
		);
	}
	// удаляем все остальные ###<...>###
	// еще раз на всякий случай
	$text = preg_replace("/###([^#]*)###/", '', $text);
	return $text;
}

function ShowInteriorSlider($sectionID, $print = false)
{
	if (!$sectionID) return false;
	$arResult = HelperCreateSlider(24, $sectionID);
	if ($print) echo $arResult;
	else return $arResult;
}

function ShowCollectionSlider($sectionID)
{
	if (!$sectionID || is_array($sectionID)) return false;

	CModule::IncludeModule('iblock');
	$arResult = '';

	$dbCollection = CIBlockSection::GetByID($sectionID);
	$arCollection = $dbCollection->GetNext();

	if ($arCollection) {
		$arResult .= '<p><a href="'.$arCollection['SECTION_PAGE_URL'].'"><b>Коллекция '.$arCollection['NAME'].'</b></a></p>';
		$arResult .= HelperCreateSlider(1, $sectionID, array('nTopCount' => 15));
	}

	return $arResult;
}

function ShowCollectionSlider2($sectionID, $name = '')
{
	if (!$sectionID || is_array($sectionID)) return false;

	CModule::IncludeModule('iblock');
	$arResult = '';

	$dbCollection = CIBlockSection::GetByID($sectionID);
	$arCollection = $dbCollection->GetNext();
	$arCollection['PICTURE'] = CFile::ResizeImageGet(
		$arCollection['PICTURE'],
		array("width" => 70, "height" => 70),
		BX_RESIZE_IMAGE_EXACT
	);

	$name = ($name) ? $name : 'Коллекция '.$arCollection['NAME'];

	if ($arCollection) {
		$arResult .= '<p>';
		$arResult .= '<a class="picture" href="'.$arCollection['SECTION_PAGE_URL'].'" style="vertical-align:middle;margin-right:20px;">';
		$arResult .= '<img src="'.$arCollection['PICTURE']['src'].'" alt="'.$name.'">';
		$arResult .= '</a>';
		$arResult .= '<a href="'.$arCollection['SECTION_PAGE_URL'].'">';
		$arResult .= '<b>'.$name.'</b>';
		$arResult .= '</a>';
		$arResult .= '</p>';
		$arResult .= HelperCreateSlider(1, $sectionID, array('nTopCount' => 15));
		$arResult .= '<p style="position:relative;top:-20px;text-align:right"><a href="'.$arCollection['SECTION_PAGE_URL'].'">все обои коллекции</a></p>';
	}

	return $arResult;
}

function HelperCreateSlider ($ib, $sections, $arCount = false)
{
	if (!$ib || !$sections) return false;

	CModule::IncludeModule('iblock');

	$arResult = '<div class="pictures-slider"><ul>';

	$dbElement = CIBlockElement::GetList(
		array('rand' => 'asc'),
		array(
			'IBLOCK_ID' => $ib,
			'ACTIVE' => 'Y',
			'SECTION_ACTIVE' => 'Y',
			'SECTION_GLOBAL_ACTIVE' => 'Y',
			'SECTION_ID' => $sections,
			'INCLUDE_SUBSECTIONS' => 'Y'
		),
		false,
		$arCount,
		array('*')
	);
	if ($dbElement->SelectedRowsCount()) {
		while ($arElement = $dbElement->GetNext()) {
			$small = CFile::ResizeImageGet(
				$arElement['DETAIL_PICTURE'],
				array("width" => 250, "height" => 250),
				BX_RESIZE_IMAGE_EXACT
			);
			$big = CFile::ResizeImageGet(
				$arElement['DETAIL_PICTURE'],
				array("width" => 1000, "height" => 1000),
				BX_RESIZE_IMAGE_PROPORTIONAL
			);
			$arResult .= '<li>';
			$arResult .= '<a href="'.$big['src'].'" class="picture colorbox" rel="gallery">';
			$arResult .= '<img src="'.$small['src'].'" alt="'.$arElement['NAME'].'" title="'.$arElement['NAME'].'">';
			$arResult .= '</a>';
			$arResult .= '</li>';
		}
	}

	$arResult .= '</ul></div>';

	return $arResult;
}

function ShowSingleImage($arImg, $float)
{
	$arResult = '';
	if (!$arImg || empty($arImg)) return false;
	if (strlen($arImg['DESCRIPTION'])) {
		$arResult .= '<a class="picture-' . $float . '" href="' . $arImg['DESCRIPTION'] . '">' . "\r\n";
		$arResult .= '<img src="' . $arImg['SRC'] . '">' . "\r\n";
		$arResult .= '</a>' . "\r\n";
	} else {
		$arResult .= '<img class="picture-' . $float . '" src="' . $arImg['SRC'] . '">' . "\r\n";
	}
	return $arResult;
}

// функция принимает массив номеров картинок, массив адресов для картинок
// и формирует витрину
// если есть описание, то оно используется как ссылка
function ShowImageCase($numbers, $images)
{
	$arResult = '';
	if (empty($numbers) || empty($images)) return false;

	$arResult = '<div class="picture-case pc' . count($numbers) . '">' . "\r\n";
	foreach ($numbers as $key => $num) {
		$arResult .= '<div class="item">' . "\r\n";
		if (strlen($images[$num]['DESCRIPTION'])) {
			$arResult .= '<a class="picture" href="' . $images[$num]['DESCRIPTION'] . '">' . "\r\n";
			$arResult .= '<img src="' . $images[$num]['SRC'] . '">' . "\r\n";
			$arResult .= '</a>' . "\r\n";
		} else {
			$arResult .= '<img class="picture" src="' . $images[$num]['SRC'] . '">' . "\r\n";
		}
		$arResult .= '</div>' . "\r\n";
	}
	$arResult .= '</div>' . "\r\n";
	return $arResult;
}

// функция принимает список ид карточек и возвращает простую витрину
// из картинок и ссылок на коллекию
// $coll == true - ссылка на товар, иначе на коллекцию
function ShowSimpleCase($arIds, $coll = false) {
	$arResult = '';
	if (!is_array($arIds) || empty($arIds) || count($arIds) > 30) return false;

	$dbElement = CIBlockElement::GetList(
		array(),
		array('IBLOCK_ID' => 1, 'ID' => $arIds, "SECTION_GLOBAL_ACTIVE" => "Y", "ACTIVE" => "Y"),
		false,
		false,
		array("ID", "DETAIL_PICTURE", "DETAIL_PAGE_URL")
	);
	if ($dbElement->SelectedRowsCount()) {
		$arResult = '<div class="advices-case">' . "\r\n";
		while($arElement = $dbElement->GetNext()) {
			// на этом сайте, чтобы получить ссылку на коллекцию,
			// достаточно удалить из ссылки элемента его ид и последний слеш
			if ($coll) {
				$url = preg_replace("#" . $arElement['ID'] . "/#", "", $arElement['DETAIL_PAGE_URL']);
			} else {
				$url = $arElement['DETAIL_PAGE_URL'];
			}
			$pic = CFile::ResizeImageGet(
				$arElement['DETAIL_PICTURE'],
				array("width" => "99", "height" => "99"),
				BX_RESIZE_IMAGE_EXACT
			);

			$arResult .= '<a class="picture" href="' . $url . '">' . "\r\n";
			$arResult .= '<img src="' . $pic['src'] . '">' . "\r\n";
			$arResult .= '</a>' . "\r\n";
		}
		$arResult .= '</div>' . "\r\n";
	}
	return $arResult;
}