3 final class PhabricatorFlagQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery
{
6 const GROUP_COLOR
= 'color';
7 const GROUP_NONE
= 'none';
14 private $groupBy = self
::GROUP_NONE
;
19 public function withIDs(array $ids) {
24 public function withOwnerPHIDs(array $owner_phids) {
25 $this->ownerPHIDs
= $owner_phids;
29 public function withTypes(array $types) {
30 $this->types
= $types;
34 public function withObjectPHIDs(array $object_phids) {
35 $this->objectPHIDs
= $object_phids;
39 public function withColors(array $colors) {
40 $this->colors
= $colors;
45 * NOTE: this is done in PHP and not in MySQL, which means its inappropriate
46 * for large datasets. Pragmatically, this is fine for user flags which are
47 * typically well under 100 flags per user.
49 public function setGroupBy($group) {
50 $this->groupBy
= $group;
54 public function needHandles($need) {
55 $this->needHandles
= $need;
59 public function needObjects($need) {
60 $this->needObjects
= $need;
64 public static function loadUserFlag(PhabricatorUser
$user, $object_phid) {
65 // Specifying the type in the query allows us to use a key.
66 return id(new PhabricatorFlagQuery())
68 ->withOwnerPHIDs(array($user->getPHID()))
69 ->withTypes(array(phid_get_type($object_phid)))
70 ->withObjectPHIDs(array($object_phid))
74 protected function loadPage() {
75 $table = new PhabricatorFlag();
76 $conn_r = $table->establishConnection('r');
80 'SELECT * FROM %T flag %Q %Q %Q',
81 $table->getTableName(),
82 $this->buildWhereClause($conn_r),
83 $this->buildOrderClause($conn_r),
84 $this->buildLimitClause($conn_r));
86 return $table->loadAllFromArray($data);
89 protected function willFilterPage(array $flags) {
90 if ($this->needObjects
) {
91 $objects = id(new PhabricatorObjectQuery())
92 ->setViewer($this->getViewer())
93 ->withPHIDs(mpull($flags, 'getObjectPHID'))
95 $objects = mpull($objects, null, 'getPHID');
96 foreach ($flags as $key => $flag) {
97 $object = idx($objects, $flag->getObjectPHID());
99 $flags[$key]->attachObject($object);
106 if ($this->needHandles
) {
107 $handles = id(new PhabricatorHandleQuery())
108 ->setViewer($this->getViewer())
109 ->withPHIDs(mpull($flags, 'getObjectPHID'))
112 foreach ($flags as $flag) {
113 $flag->attachHandle($handles[$flag->getObjectPHID()]);
117 switch ($this->groupBy
) {
118 case self
::GROUP_COLOR
:
119 $flags = msort($flags, 'getColor');
121 case self
::GROUP_NONE
:
125 pht('Unknown groupBy parameter: %s', $this->groupBy
));
132 protected function buildWhereClause(AphrontDatabaseConnection
$conn) {
135 if ($this->ids
!== null) {
142 if ($this->ownerPHIDs
) {
145 'flag.ownerPHID IN (%Ls)',
152 'flag.type IN (%Ls)',
156 if ($this->objectPHIDs
) {
159 'flag.objectPHID IN (%Ls)',
166 'flag.color IN (%Ld)',
170 $where[] = $this->buildPagingClause($conn);
172 return $this->formatWhereClause($conn, $where);
175 public function getQueryApplicationClass() {
176 return 'PhabricatorFlagsApplication';