From 6f9683dc904a7a24b7313e764f0380d111c5aa35 Mon Sep 17 00:00:00 2001 From: Ricky Elrod Date: Sun, 16 Dec 2012 12:02:34 -0800 Subject: [PATCH] Add paste samples to list view. Summary: This adds a "body" field to `PhabricatorObjectItemView` which lets you optionally add more information to the list view. It then uses this new field to show samples of pastes in the paste list view. Test Plan: {F27147} {F27148} Reviewers: epriestley CC: aran, Korvin Differential Revision: https://secure.phabricator.com/D4188 --- .../controller/PhabricatorPasteController.php | 27 +++++++++++++++++++ .../PhabricatorPasteListController.php | 17 +++++++++++- .../PhabricatorPasteViewController.php | 23 ---------------- src/view/layout/PhabricatorObjectItemView.php | 2 +- 4 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/applications/paste/controller/PhabricatorPasteController.php b/src/applications/paste/controller/PhabricatorPasteController.php index 4063282aa0..da6624078f 100644 --- a/src/applications/paste/controller/PhabricatorPasteController.php +++ b/src/applications/paste/controller/PhabricatorPasteController.php @@ -39,4 +39,31 @@ abstract class PhabricatorPasteController extends PhabricatorController { return $crumbs; } + public function buildSourceCodeView( + PhabricatorPaste $paste, + PhabricatorFile $file, + $max_lines = null) { + + $language = $paste->getLanguage(); + $source = $file->loadFileData(); + + if (empty($language)) { + $source = PhabricatorSyntaxHighlighter::highlightWithFilename( + $paste->getTitle(), + $source); + } else { + $source = PhabricatorSyntaxHighlighter::highlightWithLanguage( + $language, + $source); + } + + $lines = explode("\n", $source); + + if ($max_lines) { + $lines = array_slice($lines, 0, $max_lines); + } + + return id(new PhabricatorSourceCodeView()) + ->setLines($lines); + } } diff --git a/src/applications/paste/controller/PhabricatorPasteListController.php b/src/applications/paste/controller/PhabricatorPasteListController.php index 0d86292a3e..84167245a4 100644 --- a/src/applications/paste/controller/PhabricatorPasteListController.php +++ b/src/applications/paste/controller/PhabricatorPasteListController.php @@ -76,17 +76,32 @@ final class PhabricatorPasteListController extends PhabricatorPasteController { $this->loadHandles(mpull($pastes, 'getAuthorPHID')); + $file_phids = mpull($pastes, 'getFilePHID'); + $files = array(); + if ($file_phids) { + $files = id(new PhabricatorFile())->loadAllWhere( + "phid IN (%Ls)", + $file_phids); + } + $files_map = mpull($files, null, 'getPHID'); + $list = new PhabricatorObjectItemListView(); $list->setViewer($user); foreach ($pastes as $paste) { $created = phabricator_date($paste->getDateCreated(), $user); $author = $this->getHandle($paste->getAuthorPHID())->renderLink(); + $file_phid = $paste->getFilePHID(); + $file = idx($files_map, $file_phid); + + $source_code = $this->buildSourceCodeView($paste, $file, 5)->render(); + $item = id(new PhabricatorObjectItemView()) ->setHeader($paste->getFullName()) ->setHref('/P'.$paste->getID()) ->setObject($paste) - ->addAttribute(pht('Created %s by %s', $created, $author)); + ->addAttribute(pht('Created %s by %s', $created, $author)) + ->appendChild($source_code); $list->addItem($item); } diff --git a/src/applications/paste/controller/PhabricatorPasteViewController.php b/src/applications/paste/controller/PhabricatorPasteViewController.php index be4083e050..9c9537b1ef 100644 --- a/src/applications/paste/controller/PhabricatorPasteViewController.php +++ b/src/applications/paste/controller/PhabricatorPasteViewController.php @@ -152,27 +152,4 @@ final class PhabricatorPasteViewController extends PhabricatorPasteController { return $properties; } - private function buildSourceCodeView( - PhabricatorPaste $paste, - PhabricatorFile $file) { - - $language = $paste->getLanguage(); - $source = $file->loadFileData(); - - if (empty($language)) { - $source = PhabricatorSyntaxHighlighter::highlightWithFilename( - $paste->getTitle(), - $source); - } else { - $source = PhabricatorSyntaxHighlighter::highlightWithLanguage( - $language, - $source); - } - - $lines = explode("\n", $source); - - return id(new PhabricatorSourceCodeView()) - ->setLines($lines); - } - } diff --git a/src/view/layout/PhabricatorObjectItemView.php b/src/view/layout/PhabricatorObjectItemView.php index 76b768b793..e0d674e4c4 100644 --- a/src/view/layout/PhabricatorObjectItemView.php +++ b/src/view/layout/PhabricatorObjectItemView.php @@ -144,7 +144,7 @@ final class PhabricatorObjectItemView extends AphrontView { array( 'class' => implode(' ', $classes), ), - $icons.$header.$attrs); + $icons.$header.$attrs.$this->renderChildren()); } }