2012-08-10 19:44:04 +02:00
|
|
|
<?php
|
|
|
|
|
2012-12-12 03:03:16 +01:00
|
|
|
final class PonderQuestionQuery
|
|
|
|
extends PhabricatorCursorPagedPolicyAwareQuery {
|
2012-08-10 19:44:04 +02:00
|
|
|
|
2012-10-01 05:09:51 +02:00
|
|
|
const ORDER_CREATED = 'order-created';
|
|
|
|
const ORDER_HOTTEST = 'order-hottest';
|
2012-08-10 19:44:04 +02:00
|
|
|
|
2012-10-01 05:09:51 +02:00
|
|
|
private $ids;
|
|
|
|
private $phids;
|
|
|
|
private $authorPHIDs;
|
|
|
|
private $order = self::ORDER_CREATED;
|
2012-08-10 19:44:04 +02:00
|
|
|
|
2012-10-01 05:09:51 +02:00
|
|
|
public function withIDs(array $ids) {
|
|
|
|
$this->ids = $ids;
|
2012-08-10 19:44:04 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-10-01 05:09:51 +02:00
|
|
|
public function withPHIDs(array $phids) {
|
2012-08-10 19:44:04 +02:00
|
|
|
$this->phids = $phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-10-01 05:09:51 +02:00
|
|
|
public function withAuthorPHIDs(array $phids) {
|
|
|
|
$this->authorPHIDs = $phids;
|
2012-08-10 19:44:04 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-10-01 05:09:51 +02:00
|
|
|
public function setOrder($order) {
|
|
|
|
$this->order = $order;
|
2012-08-10 19:44:04 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function loadSingle($viewer, $id) {
|
|
|
|
if (!$viewer) {
|
|
|
|
throw new Exception("Must set viewer when calling loadSingle");
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx(id(new PonderQuestionQuery())
|
2012-12-12 03:03:16 +01:00
|
|
|
->setViewer($viewer)
|
2012-10-01 05:09:51 +02:00
|
|
|
->withIDs(array($id))
|
|
|
|
->execute(), $id);
|
2012-08-10 19:44:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function loadSingleByPHID($viewer, $phid) {
|
|
|
|
if (!$viewer) {
|
|
|
|
throw new Exception("Must set viewer when calling loadSingle");
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_shift(id(new PonderQuestionQuery())
|
2012-10-01 05:09:51 +02:00
|
|
|
->withPHIDs(array($phid))
|
2012-12-12 03:03:16 +01:00
|
|
|
->setViewer($viewer)
|
2012-08-10 19:44:04 +02:00
|
|
|
->execute());
|
|
|
|
}
|
|
|
|
|
2012-10-01 05:09:51 +02:00
|
|
|
private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
|
2012-08-10 19:44:04 +02:00
|
|
|
$where = array();
|
2012-10-01 05:09:51 +02:00
|
|
|
|
|
|
|
if ($this->ids) {
|
|
|
|
$where[] = qsprintf($conn_r, 'q.id IN (%Ld)', $this->ids);
|
2012-08-10 19:44:04 +02:00
|
|
|
}
|
2012-10-01 05:09:51 +02:00
|
|
|
|
2012-08-10 19:44:04 +02:00
|
|
|
if ($this->phids) {
|
2012-10-01 05:09:51 +02:00
|
|
|
$where[] = qsprintf($conn_r, 'q.phid IN (%Ls)', $this->phids);
|
2012-08-10 19:44:04 +02:00
|
|
|
}
|
2012-10-01 05:09:51 +02:00
|
|
|
|
|
|
|
if ($this->authorPHIDs) {
|
|
|
|
$where[] = qsprintf($conn_r, 'q.authorPHID IN (%Ls)', $this->authorPHIDs);
|
2012-08-10 19:44:04 +02:00
|
|
|
}
|
|
|
|
|
2012-12-12 03:03:16 +01:00
|
|
|
$where[] = $this->buildPagingClause($conn_r);
|
|
|
|
|
2012-10-01 05:09:51 +02:00
|
|
|
return $this->formatWhereClause($where);
|
2012-08-10 19:44:04 +02:00
|
|
|
}
|
|
|
|
|
2012-10-01 05:09:51 +02:00
|
|
|
private function buildOrderByClause(AphrontDatabaseConnection $conn_r) {
|
|
|
|
switch ($this->order) {
|
|
|
|
case self::ORDER_HOTTEST:
|
|
|
|
return qsprintf($conn_r, 'ORDER BY q.heat DESC, q.id DESC');
|
|
|
|
case self::ORDER_CREATED:
|
|
|
|
return qsprintf($conn_r, 'ORDER BY q.id DESC');
|
|
|
|
default:
|
|
|
|
throw new Exception("Unknown order '{$this->order}'!");
|
2012-08-10 19:44:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 20:28:02 +01:00
|
|
|
protected function loadPage() {
|
2012-08-10 19:44:04 +02:00
|
|
|
$question = new PonderQuestion();
|
|
|
|
$conn_r = $question->establishConnection('r');
|
|
|
|
|
|
|
|
$where = $this->buildWhereClause($conn_r);
|
|
|
|
$order_by = $this->buildOrderByClause($conn_r);
|
|
|
|
$limit = $this->buildLimitClause($conn_r);
|
|
|
|
|
|
|
|
return $question->loadAllFromArray(
|
|
|
|
queryfx_all(
|
|
|
|
$conn_r,
|
2012-10-01 05:09:51 +02:00
|
|
|
'SELECT q.* FROM %T q %Q %Q %Q',
|
|
|
|
$question->getTableName(),
|
2012-08-10 19:44:04 +02:00
|
|
|
$where,
|
|
|
|
$order_by,
|
|
|
|
$limit));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|