Path: blob/master/src/applications/flag/query/PhabricatorFlagQuery.php
12262 views
<?php12final class PhabricatorFlagQuery3extends PhabricatorCursorPagedPolicyAwareQuery {45const GROUP_COLOR = 'color';6const GROUP_NONE = 'none';78private $ids;9private $ownerPHIDs;10private $types;11private $objectPHIDs;12private $colors;13private $groupBy = self::GROUP_NONE;1415private $needHandles;16private $needObjects;1718public function withIDs(array $ids) {19$this->ids = $ids;20return $this;21}2223public function withOwnerPHIDs(array $owner_phids) {24$this->ownerPHIDs = $owner_phids;25return $this;26}2728public function withTypes(array $types) {29$this->types = $types;30return $this;31}3233public function withObjectPHIDs(array $object_phids) {34$this->objectPHIDs = $object_phids;35return $this;36}3738public function withColors(array $colors) {39$this->colors = $colors;40return $this;41}4243/**44* NOTE: this is done in PHP and not in MySQL, which means its inappropriate45* for large datasets. Pragmatically, this is fine for user flags which are46* typically well under 100 flags per user.47*/48public function setGroupBy($group) {49$this->groupBy = $group;50return $this;51}5253public function needHandles($need) {54$this->needHandles = $need;55return $this;56}5758public function needObjects($need) {59$this->needObjects = $need;60return $this;61}6263public static function loadUserFlag(PhabricatorUser $user, $object_phid) {64// Specifying the type in the query allows us to use a key.65return id(new PhabricatorFlagQuery())66->setViewer($user)67->withOwnerPHIDs(array($user->getPHID()))68->withTypes(array(phid_get_type($object_phid)))69->withObjectPHIDs(array($object_phid))70->executeOne();71}7273protected function loadPage() {74$table = new PhabricatorFlag();75$conn_r = $table->establishConnection('r');7677$data = queryfx_all(78$conn_r,79'SELECT * FROM %T flag %Q %Q %Q',80$table->getTableName(),81$this->buildWhereClause($conn_r),82$this->buildOrderClause($conn_r),83$this->buildLimitClause($conn_r));8485return $table->loadAllFromArray($data);86}8788protected function willFilterPage(array $flags) {89if ($this->needObjects) {90$objects = id(new PhabricatorObjectQuery())91->setViewer($this->getViewer())92->withPHIDs(mpull($flags, 'getObjectPHID'))93->execute();94$objects = mpull($objects, null, 'getPHID');95foreach ($flags as $key => $flag) {96$object = idx($objects, $flag->getObjectPHID());97if ($object) {98$flags[$key]->attachObject($object);99} else {100unset($flags[$key]);101}102}103}104105if ($this->needHandles) {106$handles = id(new PhabricatorHandleQuery())107->setViewer($this->getViewer())108->withPHIDs(mpull($flags, 'getObjectPHID'))109->execute();110111foreach ($flags as $flag) {112$flag->attachHandle($handles[$flag->getObjectPHID()]);113}114}115116switch ($this->groupBy) {117case self::GROUP_COLOR:118$flags = msort($flags, 'getColor');119break;120case self::GROUP_NONE:121break;122default:123throw new Exception(124pht('Unknown groupBy parameter: %s', $this->groupBy));125break;126}127128return $flags;129}130131protected function buildWhereClause(AphrontDatabaseConnection $conn) {132$where = array();133134if ($this->ids !== null) {135$where[] = qsprintf(136$conn,137'flag.id IN (%Ld)',138$this->ids);139}140141if ($this->ownerPHIDs) {142$where[] = qsprintf(143$conn,144'flag.ownerPHID IN (%Ls)',145$this->ownerPHIDs);146}147148if ($this->types) {149$where[] = qsprintf(150$conn,151'flag.type IN (%Ls)',152$this->types);153}154155if ($this->objectPHIDs) {156$where[] = qsprintf(157$conn,158'flag.objectPHID IN (%Ls)',159$this->objectPHIDs);160}161162if ($this->colors) {163$where[] = qsprintf(164$conn,165'flag.color IN (%Ld)',166$this->colors);167}168169$where[] = $this->buildPagingClause($conn);170171return $this->formatWhereClause($conn, $where);172}173174public function getQueryApplicationClass() {175return 'PhabricatorFlagsApplication';176}177178}179180181