Your IP : 18.118.254.226


Current Path : /home/bitrix/ext_www/klimatlend.ua/bitrix/modules/landing/lib/
Upload File :
Current File : /home/bitrix/ext_www/klimatlend.ua/bitrix/modules/landing/lib/templateref.php

<?php
namespace Bitrix\Landing;

use \Bitrix\Landing\Internals\TemplateRefTable;

class TemplateRef
{
	/**
	 * Entity type site.
	 */
	const ENTITY_TYPE_SITE = 'S';

	/**
	 * Entity type landing.
	 */
	const ENTITY_TYPE_LANDING = 'L';

	/**
	 * Set new template refs for entity.
	 * @param int $id Entity id.
	 * @param string $type Entity type.
	 * @param array $data Ref array.
	 * @return void
	 */
	protected static function set($id, $type, array $data = array())
	{
		$res = TemplateRefTable::getList(array(
			'select' => array(
				'ID', 'AREA', 'LANDING_ID'
			),
			'filter' => array(
				'ENTITY_ID' => $id,
				'=ENTITY_TYPE' => $type
			)
		));
		while (($row = $res->fetch()))
		{
			if (isset($data[$row['AREA']]) && $data[$row['AREA']] > 0)
			{
				if ($row['LANDING_ID'] != $data[$row['AREA']])
				{
					TemplateRefTable::update($row['ID'], array(
						'LANDING_ID' => $data[$row['AREA']]
					));
				}
				unset($data[$row['AREA']]);
			}
			else
			{
				TemplateRefTable::delete($row['ID']);
			}
		}
		foreach ($data as $area => $lid)
		{
			TemplateRefTable::add(array(
				'ENTITY_ID' => $id,
				'ENTITY_TYPE' => $type,
				'LANDING_ID' => $lid,
				'AREA' => $area
			));
		}
	}

	/**
	 * Get template refs for entity.
	 * @param int $id Entity id.
	 * @param string $type Entity type.
	 * @return array
	 */
	protected static function get($id, $type)
	{
		static $staticData = array();

		if (!isset($staticData[$type . $id]))
		{
			$data = array();
			if ($id > 0)
			{
				$res = TemplateRefTable::getList(array(
					'select' => array(
						'AREA', 'LANDING_ID'
					),
					'filter' => array(
						'ENTITY_ID' => $id,
						'=ENTITY_TYPE' => $type
					)
				));
				while (($row = $res->fetch()))
				{
					$data[$row['AREA']] = $row['LANDING_ID'];
				}
			}

			$staticData[$type . $id] = $data;
		}

		return $staticData[$type . $id];
	}

	/**
	 * Set new template refs for site.
	 * @param int $id Site id.
	 * @param array $data Ref array (area => landing).
	 * @return void
	 */
	public static function setForSite($id, array $data = array())
	{
		self::set($id, self::ENTITY_TYPE_SITE, $data);
	}

	/**
	 * Set new template refs for landing.
	 * @param int $id Landing id.
	 * @param array $data Ref array (area => landing).
	 * @return void
	 */
	public static function setForLanding($id, array $data = array())
	{
		self::set($id, self::ENTITY_TYPE_LANDING, $data);
	}

	/**
	 * Get template refs for site.
	 * @param int $id Site id.
	 * @return array
	 */
	public static function getForSite($id)
	{
		return self::get($id, self::ENTITY_TYPE_SITE);
	}

	/**
	 * Get template refs for site.
	 * @param int $id Landing id.
	 * @return array
	 */
	public static function getForLanding($id)
	{
		return self::get($id, self::ENTITY_TYPE_LANDING);
	}
}