ManiphestTaskDetailController.php 16.6 KB
Newer Older
epriestley's avatar
epriestley committed
1 2
<?php

3
final class ManiphestTaskDetailController extends ManiphestController {
epriestley's avatar
epriestley committed
4

5 6 7 8
  public function shouldAllowPublic() {
    return true;
  }

9 10 11
  public function handleRequest(AphrontRequest $request) {
    $viewer = $this->getViewer();
    $id = $request->getURIData('id');
epriestley's avatar
epriestley committed
12

13
    $task = id(new ManiphestTaskQuery())
14 15
      ->setViewer($viewer)
      ->withIDs(array($id))
16
      ->needSubscriberPHIDs(true)
17
      ->executeOne();
18 19 20
    if (!$task) {
      return new Aphront404Response();
    }
epriestley's avatar
epriestley committed
21

epriestley's avatar
epriestley committed
22 23 24
    $field_list = PhabricatorCustomField::getObjectFields(
      $task,
      PhabricatorCustomField::ROLE_VIEW);
25
    $field_list
26
      ->setViewer($viewer)
27
      ->readFieldsFromStorage($task);
28

29 30 31 32
    $edit_engine = id(new ManiphestEditEngine())
      ->setViewer($viewer)
      ->setTargetObject($task);

33 34 35 36 37 38 39
    $edge_types = array(
      ManiphestTaskHasCommitEdgeType::EDGECONST,
      ManiphestTaskHasRevisionEdgeType::EDGECONST,
      ManiphestTaskHasMockEdgeType::EDGECONST,
      PhabricatorObjectMentionedByObjectEdgeType::EDGECONST,
      PhabricatorObjectMentionsObjectEdgeType::EDGECONST,
    );
40 41 42 43 44

    $phid = $task->getPHID();

    $query = id(new PhabricatorEdgeQuery())
      ->withSourcePHIDs(array($phid))
45
      ->withEdgeTypes($edge_types);
46
    $edges = idx($query->execute(), $phid);
47
    $phids = array_fill_keys($query->getDestinationPHIDs(), true);
48

epriestley's avatar
epriestley committed
49 50 51 52 53
    if ($task->getOwnerPHID()) {
      $phids[$task->getOwnerPHID()] = true;
    }
    $phids[$task->getAuthorPHID()] = true;

54
    $phids = array_keys($phids);
55
    $handles = $viewer->loadHandles($phids);
56

57 58
    $timeline = $this->buildTransactionTimeline(
      $task,
59
      new ManiphestTransactionQuery());
epriestley's avatar
epriestley committed
60

61
    $monogram = $task->getMonogram();
62
    $crumbs = $this->buildApplicationCrumbs()
63 64
      ->addTextCrumb($monogram)
      ->setBorder(true);
65 66

    $header = $this->buildHeaderView($task);
67
    $details = $this->buildPropertyView($task, $field_list, $edges, $handles);
68
    $description = $this->buildDescriptionView($task);
69
    $curtain = $this->buildCurtain($task, $edit_engine);
Chad Little's avatar
Chad Little committed
70

71 72
    $title = pht('%s %s', $monogram, $task->getTitle());

73
    $comment_view = $edit_engine
74
      ->buildEditEngineCommentView($task);
Chad Little's avatar
Chad Little committed
75

76 77 78
    $timeline->setQuoteRef($monogram);
    $comment_view->setTransactionTimeline($timeline);

79 80
    $related_tabs = array();
    $graph_menu = null;
81

82
    $graph_limit = 100;
83 84 85
    $task_graph = id(new ManiphestTaskGraph())
      ->setViewer($viewer)
      ->setSeedPHID($task->getPHID())
86
      ->setLimit($graph_limit)
87 88
      ->loadGraph();
    if (!$task_graph->isEmpty()) {
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
      $parent_type = ManiphestTaskDependedOnByTaskEdgeType::EDGECONST;
      $subtask_type = ManiphestTaskDependsOnTaskEdgeType::EDGECONST;
      $parent_map = $task_graph->getEdges($parent_type);
      $subtask_map = $task_graph->getEdges($subtask_type);
      $parent_list = idx($parent_map, $task->getPHID(), array());
      $subtask_list = idx($subtask_map, $task->getPHID(), array());
      $has_parents = (bool)$parent_list;
      $has_subtasks = (bool)$subtask_list;

      $search_text = pht('Search...');

      // First, get a count of direct parent tasks and subtasks. If there
      // are too many of these, we just don't draw anything. You can use
      // the search button to browse tasks with the search UI instead.
      $direct_count = count($parent_list) + count($subtask_list);

      if ($direct_count > $graph_limit) {
106
        $message = pht(
107 108 109 110
          'Task graph too large to display (this task is directly connected '.
          'to more than %s other tasks). Use %s to explore connected tasks.',
          $graph_limit,
          phutil_tag('strong', array(), $search_text));
111 112 113 114
        $message = phutil_tag('em', array(), $message);
        $graph_table = id(new PHUIPropertyListView())
          ->addTextContent($message);
      } else {
115 116 117 118 119
        // If there aren't too many direct tasks, but there are too many total
        // tasks, we'll only render directly connected tasks.
        if ($task_graph->isOverLimit()) {
          $task_graph->setRenderOnlyAdjacentNodes(true);
        }
120 121 122
        $graph_table = $task_graph->newGraphTable();
      }

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
      $parents_uri = urisprintf(
        '/?subtaskIDs=%d#R',
        $task->getID());
      $parents_uri = $this->getApplicationURI($parents_uri);

      $subtasks_uri = urisprintf(
        '/?parentIDs=%d#R',
        $task->getID());
      $subtasks_uri = $this->getApplicationURI($subtasks_uri);

      $dropdown_menu = id(new PhabricatorActionListView())
        ->setViewer($viewer)
        ->addAction(
          id(new PhabricatorActionView())
            ->setHref($parents_uri)
            ->setName(pht('Search Parent Tasks'))
            ->setDisabled(!$has_parents)
            ->setIcon('fa-chevron-circle-up'))
        ->addAction(
          id(new PhabricatorActionView())
            ->setHref($subtasks_uri)
            ->setName(pht('Search Subtasks'))
            ->setDisabled(!$has_subtasks)
            ->setIcon('fa-chevron-circle-down'));

      $graph_menu = id(new PHUIButtonView())
        ->setTag('a')
        ->setIcon('fa-search')
151
        ->setText($search_text)
152 153
        ->setDropdownMenu($dropdown_menu);

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
      $related_tabs[] = id(new PHUITabView())
        ->setName(pht('Task Graph'))
        ->setKey('graph')
        ->appendChild($graph_table);
    }

    $related_tabs[] = $this->newMocksTab($task, $query);
    $related_tabs[] = $this->newMentionsTab($task, $query);

    $tab_view = null;

    $related_tabs = array_filter($related_tabs);
    if ($related_tabs) {
      $tab_group = new PHUITabGroupView();
      foreach ($related_tabs as $tab) {
        $tab_group->addTab($tab);
      }

      $related_header = id(new PHUIHeaderView())
        ->setHeader(pht('Related Objects'));
174

175 176 177 178 179 180 181 182
      if ($graph_menu) {
        $related_header->addActionLink($graph_menu);
      }

      $tab_view = id(new PHUIObjectBoxView())
        ->setHeader($related_header)
        ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
        ->addTabGroup($tab_group);
183 184
    }

185 186 187 188 189 190 191 192 193 194 195 196 197
    $view = id(new PHUITwoColumnView())
      ->setHeader($header)
      ->setCurtain($curtain)
      ->setMainColumn(
        array(
          $tab_view,
          $timeline,
          $comment_view,
        ))
      ->addPropertySection(pht('Description'), $description)
      ->addPropertySection(pht('Details'), $details);


Chad Little's avatar
Chad Little committed
198 199 200 201 202 203 204
    return $this->newPage()
      ->setTitle($title)
      ->setCrumbs($crumbs)
      ->setPageObjectPHIDs(
        array(
          $task->getPHID(),
        ))
205
      ->appendChild($view);
206

epriestley's avatar
epriestley committed
207
  }
208

209
  private function buildHeaderView(ManiphestTask $task) {
Chad Little's avatar
Chad Little committed
210
    $view = id(new PHUIHeaderView())
211 212 213
      ->setHeader($task->getTitle())
      ->setUser($this->getRequest()->getUser())
      ->setPolicyObject($task);
214

215 216 217 218
    $priority_name = ManiphestTaskPriority::getTaskPriorityName(
      $task->getPriority());
    $priority_color = ManiphestTaskPriority::getTaskPriorityColor(
      $task->getPriority());
Chad Little's avatar
Chad Little committed
219

220 221 222
    $status = $task->getStatus();
    $status_name = ManiphestTaskStatus::renderFullDescription(
      $status, $priority_name, $priority_color);
Chad Little's avatar
Chad Little committed
223
    $view->addProperty(PHUIHeaderView::PROPERTY_STATUS, $status_name);
224

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    $view->setHeaderIcon(ManiphestTaskStatus::getStatusIcon(
      $task->getStatus()).' '.$priority_color);

    if (ManiphestTaskPoints::getIsEnabled()) {
      $points = $task->getPoints();
      if ($points !== null) {
        $points_name = pht('%s %s',
          $task->getPoints(),
          ManiphestTaskPoints::getPointsLabel());
        $tag = id(new PHUITagView())
          ->setName($points_name)
          ->setShade('blue')
          ->setType(PHUITagView::TYPE_SHADE);

        $view->addTag($tag);
      }
    }

243 244 245 246
    return $view;
  }


247 248 249
  private function buildCurtain(
    ManiphestTask $task,
    PhabricatorEditEngine $edit_engine) {
epriestley's avatar
epriestley committed
250
    $viewer = $this->getViewer();
251 252 253 254

    $id = $task->getID();
    $phid = $task->getPHID();

255 256 257 258 259
    $can_edit = PhabricatorPolicyFilter::hasCapability(
      $viewer,
      $task,
      PhabricatorPolicyCapability::CAN_EDIT);

epriestley's avatar
epriestley committed
260
    $curtain = $this->newCurtainView($task);
261

epriestley's avatar
epriestley committed
262
    $curtain->addAction(
263 264
      id(new PhabricatorActionView())
        ->setName(pht('Edit Task'))
265
        ->setIcon('fa-pencil')
266
        ->setHref($this->getApplicationURI("/task/edit/{$id}/"))
267 268
        ->setDisabled(!$can_edit)
        ->setWorkflow(!$can_edit));
269

270
    $edit_config = $edit_engine->loadDefaultEditConfiguration();
271
    $can_create = (bool)$edit_config;
272 273 274 275

    $can_reassign = $edit_engine->hasEditAccessToTransaction(
      ManiphestTransaction::TYPE_OWNER);

276 277
    if ($can_create) {
      $form_key = $edit_config->getIdentifier();
278 279 280 281
      $edit_uri = id(new PhutilURI("/task/edit/form/{$form_key}/"))
        ->setQueryParam('parent', $id)
        ->setQueryParam('template', $id)
        ->setQueryParam('status', ManiphestTaskStatus::getDefaultStatus());
282 283 284 285
      $edit_uri = $this->getApplicationURI($edit_uri);
    } else {
      // TODO: This will usually give us a somewhat-reasonable error page, but
      // could be a bit cleaner.
286
      $edit_uri = "/task/edit/{$id}/";
287 288 289
      $edit_uri = $this->getApplicationURI($edit_uri);
    }

290
    $subtask_item = id(new PhabricatorActionView())
291 292 293 294 295 296
      ->setName(pht('Create Subtask'))
      ->setHref($edit_uri)
      ->setIcon('fa-level-down')
      ->setDisabled(!$can_create)
      ->setWorkflow(!$can_create);

297 298 299 300
    $relationship_list = PhabricatorObjectRelationshipList::newForObject(
      $viewer,
      $task);

301
    $submenu_actions = array(
302
      $subtask_item,
303 304 305 306 307 308 309 310 311 312 313
      ManiphestTaskHasParentRelationship::RELATIONSHIPKEY,
      ManiphestTaskHasSubtaskRelationship::RELATIONSHIPKEY,
      ManiphestTaskMergeInRelationship::RELATIONSHIPKEY,
      ManiphestTaskCloseAsDuplicateRelationship::RELATIONSHIPKEY,
    );

    $task_submenu = $relationship_list->newActionSubmenu($submenu_actions)
      ->setName(pht('Edit Related Tasks...'))
      ->setIcon('fa-anchor');

    $curtain->addAction($task_submenu);
epriestley's avatar
epriestley committed
314

315 316 317 318 319
    $relationship_submenu = $relationship_list->newActionMenu();
    if ($relationship_submenu) {
      $curtain->addAction($relationship_submenu);
    }

epriestley's avatar
epriestley committed
320
    $owner_phid = $task->getOwnerPHID();
Chad Little's avatar
Chad Little committed
321 322 323
    $author_phid = $task->getAuthorPHID();
    $handles = $viewer->loadHandles(array($owner_phid, $author_phid));

epriestley's avatar
epriestley committed
324
    if ($owner_phid) {
Chad Little's avatar
Chad Little committed
325 326 327 328 329 330 331 332
      $image_uri = $handles[$owner_phid]->getImageURI();
      $image_href = $handles[$owner_phid]->getURI();
      $owner = $viewer->renderHandle($owner_phid)->render();
      $content = phutil_tag('strong', array(), $owner);
      $assigned_to = id(new PHUIHeadThingView())
        ->setImage($image_uri)
        ->setImageHref($image_href)
        ->setContent($content);
epriestley's avatar
epriestley committed
333 334 335 336 337 338 339 340
    } else {
      $assigned_to = phutil_tag('em', array(), pht('None'));
    }

    $curtain->newPanel()
      ->setHeaderText(pht('Assigned To'))
      ->appendChild($assigned_to);

Chad Little's avatar
Chad Little committed
341 342 343 344
    $author_uri = $handles[$author_phid]->getImageURI();
    $author_href = $handles[$author_phid]->getURI();
    $author = $viewer->renderHandle($author_phid)->render();
    $content = phutil_tag('strong', array(), $author);
345 346
    $date = phabricator_date($task->getDateCreated(), $viewer);
    $content = pht('%s, %s', $content, $date);
Chad Little's avatar
Chad Little committed
347 348 349 350
    $authored_by = id(new PHUIHeadThingView())
      ->setImage($author_uri)
      ->setImageHref($author_href)
      ->setContent($content);
epriestley's avatar
epriestley committed
351 352

    $curtain->newPanel()
Chad Little's avatar
Chad Little committed
353 354
      ->setHeaderText(pht('Authored By'))
      ->appendChild($authored_by);
epriestley's avatar
epriestley committed
355 356

    return $curtain;
357 358 359 360
  }

  private function buildPropertyView(
    ManiphestTask $task,
361
    PhabricatorCustomFieldList $field_list,
362
    array $edges,
363
    $handles) {
364 365

    $viewer = $this->getRequest()->getUser();
Chad Little's avatar
Chad Little committed
366
    $view = id(new PHUIPropertyListView())
367
      ->setUser($viewer);
epriestley's avatar
epriestley committed
368

369 370 371 372 373
    $source = $task->getOriginalEmailSource();
    if ($source) {
      $subject = '[T'.$task->getID().'] '.$task->getTitle();
      $view->addProperty(
        pht('From Email'),
374
        phutil_tag(
375 376
          'a',
          array(
Joshua Spence's avatar
Joshua Spence committed
377
            'href' => 'mailto:'.$source.'?subject='.$subject,
378
          ),
379
          $source));
380 381 382
    }

    $edge_types = array(
383 384
      ManiphestTaskHasRevisionEdgeType::EDGECONST
        => pht('Differential Revisions'),
385 386
    );

387 388 389
    $revisions_commits = array();

    $commit_phids = array_keys(
390
      $edges[ManiphestTaskHasCommitEdgeType::EDGECONST]);
391
    if ($commit_phids) {
392
      $commit_drev = DiffusionCommitHasRevisionEdgeType::EDGECONST;
393 394 395 396
      $drev_edges = id(new PhabricatorEdgeQuery())
        ->withSourcePHIDs($commit_phids)
        ->withEdgeTypes(array($commit_drev))
        ->execute();
397

398
      foreach ($commit_phids as $phid) {
399
        $revisions_commits[$phid] = $handles->renderHandle($phid)
400 401
          ->setShowHovercard(true)
          ->setShowStateIcon(true);
402
        $revision_phid = key($drev_edges[$phid][$commit_drev]);
403
        $revision_handle = $handles->getHandleIfExists($revision_phid);
404
        if ($revision_handle) {
405
          $task_drev = ManiphestTaskHasRevisionEdgeType::EDGECONST;
406
          unset($edges[$task_drev][$revision_phid]);
407 408
          $revisions_commits[$phid] = hsprintf(
            '%s / %s',
409
            $revision_handle->renderHovercardLink($revision_handle->getName()),
410 411 412 413 414
            $revisions_commits[$phid]);
        }
      }
    }

415
    foreach ($edge_types as $edge_type => $edge_name) {
416 417
      if (!$edges[$edge_type]) {
        continue;
418
      }
419 420 421 422 423 424 425

      $edge_handles = $viewer->loadHandles(array_keys($edges[$edge_type]));

      $edge_list = $edge_handles->renderList()
        ->setShowStateIcons(true);

      $view->addProperty($edge_name, $edge_list);
426 427
    }

428 429 430 431 432 433
    if ($revisions_commits) {
      $view->addProperty(
        pht('Commits'),
        phutil_implode_html(phutil_tag('br'), $revisions_commits));
    }

434 435 436 437 438
    $field_list->appendFieldsToPropertyList(
      $task,
      $viewer,
      $view);

439 440 441 442 443 444 445
    if ($view->hasAnyProperties()) {
      return $view;
    }

    return null;
  }

446 447
  private function buildDescriptionView(ManiphestTask $task) {
    $viewer = $this->getViewer();
Chad Little's avatar
Chad Little committed
448 449

    $section = null;
450 451 452

    $description = $task->getDescription();
    if (strlen($description)) {
Chad Little's avatar
Chad Little committed
453 454
      $section = new PHUIPropertyListView();
      $section->addTextContent(
455
        phutil_tag(
456 457 458 459
          'div',
          array(
            'class' => 'phabricator-remarkup',
          ),
460 461
          id(new PHUIRemarkupView($viewer, $description))
            ->setContextObject($task)));
462
    }
463

Chad Little's avatar
Chad Little committed
464
    return $section;
465 466
  }

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
  private function newMocksTab(
    ManiphestTask $task,
    PhabricatorEdgeQuery $edge_query) {

    $mock_type = ManiphestTaskHasMockEdgeType::EDGECONST;
    $mock_phids = $edge_query->getDestinationPHIDs(array(), array($mock_type));
    if (!$mock_phids) {
      return null;
    }

    $viewer = $this->getViewer();
    $handles = $viewer->loadHandles($mock_phids);

    // TODO: It would be nice to render this as pinboard-style thumbnails,
    // similar to "{M123}", instead of a list of links.

    $view = id(new PHUIPropertyListView())
      ->addProperty(pht('Mocks'), $handles->renderList());

    return id(new PHUITabView())
      ->setName(pht('Mocks'))
      ->setKey('mocks')
      ->appendChild($view);
  }

  private function newMentionsTab(
    ManiphestTask $task,
    PhabricatorEdgeQuery $edge_query) {

    $in_type = PhabricatorObjectMentionedByObjectEdgeType::EDGECONST;
    $out_type = PhabricatorObjectMentionsObjectEdgeType::EDGECONST;

    $in_phids = $edge_query->getDestinationPHIDs(array(), array($in_type));
    $out_phids = $edge_query->getDestinationPHIDs(array(), array($out_type));

    // Filter out any mentioned users from the list. These are not generally
    // very interesting to show in a relationship summary since they usually
    // end up as subscribers anyway.

    $user_type = PhabricatorPeopleUserPHIDType::TYPECONST;
    foreach ($out_phids as $key => $out_phid) {
      if (phid_get_type($out_phid) == $user_type) {
        unset($out_phids[$key]);
      }
    }

    if (!$in_phids && !$out_phids) {
      return null;
    }

    $viewer = $this->getViewer();
518 519 520 521 522 523 524 525 526 527
    $in_handles = $viewer->loadHandles($in_phids);
    $out_handles = $viewer->loadHandles($out_phids);

    $in_handles = $this->getCompleteHandles($in_handles);
    $out_handles = $this->getCompleteHandles($out_handles);

    if (!count($in_handles) && !count($out_handles)) {
      return null;
    }

528 529
    $view = new PHUIPropertyListView();

530
    if (count($in_handles)) {
531 532 533
      $view->addProperty(pht('Mentioned In'), $in_handles->renderList());
    }

534
    if (count($out_handles)) {
535 536 537 538 539 540 541 542 543
      $view->addProperty(pht('Mentioned Here'), $out_handles->renderList());
    }

    return id(new PHUITabView())
      ->setName(pht('Mentions'))
      ->setKey('mentions')
      ->appendChild($view);
  }

544 545 546 547 548 549 550 551 552 553 554 555 556 557
  private function getCompleteHandles(PhabricatorHandleList $handles) {
    $phids = array();

    foreach ($handles as $phid => $handle) {
      if (!$handle->isComplete()) {
        continue;
      }
      $phids[] = $phid;
    }

    return $handles->newSublist($phids);
  }


epriestley's avatar
epriestley committed
558
}