[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/phrequent/controller/ -> PhrequentTrackController.php (source)

   1  <?php
   2  
   3  final class PhrequentTrackController
   4    extends PhrequentController {
   5  
   6    private $verb;
   7    private $phid;
   8  
   9    public function willProcessRequest(array $data) {
  10      $this->phid = $data['phid'];
  11      $this->verb = $data['verb'];
  12    }
  13  
  14    public function processRequest() {
  15      $request = $this->getRequest();
  16      $viewer = $request->getUser();
  17  
  18      $phid = $this->phid;
  19      $handle = id(new PhabricatorHandleQuery())
  20        ->setViewer($viewer)
  21        ->withPHIDs(array($phid))
  22        ->executeOne();
  23      $done_uri = $handle->getURI();
  24  
  25      $current_timer = null;
  26      switch ($this->verb) {
  27        case 'start':
  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');
  33          break;
  34        case 'stop':
  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');
  40  
  41  
  42          $current_timer = id(new PhrequentUserTimeQuery())
  43            ->setViewer($viewer)
  44            ->withUserPHIDs(array($viewer->getPHID()))
  45            ->withObjectPHIDs(array($phid))
  46            ->withEnded(PhrequentUserTimeQuery::ENDED_NO)
  47            ->executeOne();
  48          if (!$current_timer) {
  49            return $this->newDialog()
  50              ->setTitle(pht('Not Tracking Time'))
  51              ->appendParagraph(
  52                pht(
  53                  'You are not currently tracking time on this object.'))
  54              ->addCancelButton($done_uri);
  55          }
  56          break;
  57        default:
  58          return new Aphront404Response();
  59      }
  60  
  61      $errors = array();
  62      $v_note = null;
  63      $e_date = null;
  64  
  65      $epoch_control = id(new AphrontFormDateControl())
  66        ->setUser($viewer)
  67        ->setName('epoch')
  68        ->setLabel($action_text)
  69        ->setValue(time());
  70  
  71      if ($request->isDialogFormPost()) {
  72        $v_note = $request->getStr('note');
  73        $timestamp = $epoch_control->readValueFromRequest($request);
  74  
  75        if (!$epoch_control->isValid()) {
  76          $errors[] = pht('Please choose an valid date.');
  77          $e_date = pht('Invalid');
  78        } else {
  79          $max_time = PhabricatorTime::getNow();
  80          if ($timestamp > $max_time) {
  81            if ($this->isStoppingTracking()) {
  82              $errors[] = pht(
  83                'You can not stop tracking time at a future time. Enter the '.
  84                'current time, or a time in the past.');
  85            } else {
  86              $errors[] = pht(
  87                'You can not start tracking time at a future time. Enter the '.
  88                'current time, or a time in the past.');
  89            }
  90            $e_date = pht('Invalid');
  91          }
  92  
  93          if ($this->isStoppingTracking()) {
  94            $min_time = $current_timer->getDateStarted();
  95            if ($min_time > $timestamp) {
  96              $errors[] = pht(
  97                'Stop time must be after start time.');
  98              $e_date = pht('Invalid');
  99            }
 100          }
 101        }
 102  
 103        if (!$errors) {
 104          $editor = new PhrequentTrackingEditor();
 105          if ($this->isStartingTracking()) {
 106            $editor->startTracking($viewer, $this->phid, $timestamp);
 107          } else if ($this->isStoppingTracking()) {
 108            $editor->stopTracking($viewer, $this->phid, $timestamp, $v_note);
 109          }
 110  
 111          return id(new AphrontRedirectResponse())->setURI($done_uri);
 112        }
 113  
 114      }
 115  
 116      $epoch_control->setError($e_date);
 117  
 118      $dialog = $this->newDialog()
 119        ->setTitle($title_text)
 120        ->setWidth(AphrontDialogView::WIDTH_FORM)
 121        ->setErrors($errors)
 122        ->appendParagraph($inner_text);
 123  
 124      $form = new PHUIFormLayoutView();
 125  
 126      if ($this->isStoppingTracking()) {
 127        $start_time = $current_timer->getDateStarted();
 128        $start_string = pht(
 129          '%s (%s ago)',
 130          phabricator_datetime($start_time, $viewer),
 131          phutil_format_relative_time(PhabricatorTime::getNow() - $start_time));
 132  
 133        $form->appendChild(
 134          id(new AphrontFormStaticControl())
 135            ->setLabel(pht('Started At'))
 136            ->setValue($start_string));
 137      }
 138  
 139      $form->appendChild($epoch_control);
 140  
 141      if ($this->isStoppingTracking()) {
 142        $form->appendChild(
 143          id(new AphrontFormTextControl())
 144            ->setLabel(pht('Note'))
 145            ->setName('note')
 146            ->setValue($v_note));
 147      }
 148  
 149      $dialog->appendChild($form);
 150  
 151      $dialog->addCancelButton($done_uri);
 152  
 153      $dialog->addSubmitButton($action_text);
 154  
 155      return $dialog;
 156    }
 157  
 158    private function isStartingTracking() {
 159      return $this->verb === 'start';
 160    }
 161  
 162    private function isStoppingTracking() {
 163      return $this->verb === 'stop';
 164    }
 165  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1