Your IP : 18.116.81.33


Current Path : /var/www/www-root/data/www/www.monolith-realty.ru/bitrix/modules/landing/lib/
Upload File :
Current File : /var/www/www-root/data/www/www.monolith-realty.ru/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())
	{
		$id = intval($id);
		$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)
		{
			if ($lid > 0)
			{
				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();
		$id = intval($id);

		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())
	{
		if (Rights::hasAccessForSite($id, Rights::ACCESS_TYPES['sett']))
		{
			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())
	{
		if (Rights::hasAccessForLanding($id, Rights::ACCESS_TYPES['sett']))
		{
			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);
	}

	/**
	 * This landing id is used as a area?
	 * @param int|array $lid Landing id.
	 * @return boolean|array
	 */
	public static function landingIsArea($lid)
	{
		$res = TemplateRefTable::getList(array(
			'filter' => array(
				'LANDING_ID' => $lid
			)
		));
		if (is_array($lid))
		{
			$return = array();
			foreach ($lid as $id)
			{
				$return[(int)$id] = false;
			}
			while ($row = $res->fetch())
			{
				$return[$row['LANDING_ID']] = true;
			}
			return $return;
		}
		else
		{
			return $res->fetch() ? true : false;
		}
	}

	/**
	 * Delete all area-landing by id.
	 * @param integer $lid Landing id.
	 * @return void
	 */
	public static function deleteArea($lid)
	{
		$lid = intval($lid);

		$res = TemplateRefTable::getList(array(
			'filter' => array(
				'LANDING_ID' => $lid
			)
		));
		while ($row = $res->fetch())
		{
			TemplateRefTable::delete($row['ID']);
		}
	}

	/**
	 * Resolves class by type.
	 * @param string $type Entity type.
	 * @return string
	 */
	public static function resolveClassByType($type)
	{
		if ($type == self::ENTITY_TYPE_SITE)
		{
			return '\Bitrix\Landing\Site';
		}
		else if ($type == self::ENTITY_TYPE_LANDING)
		{
			return '\Bitrix\Landing\Landing';
		}
		return '';
	}
}