[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/phortune/query/ -> PhortuneAccountQuery.php (source)

   1  <?php
   2  
   3  final class PhortuneAccountQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    private $ids;
   7    private $phids;
   8    private $memberPHIDs;
   9  
  10    public static function loadAccountsForUser(
  11      PhabricatorUser $user,
  12      PhabricatorContentSource $content_source) {
  13  
  14      $accounts = id(new PhortuneAccountQuery())
  15        ->setViewer($user)
  16        ->withMemberPHIDs(array($user->getPHID()))
  17        ->execute();
  18  
  19      if (!$accounts) {
  20        $accounts = array(
  21          PhortuneAccount::createNewAccount($user, $content_source),
  22        );
  23      }
  24  
  25      $accounts = mpull($accounts, null, 'getPHID');
  26  
  27      return $accounts;
  28    }
  29  
  30    public function withIDs(array $ids) {
  31      $this->ids = $ids;
  32      return $this;
  33    }
  34  
  35    public function withPHIDs(array $phids) {
  36      $this->phids = $phids;
  37      return $this;
  38    }
  39  
  40    public function withMemberPHIDs(array $phids) {
  41      $this->memberPHIDs = $phids;
  42      return $this;
  43    }
  44  
  45    protected function loadPage() {
  46      $table = new PhortuneAccount();
  47      $conn = $table->establishConnection('r');
  48  
  49      $rows = queryfx_all(
  50        $conn,
  51        'SELECT a.* FROM %T a %Q %Q %Q %Q',
  52        $table->getTableName(),
  53        $this->buildJoinClause($conn),
  54        $this->buildWhereClause($conn),
  55        $this->buildOrderClause($conn),
  56        $this->buildLimitClause($conn));
  57  
  58      return $table->loadAllFromArray($rows);
  59    }
  60  
  61    protected function willFilterPage(array $accounts) {
  62      $query = id(new PhabricatorEdgeQuery())
  63        ->withSourcePHIDs(mpull($accounts, 'getPHID'))
  64        ->withEdgeTypes(array(PhortuneAccountHasMemberEdgeType::EDGECONST));
  65      $query->execute();
  66  
  67      foreach ($accounts as $account) {
  68        $member_phids = $query->getDestinationPHIDs(array($account->getPHID()));
  69        $member_phids = array_reverse($member_phids);
  70        $account->attachMemberPHIDs($member_phids);
  71      }
  72  
  73      return $accounts;
  74    }
  75  
  76    private function buildWhereClause(AphrontDatabaseConnection $conn) {
  77      $where = array();
  78  
  79      $where[] = $this->buildPagingClause($conn);
  80  
  81      if ($this->ids) {
  82        $where[] = qsprintf(
  83          $conn,
  84          'a.id IN (%Ld)',
  85          $this->ids);
  86      }
  87  
  88      if ($this->phids) {
  89        $where[] = qsprintf(
  90          $conn,
  91          'a.phid IN (%Ls)',
  92          $this->phids);
  93      }
  94  
  95      if ($this->memberPHIDs) {
  96        $where[] = qsprintf(
  97          $conn,
  98          'm.dst IN (%Ls)',
  99          $this->memberPHIDs);
 100      }
 101  
 102      return $this->formatWhereClause($where);
 103    }
 104  
 105    private function buildJoinClause(AphrontDatabaseConnection $conn) {
 106      $joins = array();
 107  
 108      if ($this->memberPHIDs) {
 109        $joins[] = qsprintf(
 110          $conn,
 111          'LEFT JOIN %T m ON a.phid = m.src AND m.type = %d',
 112          PhabricatorEdgeConfig::TABLE_NAME_EDGE,
 113          PhortuneAccountHasMemberEdgeType::EDGECONST);
 114      }
 115  
 116      return implode(' ', $joins);
 117    }
 118  
 119    public function getQueryApplicationClass() {
 120      return 'PhabricatorPhortuneApplication';
 121    }
 122  
 123  }


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