engelsystem/includes/model/User_model.php

217 lines
5.3 KiB
PHP
Raw Normal View History

2012-12-26 14:02:27 +01:00
<?php
2014-09-28 14:50:08 +02:00
/**
* User model
*/
2014-09-28 15:01:02 +02:00
/**
* Counts all forced active users.
*/
function User_force_active_count() {
return sql_select_single_cell("SELECT COUNT(*) FROM `User` WHERE `force_active` = 1");
}
function User_active_count() {
return sql_select_single_cell("SELECT COUNT(*) FROM `User` WHERE `Aktiv` = 1");
}
function User_arrived_count() {
return sql_select_single_cell("SELECT COUNT(*) FROM `User` WHERE `Gekommen` = 1");
}
function User_tshirts_count() {
return sql_select_single_cell("SELECT COUNT(*) FROM `User` WHERE `Tshirt` = 1");
}
/**
* Returns all column names for sorting in an array.
*/
function User_sortable_columns() {
return array(
'Nick',
'Name',
'Vorname',
'Alter',
'DECT',
'email',
'Size',
'Gekommen',
'Aktiv',
'force_active',
'Tshirt',
2014-12-12 23:39:54 +01:00
'lastLogIn'
2014-09-28 15:01:02 +02:00
);
}
2014-09-28 14:50:08 +02:00
/**
* Get all users, ordered by Nick by default or by given param.
2014-09-28 15:01:02 +02:00
*
2014-12-12 23:39:54 +01:00
* @param string $order_by
2014-09-28 14:50:08 +02:00
*/
function Users($order_by = 'Nick') {
return sql_select("SELECT * FROM `User` ORDER BY `" . sql_escape($order_by) . "` ASC");
}
2014-08-23 01:55:18 +02:00
/**
* Returns true if user is freeloader
*
2014-12-12 23:39:54 +01:00
* @param User $user
2014-08-23 01:55:18 +02:00
*/
function User_is_freeloader($user) {
global $max_freeloadable_shifts, $user;
2014-12-12 23:39:54 +01:00
2014-08-23 01:55:18 +02:00
return count(ShiftEntries_freeloaded_by_user($user)) >= $max_freeloadable_shifts;
}
/**
* Returns all users that are not member of given angeltype.
*
2014-12-12 23:39:54 +01:00
* @param Angeltype $angeltype
*/
function Users_by_angeltype_inverted($angeltype) {
return sql_select("
2014-12-12 23:39:54 +01:00
SELECT `User`.*
FROM `User`
LEFT JOIN `UserAngelTypes` ON (`User`.`UID`=`UserAngelTypes`.`user_id` AND `angeltype_id`=" . sql_escape($angeltype['id']) . ")
WHERE `UserAngelTypes`.`id` IS NULL
ORDER BY `Nick`");
}
2013-10-13 00:52:44 +02:00
/**
* Returns all members of given angeltype.
*
2014-12-12 23:39:54 +01:00
* @param Angeltype $angeltype
*/
function Users_by_angeltype($angeltype) {
return sql_select("
2014-12-12 23:39:54 +01:00
SELECT
`User`.*,
`UserAngelTypes`.`id` as `user_angeltype_id`,
`UserAngelTypes`.`confirm_user_id`,
`UserAngelTypes`.`coordinator`
FROM `User`
JOIN `UserAngelTypes` ON `User`.`UID`=`UserAngelTypes`.`user_id`
WHERE `UserAngelTypes`.`angeltype_id`=" . sql_escape($angeltype['id']) . "
ORDER BY `Nick`");
}
2013-12-29 15:08:21 +01:00
/**
* Returns User id array
*/
2014-01-07 15:50:16 +01:00
function User_ids() {
return sql_select("SELECT `UID` FROM `User`");
2013-12-29 15:08:21 +01:00
}
2013-12-27 19:45:50 +01:00
/**
* Strip unwanted characters from a users nick.
*
2014-12-12 23:39:54 +01:00
* @param string $nick
2013-12-27 19:45:50 +01:00
*/
function User_validate_Nick($nick) {
return preg_replace("/([^a-z0-9üöäß. _+*-]{1,})/ui", '', $nick);
}
2012-12-26 14:02:27 +01:00
/**
* Returns user by id.
2013-10-13 00:52:44 +02:00
*
2014-12-12 23:39:54 +01:00
* @param $id UID
2012-12-26 14:02:27 +01:00
*/
function User($id) {
$user_source = sql_select("SELECT * FROM `User` WHERE `UID`=" . sql_escape($id) . " LIMIT 1");
2013-10-13 00:52:44 +02:00
if ($user_source === false)
return false;
2013-10-13 00:52:44 +02:00
if (count($user_source) > 0)
2012-12-26 14:02:27 +01:00
return $user_source[0];
return null;
}
2013-12-29 15:08:21 +01:00
/**
2014-01-07 15:50:16 +01:00
* TODO: Merge into normal user function
2013-12-29 15:08:21 +01:00
* Returns user by id (limit informations.
*
2014-12-12 23:39:54 +01:00
* @param $id UID
2013-12-29 15:08:21 +01:00
*/
function mUser_Limit($id) {
$user_source = sql_select("SELECT `UID`, `Nick`, `Name`, `Vorname`, `Telefon`, `DECT`, `Handy`, `email`, `jabber`, `Avatar` FROM `User` WHERE `UID`=" . sql_escape($id) . " LIMIT 1");
2013-12-29 15:08:21 +01:00
if ($user_source === false)
return false;
if (count($user_source) > 0)
return $user_source[0];
return null;
}
/**
* Returns User by api_key.
2013-10-13 00:52:44 +02:00
*
* @param string $api_key
* User api key
* @return Matching user, null or false on error
*/
function User_by_api_key($api_key) {
$user = sql_select("SELECT * FROM `User` WHERE `api_key`='" . sql_escape($api_key) . "' LIMIT 1");
2013-10-13 00:52:44 +02:00
if ($user === false)
return false;
if (count($user) == 0)
return null;
return $user[0];
}
2013-12-26 13:34:48 +01:00
/**
* Returns User by email.
*
2014-12-12 23:39:54 +01:00
* @param string $email
2013-12-26 13:34:48 +01:00
* @return Matching user, null or false on error
*/
function User_by_email($email) {
$user = sql_select("SELECT * FROM `User` WHERE `email`='" . sql_escape($email) . "' LIMIT 1");
if ($user === false)
return false;
if (count($user) == 0)
return null;
return $user[0];
}
/**
* Returns User by password token.
*
2014-12-12 23:39:54 +01:00
* @param string $token
2013-12-26 13:34:48 +01:00
* @return Matching user, null or false on error
*/
function User_by_password_recovery_token($token) {
$user = sql_select("SELECT * FROM `User` WHERE `password_recovery_token`='" . sql_escape($token) . "' LIMIT 1");
if ($user === false)
return false;
if (count($user) == 0)
return null;
return $user[0];
}
/**
* Generates a new api key for given user.
2013-10-13 00:52:44 +02:00
*
2014-12-12 23:39:54 +01:00
* @param User $user
*/
2013-09-10 14:45:41 +02:00
function User_reset_api_key(&$user) {
$user['api_key'] = md5($user['Nick'] . time() . rand());
$result = sql_query("UPDATE `User` SET `api_key`='" . sql_escape($user['api_key']) . "' WHERE `UID`='" . sql_escape($user['UID']) . "' LIMIT 1");
2013-10-13 00:52:44 +02:00
if ($result === false)
return false;
2014-12-12 23:39:54 +01:00
engelsystem_log(sprintf("API key resetted (%s).",User_Nick_render($user)));
}
2013-12-26 13:34:48 +01:00
/**
* Generates a new password recovery token for given user.
*
2014-12-12 23:39:54 +01:00
* @param User $user
2013-12-26 13:34:48 +01:00
*/
function User_generate_password_recovery_token(&$user) {
$user['password_recovery_token'] = md5($user['Nick'] . time() . rand());
$result = sql_query("UPDATE `User` SET `password_recovery_token`='" . sql_escape($user['password_recovery_token']) . "' WHERE `UID`='" . sql_escape($user['UID']) . "' LIMIT 1");
if ($result === false)
return false;
engelsystem_log("Password recovery for " . $user['Nick'] . " started.");
return $user['password_recovery_token'];
}
2013-12-29 15:08:21 +01:00
?>