From 3838e14ad895f3aa2e80798607fed14cd97ad2f2 Mon Sep 17 00:00:00 2001 From: Lauri-Henrik Jalonen Date: Fri, 25 Jan 2013 13:27:49 -0800 Subject: [PATCH] Enhanced PholioMockQuery Summary: Images and cover files can now be attached calling need functions for PholioMockQuery. Mock list will show cover files for mocks. MockView uses new feature to show the first image Test Plan: Verified that images are shown. Reviewers: epriestley CC: aran, Korvin Maniphest Tasks: T2364 Differential Revision: https://secure.phabricator.com/D4644 --- .../controller/PholioMockListController.php | 6 +- .../controller/PholioMockViewController.php | 2 + .../pholio/query/PholioMockQuery.php | 60 ++++++++++++++++++- .../pholio/storage/PholioImage.php | 13 ++++ .../pholio/storage/PholioMock.php | 28 +++++++++ .../pholio/view/PholioMockImagesView.php | 8 +-- 6 files changed, 107 insertions(+), 10 deletions(-) diff --git a/src/applications/pholio/controller/PholioMockListController.php b/src/applications/pholio/controller/PholioMockListController.php index 943eab946a..5c456ec53d 100644 --- a/src/applications/pholio/controller/PholioMockListController.php +++ b/src/applications/pholio/controller/PholioMockListController.php @@ -16,7 +16,8 @@ final class PholioMockListController extends PholioController { $user = $request->getUser(); $query = id(new PholioMockQuery()) - ->setViewer($user); + ->setViewer($user) + ->needCoverFiles(true); $nav = $this->buildSideNav(); $filter = $nav->selectFilter('view/'.$this->view, 'view/all'); @@ -38,7 +39,8 @@ final class PholioMockListController extends PholioController { $board->addItem( id(new PhabricatorPinboardItemView()) ->setHeader($mock->getName()) - ->setURI('/M'.$mock->getID())); + ->setURI('/M'.$mock->getID()) + ->setImageURI($mock->getCoverFile()->getThumb160x120URI())); } $header = id(new PhabricatorHeaderView()) diff --git a/src/applications/pholio/controller/PholioMockViewController.php b/src/applications/pholio/controller/PholioMockViewController.php index b15149ac34..0bf0022fe1 100644 --- a/src/applications/pholio/controller/PholioMockViewController.php +++ b/src/applications/pholio/controller/PholioMockViewController.php @@ -18,6 +18,8 @@ final class PholioMockViewController extends PholioController { $mock = id(new PholioMockQuery()) ->setViewer($user) ->withIDs(array($this->id)) + ->needImages(true) + ->needCoverFiles(true) ->executeOne(); if (!$mock) { diff --git a/src/applications/pholio/query/PholioMockQuery.php b/src/applications/pholio/query/PholioMockQuery.php index 5aa6a5911b..bbd56e9f5f 100644 --- a/src/applications/pholio/query/PholioMockQuery.php +++ b/src/applications/pholio/query/PholioMockQuery.php @@ -10,6 +10,9 @@ final class PholioMockQuery private $phids; private $authorPHIDs; + private $needCoverFiles; + private $needImages; + public function withIDs(array $ids) { $this->ids = $ids; return $this; @@ -37,7 +40,17 @@ final class PholioMockQuery $this->buildOrderClause($conn_r), $this->buildLimitClause($conn_r)); - return $table->loadAllFromArray($data); + $mocks = $table->loadAllFromArray($data); + + if ($mocks && $this->needImages) { + $this->loadImages($mocks); + } + if ($mocks && $this->needCoverFiles) { + $this->loadCoverFiles($mocks); + } + + + return $mocks; } private function buildWhereClause(AphrontDatabaseConnection $conn_r) { @@ -69,4 +82,49 @@ final class PholioMockQuery return $this->formatWhereClause($where); } + public function needCoverFiles($need_cover_files) { + $this->needCoverFiles = $need_cover_files; + return $this; + } + + public function needImages($need_images) { + $this->needImages = $need_images; + return $this; + } + + public function loadImages(array $mocks) { + assert_instances_of($mocks, 'PholioMock'); + + $mock_ids = mpull($mocks, 'getID'); + $all_images = id(new PholioImage())->loadAllWhere( + 'mockID IN (%Ld)', + $mock_ids); + + $file_phids = mpull($all_images, 'getFilePHID'); + $all_files = mpull(id(new PhabricatorFile())->loadAllWhere( + 'phid IN (%Ls)', + $file_phids), null, 'getPHID'); + + foreach ($all_images as $image) { + $image->attachFile($all_files[$image->getFilePHID()]); + } + + $image_groups = mgroup($all_images, 'getMockID'); + + foreach ($mocks as $mock) { + $mock->attachImages($image_groups[$mock->getID()]); + } + } + + public function loadCoverFiles(array $mocks) { + assert_instances_of($mocks, 'PholioMock'); + $cover_file_phids = mpull($mocks, 'getCoverPHID'); + $cover_files = mpull(id(new PhabricatorFile())->loadAllWhere( + 'phid IN (%Ls)', + $cover_file_phids), null, 'getPHID'); + + foreach ($mocks as $mock) { + $mock->attachCoverFile($cover_files[$mock->getCoverPHID()]); + } + } } diff --git a/src/applications/pholio/storage/PholioImage.php b/src/applications/pholio/storage/PholioImage.php index 5525ff45a5..62e1862be1 100644 --- a/src/applications/pholio/storage/PholioImage.php +++ b/src/applications/pholio/storage/PholioImage.php @@ -14,6 +14,7 @@ final class PholioImage extends PholioDAO protected $description = ''; protected $sequence; + private $file; /* -( PhabricatorMarkupInterface )----------------------------------------- */ @@ -39,4 +40,16 @@ final class PholioImage extends PholioDAO return (bool)$this->getID(); } + public function attachFile(PhabricatorFile $file) { + $this->file = $file; + return $this; + } + + public function getFile() { + if ($this->file === null) { + throw new Exception("Call attachFile() before getFile()!"); + } + return $this->file; + } + } diff --git a/src/applications/pholio/storage/PholioMock.php b/src/applications/pholio/storage/PholioMock.php index 177771c95e..986c9685da 100644 --- a/src/applications/pholio/storage/PholioMock.php +++ b/src/applications/pholio/storage/PholioMock.php @@ -20,6 +20,9 @@ final class PholioMock extends PholioDAO protected $coverPHID; protected $mailKey; + private $images; + private $coverFile; + public function getConfiguration() { return array( self::CONFIG_AUX_PHID => true, @@ -37,6 +40,31 @@ final class PholioMock extends PholioDAO return parent::save(); } + public function attachImages(array $images) { + assert_instances_of($images, 'PholioImage'); + $this->images = $images; + return $this; + } + + public function getImages() { + if ($this->images === null) { + throw new Exception("Call attachImages() before getImages()!"); + } + return $this->images; + } + + public function attachCoverFile(PhabricatorFile $file) { + $this->coverFile = $file; + return $this; + } + + public function getCoverFile() { + if ($this->coverFile === null) { + throw new Exception("Call attachCoverFile() before getCoverFile()!"); + } + return $this->coverFile; + } + /* -( PhabricatorSubscribableInterface Implementation )-------------------- */ diff --git a/src/applications/pholio/view/PholioMockImagesView.php b/src/applications/pholio/view/PholioMockImagesView.php index 47383f5918..103b751a6b 100644 --- a/src/applications/pholio/view/PholioMockImagesView.php +++ b/src/applications/pholio/view/PholioMockImagesView.php @@ -13,13 +13,7 @@ final class PholioMockImagesView extends AphrontView { throw new Exception("Call setMock() before render()!"); } - $image = id(new PholioImage())->loadOneWhere( - "mockid=%d", - $this->mock->getID()); - - $file = id(new PhabricatorFile())->loadOneWhere( - "phid=%s", - $image->getFilePHID()); + $file = head($this->mock->getImages())->getFile(); $image_tag = phutil_render_tag( 'img',