[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/releeph/query/ -> ReleephProductQuery.php (source)

   1  <?php
   2  
   3  final class ReleephProductQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    private $active;
   7    private $ids;
   8    private $phids;
   9    private $repositoryPHIDs;
  10  
  11    private $needArcanistProjects;
  12  
  13    private $order    = 'order-id';
  14    const ORDER_ID    = 'order-id';
  15    const ORDER_NAME  = 'order-name';
  16  
  17    public function withActive($active) {
  18      $this->active = $active;
  19      return $this;
  20    }
  21  
  22    public function setOrder($order) {
  23      $this->order = $order;
  24      return $this;
  25    }
  26  
  27    public function withIDs(array $ids) {
  28      $this->ids = $ids;
  29      return $this;
  30    }
  31  
  32    public function withPHIDs(array $phids) {
  33      $this->phids = $phids;
  34      return $this;
  35    }
  36  
  37    public function withRepositoryPHIDs(array $repository_phids) {
  38      $this->repositoryPHIDs = $repository_phids;
  39      return $this;
  40    }
  41  
  42    public function needArcanistProjects($need) {
  43      $this->needArcanistProjects = $need;
  44      return $this;
  45    }
  46  
  47    public function loadPage() {
  48      $table = new ReleephProject();
  49      $conn_r = $table->establishConnection('r');
  50  
  51      $rows = queryfx_all(
  52        $conn_r,
  53        'SELECT * FROM %T %Q %Q %Q',
  54        $table->getTableName(),
  55        $this->buildWhereClause($conn_r),
  56        $this->buildOrderClause($conn_r),
  57        $this->buildLimitClause($conn_r));
  58  
  59      return $table->loadAllFromArray($rows);
  60    }
  61  
  62    public function willFilterPage(array $projects) {
  63      assert_instances_of($projects, 'ReleephProject');
  64  
  65      $repository_phids = mpull($projects, 'getRepositoryPHID');
  66  
  67      $repositories = id(new PhabricatorRepositoryQuery())
  68        ->setViewer($this->getViewer())
  69        ->withPHIDs($repository_phids)
  70        ->execute();
  71      $repositories = mpull($repositories, null, 'getPHID');
  72  
  73      foreach ($projects as $key => $project) {
  74        $repo = idx($repositories, $project->getRepositoryPHID());
  75        if (!$repo) {
  76          unset($projects[$key]);
  77          continue;
  78        }
  79        $project->attachRepository($repo);
  80      }
  81  
  82      return $projects;
  83    }
  84  
  85    public function didFilterPage(array $products) {
  86      if ($this->needArcanistProjects) {
  87        $project_ids = array_filter(mpull($products, 'getArcanistProjectID'));
  88        if ($project_ids) {
  89          $projects = id(new PhabricatorRepositoryArcanistProject())
  90            ->loadAllWhere('id IN (%Ld)', $project_ids);
  91          $projects = mpull($projects, null, 'getID');
  92        } else {
  93          $projects = array();
  94        }
  95  
  96        foreach ($products as $product) {
  97          $project_id = $product->getArcanistProjectID();
  98          $project = idx($projects, $project_id);
  99          $product->attachArcanistProject($project);
 100        }
 101      }
 102  
 103      return $products;
 104    }
 105  
 106    private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
 107      $where = array();
 108  
 109      if ($this->active !== null) {
 110        $where[] = qsprintf(
 111          $conn_r,
 112          'isActive = %d',
 113          (int)$this->active);
 114      }
 115  
 116      if ($this->ids !== null) {
 117        $where[] = qsprintf(
 118          $conn_r,
 119          'id IN (%Ls)',
 120          $this->ids);
 121      }
 122  
 123      if ($this->phids !== null) {
 124        $where[] = qsprintf(
 125          $conn_r,
 126          'phid IN (%Ls)',
 127          $this->phids);
 128      }
 129  
 130      if ($this->repositoryPHIDs !== null) {
 131        $where[] = qsprintf(
 132          $conn_r,
 133          'repositoryPHID IN (%Ls)',
 134          $this->repositoryPHIDs);
 135      }
 136  
 137      $where[] = $this->buildPagingClause($conn_r);
 138  
 139      return $this->formatWhereClause($where);
 140    }
 141  
 142    protected function getReversePaging() {
 143      switch ($this->order) {
 144        case self::ORDER_NAME:
 145          return true;
 146      }
 147      return parent::getReversePaging();
 148    }
 149  
 150    protected function getPagingValue($result) {
 151      switch ($this->order) {
 152        case self::ORDER_NAME:
 153          return $result->getName();
 154      }
 155      return parent::getPagingValue();
 156    }
 157  
 158    protected function getPagingColumn() {
 159      switch ($this->order) {
 160        case self::ORDER_NAME:
 161          return 'name';
 162        case self::ORDER_ID:
 163          return parent::getPagingColumn();
 164        default:
 165          throw new Exception("Uknown order '{$this->order}'!");
 166      }
 167    }
 168  
 169    public function getQueryApplicationClass() {
 170      return 'PhabricatorReleephApplication';
 171    }
 172  
 173  }


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