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 17:34:26 +02:00
|
|
|
$filter = 'event/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);
|
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');
|
2015-04-28 17:34:26 +02:00
|
|
|
$filter = 'event/edit/'.$event->getID().'/';
|
2014-02-06 19:10:07 +01:00
|
|
|
$page_title = pht('Update Event');
|
2012-10-24 22:22:24 +02:00
|
|
|
$redirect = 'updated';
|
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();
|
|
|
|
}
|
|
|
|
}
|
2012-10-24 22:22:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$errors = array();
|
|
|
|
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-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);
|
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
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')
|
|
|
|
->setValue($event->getName())
|
|
|
|
->setError($error_name);
|
|
|
|
|
2012-10-24 22:22:24 +02:00
|
|
|
$status_select = id(new AphrontFormSelectControl())
|
|
|
|
->setLabel(pht('Status'))
|
|
|
|
->setName('status')
|
2015-04-28 17:34:26 +02:00
|
|
|
->setValue($event->getStatus())
|
|
|
|
->setOptions($event->getStatusOptions());
|
2012-10-24 22:22:24 +02:00
|
|
|
|
|
|
|
$description = id(new AphrontFormTextAreaControl())
|
|
|
|
->setLabel(pht('Description'))
|
|
|
|
->setName('description')
|
2015-04-28 17:34:26 +02:00
|
|
|
->setValue($event->getDescription());
|
2013-05-29 23:35:34 +02:00
|
|
|
|
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-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-04-28 17:34:26 +02:00
|
|
|
$submit = id(new AphrontFormSubmitControl())
|
|
|
|
->setValue($submit_label);
|
2012-10-24 22:22:24 +02:00
|
|
|
if ($this->isCreate()) {
|
|
|
|
$submit->addCancelButton($this->getApplicationURI());
|
|
|
|
} else {
|
2015-04-28 17:34:26 +02:00
|
|
|
$submit->addCancelButton('/E'.$event->getID());
|
2012-10-24 22:22:24 +02:00
|
|
|
}
|
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-28 17:34:26 +02:00
|
|
|
$nav = $this->buildSideNavView($event);
|
2012-10-24 22:22:24 +02:00
|
|
|
$nav->selectFilter($filter);
|
|
|
|
|
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);
|
|
|
|
|
2012-10-24 22:22:24 +02:00
|
|
|
$nav->appendChild(
|
|
|
|
array(
|
2014-02-18 01:08:50 +01:00
|
|
|
$crumbs,
|
2015-04-28 17:34:26 +02:00
|
|
|
$object_box,
|
2013-02-19 22:33:10 +01:00
|
|
|
));
|
2012-10-24 22:22:24 +02:00
|
|
|
|
|
|
|
return $this->buildApplicationPage(
|
|
|
|
$nav,
|
|
|
|
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
|
|
|
}
|