@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 PholioImageFileTransaction
4 extends PholioImageTransactionType {
5
6 const TRANSACTIONTYPE = 'image-file';
7
8 public function generateOldValue($object) {
9 $images = $object->getActiveImages();
10 return array_values(mpull($images, 'getPHID'));
11 }
12
13 public function generateNewValue($object, $value) {
14 $editor = $this->getEditor();
15
16 $old_value = $this->getOldValue();
17 $new_value = $value;
18
19 return $editor->getPHIDList($old_value, $new_value);
20 }
21
22 public function applyExternalEffects($object, $value) {
23 $old_map = array_fuse($this->getOldValue());
24 $new_map = array_fuse($this->getNewValue());
25
26 $add_map = array_diff_key($new_map, $old_map);
27 $rem_map = array_diff_key($old_map, $new_map);
28
29 $editor = $this->getEditor();
30
31 foreach ($rem_map as $phid) {
32 $editor->loadPholioImage($object, $phid)
33 ->setIsObsolete(1)
34 ->save();
35 }
36
37 foreach ($add_map as $phid) {
38 $editor->loadPholioImage($object, $phid)
39 ->setMockPHID($object->getPHID())
40 ->save();
41 }
42 }
43
44 public function getTitle() {
45 $old = $this->getOldValue();
46 $new = $this->getNewValue();
47
48 $add = array_diff($new, $old);
49 $rem = array_diff($old, $new);
50
51 if ($add && $rem) {
52 return pht(
53 '%s edited image(s), added %d: %s; removed %d: %s.',
54 $this->renderAuthor(),
55 count($add),
56 $this->renderHandleList($add),
57 count($rem),
58 $this->renderHandleList($rem));
59 } else if ($add) {
60 return pht(
61 '%s added %d image(s): %s.',
62 $this->renderAuthor(),
63 count($add),
64 $this->renderHandleList($add));
65 } else {
66 return pht(
67 '%s removed %d image(s): %s.',
68 $this->renderAuthor(),
69 count($rem),
70 $this->renderHandleList($rem));
71 }
72 }
73
74 public function getTitleForFeed() {
75 $old = $this->getOldValue();
76 $new = $this->getNewValue();
77
78 return pht(
79 '%s updated images of %s.',
80 $this->renderAuthor(),
81 $this->renderObject());
82 }
83
84 public function getIcon() {
85 return 'fa-picture-o';
86 }
87
88 public function getColor() {
89 $old = $this->getOldValue();
90 $new = $this->getNewValue();
91
92 $add = array_diff($new, $old);
93 $rem = array_diff($old, $new);
94 if ($add && $rem) {
95 return PhabricatorTransactions::COLOR_YELLOW;
96 } else if ($add) {
97 return PhabricatorTransactions::COLOR_GREEN;
98 } else {
99 return PhabricatorTransactions::COLOR_RED;
100 }
101 }
102
103 public function extractFilePHIDs($object, $value) {
104 $editor = $this->getEditor();
105
106 // NOTE: This method is a little weird (and includes ALL the file PHIDs,
107 // including old file PHIDs) because we currently don't have a storage
108 // object when called. This might change at some point.
109
110 $new_phids = $value;
111
112 $file_phids = array();
113 foreach ($new_phids as $phids) {
114 foreach ($phids as $phid) {
115 $file_phids[] = $editor->loadPholioImage($object, $phid)
116 ->getFilePHID();
117 }
118 }
119
120 return $file_phids;
121 }
122
123 public function mergeTransactions(
124 $object,
125 PhabricatorApplicationTransaction $u,
126 PhabricatorApplicationTransaction $v) {
127 return $this->getEditor()->mergePHIDOrEdgeTransactions($u, $v);
128 }
129
130}