Path: blob/master/src/applications/legalpad/query/LegalpadDocumentQuery.php
13450 views
<?php12final class LegalpadDocumentQuery3extends PhabricatorCursorPagedPolicyAwareQuery {45private $ids;6private $phids;7private $creatorPHIDs;8private $contributorPHIDs;9private $signerPHIDs;10private $dateCreatedAfter;11private $dateCreatedBefore;12private $signatureRequired;1314private $needDocumentBodies;15private $needContributors;16private $needSignatures;17private $needViewerSignatures;1819public function withIDs(array $ids) {20$this->ids = $ids;21return $this;22}2324public function withPHIDs(array $phids) {25$this->phids = $phids;26return $this;27}2829public function withCreatorPHIDs(array $phids) {30$this->creatorPHIDs = $phids;31return $this;32}3334public function withContributorPHIDs(array $phids) {35$this->contributorPHIDs = $phids;36return $this;37}3839public function withSignerPHIDs(array $phids) {40$this->signerPHIDs = $phids;41return $this;42}4344public function withSignatureRequired($bool) {45$this->signatureRequired = $bool;46return $this;47}4849public function needDocumentBodies($need_bodies) {50$this->needDocumentBodies = $need_bodies;51return $this;52}5354public function needContributors($need_contributors) {55$this->needContributors = $need_contributors;56return $this;57}5859public function needSignatures($need_signatures) {60$this->needSignatures = $need_signatures;61return $this;62}6364public function withDateCreatedBefore($date_created_before) {65$this->dateCreatedBefore = $date_created_before;66return $this;67}6869public function withDateCreatedAfter($date_created_after) {70$this->dateCreatedAfter = $date_created_after;71return $this;72}7374public function needViewerSignatures($need) {75$this->needViewerSignatures = $need;76return $this;77}7879public function newResultObject() {80return new LegalpadDocument();81}8283protected function willFilterPage(array $documents) {84if ($this->needDocumentBodies) {85$documents = $this->loadDocumentBodies($documents);86}8788if ($this->needContributors) {89$documents = $this->loadContributors($documents);90}9192if ($this->needSignatures) {93$documents = $this->loadSignatures($documents);94}9596if ($this->needViewerSignatures) {97if ($documents) {98if ($this->getViewer()->getPHID()) {99$signatures = id(new LegalpadDocumentSignatureQuery())100->setViewer($this->getViewer())101->withSignerPHIDs(array($this->getViewer()->getPHID()))102->withDocumentPHIDs(mpull($documents, 'getPHID'))103->execute();104$signatures = mpull($signatures, null, 'getDocumentPHID');105} else {106$signatures = array();107}108109foreach ($documents as $document) {110$signature = idx($signatures, $document->getPHID());111$document->attachUserSignature(112$this->getViewer()->getPHID(),113$signature);114}115}116}117118return $documents;119}120121protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {122$joins = parent::buildJoinClauseParts($conn);123124if ($this->contributorPHIDs !== null) {125$joins[] = qsprintf(126$conn,127'JOIN edge contributor ON contributor.src = d.phid128AND contributor.type = %d',129PhabricatorObjectHasContributorEdgeType::EDGECONST);130}131132if ($this->signerPHIDs !== null) {133$joins[] = qsprintf(134$conn,135'JOIN %T signer ON signer.documentPHID = d.phid136AND signer.signerPHID IN (%Ls)',137id(new LegalpadDocumentSignature())->getTableName(),138$this->signerPHIDs);139}140141return $joins;142}143144protected function shouldGroupQueryResultRows() {145if ($this->contributorPHIDs) {146return true;147}148149if ($this->signerPHIDs) {150return true;151}152153return parent::shouldGroupQueryResultRows();154}155156protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {157$where = parent::buildWhereClauseParts($conn);158159if ($this->ids !== null) {160$where[] = qsprintf(161$conn,162'd.id IN (%Ld)',163$this->ids);164}165166if ($this->phids !== null) {167$where[] = qsprintf(168$conn,169'd.phid IN (%Ls)',170$this->phids);171}172173if ($this->creatorPHIDs !== null) {174$where[] = qsprintf(175$conn,176'd.creatorPHID IN (%Ls)',177$this->creatorPHIDs);178}179180if ($this->dateCreatedAfter !== null) {181$where[] = qsprintf(182$conn,183'd.dateCreated >= %d',184$this->dateCreatedAfter);185}186187if ($this->dateCreatedBefore !== null) {188$where[] = qsprintf(189$conn,190'd.dateCreated <= %d',191$this->dateCreatedBefore);192}193194if ($this->contributorPHIDs !== null) {195$where[] = qsprintf(196$conn,197'contributor.dst IN (%Ls)',198$this->contributorPHIDs);199}200201if ($this->signatureRequired !== null) {202$where[] = qsprintf(203$conn,204'd.requireSignature = %d',205$this->signatureRequired);206}207208return $where;209}210211private function loadDocumentBodies(array $documents) {212$body_phids = mpull($documents, 'getDocumentBodyPHID');213$bodies = id(new LegalpadDocumentBody())->loadAllWhere(214'phid IN (%Ls)',215$body_phids);216$bodies = mpull($bodies, null, 'getPHID');217218foreach ($documents as $document) {219$body = idx($bodies, $document->getDocumentBodyPHID());220$document->attachDocumentBody($body);221}222223return $documents;224}225226private function loadContributors(array $documents) {227$document_map = mpull($documents, null, 'getPHID');228$edge_type = PhabricatorObjectHasContributorEdgeType::EDGECONST;229$contributor_data = id(new PhabricatorEdgeQuery())230->withSourcePHIDs(array_keys($document_map))231->withEdgeTypes(array($edge_type))232->execute();233234foreach ($document_map as $document_phid => $document) {235$data = $contributor_data[$document_phid];236$contributors = array_keys(idx($data, $edge_type, array()));237$document->attachContributors($contributors);238}239240return $documents;241}242243private function loadSignatures(array $documents) {244$document_map = mpull($documents, null, 'getPHID');245246$signatures = id(new LegalpadDocumentSignatureQuery())247->setViewer($this->getViewer())248->withDocumentPHIDs(array_keys($document_map))249->execute();250$signatures = mgroup($signatures, 'getDocumentPHID');251252foreach ($documents as $document) {253$sigs = idx($signatures, $document->getPHID(), array());254$document->attachSignatures($sigs);255}256257return $documents;258}259260public function getQueryApplicationClass() {261return 'PhabricatorLegalpadApplication';262}263264protected function getPrimaryTableAlias() {265return 'd';266}267268}269270271