From: Raphaël Barrois Date: Sun, 24 Jan 2010 12:35:30 +0000 (+0100) Subject: Add support for returning Profiles instead of Users X-Git-Tag: xorg/1.0.0~332^2~274 X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;h=043b104b5cee2d3eb5777a1ed129288a6ca5ca8f;p=platal.git Add support for returning Profiles instead of Users Signed-off-by: Raphaël Barrois --- diff --git a/classes/userfilter.php b/classes/userfilter.php index 098abc6..64f310b 100644 --- a/classes/userfilter.php +++ b/classes/userfilter.php @@ -1298,6 +1298,24 @@ class UserFilter extends PlFilter return $fetched; } + private function getPIDList($pids = null, PlLimit &$limit) + { + $this->requireProfiles(); + $this->buildQuery(); + $lim = $limit->getSql(); + $cond = ''; + if (!is_null($pids)) { + $cond = ' AND p.pid IN ' . XDB::formatArray($pids); + } + $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS p.pid + ' . $this->query . $cond . ' + GROUP BY p.pid + ' . $this->orderby . ' + ' . $lim); + $this->lastcount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()'); + return $fetched; + } + /** Check that the user match the given rule. */ public function checkUser(PlUser &$user) @@ -1309,10 +1327,28 @@ class UserFilter extends PlFilter return $count == 1; } - /** Filter a list of user to extract the users matching the rule. + /** Check that the profile match the given rule. + */ + public function checkProfile(Profile &$profile) + { + $this->requireProfiles(); + $this->buildQuery(); + $count = (int)XDB::fetchOneCell('SELECT COUNT(*) + ' . $this->query . XDB::format(' AND p.pid = {?}', $profile->id())); + return $count == 1; + } + + /** Default filter is on users */ public function filter(array $users, PlLimit &$limit) { + return $this->filterUsers($users, $limit); + } + + /** Filter a list of users to extract the users matching the rule. + */ + public function filterUsers(array $users, PlLimit &$limit) + { $this->requireAccounts(); $this->buildQuery(); $table = array(); @@ -1334,16 +1370,51 @@ class UserFilter extends PlFilter return $output; } + /** Filter a list of profiles to extract the users matching the rule. + */ + public function filterProfiles(array $profiles, PlLimit &$limit) + { + $this->requireProfiles(); + $this->buildQuery(); + $table = array(); + $pids = array(); + foreach ($profiles as $profile) { + if ($profile instanceof Profile) { + $pid = $profile->id(); + } else { + $pid = $profile; + } + $pids[] = $pid; + $table[$pid] = $profile; + } + $fetched = $this->getPIDList($pids, $limit); + $output = array(); + foreach ($fetched as $pid) { + $output[] = $table[$pid]; + } + return $output; + } + public function getUIDs(PlLimit &$limit) { return $this->getUIDList(null, $limit); } + public function getPIDs(PlLimit &$limit) + { + return $this->getPIDList(null, $limit); + } + public function getUsers(PlLimit &$limit) { return User::getBulkUsersWithUIDs($this->getUIDs($limit)); } + public function getProfiles(PlLimit &$limit) + { + return Profile::getBulkProfilesWithPIDs($this->getPIDs($limit)); + } + public function get(PlLimit &$limit) { return $this->getUsers($limit);