2012-10-24 22:22:24 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 19:10:07 +01:00
|
|
|
final class PhabricatorCalendarEventEditController
|
2012-10-24 22:22:24 +02:00
|
|
|
extends PhabricatorCalendarController {
|
|
|
|
|
|
|
|
private $id;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->id = idx($data, 'id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isCreate() {
|
|
|
|
return !$this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
2015-04-28 17:34:26 +02:00
|
|
|
$request = $this->getRequest();
|
|
|
|
$user = $request->getUser();
|
2015-04-29 22:51:09 +02:00
|
|
|
$user_phid = $user->getPHID();
|
2015-04-28 17:34:26 +02:00
|
|
|
$error_name = true;
|
|
|
|
$validation_exception = null;
|
|
|
|
|
2012-10-24 22:22:24 +02:00
|
|
|
$start_time = id(new AphrontFormDateControl())
|
|
|
|
->setUser($user)
|
|
|
|
->setName('start')
|
|
|
|
->setLabel(pht('Start'))
|
|
|
|
->setInitialTime(AphrontFormDateControl::TIME_START_OF_DAY);
|
|
|
|
|
|
|
|
$end_time = id(new AphrontFormDateControl())
|
|
|
|
->setUser($user)
|
|
|
|
->setName('end')
|
|
|
|
->setLabel(pht('End'))
|
|
|
|
->setInitialTime(AphrontFormDateControl::TIME_END_OF_DAY);
|
|
|
|
|
|
|
|
if ($this->isCreate()) {
|
2015-04-28 17:34:26 +02:00
|
|
|
$event = PhabricatorCalendarEvent::initializeNewCalendarEvent($user);
|
2015-04-28 15:26:48 +02:00
|
|
|
$end_value = $end_time->readValueFromRequest($request);
|
|
|
|
$start_value = $start_time->readValueFromRequest($request);
|
2012-10-24 22:22:24 +02:00
|
|
|
$submit_label = pht('Create');
|
2015-04-28 15:26:48 +02:00
|
|
|
$page_title = pht('Create Event');
|
|
|
|
$redirect = 'created';
|
2015-04-28 19:40:35 +02:00
|
|
|
$subscribers = array();
|
2015-04-30 00:31:02 +02:00
|
|
|
$invitees = array($user_phid);
|
2015-05-01 02:09:45 +02:00
|
|
|
$cancel_uri = $this->getApplicationURI();
|
2012-10-24 22:22:24 +02:00
|
|
|
} else {
|
2015-04-28 17:34:26 +02:00
|
|
|
$event = id(new PhabricatorCalendarEventQuery())
|
2014-02-06 19:07:42 +01:00
|
|
|
->setViewer($user)
|
|
|
|
->withIDs(array($this->id))
|
|
|
|
->requireCapabilities(
|
|
|
|
array(
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT,
|
|
|
|
))
|
|
|
|
->executeOne();
|
2015-04-28 17:34:26 +02:00
|
|
|
if (!$event) {
|
2014-03-22 03:11:48 +01:00
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
2014-02-06 19:07:42 +01:00
|
|
|
|
2015-04-28 17:34:26 +02:00
|
|
|
$end_time->setValue($event->getDateTo());
|
|
|
|
$start_time->setValue($event->getDateFrom());
|
2012-10-24 22:22:24 +02:00
|
|
|
$submit_label = pht('Update');
|
2014-02-06 19:10:07 +01:00
|
|
|
$page_title = pht('Update Event');
|
2015-04-28 19:40:35 +02:00
|
|
|
|
|
|
|
$subscribers = PhabricatorSubscribersQuery::loadSubscribersForPHID(
|
|
|
|
$event->getPHID());
|
2015-04-30 04:48:46 +02:00
|
|
|
|
2015-04-30 00:31:02 +02:00
|
|
|
$invitees = array();
|
|
|
|
foreach ($event->getInvitees() as $invitee) {
|
|
|
|
if ($invitee->isUninvited()) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
$invitees[] = $invitee->getInviteePHID();
|
|
|
|
}
|
|
|
|
}
|
2015-05-01 02:09:45 +02:00
|
|
|
|
|
|
|
$cancel_uri = '/'.$event->getMonogram();
|
2012-10-24 22:22:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$errors = array();
|
2015-05-01 18:11:51 +02:00
|
|
|
$name = $event->getName();
|
|
|
|
$description = $event->getDescription();
|
|
|
|
$type = $event->getStatus();
|
|
|
|
|
|
|
|
$current_policies = id(new PhabricatorPolicyQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->setObject($event)
|
|
|
|
->execute();
|
|
|
|
|
2012-10-24 22:22:24 +02:00
|
|
|
if ($request->isFormPost()) {
|
2015-04-28 17:34:26 +02:00
|
|
|
$xactions = array();
|
|
|
|
$name = $request->getStr('name');
|
|
|
|
$type = $request->getInt('status');
|
2012-10-24 22:22:24 +02:00
|
|
|
$start_value = $start_time->readValueFromRequest($request);
|
2015-04-28 17:34:26 +02:00
|
|
|
$end_value = $end_time->readValueFromRequest($request);
|
2012-10-24 22:22:24 +02:00
|
|
|
$description = $request->getStr('description');
|
2015-04-28 19:40:35 +02:00
|
|
|
$subscribers = $request->getArr('subscribers');
|
2015-05-01 18:11:51 +02:00
|
|
|
$edit_policy = $request->getStr('editPolicy');
|
|
|
|
$view_policy = $request->getStr('viewPolicy');
|
2015-04-30 04:48:46 +02:00
|
|
|
|
2015-04-30 00:31:02 +02:00
|
|
|
$invitees = $request->getArr('invitees');
|
|
|
|
$new_invitees = $this->getNewInviteeList($invitees, $event);
|
2015-04-30 04:48:46 +02:00
|
|
|
$status_attending = PhabricatorCalendarEventInvitee::STATUS_ATTENDING;
|
2015-04-30 00:31:02 +02:00
|
|
|
if ($this->isCreate()) {
|
|
|
|
$status = idx($new_invitees, $user->getPHID());
|
|
|
|
if ($status) {
|
|
|
|
$new_invitees[$user->getPHID()] = $status_attending;
|
|
|
|
}
|
|
|
|
}
|
2012-10-24 22:22:24 +02:00
|
|
|
|
2015-03-26 21:02:49 +01:00
|
|
|
if ($start_time->getError()) {
|
|
|
|
$errors[] = pht('Invalid start time; reset to default.');
|
|
|
|
}
|
|
|
|
if ($end_time->getError()) {
|
|
|
|
$errors[] = pht('Invalid end time; reset to default.');
|
|
|
|
}
|
|
|
|
if (!$errors) {
|
2015-04-28 17:34:26 +02:00
|
|
|
$xactions[] = id(new PhabricatorCalendarEventTransaction())
|
|
|
|
->setTransactionType(
|
|
|
|
PhabricatorCalendarEventTransaction::TYPE_NAME)
|
|
|
|
->setNewValue($name);
|
|
|
|
|
2015-04-28 15:26:48 +02:00
|
|
|
$xactions[] = id(new PhabricatorCalendarEventTransaction())
|
|
|
|
->setTransactionType(
|
|
|
|
PhabricatorCalendarEventTransaction::TYPE_START_DATE)
|
|
|
|
->setNewValue($start_value);
|
2012-10-24 22:22:24 +02:00
|
|
|
|
2015-04-28 15:26:48 +02:00
|
|
|
$xactions[] = id(new PhabricatorCalendarEventTransaction())
|
|
|
|
->setTransactionType(
|
|
|
|
PhabricatorCalendarEventTransaction::TYPE_END_DATE)
|
|
|
|
->setNewValue($end_value);
|
|
|
|
|
|
|
|
$xactions[] = id(new PhabricatorCalendarEventTransaction())
|
|
|
|
->setTransactionType(
|
|
|
|
PhabricatorCalendarEventTransaction::TYPE_STATUS)
|
|
|
|
->setNewValue($type);
|
|
|
|
|
2015-04-28 19:40:35 +02:00
|
|
|
$xactions[] = id(new PhabricatorCalendarEventTransaction())
|
|
|
|
->setTransactionType(
|
|
|
|
PhabricatorTransactions::TYPE_SUBSCRIBERS)
|
|
|
|
->setNewValue(array('=' => array_fuse($subscribers)));
|
|
|
|
|
2015-04-30 00:31:02 +02:00
|
|
|
$xactions[] = id(new PhabricatorCalendarEventTransaction())
|
|
|
|
->setTransactionType(
|
|
|
|
PhabricatorCalendarEventTransaction::TYPE_INVITE)
|
|
|
|
->setNewValue($new_invitees);
|
|
|
|
|
2015-04-28 15:26:48 +02:00
|
|
|
$xactions[] = id(new PhabricatorCalendarEventTransaction())
|
|
|
|
->setTransactionType(
|
|
|
|
PhabricatorCalendarEventTransaction::TYPE_DESCRIPTION)
|
|
|
|
->setNewValue($description);
|
|
|
|
|
2015-04-30 23:43:48 +02:00
|
|
|
$xactions[] = id(new PhabricatorCalendarEventTransaction())
|
|
|
|
->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)
|
|
|
|
->setNewValue($request->getStr('viewPolicy'));
|
|
|
|
|
|
|
|
$xactions[] = id(new PhabricatorCalendarEventTransaction())
|
|
|
|
->setTransactionType(PhabricatorTransactions::TYPE_EDIT_POLICY)
|
|
|
|
->setNewValue($request->getStr('editPolicy'));
|
|
|
|
|
2015-04-28 15:26:48 +02:00
|
|
|
$editor = id(new PhabricatorCalendarEventEditor())
|
|
|
|
->setActor($user)
|
|
|
|
->setContentSourceFromRequest($request)
|
|
|
|
->setContinueOnNoEffect(true);
|
|
|
|
|
2015-04-28 17:34:26 +02:00
|
|
|
try {
|
|
|
|
$xactions = $editor->applyTransactions($event, $xactions);
|
|
|
|
$response = id(new AphrontRedirectResponse());
|
|
|
|
return $response->setURI('/E'.$event->getID());
|
|
|
|
} catch (PhabricatorApplicationTransactionValidationException $ex) {
|
|
|
|
$validation_exception = $ex;
|
|
|
|
$error_name = $ex
|
|
|
|
->getShortMessage(PhabricatorCalendarEventTransaction::TYPE_NAME);
|
2015-05-01 18:11:51 +02:00
|
|
|
|
|
|
|
$event->setViewPolicy($view_policy);
|
|
|
|
$event->setEditPolicy($edit_policy);
|
2015-04-28 17:34:26 +02:00
|
|
|
}
|
2015-05-01 18:11:51 +02:00
|
|
|
} else {
|
|
|
|
$event->setViewPolicy($view_policy);
|
|
|
|
$event->setEditPolicy($edit_policy);
|
2012-10-24 22:22:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$error_view = null;
|
|
|
|
if ($errors) {
|
2015-03-01 23:45:56 +01:00
|
|
|
$error_view = id(new PHUIInfoView())
|
2013-02-23 16:12:36 +01:00
|
|
|
->setTitle(pht('Status can not be set!'))
|
2012-10-24 22:22:24 +02:00
|
|
|
->setErrors($errors);
|
|
|
|
}
|
|
|
|
|
2015-04-28 17:34:26 +02:00
|
|
|
$name = id(new AphrontFormTextControl())
|
|
|
|
->setLabel(pht('Name'))
|
|
|
|
->setName('name')
|
2015-05-01 18:11:51 +02:00
|
|
|
->setValue($name)
|
2015-04-28 17:34:26 +02:00
|
|
|
->setError($error_name);
|
|
|
|
|
2012-10-24 22:22:24 +02:00
|
|
|
$status_select = id(new AphrontFormSelectControl())
|
|
|
|
->setLabel(pht('Status'))
|
|
|
|
->setName('status')
|
2015-05-01 18:11:51 +02:00
|
|
|
->setValue($type)
|
2015-04-28 17:34:26 +02:00
|
|
|
->setOptions($event->getStatusOptions());
|
2012-10-24 22:22:24 +02:00
|
|
|
|
|
|
|
$description = id(new AphrontFormTextAreaControl())
|
|
|
|
->setLabel(pht('Description'))
|
|
|
|
->setName('description')
|
2015-05-01 18:11:51 +02:00
|
|
|
->setValue($description);
|
2013-05-29 23:35:34 +02:00
|
|
|
|
2015-04-30 23:43:48 +02:00
|
|
|
$view_policies = id(new AphrontFormPolicyControl())
|
|
|
|
->setUser($user)
|
|
|
|
->setCapability(PhabricatorPolicyCapability::CAN_VIEW)
|
|
|
|
->setPolicyObject($event)
|
|
|
|
->setPolicies($current_policies)
|
|
|
|
->setName('viewPolicy');
|
|
|
|
$edit_policies = id(new AphrontFormPolicyControl())
|
|
|
|
->setUser($user)
|
|
|
|
->setCapability(PhabricatorPolicyCapability::CAN_EDIT)
|
|
|
|
->setPolicyObject($event)
|
|
|
|
->setPolicies($current_policies)
|
|
|
|
->setName('editPolicy');
|
|
|
|
|
2015-04-28 19:40:35 +02:00
|
|
|
$subscribers = id(new AphrontFormTokenizerControl())
|
|
|
|
->setLabel(pht('Subscribers'))
|
|
|
|
->setName('subscribers')
|
|
|
|
->setValue($subscribers)
|
|
|
|
->setUser($user)
|
|
|
|
->setDatasource(new PhabricatorMetaMTAMailableDatasource());
|
|
|
|
|
2015-04-30 00:31:02 +02:00
|
|
|
$invitees = id(new AphrontFormTokenizerControl())
|
|
|
|
->setLabel(pht('Invitees'))
|
|
|
|
->setName('invitees')
|
|
|
|
->setValue($invitees)
|
|
|
|
->setUser($user)
|
|
|
|
->setDatasource(new PhabricatorMetaMTAMailableDatasource());
|
|
|
|
|
2015-04-28 17:34:26 +02:00
|
|
|
$form = id(new AphrontFormView())
|
|
|
|
->setUser($user)
|
|
|
|
->appendChild($name)
|
2012-10-24 22:22:24 +02:00
|
|
|
->appendChild($status_select)
|
|
|
|
->appendChild($start_time)
|
|
|
|
->appendChild($end_time)
|
2015-04-30 23:43:48 +02:00
|
|
|
->appendControl($view_policies)
|
|
|
|
->appendControl($edit_policies)
|
2015-04-28 19:40:35 +02:00
|
|
|
->appendControl($subscribers)
|
2015-04-30 00:31:02 +02:00
|
|
|
->appendControl($invitees)
|
2012-10-24 22:22:24 +02:00
|
|
|
->appendChild($description);
|
|
|
|
|
2015-05-01 02:09:45 +02:00
|
|
|
|
|
|
|
if ($request->isAjax()) {
|
|
|
|
return $this->newDialog()
|
|
|
|
->setTitle($page_title)
|
|
|
|
->setWidth(AphrontDialogView::WIDTH_FULL)
|
|
|
|
->appendForm($form)
|
|
|
|
->addCancelButton($cancel_uri)
|
|
|
|
->addSubmitButton($submit_label);
|
|
|
|
}
|
|
|
|
|
2015-04-28 17:34:26 +02:00
|
|
|
$submit = id(new AphrontFormSubmitControl())
|
2015-05-01 02:09:45 +02:00
|
|
|
->addCancelButton($cancel_uri)
|
2015-04-28 17:34:26 +02:00
|
|
|
->setValue($submit_label);
|
2013-05-29 23:35:34 +02:00
|
|
|
|
2012-10-24 22:22:24 +02:00
|
|
|
$form->appendChild($submit);
|
|
|
|
|
2013-09-25 20:23:29 +02:00
|
|
|
$form_box = id(new PHUIObjectBoxView())
|
2013-08-26 20:53:11 +02:00
|
|
|
->setHeaderText($page_title)
|
2014-01-10 18:17:37 +01:00
|
|
|
->setFormErrors($errors)
|
2013-08-26 20:53:11 +02:00
|
|
|
->setForm($form);
|
|
|
|
|
2015-04-27 23:27:00 +02:00
|
|
|
$crumbs = $this->buildApplicationCrumbs();
|
|
|
|
|
|
|
|
if (!$this->isCreate()) {
|
2015-04-28 17:34:26 +02:00
|
|
|
$crumbs->addTextCrumb('E'.$event->getId(), '/E'.$event->getId());
|
2015-04-27 23:27:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$crumbs->addTextCrumb($page_title);
|
2014-02-18 01:08:50 +01:00
|
|
|
|
2015-04-28 17:34:26 +02:00
|
|
|
$object_box = id(new PHUIObjectBoxView())
|
|
|
|
->setHeaderText($page_title)
|
|
|
|
->setValidationException($validation_exception)
|
|
|
|
->appendChild($form);
|
|
|
|
|
2015-05-01 01:18:15 +02:00
|
|
|
return $this->buildApplicationPage(
|
2012-10-24 22:22:24 +02:00
|
|
|
array(
|
2014-02-18 01:08:50 +01:00
|
|
|
$crumbs,
|
2015-04-28 17:34:26 +02:00
|
|
|
$object_box,
|
2015-05-01 01:18:15 +02:00
|
|
|
),
|
2012-10-24 22:22:24 +02:00
|
|
|
array(
|
|
|
|
'title' => $page_title,
|
2013-02-19 22:33:10 +01:00
|
|
|
));
|
2012-10-24 22:22:24 +02:00
|
|
|
}
|
|
|
|
|
2015-04-30 00:31:02 +02:00
|
|
|
|
|
|
|
public function getNewInviteeList(array $phids, $event) {
|
|
|
|
$invitees = $event->getInvitees();
|
|
|
|
$invitees = mpull($invitees, null, 'getInviteePHID');
|
|
|
|
$invited_status = PhabricatorCalendarEventInvitee::STATUS_INVITED;
|
|
|
|
$uninvited_status = PhabricatorCalendarEventInvitee::STATUS_UNINVITED;
|
|
|
|
$phids = array_fuse($phids);
|
|
|
|
|
|
|
|
$new = array();
|
|
|
|
foreach ($phids as $phid) {
|
2015-04-30 04:48:46 +02:00
|
|
|
$old_status = $event->getUserInviteStatus($phid);
|
|
|
|
if ($old_status != $uninvited_status) {
|
|
|
|
continue;
|
2015-04-30 00:31:02 +02:00
|
|
|
}
|
|
|
|
$new[$phid] = $invited_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($invitees as $invitee) {
|
|
|
|
$deleted_invitee = !idx($phids, $invitee->getInviteePHID());
|
|
|
|
if ($deleted_invitee) {
|
|
|
|
$new[$invitee->getInviteePHID()] = $uninvited_status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $new;
|
|
|
|
}
|
|
|
|
|
2012-10-24 22:22:24 +02:00
|
|
|
}
|