3 final class PhrequentTrackController
4 extends PhrequentController
{
9 public function willProcessRequest(array $data) {
10 $this->phid
= $data['phid'];
11 $this->verb
= $data['verb'];
14 public function processRequest() {
15 $request = $this->getRequest();
16 $viewer = $request->getUser();
19 $handle = id(new PhabricatorHandleQuery())
21 ->withPHIDs(array($phid))
23 $done_uri = $handle->getURI();
25 $current_timer = null;
26 switch ($this->verb
) {
28 $button_text = pht('Start Tracking');
29 $title_text = pht('Start Tracking Time');
30 $inner_text = pht('What time did you start working?');
31 $action_text = pht('Start Timer');
32 $label_text = pht('Start Time');
35 $button_text = pht('Stop Tracking');
36 $title_text = pht('Stop Tracking Time');
37 $inner_text = pht('What time did you stop working?');
38 $action_text = pht('Stop Timer');
39 $label_text = pht('Stop Time');
42 $current_timer = id(new PhrequentUserTimeQuery())
44 ->withUserPHIDs(array($viewer->getPHID()))
45 ->withObjectPHIDs(array($phid))
46 ->withEnded(PhrequentUserTimeQuery
::ENDED_NO
)
48 if (!$current_timer) {
49 return $this->newDialog()
50 ->setTitle(pht('Not Tracking Time'))
52 pht('You are not currently tracking time on this object.'))
53 ->addCancelButton($done_uri);
57 return new Aphront404Response();
64 $timestamp = AphrontFormDateControlValue
::newFromEpoch(
68 if ($request->isDialogFormPost()) {
69 $v_note = $request->getStr('note');
70 $timestamp = AphrontFormDateControlValue
::newFromRequest(
74 if (!$timestamp->isValid()) {
75 $errors[] = pht('Please choose a valid date.');
76 $e_date = pht('Invalid');
78 $max_time = PhabricatorTime
::getNow();
79 if ($timestamp->getEpoch() > $max_time) {
80 if ($this->isStoppingTracking()) {
82 'You can not stop tracking time at a future time. Enter the '.
83 'current time, or a time in the past.');
86 'You can not start tracking time at a future time. Enter the '.
87 'current time, or a time in the past.');
89 $e_date = pht('Invalid');
92 if ($this->isStoppingTracking()) {
93 $min_time = $current_timer->getDateStarted();
94 if ($min_time > $timestamp->getEpoch()) {
95 $errors[] = pht('Stop time must be after start time.');
96 $e_date = pht('Invalid');
102 $editor = new PhrequentTrackingEditor();
103 if ($this->isStartingTracking()) {
104 $editor->startTracking(
107 $timestamp->getEpoch());
108 } else if ($this->isStoppingTracking()) {
109 $editor->stopTracking(
112 $timestamp->getEpoch(),
116 return id(new AphrontRedirectResponse())->setURI($done_uri);
121 $dialog = $this->newDialog()
122 ->setTitle($title_text)
123 ->setWidth(AphrontDialogView
::WIDTH_FORM
)
125 ->appendParagraph($inner_text);
127 $form = new PHUIFormLayoutView();
129 if ($this->isStoppingTracking()) {
130 $start_time = $current_timer->getDateStarted();
133 phabricator_datetime($start_time, $viewer),
134 phutil_format_relative_time(PhabricatorTime
::getNow() - $start_time));
137 id(new AphrontFormStaticControl())
138 ->setLabel(pht('Started At'))
139 ->setValue($start_string));
143 id(new AphrontFormDateControl())
146 ->setLabel($action_text)
148 ->setValue($timestamp));
150 if ($this->isStoppingTracking()) {
152 id(new AphrontFormTextControl())
153 ->setLabel(pht('Note'))
155 ->setValue($v_note));
158 $dialog->appendChild($form);
160 $dialog->addCancelButton($done_uri);
162 $dialog->addSubmitButton($action_text);
167 private function isStartingTracking() {
168 return $this->verb
=== 'start';
171 private function isStoppingTracking() {
172 return $this->verb
=== 'stop';