diff options
Diffstat (limited to 'src/web/_model/people.php')
-rw-r--r-- | src/web/_model/people.php | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/web/_model/people.php b/src/web/_model/people.php new file mode 100644 index 0000000..bf540cf --- /dev/null +++ b/src/web/_model/people.php @@ -0,0 +1,72 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ + +class People_model extends XSS_Model { + + private function get_filted_query($select): DatabaseQuery { + $filter_type = $this->get_string('filter'); + $filter_uid = $this->get_int('uid'); + $max = $this->get_int('max'); + $query = $this->db() + ->select($select) + ->from('api.user u'); + + if ($filter_type && $filter_uid) { + switch ($filter_type) { + // only show followers + case 'follower': + $query = $query + ->join('xssbook.follow f', 'f.follower_id = u.id AND f.followee_id', 'INNER') + ->eq($filter_uid) + ->where('f.value = TRUE'); + break; + // only show followees + case 'followee': + $query = $query + ->join('xssbook.follow f', 'f.followee_id = u.id AND f.follower_id', 'INNER') + ->eq($filter_uid) + ->where('f.value = TRUE'); + break; + } + } + + if ($max) { + $query = $query + ->where('u.id') + ->le($max); + } + + return $query; + } + + public function get_people(): array { + $filter_type = $this->get_string('filter'); + $filter_uid = $this->get_int('uid'); + $page = $this->get_int('page', 0); + + $page_size = PEOPLE_PAGE_SIZE; + $offset = $page_size * $page; + + $users = $this->get_filted_query('u.*') + ->order_by('u.id', 'DESC') + ->offset($offset) + ->limit($page_size) + ->rows(); + + $count = $this->get_filted_query('COUNT(u.id) AS count') + ->row()['count']; + + $max = 0; + + foreach ($users as $user) + $max = max($max, $user['id']); + + return array( + 'users' => $users, + 'count' => $count, + 'page_size' => $page_size, + 'max_id' => $max, + 'filter_type' => $filter_type, + 'filter_uid' => $filter_uid, + ); + } +} |