Your IP : 3.147.78.141


Current Path : /var/www/axolotl/data/www/murmansk.axolotls.ru/bitrix/modules/location/lib/controller/
Upload File :
Current File : /var/www/axolotl/data/www/murmansk.axolotls.ru/bitrix/modules/location/lib/controller/location.php

<?php

namespace Bitrix\Location\Controller;

use Bitrix\Location\Entity\Generic\Collection;
use Bitrix\Location\Common\Point;
use Bitrix\Location\Entity\Location\Parents;
use Bitrix\Location\Infrastructure\Service\ErrorService;
use Bitrix\Location\Service;
use Bitrix\Main\Engine\ActionFilter\Cors;
use \Bitrix\Location\Entity;
use Bitrix\Main\Web\Json;

/**
 * Class Location
 * @package Bitrix\Location\Controller
 * Facade
 */
class Location extends \Bitrix\Main\Engine\Controller
{
	protected function init()
	{
		parent::init();
		ErrorService::getInstance()->setThrowExceptionOnError(true);
	}

	protected function getDefaultPreFilters()
	{
		return [
			new Cors(),
		];
	}

	/**
	 * @param int $locationId
	 * @param string $languageId
	 * @return array
	 */
	public function findByIdAction(int $locationId, string $languageId)
	{
		$result = null;

		if($location = Service\LocationService::getInstance()->findById($locationId, $languageId))
		{
			$result = $location->toArray();
		}

		return $result;
	}

	/**
	 * @param array $location
	 * @return array
	 * @throws \Bitrix\Main\ArgumentOutOfRangeException
	 */
	public function findParentsAction(array $location)
	{
		$result = new Parents();

		if ($entity = Entity\Location::fromArray($location))
		{
			$result = $entity->getParents();
		}

		return \Bitrix\Location\Entity\Location\Converter\ArrayConverter::convertParentsToArray($result);
	}

	public function findChildrenAction(array $location, string $languageId)
	{
		$result = new \Bitrix\Location\Entity\Location\Collection();

		if($entity = Entity\Location::fromArray($location))
		{
			$result = Service\LocationService::getInstance()->findChildren($entity, $languageId);
		}

		return \Bitrix\Location\Entity\Location\Converter\ArrayConverter::convertCollectionToArray($result);
	}

	/**
	 * array $fields
	 * @return array|null
	 */
	public function findByExternalIdAction(string $externalId, string $sourceCode, string $languageId)
	{
		$result = null;

		if($location = Service\LocationService::getInstance()->findByExternalId($externalId, $sourceCode, $languageId))
		{
			$result = \Bitrix\Location\Entity\Location\Converter\ArrayConverter::convertToArray($location);
		}

		return $result;
	}

	/**
	 * @param string $input
	 * @param array $restriction
	 * @param string $languageId
	 * @return array|null
	 * todo: make restriction as a ValueObject
	 */
	public function autocompleteAction(string $input, array $restriction, string $languageId)
	{
		$result = null;

		if($collection = Service\LocationService::getInstance()->autocomplete($input, $restriction, $languageId))
		{
			$result = \Bitrix\Location\Entity\Location\Converter\ArrayConverter::convertCollectionToArray($collection);
		}

		return $result;
	}

	/**
	 * @param array $point
	 * @param string $languageId
	 * @return Collection|bool
	 */
	public function findByPointAction(array $point, string $languageId = LANGUAGE_ID)
	{

		return Service\LocationService::getInstance()->findByPoint(new Point($point[0], $point[1]), $languageId);
	}

	/**
	 * @param string $text
	 * @param string $languageId
	 * @return array|bool
	 */
	public function findByTextAction(string $text, string $languageId = LANGUAGE_ID)
	{
		$result =  Service\LocationService::getInstance()->findByText($text, $languageId);
		return  $result ? \Bitrix\Location\Entity\Location\Converter\ArrayConverter::convertCollectionToArray($result) : [];
	}

	public static function saveAction(array $location)
	{
		$entity = Entity\Location::fromArray($location);
		$result = $entity->save();

		return [
			'isSuccess' => $result->isSuccess(),
			'errors' => $result->getErrorMessages(),
			'location' => \Bitrix\Location\Entity\Location\Converter\ArrayConverter::convertToArray($entity)
		];
	}

	/**
	 * @param array $location
	 * @return \Bitrix\Main\Result
	 */
	public function deleteAction(array $location)
	{
		return Service\LocationService::getInstance()->delete(
			Entity\Location::fromArray($location)
		);
	}
}