|
| 1 | +<?php |
| 2 | + |
| 3 | +final class PhabricatorFileAttachmentQuery |
| 4 | + extends PhabricatorCursorPagedPolicyAwareQuery { |
| 5 | + |
| 6 | + private $objectPHIDs; |
| 7 | + private $needFiles; |
| 8 | + |
| 9 | + public function withObjectPHIDs(array $object_phids) { |
| 10 | + $this->objectPHIDs = $object_phids; |
| 11 | + return $this; |
| 12 | + } |
| 13 | + |
| 14 | + public function needFiles($need) { |
| 15 | + $this->needFiles = $need; |
| 16 | + return $this; |
| 17 | + } |
| 18 | + |
| 19 | + public function newResultObject() { |
| 20 | + return new PhabricatorFileAttachment(); |
| 21 | + } |
| 22 | + |
| 23 | + protected function loadPage() { |
| 24 | + return $this->loadStandardPage($this->newResultObject()); |
| 25 | + } |
| 26 | + |
| 27 | + protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { |
| 28 | + $where = parent::buildWhereClauseParts($conn); |
| 29 | + |
| 30 | + if ($this->objectPHIDs !== null) { |
| 31 | + $where[] = qsprintf( |
| 32 | + $conn, |
| 33 | + 'attachments.objectPHID IN (%Ls)', |
| 34 | + $this->objectPHIDs); |
| 35 | + } |
| 36 | + |
| 37 | + return $where; |
| 38 | + } |
| 39 | + |
| 40 | + protected function willFilterPage(array $attachments) { |
| 41 | + $viewer = $this->getViewer(); |
| 42 | + $object_phids = array(); |
| 43 | + |
| 44 | + foreach ($attachments as $attachment) { |
| 45 | + $object_phid = $attachment->getObjectPHID(); |
| 46 | + $object_phids[$object_phid] = $object_phid; |
| 47 | + } |
| 48 | + |
| 49 | + if ($object_phids) { |
| 50 | + $objects = id(new PhabricatorObjectQuery()) |
| 51 | + ->setViewer($viewer) |
| 52 | + ->setParentQuery($this) |
| 53 | + ->withPHIDs($object_phids) |
| 54 | + ->execute(); |
| 55 | + $objects = mpull($objects, null, 'getPHID'); |
| 56 | + } else { |
| 57 | + $objects = array(); |
| 58 | + } |
| 59 | + |
| 60 | + foreach ($attachments as $key => $attachment) { |
| 61 | + $object_phid = $attachment->getObjectPHID(); |
| 62 | + $object = idx($objects, $object_phid); |
| 63 | + |
| 64 | + if (!$object) { |
| 65 | + $this->didRejectResult($attachment); |
| 66 | + unset($attachments[$key]); |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + $attachment->attachObject($object); |
| 71 | + } |
| 72 | + |
| 73 | + if ($this->needFiles) { |
| 74 | + $file_phids = array(); |
| 75 | + foreach ($attachments as $attachment) { |
| 76 | + $file_phid = $attachment->getFilePHID(); |
| 77 | + $file_phids[$file_phid] = $file_phid; |
| 78 | + } |
| 79 | + |
| 80 | + if ($file_phids) { |
| 81 | + $files = id(new PhabricatorFileQuery()) |
| 82 | + ->setViewer($viewer) |
| 83 | + ->setParentQuery($this) |
| 84 | + ->withPHIDs($file_phids) |
| 85 | + ->execute(); |
| 86 | + $files = mpull($files, null, 'getPHID'); |
| 87 | + } else { |
| 88 | + $files = array(); |
| 89 | + } |
| 90 | + |
| 91 | + foreach ($attachments as $key => $attachment) { |
| 92 | + $file_phid = $attachment->getFilePHID(); |
| 93 | + $file = idx($files, $file_phid); |
| 94 | + |
| 95 | + $attachment->attachFile($file); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return $attachments; |
| 100 | + } |
| 101 | + |
| 102 | + protected function getPrimaryTableAlias() { |
| 103 | + return 'attachments'; |
| 104 | + } |
| 105 | + |
| 106 | + public function getQueryApplicationClass() { |
| 107 | + return 'PhabricatorFilesApplication'; |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments