3 final class PhabricatorUserPreferencesQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery
{
11 private $users = array();
14 public function withIDs(array $ids) {
19 public function withPHIDs(array $phids) {
20 $this->phids
= $phids;
24 public function withHasUserPHID($is_user) {
25 $this->hasUserPHID
= $is_user;
29 public function withUserPHIDs(array $phids) {
30 $this->userPHIDs
= $phids;
34 public function withUsers(array $users) {
35 assert_instances_of($users, 'PhabricatorUser');
36 $this->users
= mpull($users, null, 'getPHID');
37 $this->withUserPHIDs(array_keys($this->users
));
41 public function withBuiltinKeys(array $keys) {
42 $this->builtinKeys
= $keys;
47 * Always return preferences for every queried user.
49 * If no settings exist for a user, a new empty settings object with
50 * appropriate defaults is returned.
52 * @param bool True to generate synthetic preferences for missing users.
54 public function needSyntheticPreferences($synthetic) {
55 $this->synthetic
= $synthetic;
59 public function newResultObject() {
60 return new PhabricatorUserPreferences();
63 protected function loadPage() {
64 $preferences = $this->loadStandardPage($this->newResultObject());
66 if ($this->synthetic
) {
67 $user_map = mpull($preferences, null, 'getUserPHID');
68 foreach ($this->userPHIDs
as $user_phid) {
69 if (isset($user_map[$user_phid])) {
72 $preferences[] = $this->newResultObject()
73 ->setUserPHID($user_phid);
80 protected function willFilterPage(array $prefs) {
81 $user_phids = mpull($prefs, 'getUserPHID');
82 $user_phids = array_filter($user_phids);
84 // If some of the preferences are attached to users, try to use any objects
85 // we were handed first. If we're missing some, load them.
88 $users = $this->users
;
90 $user_phids = array_fuse($user_phids);
91 $load_phids = array_diff_key($user_phids, $users);
92 $load_phids = array_keys($load_phids);
95 $load_users = id(new PhabricatorPeopleQuery())
96 ->setViewer($this->getViewer())
97 ->withPHIDs($load_phids)
99 $load_users = mpull($load_users, null, 'getPHID');
100 $users +
= $load_users;
106 $need_global = array();
107 foreach ($prefs as $key => $pref) {
108 $user_phid = $pref->getUserPHID();
110 $pref->attachUser(null);
114 $need_global[] = $pref;
116 $user = idx($users, $user_phid);
118 $this->didRejectResult($pref);
123 $pref->attachUser($user);
126 // If we loaded any user preferences, load the global defaults and attach
127 // them if they exist.
129 $global = id(new self())
130 ->setViewer($this->getViewer())
133 PhabricatorUserPreferences
::BUILTIN_GLOBAL_DEFAULT
,
137 foreach ($need_global as $pref) {
138 $pref->attachDefaultSettings($global);
146 protected function buildWhereClauseParts(AphrontDatabaseConnection
$conn) {
147 $where = parent
::buildWhereClauseParts($conn);
149 if ($this->ids
!== null) {
156 if ($this->phids
!== null) {
163 if ($this->userPHIDs
!== null) {
170 if ($this->builtinKeys
!== null) {
173 'builtinKey IN (%Ls)',
177 if ($this->hasUserPHID
!== null) {
178 if ($this->hasUserPHID
) {
181 'userPHID IS NOT NULL');
192 public function getQueryApplicationClass() {
193 return 'PhabricatorSettingsApplication';