Generate file attachment transactions for explicit Remarkup attachments on common...
[phabricator.git] / src / applications / differential / view / DifferentialTransactionView.php
blob5fb8c1320cdbbbacc63d188d2c71133f149ec90a
1 <?php
3 final class DifferentialTransactionView
4 extends PhabricatorApplicationTransactionView {
6 private $changesets = array();
7 private $revision;
8 private $rightDiff;
9 private $leftDiff;
11 public function setLeftDiff(DifferentialDiff $left_diff) {
12 $this->leftDiff = $left_diff;
13 return $this;
16 public function getLeftDiff() {
17 return $this->leftDiff;
20 public function setRightDiff(DifferentialDiff $right_diff) {
21 $this->rightDiff = $right_diff;
22 return $this;
25 public function getRightDiff() {
26 return $this->rightDiff;
29 public function setRevision(DifferentialRevision $revision) {
30 $this->revision = $revision;
31 return $this;
34 public function getRevision() {
35 return $this->revision;
38 public function setChangesets(array $changesets) {
39 assert_instances_of($changesets, 'DifferentialChangeset');
40 $this->changesets = $changesets;
41 return $this;
44 public function getChangesets() {
45 return $this->changesets;
48 // TODO: There's a whole lot of code duplication between this and
49 // PholioTransactionView to handle inlines. Merge this into the core? Some of
50 // it can probably be shared, while other parts are trickier.
52 protected function shouldGroupTransactions(
53 PhabricatorApplicationTransaction $u,
54 PhabricatorApplicationTransaction $v) {
56 if ($u->getAuthorPHID() != $v->getAuthorPHID()) {
57 // Don't group transactions by different authors.
58 return false;
61 if (($v->getDateCreated() - $u->getDateCreated()) > 60) {
62 // Don't group if transactions that happened more than 60s apart.
63 return false;
66 switch ($u->getTransactionType()) {
67 case PhabricatorTransactions::TYPE_COMMENT:
68 case DifferentialTransaction::TYPE_INLINE:
69 break;
70 default:
71 return false;
74 switch ($v->getTransactionType()) {
75 case DifferentialTransaction::TYPE_INLINE:
76 return true;
79 return parent::shouldGroupTransactions($u, $v);
82 protected function renderTransactionContent(
83 PhabricatorApplicationTransaction $xaction) {
85 $out = array();
87 $type_inline = DifferentialTransaction::TYPE_INLINE;
89 $group = $xaction->getTransactionGroup();
90 if ($xaction->getTransactionType() == $type_inline) {
91 array_unshift($group, $xaction);
92 } else {
93 $out[] = parent::renderTransactionContent($xaction);
96 // If we're rendering a preview, we show the inline comments in a separate
97 // section underneath the main transaction preview, so we skip rendering
98 // them in the preview body.
99 if ($this->getIsPreview()) {
100 return $out;
103 if (!$group) {
104 return $out;
107 $inlines = array();
108 foreach ($group as $xaction) {
109 switch ($xaction->getTransactionType()) {
110 case DifferentialTransaction::TYPE_INLINE:
111 $inlines[] = $xaction;
112 break;
113 default:
114 throw new Exception(pht('Unknown grouped transaction type!'));
118 if ($inlines) {
119 $inline_view = new PhabricatorInlineSummaryView();
121 $changesets = $this->getChangesets();
123 $inline_groups = DifferentialTransactionComment::sortAndGroupInlines(
124 $inlines,
125 $changesets);
126 foreach ($inline_groups as $changeset_id => $group) {
127 $changeset = $changesets[$changeset_id];
128 $items = array();
129 foreach ($group as $inline) {
130 $comment = $inline->getComment();
131 $item = array(
132 'id' => $comment->getID(),
133 'line' => $comment->getLineNumber(),
134 'length' => $comment->getLineLength(),
135 'content' => parent::renderTransactionContent($inline),
138 $changeset_diff_id = $changeset->getDiffID();
139 if ($comment->getIsNewFile()) {
140 $visible_diff_id = $this->getRightDiff()->getID();
141 } else {
142 $visible_diff_id = $this->getLeftDiff()->getID();
145 // TODO: We still get one edge case wrong here, when we have a
146 // versus diff and the file didn't exist in the old version. The
147 // comment is visible because we show the left side of the target
148 // diff when there's no corresponding file in the versus diff, but
149 // we incorrectly link it off-page.
151 $is_visible = ($changeset_diff_id == $visible_diff_id);
152 if (!$is_visible) {
153 $revision_id = $this->getRevision()->getID();
154 $comment_id = $comment->getID();
155 $item['href'] =
156 '/D'.$revision_id.
157 '?id='.$changeset_diff_id.
158 '#inline-'.$comment_id;
159 $item['where'] = pht('(On Diff #%d)', $changeset_diff_id);
162 $items[] = $item;
164 $inline_view->addCommentGroup(
165 $changeset->getFilename(),
166 $items);
169 $out[] = $inline_view;
172 return $out;