konratnox
1/19/2017 - 12:11 PM

favorite products component на cookies

favorite products component на cookies

<?
if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true) die();
/** @var CBitrixComponent $this */
/** @var array $arParams */
/** @var array $arResult */
/** @var string $componentPath */
/** @var string $componentName */
/** @var string $componentTemplate */
/** @global CDatabase $DB */
/** @global CUser $USER */
/** @global CMain $APPLICATION */

use Bitrix\Main\Loader;

if(!Loader::includeModule("iblock"))
{
	ShowError(GetMessage("IBLOCK_MODULE_NOT_INSTALLED"));
	return;
}
/*************************************************************************
	Processing of received parameters
*************************************************************************/
unset($arParams["IBLOCK_TYPE"]);
$arParams["IBLOCK_ID"] = intval($arParams["IBLOCK_ID"]);

$arParams["DETAIL_URL"]=trim($arParams["DETAIL_URL"]);
$arParams["COMPARE_URL"]=trim($arParams["COMPARE_URL"]);
if($arParams["COMPARE_URL"] == '')
	$arParams["COMPARE_URL"] = "/favorite/";


$arParams["KEEP_COOKIE"] = intval($arParams["KEEP_COOKIE"]);
if($arParams["KEEP_COOKIE"] == 0)
	$arParams["COMPARE_URL"] = 20;

$arParams["NAME"]=trim($arParams["NAME"]);
if($arParams["NAME"] == '')
	$arParams["NAME"] = "CATALOG_FAVORITE_LIST";

$arParams["ACTION_VARIABLE"] = trim($arParams["ACTION_VARIABLE"]);
if ($arParams["ACTION_VARIABLE"] == '' || !preg_match("/^[A-Za-z_][A-Za-z01-9_]*$/", $arParams["ACTION_VARIABLE"]))
	$arParams["ACTION_VARIABLE"] = "action_fav";

$arParams["PRODUCT_ID_VARIABLE"] = trim($arParams["PRODUCT_ID_VARIABLE"]);
if ($arParams["PRODUCT_ID_VARIABLE"] == '' || !preg_match("/^[A-Za-z_][A-Za-z01-9_]*$/", $arParams["PRODUCT_ID_VARIABLE"]))
	$arParams["PRODUCT_ID_VARIABLE"] = "id_fav";



$favoriteProducts = unserialize($APPLICATION->get_cookie($arParams["NAME"]));






if(!is_array($favoriteProducts)) {
    $favoriteProducts = array();
}

if(!isset($favoriteProducts[$arParams["IBLOCK_ID"]]) || !is_array($favoriteProducts[$arParams["IBLOCK_ID"]]))
    $favoriteProducts[$arParams["IBLOCK_ID"]] = array();


if(!isset($favoriteProducts[$arParams["IBLOCK_ID"]]["ITEMS"]) || !is_array(  $favoriteProducts[$arParams["IBLOCK_ID"]]["ITEMS"]))
    $favoriteProducts[$arParams["IBLOCK_ID"]]["ITEMS"] = array();


if (isset($_REQUEST[$arParams['ACTION_VARIABLE']]) && isset($_REQUEST[$arParams['PRODUCT_ID_VARIABLE']]))
{
	$successfulAction = true;
	$actionMessage = '';

	$actionByAjax = isset($_REQUEST['ajax_action_fav']) && $_REQUEST['ajax_action_fav'] == 'Y';

	$productID = (int)$_REQUEST[$arParams['PRODUCT_ID_VARIABLE']];
	$resultCount = 0;
	if ($productID > 0)
	{
		switch ($_REQUEST[$arParams['ACTION_VARIABLE']])
		{
			case 'ADD_TO_FAVORITE_LIST':
				$actionMessage = GetMessage('CP_BCCL_MESS_SUCCESSFUL_ADD_TO_FAV');
				if (!isset(   $favoriteProducts[$arParams["IBLOCK_ID"]]["ITEMS"][$productID]))
				{
					$arOffers = CIBlockPriceTools::GetOffersIBlock($arParams["IBLOCK_ID"]);
					$OFFERS_IBLOCK_ID = $arOffers ? $arOffers["OFFERS_IBLOCK_ID"]: 0;

					$arSelect = array(
						"ID",
						"IBLOCK_ID",
						"IBLOCK_SECTION_ID",
						"NAME",
						"DETAIL_PAGE_URL",
                        'PREVIEW_TEXT'
					);
					$arFilter = array(
						"ID" => $productID,
						"IBLOCK_LID" => SITE_ID,
						"IBLOCK_ACTIVE" => "Y",
						"ACTIVE_DATE" => "Y",
						"ACTIVE" => "Y",
						"CHECK_PERMISSIONS" => "Y",
						"MIN_PERMISSION" => "R"
					);
					$arFilter["IBLOCK_ID"] = ($OFFERS_IBLOCK_ID > 0 ? array($arParams["IBLOCK_ID"], $OFFERS_IBLOCK_ID) : $arParams["IBLOCK_ID"]);

					$rsElement = CIBlockElement::GetList(array(), $arFilter, false, false, $arSelect);

					$rsElement->SetUrlTemplates($arParams["DETAIL_URL"]);
					if ($arElement = $rsElement->GetNext())
					{
						$arMaster = false;
						if ($arElement["IBLOCK_ID"] == $OFFERS_IBLOCK_ID)
						{
							$rsMasterProperty = CIBlockElement::GetProperty($arElement["IBLOCK_ID"], $arElement["ID"], array(), array("ID" => $arOffers["OFFERS_PROPERTY_ID"], "EMPTY" => "N"));
							if ($arMasterProperty = $rsMasterProperty->Fetch())
							{
								$rsMaster = CIBlockElement::GetList(
									array()
									,array(
										"ID" => $arMasterProperty["VALUE"],
										"IBLOCK_ID" => $arMasterProperty["LINK_IBLOCK_ID"],
										"ACTIVE" => "Y",
									)
									,false, false, $arSelect);
								$rsMaster->SetUrlTemplates($arParams["DETAIL_URL"]);
								$arMaster = $rsMaster->GetNext();
							}
						}
						if ($arMaster)
						{
							$arMaster['PARENT_ID'] = $productID;
							$arMaster['NAME'] = $arElement["NAME"];
							$arMaster['DELETE_URL'] = htmlspecialcharsbx($APPLICATION->GetCurPageParam($arParams['ACTION_VARIABLE']."=DELETE_FROM_FAVORITE_LIST&".$arParams['PRODUCT_ID_VARIABLE']."=".$arMaster["ID"], array($arParams['ACTION_VARIABLE'], $arParams['PRODUCT_ID_VARIABLE'])));
                            $favoriteProducts[$arParams["IBLOCK_ID"]]["ITEMS"][$productID] = $arMaster;
						}
						else
						{
							$arElement['PARENT_ID'] = $productID;
							$arElement['DELETE_URL'] = htmlspecialcharsbx($APPLICATION->GetCurPageParam($arParams['ACTION_VARIABLE']."=DELETE_FROM_FAVORITE_LIST&".$arParams['PRODUCT_ID_VARIABLE']."=".$arElement["ID"], array($arParams['ACTION_VARIABLE'], $arParams['PRODUCT_ID_VARIABLE'])));
                            $favoriteProducts[$arParams["IBLOCK_ID"]]["ITEMS"][$productID] = $arElement;
						}
                        $favoriteProducts[$arParams["IBLOCK_ID"]]['COUNT'] = count($favoriteProducts[$arParams["IBLOCK_ID"]]["ITEMS"]);
                        $APPLICATION->set_cookie($arParams["NAME"],  serialize($favoriteProducts), time()+60*60*24*$arParams['KEEP_COOKIE']);
					}
					else
					{
						$successfulAction = false;
						$actionMessage = GetMessage('CP_BCCL_ERR_MESS_PRODUCT_NOT_FOUND');
					}
				}
				break;
			case 'DELETE_FROM_FAVORITE_LIST':
				if (isset($favoriteProducts[$arParams["IBLOCK_ID"]]["ITEMS"][$productID])) {
                    unset($favoriteProducts[$arParams["IBLOCK_ID"]]["ITEMS"][$productID]);

                }

				$actionMessage = GetMessage('CP_BCCL_MESS_SUCCESSFUL_DELETE_FROM_FAV');
                $favoriteProducts[$arParams["IBLOCK_ID"]]['COUNT'] = count($favoriteProducts[$arParams["IBLOCK_ID"]]["ITEMS"]);
                $APPLICATION->set_cookie($arParams["NAME"],  serialize($favoriteProducts), time()+60*60*24*$arParams['KEEP_COOKIE']);
				break;
		}
	}
	else
	{
		$successfulAction = false;
		$actionMessage = GetMessage('CP_BCCL_ERR_MESS_PRODUCT_NOT_FOUND');
	}


	if ($actionByAjax)
	{


		if ($successfulAction)
		{
			$addResult = array('STATUS' => 'OK', 'MESSAGE' => $actionMessage, 'ID' => $productID, 'COUNT' => count($favoriteProducts[$arParams["IBLOCK_ID"]]["ITEMS"]));
		}
		else
		{
			$addResult = array('STATUS' => 'ERROR', 'MESSAGE' => $actionMessage);
		}
		$APPLICATION->RestartBuffer();
		echo CUtil::PhpToJSObject($addResult);
		die();
	}
}

$arResult = $favoriteProducts;

$this->IncludeComponentTemplate();