Path: blob/master/src/applications/calendar/xaction/PhabricatorCalendarEventInviteTransaction.php
12256 views
<?php12final class PhabricatorCalendarEventInviteTransaction3extends PhabricatorCalendarEventTransactionType {45const TRANSACTIONTYPE = 'calendar.invite';67public function generateOldValue($object) {8$status_uninvited = PhabricatorCalendarEventInvitee::STATUS_UNINVITED;910$invitees = $object->getInvitees();11foreach ($invitees as $key => $invitee) {12if ($invitee->getStatus() == $status_uninvited) {13unset($invitees[$key]);14}15}1617return array_values(mpull($invitees, 'getInviteePHID'));18}1920private function generateChangeMap($object, $new_value) {21$status_invited = PhabricatorCalendarEventInvitee::STATUS_INVITED;22$status_uninvited = PhabricatorCalendarEventInvitee::STATUS_UNINVITED;23$status_attending = PhabricatorCalendarEventInvitee::STATUS_ATTENDING;2425$old = $this->generateOldValue($object);2627$add = array_diff($new_value, $old);28$rem = array_diff($old, $new_value);2930$map = array();31foreach ($add as $phid) {32$map[$phid] = $status_invited;33}3435foreach ($rem as $phid) {36$map[$phid] = $status_uninvited;37}3839// If we're creating this event and the actor is inviting themselves,40// mark them as attending.41if ($this->isNewObject()) {42$acting_phid = $this->getActingAsPHID();43if (isset($map[$acting_phid])) {44$map[$acting_phid] = $status_attending;45}46}4748return $map;49}5051public function applyExternalEffects($object, $value) {52$map = $this->generateChangeMap($object, $value);5354$invitees = $object->getInvitees();55$invitees = mpull($invitees, null, 'getInviteePHID');5657foreach ($map as $phid => $status) {58$invitee = idx($invitees, $phid);59if (!$invitee) {60$invitee = id(new PhabricatorCalendarEventInvitee())61->setEventPHID($object->getPHID())62->setInviteePHID($phid)63->setInviterPHID($this->getActingAsPHID());64$invitees[] = $invitee;65}66$invitee->setStatus($status)67->save();68}6970$object->attachInvitees($invitees);71}7273public function validateTransactions($object, array $xactions) {74$actor = $this->getActor();7576$errors = array();7778$old = $object->getInvitees();79$old = mpull($old, null, 'getInviteePHID');80foreach ($xactions as $xaction) {81$new = $xaction->getNewValue();82$new = array_fuse($new);83$add = array_diff_key($new, $old);84if (!$add) {85continue;86}8788// In the UI, we only allow you to invite mailable objects, but there89// is no definitive marker for "invitable object" today. Just allow90// any valid object to be invited.91$objects = id(new PhabricatorObjectQuery())92->setViewer($actor)93->withPHIDs($add)94->execute();95$objects = mpull($objects, null, 'getPHID');96foreach ($add as $phid) {97if (isset($objects[$phid])) {98continue;99}100101$errors[] = $this->newInvalidError(102pht(103'Invitee "%s" identifies an object that does not exist or '.104'which you do not have permission to view.',105$phid),106$xaction);107}108}109110return $errors;111}112113public function getIcon() {114return 'fa-user-plus';115}116117public function getTitle() {118list($add, $rem) = $this->getChanges();119120if ($add && !$rem) {121return pht(122'%s invited %s attendee(s): %s.',123$this->renderAuthor(),124phutil_count($add),125$this->renderHandleList($add));126} else if (!$add && $rem) {127return pht(128'%s uninvited %s attendee(s): %s.',129$this->renderAuthor(),130phutil_count($rem),131$this->renderHandleList($rem));132} else {133return pht(134'%s invited %s attendee(s): %s; uninvited %s attendee(s): %s.',135$this->renderAuthor(),136phutil_count($add),137$this->renderHandleList($add),138phutil_count($rem),139$this->renderHandleList($rem));140}141}142143public function getTitleForFeed() {144list($add, $rem) = $this->getChanges();145146if ($add && !$rem) {147return pht(148'%s invited %s attendee(s) to %s: %s.',149$this->renderAuthor(),150phutil_count($add),151$this->renderObject(),152$this->renderHandleList($add));153} else if (!$add && $rem) {154return pht(155'%s uninvited %s attendee(s) to %s: %s.',156$this->renderAuthor(),157phutil_count($rem),158$this->renderObject(),159$this->renderHandleList($rem));160} else {161return pht(162'%s updated the invite list for %s, invited %s: %s; '.163'uninvited %s: %s.',164$this->renderAuthor(),165$this->renderObject(),166phutil_count($add),167$this->renderHandleList($add),168phutil_count($rem),169$this->renderHandleList($rem));170}171}172173private function getChanges() {174$old = $this->getOldValue();175$new = $this->getNewValue();176177$add = array_diff($new, $old);178$rem = array_diff($old, $new);179180return array(array_fuse($add), array_fuse($rem));181}182183}184185186