@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 PholioMockThumbGridView extends AphrontView {
4
5 private $mock;
6
7 public function setMock(PholioMock $mock) {
8 $this->mock = $mock;
9 return $this;
10 }
11
12 public function render() {
13 $mock = $this->mock;
14
15 $all_images = $mock->getImages();
16 $all_images = mpull($all_images, null, 'getPHID');
17
18 $history = mpull($all_images, 'getReplacesImagePHID', 'getPHID');
19
20 $replaced = array();
21 foreach ($history as $phid => $replaces_phid) {
22 if ($replaces_phid) {
23 $replaced[$replaces_phid] = true;
24 }
25 }
26
27 // Figure out the columns. Start with all the active images.
28 $images = mpull($mock->getActiveImages(), null, 'getPHID');
29
30 // Now, find deleted images: obsolete images which were not replaced.
31 foreach ($mock->getImages() as $image) {
32 if (!$image->getIsObsolete()) {
33 // Image is current.
34 continue;
35 }
36
37 if (isset($replaced[$image->getPHID()])) {
38 // Image was replaced.
39 continue;
40 }
41
42 // This is an obsolete image which was not replaced, so it must be
43 // a deleted image.
44 $images[$image->getPHID()] = $image;
45 }
46
47 $cols = array();
48 $depth = 0;
49 foreach ($images as $image) {
50 $phid = $image->getPHID();
51
52 $col = array();
53
54 // If this is a deleted image, null out the final column.
55 if ($image->getIsObsolete()) {
56 $col[] = null;
57 }
58
59 $col[] = $phid;
60 while ($phid && isset($history[$phid])) {
61 $col[] = $history[$phid];
62 $phid = $history[$phid];
63 }
64
65 $cols[] = $col;
66 $depth = max($depth, count($col));
67 }
68
69 $grid = array();
70 $jj = $depth;
71 for ($ii = 0; $ii < $depth; $ii++) {
72 $row = array();
73 if ($depth == $jj) {
74 $row[] = phutil_tag(
75 'th',
76 array(
77 'valign' => 'middle',
78 'class' => 'pholio-history-header',
79 ),
80 pht('Current Revision'));
81 } else {
82 $row[] = phutil_tag('th', array(), null);
83 }
84 foreach ($cols as $col) {
85 if (empty($col[$ii])) {
86 $row[] = phutil_tag('td', array(), null);
87 } else {
88 $thumb = $this->renderThumbnail($all_images[$col[$ii]]);
89 $row[] = phutil_tag('td', array(), $thumb);
90 }
91 }
92 $grid[] = phutil_tag('tr', array(), $row);
93 $jj--;
94 }
95
96 $grid = phutil_tag(
97 'table',
98 array(
99 'id' => 'pholio-mock-thumb-grid',
100 'class' => 'pholio-mock-thumb-grid',
101 ),
102 $grid);
103
104 $grid = id(new PHUIBoxView())
105 ->addClass('pholio-mock-thumb-grid-container')
106 ->appendChild($grid);
107
108 return id(new PHUIObjectBoxView())
109 ->setHeaderText(pht('Mock History'))
110 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
111 ->appendChild($grid);
112 }
113
114
115 private function renderThumbnail(PholioImage $image) {
116 $thumbfile = $image->getFile();
117
118 $preview_key = PhabricatorFileThumbnailTransform::TRANSFORM_THUMBGRID;
119 $xform = PhabricatorFileTransform::getTransformByKey($preview_key);
120 Javelin::initBehavior('phabricator-tooltips');
121
122 $attributes = array(
123 'class' => 'pholio-mock-thumb-grid-image',
124 'src' => $thumbfile->getURIForTransform($xform),
125 );
126
127 if ($image->getFile()->isViewableImage()) {
128 $dimensions = $xform->getTransformedDimensions($thumbfile);
129 if ($dimensions) {
130 list($x, $y) = $dimensions;
131 $attributes += array(
132 'width' => $x,
133 'height' => $y,
134 'style' => 'top: '.floor((100 - $y) / 2).'px',
135 );
136 }
137 } else {
138 // If this is a PDF or a text file or something, we'll end up using a
139 // generic thumbnail which is always sized correctly.
140 $attributes += array(
141 'width' => 100,
142 'height' => 100,
143 );
144 }
145
146 $tag = phutil_tag('img', $attributes);
147
148 $classes = array('pholio-mock-thumb-grid-item');
149 if ($image->getIsObsolete()) {
150 $classes[] = 'pholio-mock-thumb-grid-item-obsolete';
151 }
152
153 $inline_count = null;
154 if ($image->getInlineComments()) {
155 $inline_count[] = phutil_tag(
156 'span',
157 array(
158 'class' => 'pholio-mock-thumb-grid-comment-count',
159 ),
160 pht('%s', phutil_count($image->getInlineComments())));
161 }
162
163 return javelin_tag(
164 'a',
165 array(
166 'sigil' => 'mock-thumbnail has-tooltip',
167 'class' => implode(' ', $classes),
168 'href' => '#',
169 'meta' => array(
170 'imageID' => $image->getID(),
171 'tip' => $image->getName(),
172 'align' => 'N',
173 ),
174 ),
175 array(
176 $tag,
177 $inline_count,
178 ));
179 }
180
181}