Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
TaskItem.php
1<?php
2
4
12use Bitrix\Tasks\Internals\TaskObject;
13use Bitrix\Im\V2\Common\ContextCustomer;
14use Bitrix\Tasks\Provider\TaskList;
15use Bitrix\Tasks\Provider\TaskQuery;
16
18{
19 use ContextCustomer;
20
21 protected int $taskId;
22 protected string $title;
23 protected int $status;
24 protected string $statusTitle;
25 protected ?DateTime $deadline;
26 protected string $state;
27 protected string $color;
28 protected int $creatorId;
29 protected int $responsibleId;
30 protected array $membersIds;
31
32 public function getId(): int
33 {
34 return $this->getTaskId();
35 }
36
37 public static function getRestEntityName(): string
38 {
39 return 'task';
40 }
41
42 public function toRestFormat(array $option = []): array
43 {
44 return [
45 'id' => $this->getTaskId(),
46 'title' => $this->getTitle(),
47 'creatorId' => $this->getCreatorId(),
48 'responsibleId' => $this->getResponsibleId(),
49 'status' => $this->getStatus(),
50 'statusTitle' => $this->getStatusTitle(),
51 'deadline' => $this->getDeadline() ? $this->getDeadline()->format('c') : null,
52 'state' => $this->getState(),
53 'color' => $this->getColor(),
54 'source' => \CTaskNotifications::getNotificationPath(['ID' => $this->getContext()->getUserId()], $this->getTaskId()),
55 ];
56 }
57
58 public static function getById(int $id, ?Context $context = null): ?self
59 {
60 $context = $context ?? Locator::getContext();
61 $taskQuery = new TaskQuery($context->getUserId());
62 $taskQuery
63 ->setSelect(\Bitrix\Im\V2\Link\Task\TaskCollection::SELECT_FIELDS)
64 ->setWhere(['=ID' => $id])
65 ;
66 $rows = (new TaskList())->getList($taskQuery);
67
68 if (!isset($rows[0]))
69 {
70 return null;
71 }
72
73 return self::initByRow($rows[0]);
74 }
75
76 public static function initByRow(array $row): self
77 {
78 $taskEntity = new static();
79
80 $membersIds = array_merge(
81 [(int)$row['CREATED_BY']],
82 [(int)$row['RESPONSIBLE_ID']],
83 array_map(static fn ($id) => (int)$id, $row['AUDITORS'] ?? []),
84 array_map(static fn ($id) => (int)$id, $row['ACCOMPLICES'] ?? [])
85 );
86 $uniqueMembersIds = array_unique($membersIds);
87
88 $taskEntity
89 ->setTaskId((int)$row['ID'])
90 ->setTitle($row['TITLE'])
91 ->setDeadline(isset($row['DEADLINE']) ? new DateTime($row['DEADLINE']) : null)
92 ->setStatus((int)$row['REAL_STATUS'])
93 ->setCreatorId((int)$row['CREATED_BY'])
94 ->setResponsibleId((int)$row['RESPONSIBLE_ID'])
95 ->setMembersIds(array_values($uniqueMembersIds))
96 ;
97
98 return $taskEntity;
99 }
100
101 public static function initByTaskObject(TaskObject $taskObject): self
102 {
103 $taskEntity = new static();
104
105 $taskEntity
106 ->setTaskId($taskObject->getId())
107 ->setTitle($taskObject->getTitle())
108 ->setDeadline($taskObject->getDeadline())
109 ->setStatus($taskObject->getStatus())
110 ->setCreatorId($taskObject->getCreatedBy())
111 ->setResponsibleId($taskObject->getResponsibleId())
112 ->setMembersIds(array_unique($taskObject->getMemberList()->getUserIdList()))
113 ;
114
115 return $taskEntity;
116 }
117
118 public function getUrl(): string
119 {
120 return \CTaskNotifications::getNotificationPath(
121 ['ID' => $this->getContext()->getUserId()],
122 $this->getTaskId()
123 );
124 }
125
126 protected function updateState(): void
127 {
128 if (isset($this->status))
129 {
130 $state = (new \Bitrix\Tasks\UI\Task\Deadline())->buildState($this->status, $this->deadline);
131 $this->setState($state['state']);
132 $this->setColor($state['color']);
133 }
134 }
135
136 public function getPopupData(array $excludedList = []): PopupData
137 {
138 return new PopupData([new UserPopupItem([$this->getCreatorId(), $this->getResponsibleId()])], $excludedList);
139 }
140
141 //region Getters & setters
142
143 public function getTaskId(): int
144 {
145 return $this->taskId;
146 }
147
148 public function setTaskId(int $taskId): TaskItem
149 {
150 $this->taskId = $taskId;
151 return $this;
152 }
153
154 public function getTitle(): string
155 {
156 return $this->title;
157 }
158
159 public function setTitle(string $title): TaskItem
160 {
161 $this->title = $title;
162 return $this;
163 }
164
165 public function getStatus(): int
166 {
167 return $this->status;
168 }
169
170 public function setStatus(int $status): TaskItem
171 {
172 $this->status = $status;
173
174 $statusTitle = \Bitrix\Tasks\UI\Task\Status::getList()[$this->status];
175 $this->setStatusTitle($statusTitle);
176
177 $this->updateState();
178
179 return $this;
180 }
181
182 public function getStatusTitle(): string
183 {
184 return $this->statusTitle;
185 }
186
187 public function setStatusTitle(string $statusTitle): TaskItem
188 {
189 $this->statusTitle = $statusTitle;
190 return $this;
191 }
192
193 public function getDeadline(): ?DateTime
194 {
195 return $this->deadline;
196 }
197
199 {
200 $this->deadline = $deadline;
201
202 $this->updateState();
203
204 return $this;
205 }
206
207 public function getState(): string
208 {
209 return $this->state;
210 }
211
212 public function setState(string $state): TaskItem
213 {
214 $this->state = $state;
215 return $this;
216 }
217
218 public function getColor(): string
219 {
220 return $this->color;
221 }
222
223 public function setColor(string $color): TaskItem
224 {
225 $this->color = $color;
226 return $this;
227 }
228
229 public function getCreatorId(): int
230 {
231 return $this->creatorId;
232 }
233
234 public function setCreatorId(int $creatorId): TaskItem
235 {
236 $this->creatorId = $creatorId;
237 return $this;
238 }
239
240 public function getResponsibleId(): int
241 {
243 }
244
246 {
247 $this->responsibleId = $responsibleId;
248 return $this;
249 }
250
254 public function getMembersIds(): array
255 {
256 return $this->membersIds;
257 }
258
259 public function setMembersIds(array $membersIds): self
260 {
261 $this->membersIds = $membersIds;
262 return $this;
263 }
264
265 //endregion
266}
setDeadline(?DateTime $deadline)
Definition TaskItem.php:198
toRestFormat(array $option=[])
Definition TaskItem.php:42
static initByRow(array $row)
Definition TaskItem.php:76
getPopupData(array $excludedList=[])
Definition TaskItem.php:136
setResponsibleId(int $responsibleId)
Definition TaskItem.php:245
setMembersIds(array $membersIds)
Definition TaskItem.php:259
setStatusTitle(string $statusTitle)
Definition TaskItem.php:187
static initByTaskObject(TaskObject $taskObject)
Definition TaskItem.php:101
static getById(int $id, ?Context $context=null)
Definition TaskItem.php:58