Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
entitylink.php
1<?php
2
4
5use Bitrix\Crm\Service\Container;
7use CCrmOwnerType;
8
9Loader::requireModule('crm');
10
15{
16 private int $entityTypeId;
17 private int $entityId;
18 private string $href;
19
25 public function __construct(int $entityTypeId, int $entityId, string $href)
26 {
27 $this->entityTypeId = $entityTypeId;
28 $this->entityId = $entityId;
29 $this->href = $href;
30 }
31
44 public static function createByOrder(int $orderId, string $link): self
45 {
46 $entityId = $orderId;
47 $entityTypeId = CCrmOwnerType::Undefined;
48
49 $order = \Bitrix\Crm\Order\Order::load($orderId);
50 if ($order)
51 {
52 $binding = $order->getEntityBinding();
53 if ($binding)
54 {
55 $entityId = $binding->getOwnerId();
56 $entityTypeId = $binding->getOwnerTypeId();
57 }
58 else
59 {
60 $entityId = $orderId;
61 $entityTypeId = CCrmOwnerType::Order;
62 }
63 }
64
65 return new static($entityTypeId, $entityId, $link);
66 }
67
73 private function getLabel(): string
74 {
75 // we process orders separately, because the factory does not know how to work with items.
76 if ($this->entityTypeId === CCrmOwnerType::Order)
77 {
78 $description = CCrmOwnerType::GetDescription($this->entityTypeId);
79
80 return "{$description} #{$this->entityId}";
81 }
82
83 $factory = Container::getInstance()->getFactory($this->entityTypeId);
84 if ($factory)
85 {
86 $item = $factory->getItem($this->entityId);
87 if ($item)
88 {
89 $title = $item->getHeading();
90 if ($title)
91 {
92 return $title;
93 }
94 }
95 }
96
97 return $this->entityId;
98 }
99
105 public function render(): string
106 {
107 $href = htmlspecialcharsbx($this->href);
108 $label = $this->getLabel();
109
110 if (!$href)
111 {
112 return $label;
113 }
114
115 return "<a href=\"{$href}\">{$label}</a>";
116 }
117}