Files
endurain/frontend/inc/func/users-funcs.php
2023-11-29 16:30:23 +00:00

206 lines
6.2 KiB
PHP

<?php
/* ************************************************************************** */
/* Users */
/* ************************************************************************** */
/* Get all users */
function getUsers(){
$response = callAPIRoute("/users/all", 1, 0, NULL);
if ($response[0] === false) {
return -1;
} else {
if ($response[1] === 200) {
return json_decode($response[0], true);
} else {
return -2;
}
}
}
/* Get all users with pagination */
function getUsersPagination($pageNumber, $numRecords){
$response = callAPIRoute("/users/all/pagenumber/$pageNumber/numRecords/$numRecords", 1, 0, NULL);
if ($response[0] === false) {
return -1;
} else {
if ($response[1] === 200) {
return json_decode($response[0], true);
} else {
return -2;
}
}
}
/* Get number of users */
function numUsers(){
$response = callAPIRoute("/users/number", 0, 0, NULL);
if ($response[0] === false) {
return -1;
} else {
if ($response[1] === 200) {
$data = json_decode($response[0], true);
return $data[0];
} else {
return -2;
}
}
}
/* Get user from username */
function getUserFromUsername($usernameUser){
$response = callAPIRoute("/users/$usernameUser/userfromusername", 1, 0, NULL);
if ($response[0] === false) {
return -1;
} else {
if ($response[1] === 200) {
return json_decode($response[0], true);
} else {
return -2;
}
}
}
/* Get user from ID */
function getUserFromId($id){
$response = callAPIRoute("/users/$id/userfromid", 1, 0, NULL);
if ($response[0] === false) {
return -1;
} else {
if ($response[1] === 200) {
return json_decode($response[0], true);
} else {
return -2;
}
}
}
/* Get user from ID */
function getUserIdFromUsername($usernameUser){
$response = callAPIRoute("/users/$id/useridfromusername", 1, 0, NULL);
if ($response[0] === false) {
return -1;
} else {
if ($response[1] === 200) {
return json_decode($response[0], true);
} else {
return -2;
}
}
}
/* Get user photo path from ID */
function getUserPhotoFromID($id){
$response = callAPIRoute("/users/$id/userphotofromid", 0, 0, NULL);
if ($response[0] === false) {
return -1;
} else {
if ($response[1] === 200) {
return json_decode($response[0], true);
} else {
return -2;
}
}
}
/* Get user photo path aux from ID */
function getUserPhotoAuxFromID($id){
$response = callAPIRoute("/users/$id/userphotoauxfromid", 0, 0, NULL);
if ($response[0] === false) {
return -1;
} else {
if ($response[1] === 200) {
return json_decode($response[0], true);
} else {
return -2;
}
}
}
/* Creates a new user */
function newUser($name, $username, $email, $password, $gender, $preferred_language, $city, $birthdate, $access_type, $photo_path, $photo_path_aux, $is_active){
if(getUserFromUsername($username) != NULL){
return -3;
}
$response = callAPIRoute("/users/create", 0, 4, json_encode(array(
'name' => $name,
'username' => $username,
'email' => $email,
'password' => hash("sha256",$password),
'preferred_language' => $preferred_language,
'city' => $city,
'birthdate' => $birthdate,
'gender' => $gender,
'access_type' => $access_type,
'photo_path' => $photo_path,
'photo_path_aux' => $photo_path_aux,
'is_active' => $is_active,
)));
if ($response[0] === false) {
return -1;
} else {
if ($response[1] === 200) {
return 0;
} else {
return -2;
}
}
}
/* Edit user */
function editUser($name, $username, $email, $id, $preferred_language, $city, $birthdate, $gender, $access_type, $photo_path, $photo_path_aux, $is_active){
$response = callAPIRoute("/users/$id/edit", 0, 3, json_encode(array(
'name' => $name,
'username' => $username,
'email' => $email,
'preferred_language' => $preferred_language,
'city' => $city,
'birthdate' => $birthdate,
'gender' => $gender,
'access_type' => $access_type,
'photo_path' => $photo_path,
'photo_path_aux' => $photo_path_aux,
'is_active' => $is_active,
)));
if ($response[0] === false) {
return -1;
} else {
if ($response[1] === 200) {
return 0;
} else {
return -2;
}
}
}
/* Unset user photo */
function unsetUserPhoto($id){
$response = callAPIRoute("/users/$id/delete-photo", 0, 3, NULL);
if ($response[0] === false) {
return -1;
} else {
if ($response[1] === 200) {
return 0;
} else {
return -2;
}
}
}
/* Deletes a user based on its ID */
function deleteUser($id){
$response = callAPIRoute("/users/$id/delete", 0, 1, NULL);
if ($response[0] === false) {
return -1;
} else {
if ($response[1] === 200) {
return 0;
} else {
if ($response[1] === 409) {
return -409;
} else {
return -2;
}
}
}
}
?>