[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/legalpad/query/ -> LegalpadDocumentQuery.php (source)

   1  <?php
   2  
   3  final class LegalpadDocumentQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    private $ids;
   7    private $phids;
   8    private $creatorPHIDs;
   9    private $contributorPHIDs;
  10    private $signerPHIDs;
  11    private $dateCreatedAfter;
  12    private $dateCreatedBefore;
  13  
  14    private $needDocumentBodies;
  15    private $needContributors;
  16    private $needSignatures;
  17    private $needViewerSignatures;
  18  
  19    public function withIDs(array $ids) {
  20      $this->ids = $ids;
  21      return $this;
  22    }
  23  
  24    public function withPHIDs(array $phids) {
  25      $this->phids = $phids;
  26      return $this;
  27    }
  28  
  29    public function withCreatorPHIDs(array $phids) {
  30      $this->creatorPHIDs = $phids;
  31      return $this;
  32    }
  33  
  34    public function withContributorPHIDs(array $phids) {
  35      $this->contributorPHIDs = $phids;
  36      return $this;
  37    }
  38  
  39    public function withSignerPHIDs(array $phids) {
  40      $this->signerPHIDs = $phids;
  41      return $this;
  42    }
  43  
  44    public function needDocumentBodies($need_bodies) {
  45      $this->needDocumentBodies = $need_bodies;
  46      return $this;
  47    }
  48  
  49    public function needContributors($need_contributors) {
  50      $this->needContributors = $need_contributors;
  51      return $this;
  52    }
  53  
  54    public function needSignatures($need_signatures) {
  55      $this->needSignatures = $need_signatures;
  56      return $this;
  57    }
  58  
  59    public function withDateCreatedBefore($date_created_before) {
  60      $this->dateCreatedBefore = $date_created_before;
  61      return $this;
  62    }
  63  
  64    public function withDateCreatedAfter($date_created_after) {
  65      $this->dateCreatedAfter = $date_created_after;
  66      return $this;
  67    }
  68  
  69    public function needViewerSignatures($need) {
  70      $this->needViewerSignatures = $need;
  71      return $this;
  72    }
  73  
  74    protected function loadPage() {
  75      $table = new LegalpadDocument();
  76      $conn_r = $table->establishConnection('r');
  77  
  78      $data = queryfx_all(
  79        $conn_r,
  80        'SELECT d.* FROM %T d %Q %Q %Q %Q %Q',
  81        $table->getTableName(),
  82        $this->buildJoinClause($conn_r),
  83        $this->buildWhereClause($conn_r),
  84        $this->buildGroupClause($conn_r),
  85        $this->buildOrderClause($conn_r),
  86        $this->buildLimitClause($conn_r));
  87  
  88      $documents = $table->loadAllFromArray($data);
  89  
  90      return $documents;
  91    }
  92  
  93    protected function willFilterPage(array $documents) {
  94      if ($this->needDocumentBodies) {
  95        $documents = $this->loadDocumentBodies($documents);
  96      }
  97  
  98      if ($this->needContributors) {
  99        $documents = $this->loadContributors($documents);
 100      }
 101  
 102      if ($this->needSignatures) {
 103        $documents = $this->loadSignatures($documents);
 104      }
 105  
 106      if ($this->needViewerSignatures) {
 107        if ($documents) {
 108          if ($this->getViewer()->getPHID()) {
 109            $signatures = id(new LegalpadDocumentSignatureQuery())
 110              ->setViewer($this->getViewer())
 111              ->withSignerPHIDs(array($this->getViewer()->getPHID()))
 112              ->withDocumentPHIDs(mpull($documents, 'getPHID'))
 113              ->execute();
 114            $signatures = mpull($signatures, null, 'getDocumentPHID');
 115          } else {
 116            $signatures = array();
 117          }
 118  
 119          foreach ($documents as $document) {
 120            $signature = idx($signatures, $document->getPHID());
 121            $document->attachUserSignature(
 122              $this->getViewer()->getPHID(),
 123              $signature);
 124          }
 125        }
 126      }
 127  
 128      return $documents;
 129    }
 130  
 131    private function buildJoinClause($conn_r) {
 132      $joins = array();
 133  
 134      if ($this->contributorPHIDs !== null) {
 135        $joins[] = qsprintf(
 136          $conn_r,
 137          'JOIN edge contributor ON contributor.src = d.phid
 138            AND contributor.type = %d',
 139          PhabricatorEdgeConfig::TYPE_OBJECT_HAS_CONTRIBUTOR);
 140      }
 141  
 142      if ($this->signerPHIDs !== null) {
 143        $joins[] = qsprintf(
 144          $conn_r,
 145          'JOIN %T signer ON signer.documentPHID = d.phid
 146            AND signer.signerPHID IN (%Ls)',
 147          id(new LegalpadDocumentSignature())->getTableName(),
 148          $this->signerPHIDs);
 149      }
 150  
 151      return implode(' ', $joins);
 152    }
 153  
 154    private function buildGroupClause(AphrontDatabaseConnection $conn_r) {
 155      if ($this->contributorPHIDs || $this->signerPHIDs) {
 156        return 'GROUP BY d.id';
 157      } else {
 158        return '';
 159      }
 160    }
 161  
 162    protected function buildWhereClause($conn_r) {
 163      $where = array();
 164  
 165      if ($this->ids !== null) {
 166        $where[] = qsprintf(
 167          $conn_r,
 168          'd.id IN (%Ld)',
 169          $this->ids);
 170      }
 171  
 172      if ($this->phids !== null) {
 173        $where[] = qsprintf(
 174          $conn_r,
 175          'd.phid IN (%Ls)',
 176          $this->phids);
 177      }
 178  
 179      if ($this->creatorPHIDs !== null) {
 180        $where[] = qsprintf(
 181          $conn_r,
 182          'd.creatorPHID IN (%Ls)',
 183          $this->creatorPHIDs);
 184      }
 185  
 186      if ($this->dateCreatedAfter !== null) {
 187        $where[] = qsprintf(
 188          $conn_r,
 189          'd.dateCreated >= %d',
 190          $this->dateCreatedAfter);
 191      }
 192  
 193      if ($this->dateCreatedBefore !== null) {
 194        $where[] = qsprintf(
 195          $conn_r,
 196          'd.dateCreated <= %d',
 197          $this->dateCreatedBefore);
 198      }
 199  
 200      if ($this->contributorPHIDs !== null) {
 201        $where[] = qsprintf(
 202          $conn_r,
 203          'contributor.dst IN (%Ls)',
 204          $this->contributorPHIDs);
 205      }
 206  
 207      $where[] = $this->buildPagingClause($conn_r);
 208  
 209      return $this->formatWhereClause($where);
 210    }
 211  
 212    private function loadDocumentBodies(array $documents) {
 213      $body_phids = mpull($documents, 'getDocumentBodyPHID');
 214      $bodies = id(new LegalpadDocumentBody())->loadAllWhere(
 215        'phid IN (%Ls)',
 216        $body_phids);
 217      $bodies = mpull($bodies, null, 'getPHID');
 218  
 219      foreach ($documents as $document) {
 220        $body = idx($bodies, $document->getDocumentBodyPHID());
 221        $document->attachDocumentBody($body);
 222      }
 223  
 224      return $documents;
 225    }
 226  
 227    private function loadContributors(array $documents) {
 228      $document_map = mpull($documents, null, 'getPHID');
 229      $edge_type = PhabricatorEdgeConfig::TYPE_OBJECT_HAS_CONTRIBUTOR;
 230      $contributor_data = id(new PhabricatorEdgeQuery())
 231        ->withSourcePHIDs(array_keys($document_map))
 232        ->withEdgeTypes(array($edge_type))
 233        ->execute();
 234  
 235      foreach ($document_map as $document_phid => $document) {
 236        $data = $contributor_data[$document_phid];
 237        $contributors = array_keys(idx($data, $edge_type, array()));
 238        $document->attachContributors($contributors);
 239      }
 240  
 241      return $documents;
 242    }
 243  
 244    private function loadSignatures(array $documents) {
 245      $document_map = mpull($documents, null, 'getPHID');
 246  
 247      $signatures = id(new LegalpadDocumentSignatureQuery())
 248        ->setViewer($this->getViewer())
 249        ->withDocumentPHIDs(array_keys($document_map))
 250        ->execute();
 251      $signatures = mgroup($signatures, 'getDocumentPHID');
 252  
 253      foreach ($documents as $document) {
 254        $sigs = idx($signatures, $document->getPHID(), array());
 255        $document->attachSignatures($sigs);
 256      }
 257  
 258      return $documents;
 259    }
 260  
 261    public function getQueryApplicationClass() {
 262      return 'PhabricatorLegalpadApplication';
 263    }
 264  
 265  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1