Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/im/lib/V2/Marketplace/ |
Upload File : |
<?php namespace Bitrix\Im\V2\Marketplace; use Bitrix\Im\V2\Common\ContextCustomer; use Bitrix\Im\V2\Marketplace\Types\Role; use Bitrix\Im\V2\Rest\RestConvertible; use Bitrix\Im\V2\Result; use Bitrix\Im\V2\Service\Context; use Bitrix\Main\Error; use Bitrix\Main\Web\Uri; use Bitrix\Rest\PlacementTable; class Application implements RestConvertible { use ContextCustomer; private const DEFAULT_ORDER = 50; private const SETTING_NAME = 'im_placement'; public function __construct(?int $userId = null) { $context = (new Context())->setUserId($userId); $this->setContext($context); } /** * @return Application\Entity[] */ public function getApplications(?array $placementList = null): array { $orderList = $this->getOrderList(); $result = []; foreach ($this->generateApplicationList($placementList) as $application) { if ($this->isApplicationAvailable($application)) { $application->order = $orderList[$application->id] ?? self::DEFAULT_ORDER; $result[] = $application; } } return $result; } public function getLoadUri(): string { $url = '/bitrix/components/bitrix/app.layout/lazyload.ajax.php?' . bitrix_sessid_get(); return (new Uri($url)) ->addParams(['site' => SITE_ID]) ->getUri() ; } public function setOrder(int $applicationsId, int $order): Result { $orderList = $this->getOrderList(); $orderList[$applicationsId] = $order; $orderList = $this->filterDeletedApplications($orderList); return $this->setOrderList($orderList); } private function getOrderList(): array { return \CUserOptions::GetOption('im', self::SETTING_NAME, []); } private function setOrderList(array $orderList): Result { $result = new Result(); $isSuccess = \CUserOptions::SetOption('im', self::SETTING_NAME, $orderList); if (!$isSuccess) { $result->addError(new Error('Error writing the parameter order', 'SERVER_ERROR')); } return $result; } private function isApplicationAvailable(Application\Entity $application): bool { return $this->checkRole($application->options['role']) && $this->checkExtranet($application->options['extranet']) ; } private function checkExtranet(string $extranetOption): bool { if ($this->getContext()->getUser()->isExtranet()) { return $extranetOption === 'Y'; } return true; } private function checkRole(string $roleOptions): bool { if (!$this->getContext()->getUser()->isAdmin()) { return mb_strtoupper($roleOptions) === Role::USER; } return true; } private function filterDeletedApplications(array $userApplications): array { $applicationIdList = $this->getApplicationIdList(); $result = []; foreach ($userApplications as $applicationId => $order) { if (in_array($applicationId, $applicationIdList, true)) { $result[$applicationId] = $order; } } return $result; } /** * @return \Generator<Application\Entity> */ private function generateApplicationList(?array $placementList = null): \Iterator { foreach ($placementList ?? Placement::getPlacementList() as $placement) { foreach (PlacementTable::getHandlersList($placement) as $handler) { yield new Application\Entity([ 'id' => $handler['ID'], 'placement' => $placement, 'options' => $handler['OPTIONS'], 'restApplicationId' => $handler['APP_ID'], 'title' => $handler['TITLE'], ]); } } } private function getApplicationIdList(): array { $result = []; foreach ($this->generateApplicationList() as $application) { if ($this->isApplicationAvailable($application)) { $result[] = $application->id; } } return $result; } public static function getRestEntityName(): string { return 'placementApplications'; } public function toRestFormat(array $option = []): array { return [ 'items' => array_map( static fn ($appEntity) => $appEntity->toRestFormat(), $this->getApplications() ), 'links' => [ 'load' => $this->getLoadUri(), ], ]; } }