[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/transactions/query/ -> PhabricatorApplicationTransactionQuery.php (source)

   1  <?php
   2  
   3  abstract class PhabricatorApplicationTransactionQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    private $phids;
   7    private $objectPHIDs;
   8    private $authorPHIDs;
   9    private $transactionTypes;
  10  
  11    private $needComments = true;
  12    private $needHandles  = true;
  13  
  14    abstract public function getTemplateApplicationTransaction();
  15  
  16    protected function buildMoreWhereClauses(AphrontDatabaseConnection $conn_r) {
  17      return array();
  18    }
  19  
  20    protected function getReversePaging() {
  21      return true;
  22    }
  23  
  24    public function withPHIDs(array $phids) {
  25      $this->phids = $phids;
  26      return $this;
  27    }
  28  
  29    public function withObjectPHIDs(array $object_phids) {
  30      $this->objectPHIDs = $object_phids;
  31      return $this;
  32    }
  33  
  34    public function withAuthorPHIDs(array $author_phids) {
  35      $this->authorPHIDs = $author_phids;
  36      return $this;
  37    }
  38  
  39    public function withTransactionTypes(array $transaction_types) {
  40      $this->transactionTypes = $transaction_types;
  41      return $this;
  42    }
  43  
  44    public function needComments($need) {
  45      $this->needComments = $need;
  46      return $this;
  47    }
  48  
  49    public function needHandles($need) {
  50      $this->needHandles = $need;
  51      return $this;
  52    }
  53  
  54    protected function loadPage() {
  55      $table = $this->getTemplateApplicationTransaction();
  56      $conn_r = $table->establishConnection('r');
  57  
  58      $data = queryfx_all(
  59        $conn_r,
  60        'SELECT * FROM %T x %Q %Q %Q',
  61        $table->getTableName(),
  62        $this->buildWhereClause($conn_r),
  63        $this->buildOrderClause($conn_r),
  64        $this->buildLimitClause($conn_r));
  65  
  66      $xactions = $table->loadAllFromArray($data);
  67  
  68      foreach ($xactions as $xaction) {
  69        $xaction->attachViewer($this->getViewer());
  70      }
  71  
  72      if ($this->needComments) {
  73        $comment_phids = array_filter(mpull($xactions, 'getCommentPHID'));
  74  
  75        $comments = array();
  76        if ($comment_phids) {
  77          $comments = id(new PhabricatorApplicationTransactionCommentQuery())
  78            ->setTemplate($table->getApplicationTransactionCommentObject())
  79            ->setViewer($this->getViewer())
  80            ->withPHIDs($comment_phids)
  81            ->execute();
  82          $comments = mpull($comments, null, 'getPHID');
  83        }
  84  
  85        foreach ($xactions as $xaction) {
  86          if ($xaction->getCommentPHID()) {
  87            $comment = idx($comments, $xaction->getCommentPHID());
  88            if ($comment) {
  89              $xaction->attachComment($comment);
  90            }
  91          }
  92        }
  93      } else {
  94        foreach ($xactions as $xaction) {
  95          $xaction->setCommentNotLoaded(true);
  96        }
  97      }
  98  
  99      return $xactions;
 100    }
 101  
 102    protected function willFilterPage(array $xactions) {
 103      $object_phids = array_keys(mpull($xactions, null, 'getObjectPHID'));
 104  
 105      $objects = id(new PhabricatorObjectQuery())
 106        ->setViewer($this->getViewer())
 107        ->setParentQuery($this)
 108        ->withPHIDs($object_phids)
 109        ->execute();
 110  
 111      foreach ($xactions as $key => $xaction) {
 112        $object_phid = $xaction->getObjectPHID();
 113        if (empty($objects[$object_phid])) {
 114          unset($xactions[$key]);
 115          continue;
 116        }
 117        $xaction->attachObject($objects[$object_phid]);
 118      }
 119  
 120      // NOTE: We have to do this after loading objects, because the objects
 121      // may help determine which handles are required (for example, in the case
 122      // of custom fields).
 123  
 124      if ($this->needHandles) {
 125        $phids = array();
 126        foreach ($xactions as $xaction) {
 127          $phids[$xaction->getPHID()] = $xaction->getRequiredHandlePHIDs();
 128        }
 129        $handles = array();
 130        $merged = array_mergev($phids);
 131        if ($merged) {
 132          $handles = id(new PhabricatorHandleQuery())
 133            ->setViewer($this->getViewer())
 134            ->withPHIDs($merged)
 135            ->execute();
 136        }
 137        foreach ($xactions as $xaction) {
 138          $xaction->setHandles(
 139            array_select_keys(
 140              $handles,
 141              $phids[$xaction->getPHID()]));
 142        }
 143      }
 144  
 145      return $xactions;
 146    }
 147  
 148    private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
 149      $where = array();
 150  
 151      if ($this->phids) {
 152        $where[] = qsprintf(
 153          $conn_r,
 154          'phid IN (%Ls)',
 155          $this->phids);
 156      }
 157  
 158      if ($this->objectPHIDs) {
 159        $where[] = qsprintf(
 160          $conn_r,
 161          'objectPHID IN (%Ls)',
 162          $this->objectPHIDs);
 163      }
 164  
 165      if ($this->authorPHIDs) {
 166        $where[] = qsprintf(
 167          $conn_r,
 168          'authorPHID IN (%Ls)',
 169          $this->authorPHIDs);
 170      }
 171  
 172      if ($this->transactionTypes) {
 173        $where[] = qsprintf(
 174          $conn_r,
 175          'transactionType IN (%Ls)',
 176          $this->transactionTypes);
 177      }
 178  
 179      foreach ($this->buildMoreWhereClauses($conn_r) as $clause) {
 180        $where[] = $clause;
 181      }
 182  
 183      $where[] = $this->buildPagingClause($conn_r);
 184  
 185      return $this->formatWhereClause($where);
 186    }
 187  
 188  
 189    public function getQueryApplicationClass() {
 190      // TODO: Sort this out?
 191      return null;
 192    }
 193  
 194  }


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