diff --git a/src/applications/phrequent/controller/PhrequentTrackController.php b/src/applications/phrequent/controller/PhrequentTrackController.php --- a/src/applications/phrequent/controller/PhrequentTrackController.php +++ b/src/applications/phrequent/controller/PhrequentTrackController.php @@ -15,18 +15,91 @@ $request = $this->getRequest(); $user = $request->getUser(); + $phid = $this->phid; + $handle = id(new PhabricatorHandleQuery()) + ->setViewer($user) + ->withPHIDs(array($phid)) + ->executeOne(); + if (!$this->isStartingTracking() && !$this->isStoppingTracking()) { throw new Exception('Unrecognized verb: ' . $this->verb); } - if ($this->isStartingTracking()) { - $this->startTracking($user, $this->phid); - } else if ($this->isStoppingTracking()) { - $this->stopTracking($user, $this->phid); + $verb_formatted = ucwords($this->verb); + + $epoch_control = id(new AphrontFormDateControl()) + ->setUser($user) + ->setName('epoch') + ->setLabel(pht('%s Time', $verb_formatted)) + ->setValue(time()); + + + $err = array(); + + if ($request->isDialogFormPost()) { + $timestamp = $epoch_control->readValueFromRequest($request); + $note = $request->getStr('note'); + + if (!$epoch_control->isValid() || $timestamp > time()) { + $err[] = pht('Invalid date, please enter a valid non-future date'); + } + + if (!$err) { + if ($this->isStartingTracking()) { + $this->startTracking($user, $this->phid, $timestamp); + } else if ($this->isStoppingTracking()) { + $this->stopTracking($user, $this->phid, $timestamp, $note); + } + return id(new AphrontRedirectResponse()); + } + + } + + switch ($this->verb) { + case 'start': + $button_text = pht('Start Tracking'); + $title_text = pht('Start Tracking Time'); + $inner_text = pht('What time did you start working?'); + break; + case 'stop': + $button_text = pht('Stop Tracking'); + $title_text = pht('Stop Tracking Time'); + $inner_text = pht('What time did you stop working?'); + break; + } + + $dialog = $this->newDialog(); + + $dialog->settitle($title_text); + + if ($err) { + $dialog->setErrors($err); } - return id(new AphrontRedirectResponse()); + $form = new PHUIFormLayoutView(); + $form + ->appendChild(hsprintf( + "<p>%s</p><br />", $inner_text)); + + $form->appendChild($epoch_control); + + if ($this->isStoppingTracking()) { + $form + ->appendChild( + id(new AphrontFormTextControl()) + ->setLabel(pht('Note')) + ->setName('note')); + } + + $dialog->appendChild($form); + + $dialog->addCancelButton($handle->getURI()); + $dialog->addSubmitButton(pht('%s Time', $verb_formatted)); + + return $dialog; + + } private function isStartingTracking() { @@ -37,15 +110,15 @@ return $this->verb === 'stop'; } - private function startTracking($user, $phid) { + private function startTracking($user, $phid, $timestamp) { $usertime = new PhrequentUserTime(); - $usertime->setDateStarted(time()); + $usertime->setDateStarted($timestamp); $usertime->setUserPHID($user->getPHID()); $usertime->setObjectPHID($phid); $usertime->save(); } - private function stopTracking($user, $phid) { + private function stopTracking($user, $phid, $timestamp, $note) { if (!PhrequentUserTimeQuery::isUserTrackingObject($user, $phid)) { // Don't do anything, it's not being tracked. return; @@ -57,13 +130,16 @@ queryfx( $conn, 'UPDATE %T usertime '. - 'SET usertime.dateEnded = UNIX_TIMESTAMP() '. + 'SET usertime.dateEnded = %d, '. + 'usertime.note = %s '. 'WHERE usertime.userPHID = %s '. 'AND usertime.objectPHID = %s '. 'AND usertime.dateEnded IS NULL '. 'ORDER BY usertime.dateStarted, usertime.id DESC '. 'LIMIT 1', $usertime_dao->getTableName(), + $timestamp, + $note, $user->getPHID(), $phid); }