Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/calendar/xaction/PhabricatorCalendarEventInviteTransaction.php
12256 views
1
<?php
2
3
final class PhabricatorCalendarEventInviteTransaction
4
extends PhabricatorCalendarEventTransactionType {
5
6
const TRANSACTIONTYPE = 'calendar.invite';
7
8
public function generateOldValue($object) {
9
$status_uninvited = PhabricatorCalendarEventInvitee::STATUS_UNINVITED;
10
11
$invitees = $object->getInvitees();
12
foreach ($invitees as $key => $invitee) {
13
if ($invitee->getStatus() == $status_uninvited) {
14
unset($invitees[$key]);
15
}
16
}
17
18
return array_values(mpull($invitees, 'getInviteePHID'));
19
}
20
21
private function generateChangeMap($object, $new_value) {
22
$status_invited = PhabricatorCalendarEventInvitee::STATUS_INVITED;
23
$status_uninvited = PhabricatorCalendarEventInvitee::STATUS_UNINVITED;
24
$status_attending = PhabricatorCalendarEventInvitee::STATUS_ATTENDING;
25
26
$old = $this->generateOldValue($object);
27
28
$add = array_diff($new_value, $old);
29
$rem = array_diff($old, $new_value);
30
31
$map = array();
32
foreach ($add as $phid) {
33
$map[$phid] = $status_invited;
34
}
35
36
foreach ($rem as $phid) {
37
$map[$phid] = $status_uninvited;
38
}
39
40
// If we're creating this event and the actor is inviting themselves,
41
// mark them as attending.
42
if ($this->isNewObject()) {
43
$acting_phid = $this->getActingAsPHID();
44
if (isset($map[$acting_phid])) {
45
$map[$acting_phid] = $status_attending;
46
}
47
}
48
49
return $map;
50
}
51
52
public function applyExternalEffects($object, $value) {
53
$map = $this->generateChangeMap($object, $value);
54
55
$invitees = $object->getInvitees();
56
$invitees = mpull($invitees, null, 'getInviteePHID');
57
58
foreach ($map as $phid => $status) {
59
$invitee = idx($invitees, $phid);
60
if (!$invitee) {
61
$invitee = id(new PhabricatorCalendarEventInvitee())
62
->setEventPHID($object->getPHID())
63
->setInviteePHID($phid)
64
->setInviterPHID($this->getActingAsPHID());
65
$invitees[] = $invitee;
66
}
67
$invitee->setStatus($status)
68
->save();
69
}
70
71
$object->attachInvitees($invitees);
72
}
73
74
public function validateTransactions($object, array $xactions) {
75
$actor = $this->getActor();
76
77
$errors = array();
78
79
$old = $object->getInvitees();
80
$old = mpull($old, null, 'getInviteePHID');
81
foreach ($xactions as $xaction) {
82
$new = $xaction->getNewValue();
83
$new = array_fuse($new);
84
$add = array_diff_key($new, $old);
85
if (!$add) {
86
continue;
87
}
88
89
// In the UI, we only allow you to invite mailable objects, but there
90
// is no definitive marker for "invitable object" today. Just allow
91
// any valid object to be invited.
92
$objects = id(new PhabricatorObjectQuery())
93
->setViewer($actor)
94
->withPHIDs($add)
95
->execute();
96
$objects = mpull($objects, null, 'getPHID');
97
foreach ($add as $phid) {
98
if (isset($objects[$phid])) {
99
continue;
100
}
101
102
$errors[] = $this->newInvalidError(
103
pht(
104
'Invitee "%s" identifies an object that does not exist or '.
105
'which you do not have permission to view.',
106
$phid),
107
$xaction);
108
}
109
}
110
111
return $errors;
112
}
113
114
public function getIcon() {
115
return 'fa-user-plus';
116
}
117
118
public function getTitle() {
119
list($add, $rem) = $this->getChanges();
120
121
if ($add && !$rem) {
122
return pht(
123
'%s invited %s attendee(s): %s.',
124
$this->renderAuthor(),
125
phutil_count($add),
126
$this->renderHandleList($add));
127
} else if (!$add && $rem) {
128
return pht(
129
'%s uninvited %s attendee(s): %s.',
130
$this->renderAuthor(),
131
phutil_count($rem),
132
$this->renderHandleList($rem));
133
} else {
134
return pht(
135
'%s invited %s attendee(s): %s; uninvited %s attendee(s): %s.',
136
$this->renderAuthor(),
137
phutil_count($add),
138
$this->renderHandleList($add),
139
phutil_count($rem),
140
$this->renderHandleList($rem));
141
}
142
}
143
144
public function getTitleForFeed() {
145
list($add, $rem) = $this->getChanges();
146
147
if ($add && !$rem) {
148
return pht(
149
'%s invited %s attendee(s) to %s: %s.',
150
$this->renderAuthor(),
151
phutil_count($add),
152
$this->renderObject(),
153
$this->renderHandleList($add));
154
} else if (!$add && $rem) {
155
return pht(
156
'%s uninvited %s attendee(s) to %s: %s.',
157
$this->renderAuthor(),
158
phutil_count($rem),
159
$this->renderObject(),
160
$this->renderHandleList($rem));
161
} else {
162
return pht(
163
'%s updated the invite list for %s, invited %s: %s; '.
164
'uninvited %s: %s.',
165
$this->renderAuthor(),
166
$this->renderObject(),
167
phutil_count($add),
168
$this->renderHandleList($add),
169
phutil_count($rem),
170
$this->renderHandleList($rem));
171
}
172
}
173
174
private function getChanges() {
175
$old = $this->getOldValue();
176
$new = $this->getNewValue();
177
178
$add = array_diff($new, $old);
179
$rem = array_diff($old, $new);
180
181
return array(array_fuse($add), array_fuse($rem));
182
}
183
184
}
185
186