@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PhabricatorBoardRenderingEngine extends Phobject {
4
5 private $viewer;
6 private $objects;
7 private $excludedProjectPHIDs;
8 private $editMap;
9
10 private $loaded;
11 private $handles;
12 private $coverFiles;
13
14 public function setViewer(PhabricatorUser $viewer) {
15 $this->viewer = $viewer;
16 return $this;
17 }
18
19 public function getViewer() {
20 return $this->viewer;
21 }
22
23 public function setObjects(array $objects) {
24 $this->objects = mpull($objects, null, 'getPHID');
25 return $this;
26 }
27
28 public function getObjects() {
29 return $this->objects;
30 }
31
32 public function setExcludedProjectPHIDs(array $phids) {
33 $this->excludedProjectPHIDs = $phids;
34 return $this;
35 }
36
37 public function getExcludedProjectPHIDs() {
38 return $this->excludedProjectPHIDs;
39 }
40
41 public function setEditMap(array $edit_map) {
42 $this->editMap = $edit_map;
43 return $this;
44 }
45
46 public function getEditMap() {
47 return $this->editMap;
48 }
49
50 public function renderCard($phid) {
51 $this->willRender();
52
53 $viewer = $this->getViewer();
54 $object = idx($this->getObjects(), $phid);
55
56 $card = id(new ProjectBoardTaskCard())
57 ->setViewer($viewer)
58 ->setTask($object)
59 ->setShowEditControls(true)
60 ->setCanEdit($this->getCanEdit($phid));
61
62 $owner_phid = $object->getOwnerPHID();
63 if ($owner_phid) {
64 $owner_handle = $this->handles[$owner_phid];
65 $card->setOwner($owner_handle);
66 }
67
68 $project_phids = $object->getProjectPHIDs();
69 $project_handles = array_select_keys($this->handles, $project_phids);
70 if ($project_handles) {
71 $card
72 ->setHideArchivedProjects(true)
73 ->setProjectHandles($project_handles);
74 }
75
76 $cover_phid = $object->getCoverImageThumbnailPHID();
77 if ($cover_phid) {
78 $cover_file = idx($this->coverFiles, $cover_phid);
79 if ($cover_file) {
80 $card->setCoverImageFile($cover_file);
81 }
82 }
83
84 return $card;
85 }
86
87 private function willRender() {
88 if ($this->loaded) {
89 return;
90 }
91
92 $phids = array();
93 foreach ($this->objects as $object) {
94 $owner_phid = $object->getOwnerPHID();
95 if ($owner_phid) {
96 $phids[$owner_phid] = $owner_phid;
97 }
98
99 foreach ($object->getProjectPHIDs() as $phid) {
100 $phids[$phid] = $phid;
101 }
102 }
103
104 if ($this->excludedProjectPHIDs) {
105 foreach ($this->excludedProjectPHIDs as $excluded_phid) {
106 unset($phids[$excluded_phid]);
107 }
108 }
109
110 $viewer = $this->getViewer();
111
112 $handles = $viewer->loadHandles($phids);
113 $handles = iterator_to_array($handles);
114 $this->handles = $handles;
115
116 $cover_phids = array();
117 foreach ($this->objects as $object) {
118 $cover_phid = $object->getCoverImageThumbnailPHID();
119 if ($cover_phid) {
120 $cover_phids[$cover_phid] = $cover_phid;
121 }
122 }
123
124 if ($cover_phids) {
125 $cover_files = id(new PhabricatorFileQuery())
126 ->setViewer($viewer)
127 ->withPHIDs($cover_phids)
128 ->execute();
129 $cover_files = mpull($cover_files, null, 'getPHID');
130 } else {
131 $cover_files = array();
132 }
133
134 $this->coverFiles = $cover_files;
135
136 $this->loaded = true;
137 }
138
139 private function getCanEdit($phid) {
140 if ($this->editMap === null) {
141 return true;
142 }
143
144 return idx($this->editMap, $phid);
145 }
146
147}