Changeset View
Changeset View
Standalone View
Standalone View
src/view/phui/PHUITimelineView.php
Show First 20 Lines • Show All 139 Lines • ▼ Show 20 Lines | if ($more && $this->getPager()) { | ||||
)); | )); | ||||
if ($show) { | if ($show) { | ||||
$events[] = $spacer; | $events[] = $spacer; | ||||
} | } | ||||
} | } | ||||
if ($show) { | if ($show) { | ||||
$this->prepareBadgeData($show); | |||||
$events[] = phutil_implode_html($spacer, $show); | $events[] = phutil_implode_html($spacer, $show); | ||||
} | } | ||||
if ($events) { | if ($events) { | ||||
if ($this->shouldAddSpacers) { | if ($this->shouldAddSpacers) { | ||||
$events = array($spacer, $events, $spacer); | $events = array($spacer, $events, $spacer); | ||||
} | } | ||||
} else { | } else { | ||||
Show All 22 Lines | return phutil_tag( | ||||
'div', | 'div', | ||||
array( | array( | ||||
'class' => 'phui-timeline-event-view '. | 'class' => 'phui-timeline-event-view '. | ||||
'the-worlds-end', | 'the-worlds-end', | ||||
), | ), | ||||
''); | ''); | ||||
} | } | ||||
private function prepareBadgeData(array $events) { | |||||
assert_instances_of($events, 'PHUITimelineEventView'); | |||||
$viewer = $this->getUser(); | |||||
$can_use_badges = PhabricatorApplication::isClassInstalledForViewer( | |||||
'PhabricatorBadgesApplication', | |||||
$viewer); | |||||
if (!$can_use_badges) { | |||||
return; | |||||
} | |||||
$user_phid_type = PhabricatorPeopleUserPHIDType::TYPECONST; | |||||
$badge_edge_type = PhabricatorRecipientHasBadgeEdgeType::EDGECONST; | |||||
$user_phids = array(); | |||||
foreach ($events as $key => $event) { | |||||
if (!$event->hasChildren()) { | |||||
// This is a minor event, so we don't have space to show badges. | |||||
unset($events[$key]); | |||||
continue; | |||||
} | |||||
$author_phid = $event->getAuthorPHID(); | |||||
if (!$author_phid) { | |||||
unset($events[$key]); | |||||
continue; | |||||
} | |||||
if (phid_get_type($author_phid) != $user_phid_type) { | |||||
// This is likely an application actor, like "Herald" or "Harbormaster". | |||||
// They can't have badges. | |||||
unset($events[$key]); | |||||
continue; | |||||
} | |||||
$user_phids[$author_phid] = $author_phid; | |||||
} | |||||
if (!$user_phids) { | |||||
return; | |||||
} | |||||
$edges = id(new PhabricatorEdgeQuery()) | |||||
->withSourcePHIDs($user_phids) | |||||
->withEdgeTypes(array($badge_edge_type)); | |||||
$edges->execute(); | |||||
$badge_phids = $edges->getDestinationPHIDs(); | |||||
if (!$badge_phids) { | |||||
return; | |||||
} | |||||
$all_badges = id(new PhabricatorBadgesQuery()) | |||||
->setViewer($viewer) | |||||
->withPHIDs($badge_phids) | |||||
->execute(); | |||||
$all_badges = mpull($all_badges, null, 'getPHID'); | |||||
foreach ($events as $event) { | |||||
$author_phid = $event->getAuthorPHID(); | |||||
$event_phids = $edges->getDestinationPHIDs(array($author_phid)); | |||||
$badges = array_select_keys($all_badges, $event_phids); | |||||
// TODO: Pick the "best" badges in some smart way. For now, just pick | |||||
// the first two. | |||||
$badges = array_slice($badges, 0, 2); | |||||
foreach ($badges as $badge) { | |||||
$badge_view = id(new PHUIBadgeMiniView()) | |||||
->setIcon($badge->getIcon()) | |||||
->setQuality($badge->getQuality()) | |||||
->setHeader($badge->getName()) | |||||
->setHref('/badges/view/'.$badge->getID()); | |||||
$event->addBadge($badge_view); | |||||
} | |||||
} | |||||
} | |||||
} | } |