|
| 1 | +<?php |
| 2 | + |
| 3 | +final class PhabricatorUserPreferencesQuery |
| 4 | + extends PhabricatorCursorPagedPolicyAwareQuery { |
| 5 | + |
| 6 | + private $ids; |
| 7 | + private $phids; |
| 8 | + private $userPHIDs; |
| 9 | + private $users = array(); |
| 10 | + |
| 11 | + public function withIDs(array $ids) { |
| 12 | + $this->ids = $ids; |
| 13 | + return $this; |
| 14 | + } |
| 15 | + |
| 16 | + public function withPHIDs(array $phids) { |
| 17 | + $this->phids = $phids; |
| 18 | + return $this; |
| 19 | + } |
| 20 | + |
| 21 | + public function withUserPHIDs(array $phids) { |
| 22 | + $this->userPHIDs = $phids; |
| 23 | + return $this; |
| 24 | + } |
| 25 | + |
| 26 | + public function withUsers(array $users) { |
| 27 | + assert_instances_of($users, 'PhabricatorUser'); |
| 28 | + $this->users = mpull($users, null, 'getPHID'); |
| 29 | + $this->withUserPHIDs(array_keys($this->users)); |
| 30 | + return $this; |
| 31 | + } |
| 32 | + |
| 33 | + public function newResultObject() { |
| 34 | + return new PhabricatorUserPreferences(); |
| 35 | + } |
| 36 | + |
| 37 | + protected function loadPage() { |
| 38 | + return $this->loadStandardPage($this->newResultObject()); |
| 39 | + } |
| 40 | + |
| 41 | + protected function willFilterPage(array $prefs) { |
| 42 | + $user_phids = mpull($prefs, 'getUserPHID'); |
| 43 | + $user_phids = array_filter($user_phids); |
| 44 | + |
| 45 | + // If some of the preferences are attached to users, try to use any objects |
| 46 | + // we were handed first. If we're missing some, load them. |
| 47 | + |
| 48 | + if ($user_phids) { |
| 49 | + $users = $this->users; |
| 50 | + |
| 51 | + $user_phids = array_fuse($user_phids); |
| 52 | + $load_phids = array_diff_key($user_phids, $users); |
| 53 | + $load_phids = array_keys($load_phids); |
| 54 | + |
| 55 | + if ($load_phids) { |
| 56 | + $load_users = id(new PhabricatorPeopleQuery()) |
| 57 | + ->setViewer($this->getViewer()) |
| 58 | + ->withPHIDs($load_phids) |
| 59 | + ->execute(); |
| 60 | + $load_users = mpull($load_users, null, 'getPHID'); |
| 61 | + $users += $load_users; |
| 62 | + } |
| 63 | + } else { |
| 64 | + $users = array(); |
| 65 | + } |
| 66 | + |
| 67 | + foreach ($prefs as $key => $pref) { |
| 68 | + $user_phid = $pref->getUserPHID(); |
| 69 | + if (!$user_phid) { |
| 70 | + $pref->attachUser(null); |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + $user = idx($users, $user_phid); |
| 75 | + if (!$user) { |
| 76 | + $this->didRejectResult($pref); |
| 77 | + unset($prefs[$key]); |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + $pref->attachUser($user); |
| 82 | + } |
| 83 | + |
| 84 | + return $prefs; |
| 85 | + } |
| 86 | + |
| 87 | + protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { |
| 88 | + $where = parent::buildWhereClauseParts($conn); |
| 89 | + |
| 90 | + if ($this->ids !== null) { |
| 91 | + $where[] = qsprintf( |
| 92 | + $conn, |
| 93 | + 'id IN (%Ld)', |
| 94 | + $this->ids); |
| 95 | + } |
| 96 | + |
| 97 | + if ($this->phids !== null) { |
| 98 | + $where[] = qsprintf( |
| 99 | + $conn, |
| 100 | + 'phid IN (%Ls)', |
| 101 | + $this->phids); |
| 102 | + } |
| 103 | + |
| 104 | + if ($this->userPHIDs !== null) { |
| 105 | + $where[] = qsprintf( |
| 106 | + $conn, |
| 107 | + 'userPHID IN (%Ls)', |
| 108 | + $this->userPHIDs); |
| 109 | + } |
| 110 | + |
| 111 | + return $where; |
| 112 | + } |
| 113 | + |
| 114 | + public function getQueryApplicationClass() { |
| 115 | + return 'PhabricatorSettingsApplication'; |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments