diff options
author | msquare <msquare@notrademark.de> | 2017-06-20 16:50:21 +0200 |
---|---|---|
committer | msquare <msquare@notrademark.de> | 2017-06-20 16:50:21 +0200 |
commit | 56814fa2fdf58b4013f4d57c5ea87619c7122957 (patch) | |
tree | 640945769b7e9626cdf43162c786147f5c962029 /includes/model | |
parent | a5fc5bd0979e8de1fce8a8addd351a6e7bd6aeb8 (diff) | |
parent | cd30017b97afc3c7001fbb9eb14b54dbb980b7b6 (diff) |
Merge branch 'pr/316' into feature-igel-rewrite
Diffstat (limited to 'includes/model')
-rw-r--r-- | includes/model/AngelType_model.php | 388 | ||||
-rw-r--r-- | includes/model/EventConfig_model.php | 107 | ||||
-rw-r--r-- | includes/model/LogEntries_model.php | 56 | ||||
-rw-r--r-- | includes/model/Message_model.php | 81 | ||||
-rw-r--r-- | includes/model/NeededAngelTypes_model.php | 138 | ||||
-rw-r--r-- | includes/model/Room_model.php | 92 | ||||
-rw-r--r-- | includes/model/ShiftEntry_model.php | 281 | ||||
-rw-r--r-- | includes/model/ShiftSignupState.php | 234 | ||||
-rw-r--r-- | includes/model/ShiftTypes_model.php | 114 | ||||
-rw-r--r-- | includes/model/ShiftsFilter.php | 251 | ||||
-rw-r--r-- | includes/model/Shifts_model.php | 841 | ||||
-rw-r--r-- | includes/model/UserAngelTypes_model.php | 344 | ||||
-rw-r--r-- | includes/model/UserDriverLicenses_model.php | 180 | ||||
-rw-r--r-- | includes/model/UserGroups_model.php | 27 | ||||
-rw-r--r-- | includes/model/User_model.php | 630 | ||||
-rw-r--r-- | includes/model/ValidationResult.php | 62 |
16 files changed, 2362 insertions, 1464 deletions
diff --git a/includes/model/AngelType_model.php b/includes/model/AngelType_model.php index 8d6a89b3..d437f526 100644 --- a/includes/model/AngelType_model.php +++ b/includes/model/AngelType_model.php @@ -1,236 +1,298 @@ <?php + +use Engelsystem\Database\DB; use Engelsystem\ValidationResult; /** * Returns an array containing the basic attributes of angeltypes. * FIXME! This is the big sign for needing entity objects */ -function AngelType_new() { - return [ - 'id' => null, - 'name' => "", - 'restricted' => false, - 'no_self_signup' => false, - 'description' => '', - 'requires_driver_license' => false, - 'contact_user_id' => null, - 'contact_name' => null, - 'contact_dect' => null, - 'contact_email' => null - ]; +function AngelType_new() +{ + return [ + 'id' => null, + 'name' => '', + 'restricted' => false, + 'no_self_signup' => false, + 'description' => '', + 'requires_driver_license' => false, + 'contact_user_id' => null, + 'contact_name' => null, + 'contact_dect' => null, + 'contact_email' => null + ]; } /** * Validates the contact user * - * @param Angeltype $angeltype - * The angeltype + * @param array $angeltype The angeltype * @return ValidationResult */ -function AngelType_validate_contact_user_id($angeltype) { - if (! isset($angeltype['contact_user_id'])) { - return new ValidationResult(true, null); - } - if (isset($angeltype['contact_name']) || isset($angeltype['contact_dect']) || isset($angeltype['contact_email'])) { - return new ValidationResult(false, $angeltype['contact_user_id']); - } - if (User($angeltype['contact_user_id']) == null) { - return new ValidationResult(false, $angeltype['contact_user_id']); - } - return new ValidationResult(true, $angeltype['contact_user_id']); +function AngelType_validate_contact_user_id($angeltype) +{ + if (!isset($angeltype['contact_user_id'])) { + return new ValidationResult(true, null); + } + if (isset($angeltype['contact_name']) || isset($angeltype['contact_dect']) || isset($angeltype['contact_email'])) { + return new ValidationResult(false, $angeltype['contact_user_id']); + } + if (User($angeltype['contact_user_id']) == null) { + return new ValidationResult(false, $angeltype['contact_user_id']); + } + return new ValidationResult(true, $angeltype['contact_user_id']); } /** * Returns contact data (name, dect, email) for given angeltype or null * - * @param Angeltype $angeltype - * The angeltype + * @param array $angeltype The angeltype + * @return array|null */ -function AngelType_contact_info($angeltype) { - if (isset($angeltype['contact_user_id'])) { - $contact_user = User($angeltype['contact_user_id']); - $contact_data = [ - 'contact_name' => $contact_user['Nick'], - 'contact_dect' => $contact_user['DECT'] - ]; - if ($contact_user['email_by_human_allowed']) { - $contact_data['contact_email'] = $contact_user['email']; +function AngelType_contact_info($angeltype) +{ + if (isset($angeltype['contact_user_id'])) { + $contact_user = User($angeltype['contact_user_id']); + $contact_data = [ + 'contact_name' => $contact_user['Nick'], + 'contact_dect' => $contact_user['DECT'] + ]; + if ($contact_user['email_by_human_allowed']) { + $contact_data['contact_email'] = $contact_user['email']; + } + return $contact_data; } - return $contact_data; - } - if (isset($angeltype['contact_name'])) { - return [ - 'contact_name' => $angeltype['contact_name'], - 'contact_dect' => $angeltype['contact_dect'], - 'contact_email' => $angeltype['contact_email'] - ]; - } - return null; + if (isset($angeltype['contact_name'])) { + return [ + 'contact_name' => $angeltype['contact_name'], + 'contact_dect' => $angeltype['contact_dect'], + 'contact_email' => $angeltype['contact_email'] + ]; + } + + return null; } /** * Delete an Angeltype. * - * @param Angeltype $angeltype + * @param array $angeltype + * @return bool */ -function AngelType_delete($angeltype) { - $result = sql_query(" - DELETE FROM `AngelTypes` - WHERE `id`='" . sql_escape($angeltype['id']) . "' - LIMIT 1"); - if ($result === false) { - engelsystem_error("Unable to delete angeltype."); - } - engelsystem_log("Deleted angeltype: " . AngelType_name_render($angeltype)); - return $result; +function AngelType_delete($angeltype) +{ + $result = DB::delete(' + DELETE FROM `AngelTypes` + WHERE `id`=? + LIMIT 1 + ', [$angeltype['id']]); + if (is_null($result)) { + engelsystem_error('Unable to delete angeltype.'); + } + engelsystem_log('Deleted angeltype: ' . AngelType_name_render($angeltype)); + return true; } /** * Update Angeltype. * - * @param Angeltype $angeltype - * The angeltype + * @param array $angeltype The angeltype + * @return bool */ -function AngelType_update($angeltype) { - $result = sql_query(" - UPDATE `AngelTypes` SET - `name`='" . sql_escape($angeltype['name']) . "', - `restricted`=" . sql_bool($angeltype['restricted']) . ", - `description`='" . sql_escape($angeltype['description']) . "', - `requires_driver_license`=" . sql_bool($angeltype['requires_driver_license']) . ", - `no_self_signup`=" . sql_bool($angeltype['no_self_signup']) . ", - `contact_user_id`=" . sql_null($angeltype['contact_user_id']) . ", - `contact_name`=" . sql_null($angeltype['contact_name']) . ", - `contact_dect`=" . sql_null($angeltype['contact_dect']) . ", - `contact_email`=" . sql_null($angeltype['contact_email']) . " - WHERE `id`='" . sql_escape($angeltype['id']) . "'"); - if ($result === false) { - engelsystem_error("Unable to update angeltype."); - } - engelsystem_log("Updated angeltype: " . $angeltype['name'] . ($angeltype['restricted'] ? ", restricted" : "") . ($angeltype['no_self_signup'] ? ", no_self_signup" : "") . ($angeltype['requires_driver_license'] ? ", requires driver license" : "")); - return $result; +function AngelType_update($angeltype) +{ + $result = DB::update(' + UPDATE `AngelTypes` SET + `name` = ?, + `restricted` = ?, + `description` = ?, + `requires_driver_license` = ?, + `no_self_signup` = ?, + `contact_user_id` = ?, + `contact_name` = ?, + `contact_dect` = ?, + `contact_email` = ? + WHERE `id` = ?', + [ + $angeltype['name'], + $angeltype['restricted'], + $angeltype['description'], + $angeltype['requires_driver_license'], + $angeltype['no_self_signup'], + $angeltype['contact_user_id'], + $angeltype['contact_name'], + $angeltype['contact_dect'], + $angeltype['contact_email'], + $angeltype['id'], + ] + ); + if (is_null($result)) { + engelsystem_error('Unable to update angeltype.'); + } + engelsystem_log( + 'Updated angeltype: ' . $angeltype['name'] . ($angeltype['restricted'] ? ', restricted' : '') + . ($angeltype['no_self_signup'] ? ', no_self_signup' : '') + . ($angeltype['requires_driver_license'] ? ', requires driver license' : '') + ); + return true; } /** * Create an Angeltype. * - * @param Angeltype $angeltype - * The angeltype - * @return the created angeltype + * @param array $angeltype The angeltype + * @return array the created angeltype */ -function AngelType_create($angeltype) { - $result = sql_query(" - INSERT INTO `AngelTypes` SET - `name`='" . sql_escape($angeltype['name']) . "', - `restricted`=" . sql_bool($angeltype['restricted']) . ", - `description`='" . sql_escape($angeltype['description']) . "', - `requires_driver_license`=" . sql_bool($angeltype['requires_driver_license']) . ", - `no_self_signup`=" . sql_bool($angeltype['no_self_signup']) . ", - `contact_user_id`=" . sql_null($angeltype['contact_user_id']) . ", - `contact_name`=" . sql_null($angeltype['contact_name']) . ", - `contact_dect`=" . sql_null($angeltype['contact_dect']) . ", - `contact_email`=" . sql_null($angeltype['contact_email'])); - if ($result === false) { - engelsystem_error("Unable to create angeltype."); - } - $angeltype['id'] = sql_id(); - engelsystem_log("Created angeltype: " . $angeltype['name'] . ($angeltype['restricted'] ? ", restricted" : "") . ($angeltype['requires_driver_license'] ? ", requires driver license" : "")); - return $angeltype; +function AngelType_create($angeltype) +{ + $result = DB::insert(' + INSERT INTO `AngelTypes` ( + `name`, + `restricted`, + `description`, + `requires_driver_license`, + `no_self_signup`, + `contact_user_id`, + `contact_name`, + `contact_dect`, + `contact_email` + ) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) + ', + [ + $angeltype['name'], + (bool)$angeltype['restricted'], + $angeltype['description'], + (bool)$angeltype['requires_driver_license'], + (bool)$angeltype['no_self_signup'], + $angeltype['contact_user_id'], + $angeltype['contact_name'], + $angeltype['contact_dect'], + $angeltype['contact_email'], + ] + ); + if (is_null($result)) { + engelsystem_error('Unable to create angeltype.'); + } + $angeltype['id'] = DB::getPdo()->lastInsertId(); + engelsystem_log( + 'Created angeltype: ' . $angeltype['name'] + . ($angeltype['restricted'] ? ', restricted' : '') + . ($angeltype['requires_driver_license'] ? ', requires driver license' : '') + ); + return $angeltype; } /** * Validates a name for angeltypes. * Returns ValidationResult containing validation success and validated name. * - * @param string $name - * Wanted name for the angeltype - * @param AngelType $angeltype - * The angeltype the name is for + * @param string $name Wanted name for the angeltype + * @param array $angeltype The angeltype the name is for + * * @return ValidationResult result and validated name */ -function AngelType_validate_name($name, $angeltype) { - $name = strip_item($name); - if ($name == "") { - return new ValidationResult(false, ""); - } - if ($angeltype != null && isset($angeltype['id'])) { - $valid = sql_num_query(" - SELECT * - FROM `AngelTypes` - WHERE `name`='" . sql_escape($name) . "' - AND NOT `id`='" . sql_escape($angeltype['id']) . "' - LIMIT 1") == 0; +function AngelType_validate_name($name, $angeltype) +{ + $name = strip_item($name); + if ($name == '') { + return new ValidationResult(false, ''); + } + if ($angeltype != null && isset($angeltype['id'])) { + $valid = (count(DB::select(' + SELECT `id` + FROM `AngelTypes` + WHERE `name`=? + AND NOT `id`=? + LIMIT 1 + ', [$name, $angeltype['id']])) == 0); + return new ValidationResult($valid, $name); + } + $valid = (count(DB::select(' + SELECT `id` + FROM `AngelTypes` + WHERE `name`=? + LIMIT 1', [$name])) == 0); return new ValidationResult($valid, $name); - } - $valid = sql_num_query(" - SELECT `id` - FROM `AngelTypes` - WHERE `name`='" . sql_escape($name) . "' - LIMIT 1") == 0; - return new ValidationResult($valid, $name); } /** * Returns all angeltypes and subscription state to each of them for given user. * - * @param User $user + * @param array $user + * @return array */ -function AngelTypes_with_user($user) { - $result = sql_select(" - SELECT `AngelTypes`.*, - `UserAngelTypes`.`id` as `user_angeltype_id`, +function AngelTypes_with_user($user) +{ + $result = DB::select(' + SELECT `AngelTypes`.*, + `UserAngelTypes`.`id` AS `user_angeltype_id`, `UserAngelTypes`.`confirm_user_id`, `UserAngelTypes`.`supporter` - FROM `AngelTypes` - LEFT JOIN `UserAngelTypes` ON `AngelTypes`.`id`=`UserAngelTypes`.`angeltype_id` - AND `UserAngelTypes`.`user_id`=" . $user['UID'] . " - ORDER BY `name`"); - if ($result === false) { - engelsystem_error("Unable to load angeltypes."); - } - return $result; + FROM `AngelTypes` + LEFT JOIN `UserAngelTypes` ON `AngelTypes`.`id`=`UserAngelTypes`.`angeltype_id` + AND `UserAngelTypes`.`user_id` = ? + ORDER BY `name`', [$user['UID']]); + + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load angeltypes.'); + } + return $result; } /** * Returns all angeltypes. + * + * @return array */ -function AngelTypes() { - $result = sql_select(" - SELECT * - FROM `AngelTypes` - ORDER BY `name`"); - if ($result === false) { - engelsystem_error("Unable to load angeltypes."); - } - return $result; +function AngelTypes() +{ + $result = DB::select(' + SELECT * + FROM `AngelTypes` + ORDER BY `name`'); + + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load angeltypes.'); + } + return $result; } /** * Returns AngelType id array + * + * @return array */ -function AngelType_ids() { - $result = sql_select("SELECT `id` FROM `AngelTypes`"); - if ($result === false) { - engelsystem_error("Unable to load angeltypes."); - } - return select_array($result, 'id', 'id'); +function AngelType_ids() +{ + $result = DB::select('SELECT `id` FROM `AngelTypes`'); + + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load angeltypes.'); + } + return select_array($result, 'id', 'id'); } /** * Returns angelType by id. * - * @param $angeltype_id angelType - * ID + * @param int $angeltype_id angelType ID + * @return array|null */ -function AngelType($angeltype_id) { - $angelType_source = sql_select("SELECT * FROM `AngelTypes` WHERE `id`='" . sql_escape($angeltype_id) . "'"); - if ($angelType_source === false) { - engelsystem_error("Unable to load angeltype."); - } - if (count($angelType_source) > 0) { - return $angelType_source[0]; - } - return null; -} +function AngelType($angeltype_id) +{ + $angelType_source = DB::select( + 'SELECT * FROM `AngelTypes` WHERE `id`=?', + [$angeltype_id] + ); + + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load angeltype.'); + } -?> + if (empty($angelType_source)) { + return null; + } + + return array_shift($angelType_source); +} diff --git a/includes/model/EventConfig_model.php b/includes/model/EventConfig_model.php index de5073d0..f5846870 100644 --- a/includes/model/EventConfig_model.php +++ b/includes/model/EventConfig_model.php @@ -1,46 +1,85 @@ <?php +use Engelsystem\Database\DB; + /** * Get event config. + * + * @return array|null */ -function EventConfig() { - $event_config = sql_select("SELECT * FROM `EventConfig` LIMIT 1"); - if ($event_config === false) { - engelsystem_error("Unable to load event config."); - return false; - } - if (count($event_config) > 0) { - return $event_config[0]; - } - return null; +function EventConfig() +{ + $event_config = DB::select('SELECT * FROM `EventConfig` LIMIT 1'); + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load event config.'); + return null; + } + + if (empty($event_config)) { + return null; + } + + return array_shift($event_config); } /** * Update event config. * - * @param string $event_name - * @param int $buildup_start_date - * @param int $event_start_date - * @param int $event_end_date - * @param int $teardown_end_date - * @param string $event_welcome_msg + * @param string $event_name + * @param int $buildup_start_date + * @param int $event_start_date + * @param int $event_end_date + * @param int $teardown_end_date + * @param string $event_welcome_msg + * @return bool */ -function EventConfig_update($event_name, $buildup_start_date, $event_start_date, $event_end_date, $teardown_end_date, $event_welcome_msg) { - if (EventConfig() == null) { - return sql_query("INSERT INTO `EventConfig` SET - `event_name`=" . sql_null($event_name) . ", - `buildup_start_date`=" . sql_null($buildup_start_date) . ", - `event_start_date`=" . sql_null($event_start_date) . ", - `event_end_date`=" . sql_null($event_end_date) . ", - `teardown_end_date`=" . sql_null($teardown_end_date) . ", - `event_welcome_msg`=" . sql_null($event_welcome_msg)); - } - return sql_query("UPDATE `EventConfig` SET - `event_name`=" . sql_null($event_name) . ", - `buildup_start_date`=" . sql_null($buildup_start_date) . ", - `event_start_date`=" . sql_null($event_start_date) . ", - `event_end_date`=" . sql_null($event_end_date) . ", - `teardown_end_date`=" . sql_null($teardown_end_date) . ", - `event_welcome_msg`=" . sql_null($event_welcome_msg)); +function EventConfig_update( + $event_name, + $buildup_start_date, + $event_start_date, + $event_end_date, + $teardown_end_date, + $event_welcome_msg +) { + if (EventConfig() == null) { + return DB::insert(' + INSERT INTO `EventConfig` ( + `event_name`, + `buildup_start_date`, + `event_start_date`, + `event_end_date`, + `teardown_end_date`, + `event_welcome_msg` + ) + VALUES (?, ?, ?, ?, ?, ?) + ', + [ + $event_name, + $buildup_start_date, + $event_start_date, + $event_end_date, + $teardown_end_date, + $event_welcome_msg + ] + ); + } + + return (bool)DB::update(' + UPDATE `EventConfig` SET + `event_name` = ?, + `buildup_start_date` = ?, + `event_start_date` = ?, + `event_end_date` = ?, + `teardown_end_date` = ?, + `event_welcome_msg` = ? + ', + [ + $event_name, + $buildup_start_date, + $event_start_date, + $event_end_date, + $teardown_end_date, + $event_welcome_msg, + ] + ); } -?> diff --git a/includes/model/LogEntries_model.php b/includes/model/LogEntries_model.php index 09bd03dc..0e11bf8e 100644 --- a/includes/model/LogEntries_model.php +++ b/includes/model/LogEntries_model.php @@ -1,38 +1,62 @@ <?php +use Engelsystem\Database\DB; + /** * Creates a log entry. * - * @param $nick Username - * @param $message Log - * Message + * @param string $nick Username + * @param string $message Log Message + * @return bool */ -function LogEntry_create($nick, $message) { - return sql_query("INSERT INTO `LogEntries` SET `timestamp`='" . sql_escape(time()) . "', `nick`='" . sql_escape($nick) . "', `message`='" . sql_escape($message) . "'"); +function LogEntry_create($nick, $message) +{ + return DB::insert(' + INSERT INTO `LogEntries` (`timestamp`, `nick`, `message`) + VALUES(?, ?, ?) + ', [time(), $nick, $message]); } /** * Returns log entries with maximum count of 10000. + * + * @return array */ -function LogEntries() { - return sql_select("SELECT * FROM `LogEntries` ORDER BY `timestamp` DESC LIMIT 10000"); +function LogEntries() +{ + return DB::select('SELECT * FROM `LogEntries` ORDER BY `timestamp` DESC LIMIT 10000'); } /** * Returns log entries filtered by a keyword + * + * @param string $keyword + * @return array */ -function LogEntries_filter($keyword) { - if ($keyword == "") { - return LogEntries(); - } - return sql_select("SELECT * FROM `LogEntries` WHERE `nick` LIKE '%" . sql_escape($keyword) . "%' OR `message` LIKE '%" . sql_escape($keyword) . "%' ORDER BY `timestamp` DESC"); +function LogEntries_filter($keyword) +{ + if ($keyword == '') { + return LogEntries(); + } + + $keyword = '%' . $keyword . '%'; + return DB::select(' + SELECT * + FROM `LogEntries` + WHERE `nick` LIKE ? + OR `message` LIKE ? + ORDER BY `timestamp` DESC + ', + [$keyword, $keyword] + ); } /** * Delete all log entries. + * + * @return bool */ -function LogEntries_clear_all() { - return sql_query("TRUNCATE `LogEntries`"); +function LogEntries_clear_all() +{ + return DB::statement('TRUNCATE `LogEntries`'); } - -?> diff --git a/includes/model/Message_model.php b/includes/model/Message_model.php index 652b60ea..ebd4b37e 100644 --- a/includes/model/Message_model.php +++ b/includes/model/Message_model.php @@ -1,27 +1,30 @@ <?php +use Engelsystem\Database\DB; + /** * Returns Message id array + * + * @return array */ -function Message_ids() { - return sql_select("SELECT `id` FROM `Messages`"); +function Message_ids() +{ + return DB::select('SELECT `id` FROM `Messages`'); } /** * Returns message by id. * - * @param $message_id message - * ID + * @param int $message_id message ID + * @return array|null */ -function Message($message_id) { - $message_source = sql_select("SELECT * FROM `Messages` WHERE `id`='" . sql_escape($message_id) . "' LIMIT 1"); - if ($message_source === false) { - return false; - } - if (count($message_source) > 0) { - return $message_source[0]; - } - return null; +function Message($message_id) +{ + $message_source = DB::select('SELECT * FROM `Messages` WHERE `id`=? LIMIT 1', [$message_id]); + if (empty($message_source)) { + return null; + } + return array_shift($message_source); } /** @@ -29,23 +32,39 @@ function Message($message_id) { * TODO: global $user con not be used in model! * send message * - * @param $receiver_user_id User - * ID of Reciever - * @param $text Text - * of Message + * @param int $receiver_user_id User ID of Reciever + * @param string $text Text of Message + * @return bool */ -function Message_send($receiver_user_id, $text) { - global $user; - - $text = preg_replace("/([^\p{L}\p{P}\p{Z}\p{N}\n]{1,})/ui", '', strip_tags($text)); - $receiver_user_id = preg_replace("/([^0-9]{1,})/ui", '', strip_tags($receiver_user_id)); - - if (($text != "" && is_numeric($receiver_user_id)) && (sql_num_query("SELECT * FROM `User` WHERE `UID`='" . sql_escape($receiver_user_id) . "' AND NOT `UID`='" . sql_escape($user['UID']) . "' LIMIT 1") > 0)) { - sql_query("INSERT INTO `Messages` SET `Datum`='" . sql_escape(time()) . "', `SUID`='" . sql_escape($user['UID']) . "', `RUID`='" . sql_escape($receiver_user_id) . "', `Text`='" . sql_escape($text) . "'"); - return true; - } - - return false; -} +function Message_send($receiver_user_id, $text) +{ + global $user; -?>
\ No newline at end of file + $text = preg_replace("/([^\p{L}\p{P}\p{Z}\p{N}\n]{1,})/ui", '', strip_tags($text)); + $receiver_user_id = preg_replace('/([^\d]{1,})/ui', '', strip_tags($receiver_user_id)); + + if ( + ($text != '' && is_numeric($receiver_user_id)) + && count(DB::select(' + SELECT `UID` + FROM `User` + WHERE `UID` = ? + AND NOT `UID` = ? + LIMIT 1 + ', [$receiver_user_id, $user['UID']])) > 0 + ) { + return DB::insert(' + INSERT INTO `Messages` (`Datum`, `SUID`, `RUID`, `Text`) + VALUES(?, ?, ?, ?) + ', + [ + time(), + $user['UID'], + $receiver_user_id, + $text + ] + ); + } + + return false; +} diff --git a/includes/model/NeededAngelTypes_model.php b/includes/model/NeededAngelTypes_model.php index 3bd3660f..97b085f0 100644 --- a/includes/model/NeededAngelTypes_model.php +++ b/includes/model/NeededAngelTypes_model.php @@ -1,5 +1,7 @@ <?php +use Engelsystem\Database\DB; + /** * Entity needed angeltypes describes how many angels of given type are needed for a shift or in a room. */ @@ -7,99 +9,107 @@ /** * Insert a new needed angel type. * - * @param int $shift_id - * The shift. Can be null, but then a room_id must be given. - * @param int $angeltype_id - * The angeltype - * @param int $room_id - * The room. Can be null, but then a shift_id must be given. - * @param int $count - * How many angels are needed? + * @param int $shift_id The shift. Can be null, but then a room_id must be given. + * @param int $angeltype_id The angeltype + * @param int $room_id The room. Can be null, but then a shift_id must be given. + * @param int $count How many angels are needed? + * @return int|false */ -function NeededAngelType_add($shift_id, $angeltype_id, $room_id, $count) { - $result = sql_query(" - INSERT INTO `NeededAngelTypes` SET - `shift_id`=" . sql_null($shift_id) . ", - `angel_type_id`='" . sql_escape($angeltype_id) . "', - `room_id`=" . sql_null($room_id) . ", - `count`='" . sql_escape($count) . "'"); - if ($result === false) { - return false; - } - return sql_id(); +function NeededAngelType_add($shift_id, $angeltype_id, $room_id, $count) +{ + $result = DB::insert(' + INSERT INTO `NeededAngelTypes` ( `shift_id`, `angel_type_id`, `room_id`, `count`) + VALUES (?, ?, ?, ?) + ', + [ + $shift_id, + $angeltype_id, + $room_id, + $count, + ]); + if ($result === false) { + return false; + } + + return DB::getPdo()->lastInsertId(); } /** * Deletes all needed angel types from given shift. * - * @param int $shift_id - * id of the shift + * @param int $shift_id id of the shift + * @return int count of affected rows */ -function NeededAngelTypes_delete_by_shift($shift_id) { - return sql_query("DELETE FROM `NeededAngelTypes` WHERE `shift_id`='" . sql_escape($shift_id) . "'"); +function NeededAngelTypes_delete_by_shift($shift_id) +{ + return (int)DB::delete('DELETE FROM `NeededAngelTypes` WHERE `shift_id` = ?', [$shift_id]); } /** * Deletes all needed angel types from given room. * - * @param int $room_id - * id of the room + * @param int $room_id id of the room + * @return int count of affected rows */ -function NeededAngelTypes_delete_by_room($room_id) { - return sql_query("DELETE FROM `NeededAngelTypes` WHERE `room_id`='" . sql_escape($room_id) . "'"); +function NeededAngelTypes_delete_by_room($room_id) +{ + return (int)DB::delete( + 'DELETE FROM `NeededAngelTypes` WHERE `room_id` = ?', + [$room_id] + ); } /** * Returns all needed angeltypes and already taken needs. * - * @param int $shiftID - * id of shift + * @param int $shiftId id of shift + * @return array */ -function NeededAngelTypes_by_shift($shiftId) { - $needed_angeltypes_source = sql_select(" +function NeededAngelTypes_by_shift($shiftId) +{ + $needed_angeltypes_source = DB::select(' SELECT `NeededAngelTypes`.*, `AngelTypes`.`id`, `AngelTypes`.`name`, `AngelTypes`.`restricted`, `AngelTypes`.`no_self_signup` FROM `NeededAngelTypes` JOIN `AngelTypes` ON `AngelTypes`.`id` = `NeededAngelTypes`.`angel_type_id` - WHERE `shift_id`='" . sql_escape($shiftId) . "' + WHERE `shift_id` = ? AND `count` > 0 - ORDER BY `room_id` DESC - "); - if ($needed_angeltypes_source === false) { - engelsystem_error("Unable to load needed angeltypes."); - } - - // Use settings from room - if (count($needed_angeltypes_source) == 0) { - $needed_angeltypes_source = sql_select(" + ORDER BY `room_id` DESC', + [$shiftId] + ); + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load needed angeltypes.'); + } + + // Use settings from room + if (count($needed_angeltypes_source) == 0) { + $needed_angeltypes_source = DB::select(' SELECT `NeededAngelTypes`.*, `AngelTypes`.`name`, `AngelTypes`.`restricted` FROM `NeededAngelTypes` JOIN `AngelTypes` ON `AngelTypes`.`id` = `NeededAngelTypes`.`angel_type_id` JOIN `Shifts` ON `Shifts`.`RID` = `NeededAngelTypes`.`room_id` - WHERE `Shifts`.`SID`='" . sql_escape($shiftId) . "' + WHERE `Shifts`.`SID` = ? AND `count` > 0 ORDER BY `room_id` DESC - "); - if ($needed_angeltypes_source === false) { - engelsystem_error("Unable to load needed angeltypes."); + ', [$shiftId]); + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load needed angeltypes.'); + } } - } - - $shift_entries = ShiftEntries_by_shift($shiftId); - $needed_angeltypes = []; - foreach ($needed_angeltypes_source as $angeltype) { - $angeltype['shift_entries'] = []; - $angeltype['taken'] = 0; - foreach ($shift_entries as $shift_entry) { - if ($shift_entry['TID'] == $angeltype['angel_type_id'] && $shift_entry['freeloaded'] == 0) { - $angeltype['taken'] ++; - $angeltype['shift_entries'][] = $shift_entry; - } + + $shift_entries = ShiftEntries_by_shift($shiftId); + $needed_angeltypes = []; + foreach ($needed_angeltypes_source as $angeltype) { + $angeltype['shift_entries'] = []; + $angeltype['taken'] = 0; + foreach ($shift_entries as $shift_entry) { + if ($shift_entry['TID'] == $angeltype['angel_type_id'] && $shift_entry['freeloaded'] == 0) { + $angeltype['taken']++; + $angeltype['shift_entries'][] = $shift_entry; + } + } + + $needed_angeltypes[] = $angeltype; } - - $needed_angeltypes[] = $angeltype; - } - - return $needed_angeltypes; -} -?>
\ No newline at end of file + return $needed_angeltypes; +} diff --git a/includes/model/Room_model.php b/includes/model/Room_model.php index 14935de0..c8399bc4 100644 --- a/includes/model/Room_model.php +++ b/includes/model/Room_model.php @@ -1,60 +1,82 @@ <?php +use Engelsystem\Database\DB; + /** * returns a list of rooms. + * * @param boolean $show_all returns also hidden rooms when true + * @return array */ -function Rooms($show_all = false) { - return sql_select("SELECT * FROM `Room`" . ($show_all ? "" : " WHERE `show`='Y'") . " ORDER BY `Name`"); +function Rooms($show_all = false) +{ + return DB::select('SELECT * FROM `Room`' . ($show_all ? '' : ' WHERE `show`=\'Y\'') . ' ORDER BY `Name`'); } /** * Delete a room * - * @param int $room_id + * @param int $room_id + * @return bool */ -function Room_delete($room_id) { - return sql_query("DELETE FROM `Room` WHERE `RID`=" . sql_escape($room_id)); +function Room_delete($room_id) +{ + return DB::delete('DELETE FROM `Room` WHERE `RID` = ?', [$room_id]); } /** * Create a new room * - * @param string $name - * Name of the room - * @param boolean $from_frab - * Is this a frab imported room? - * @param boolean $public - * Is the room visible for angels? + * @param string $name Name of the room + * @param boolean $from_frab Is this a frab imported room? + * @param boolean $public Is the room visible for angels? + * @param int $number Room number + * @return false|int */ -function Room_create($name, $from_frab, $public) { - $result = sql_query(" - INSERT INTO `Room` SET - `Name`='" . sql_escape($name) . "', - `FromPentabarf`='" . sql_escape($from_frab ? 'Y' : '') . "', - `show`='" . sql_escape($public ? 'Y' : '') . "', - `Number`=0"); - if ($result === false) { - return false; - } - return sql_id(); +function Room_create($name, $from_frab, $public, $number = null) +{ + $result = DB::insert(' + INSERT INTO `Room` (`Name`, `FromPentabarf`, `show`, `Number`) + VALUES (?, ?, ?, ?) + ', + [ + $name, + $from_frab ? 'Y' : '', + $public ? 'Y' : '', + (int)$number, + ] + ); + if (!$result) { + return false; + } + + return DB::getPdo()->lastInsertId(); } /** * Returns room by id. * - * @param $room_id RID + * @param int $room_id RID + * @param bool $show_only + * @return array|false */ -function Room($room_id) { - $room_source = sql_select("SELECT * FROM `Room` WHERE `RID`='" . sql_escape($room_id) . "'"); - - if ($room_source === false) { - return false; - } - if (count($room_source) > 0) { - return $room_source[0]; - } - return null; -} +function Room($room_id, $show_only = true) +{ + $room_source = DB::select(' + SELECT * + FROM `Room` + WHERE `RID` = ? + ' . ($show_only ? 'AND `show` = \'Y\'' : ''), + [$room_id] + ); -?> + if (DB::getStm()->errorCode() != '00000') { + return false; + } + + if (empty($room_source)) { + return null; + } + + return array_shift($room_source); +} diff --git a/includes/model/ShiftEntry_model.php b/includes/model/ShiftEntry_model.php index 9f4faf9e..d7810feb 100644 --- a/includes/model/ShiftEntry_model.php +++ b/includes/model/ShiftEntry_model.php @@ -1,152 +1,251 @@ <?php +use Engelsystem\Database\DB; + /** * Returns an array with the attributes of shift entries. * FIXME! Needs entity object. + * + * @return array */ -function ShiftEntry_new() { - return [ - 'id' => null, - 'SID' => null, - 'TID' => null, - 'UID' => null, - 'Comment' => null, - 'freeloaded_comment' => null, - 'freeloaded' => false - ]; +function ShiftEntry_new() +{ + return [ + 'id' => null, + 'SID' => null, + 'TID' => null, + 'UID' => null, + 'Comment' => null, + 'freeloaded_comment' => null, + 'freeloaded' => false + ]; } /** * Counts all freeloaded shifts. + * + * @return int */ -function ShiftEntries_freeleaded_count() { - return sql_select_single_cell("SELECT COUNT(*) FROM `ShiftEntry` WHERE `freeloaded` = 1"); +function ShiftEntries_freeleaded_count() +{ + $result = DB::select('SELECT COUNT(*) FROM `ShiftEntry` WHERE `freeloaded` = 1'); + $result = array_shift($result); + + if (!is_array($result)) { + return 0; + } + + return (int)array_shift($result); } /** * List users subsribed to a given shift. + * + * @param int $shift_id + * @return array */ -function ShiftEntries_by_shift($shift_id) { - return sql_select(" - SELECT `User`.`Nick`, `User`.`email`, `User`.`email_shiftinfo`, `User`.`Sprache`, `User`.`Gekommen`, `ShiftEntry`.`UID`, `ShiftEntry`.`TID`, `ShiftEntry`.`SID`, `AngelTypes`.`name` as `angel_type_name`, `ShiftEntry`.`Comment`, `ShiftEntry`.`freeloaded` - FROM `ShiftEntry` - JOIN `User` ON `ShiftEntry`.`UID`=`User`.`UID` - JOIN `AngelTypes` ON `ShiftEntry`.`TID`=`AngelTypes`.`id` - WHERE `ShiftEntry`.`SID`='" . sql_escape($shift_id) . "'"); +function ShiftEntries_by_shift($shift_id) +{ + return DB::select(' + SELECT + `User`.`Nick`, + `User`.`email`, + `User`.`email_shiftinfo`, + `User`.`Sprache`, + `User`.`Gekommen`, + `ShiftEntry`.`UID`, + `ShiftEntry`.`TID`, + `ShiftEntry`.`SID`, + `AngelTypes`.`name` AS `angel_type_name`, + `ShiftEntry`.`Comment`, + `ShiftEntry`.`freeloaded` + FROM `ShiftEntry` + JOIN `User` ON `ShiftEntry`.`UID`=`User`.`UID` + JOIN `AngelTypes` ON `ShiftEntry`.`TID`=`AngelTypes`.`id` + WHERE `ShiftEntry`.`SID` = ?', + [$shift_id] + ); } /** * Create a new shift entry. * - * @param ShiftEntry $shift_entry + * @param array $shift_entry + * @return bool */ -function ShiftEntry_create($shift_entry) { - mail_shift_assign(User($shift_entry['UID']), Shift($shift_entry['SID'])); - return sql_query("INSERT INTO `ShiftEntry` SET - `SID`='" . sql_escape($shift_entry['SID']) . "', - `TID`='" . sql_escape($shift_entry['TID']) . "', - `UID`='" . sql_escape($shift_entry['UID']) . "', - `Comment`='" . sql_escape($shift_entry['Comment']) . "', - `freeload_comment`='" . sql_escape($shift_entry['freeload_comment']) . "', - `freeloaded`=" . sql_bool($shift_entry['freeloaded'])); +function ShiftEntry_create($shift_entry) +{ + mail_shift_assign(User($shift_entry['UID']), Shift($shift_entry['SID'])); + return DB::insert(' + INSERT INTO `ShiftEntry` ( + `SID`, + `TID`, + `UID`, + `Comment`, + `freeload_comment`, + `freeloaded` + ) + VALUES(?, ?, ?, ?, ?, ?) + ', + [ + $shift_entry['SID'], + $shift_entry['TID'], + $shift_entry['UID'], + $shift_entry['Comment'], + $shift_entry['freeload_comment'], + $shift_entry['freeloaded'], + ] + ); } /** * Update a shift entry. + * + * @param array $shift_entry + * @return bool */ -function ShiftEntry_update($shift_entry) { - return sql_query("UPDATE `ShiftEntry` SET - `Comment`='" . sql_escape($shift_entry['Comment']) . "', - `freeload_comment`='" . sql_escape($shift_entry['freeload_comment']) . "', - `freeloaded`=" . sql_bool($shift_entry['freeloaded']) . " - WHERE `id`='" . sql_escape($shift_entry['id']) . "'"); +function ShiftEntry_update($shift_entry) +{ + DB::update(' + UPDATE `ShiftEntry` + SET + `Comment` = ?, + `freeload_comment` = ?, + `freeloaded` = ? + WHERE `id` = ?', + [ + $shift_entry['Comment'], + $shift_entry['freeload_comment'], + $shift_entry['freeloaded'], + $shift_entry['id'] + ] + ); + + return (DB::getStm()->errorCode() == '00000'); } /** * Get a shift entry. + * + * @param int $shift_entry_id + * @return array|false|null */ -function ShiftEntry($shift_entry_id) { - $shift_entry = sql_select("SELECT * FROM `ShiftEntry` WHERE `id`='" . sql_escape($shift_entry_id) . "'"); - if ($shift_entry === false) { - return false; - } - if (count($shift_entry) == 0) { - return null; - } - return $shift_entry[0]; +function ShiftEntry($shift_entry_id) +{ + $shift_entry = DB::select('SELECT * FROM `ShiftEntry` WHERE `id` = ?', [$shift_entry_id]); + if (DB::getStm()->errorCode() != '00000') { + return false; + } + if (empty($shift_entry)) { + return null; + } + return $shift_entry[0]; } /** * Delete a shift entry. + * + * @param int $shift_entry_id + * @return bool */ -function ShiftEntry_delete($shift_entry_id) { - $shift_entry = ShiftEntry($shift_entry_id); - mail_shift_removed(User($shift_entry['UID']), Shift($shift_entry['SID'])); - return sql_query("DELETE FROM `ShiftEntry` WHERE `id`='" . sql_escape($shift_entry_id) . "'"); +function ShiftEntry_delete($shift_entry_id) +{ + $shift_entry = ShiftEntry($shift_entry_id); + mail_shift_removed(User($shift_entry['UID']), Shift($shift_entry['SID'])); + return DB::delete('DELETE FROM `ShiftEntry` WHERE `id` = ?', [$shift_entry_id]); } /** * Returns next (or current) shifts of given user. * - * @param User $user + * @param array $user + * @return array */ -function ShiftEntries_upcoming_for_user($user) { - return sql_select(" - SELECT * - FROM `ShiftEntry` - JOIN `Shifts` ON (`Shifts`.`SID` = `ShiftEntry`.`SID`) - JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id` - WHERE `ShiftEntry`.`UID`=" . sql_escape($user['UID']) . " - AND `Shifts`.`end` > " . sql_escape(time()) . " - ORDER BY `Shifts`.`end` - "); +function ShiftEntries_upcoming_for_user($user) +{ + return DB::select(' + SELECT * + FROM `ShiftEntry` + JOIN `Shifts` ON (`Shifts`.`SID` = `ShiftEntry`.`SID`) + JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id` + WHERE `ShiftEntry`.`UID` = ? + AND `Shifts`.`end` > ? + ORDER BY `Shifts`.`end` + ', + [ + $user['UID'], + time(), + ] + ); } /** * Returns shifts completed by the given user. * - * @param User $user + * @param array $user + * @return array */ -function ShiftEntries_finished_by_user($user) { - return sql_select(" - SELECT * - FROM `ShiftEntry` - JOIN `Shifts` ON (`Shifts`.`SID` = `ShiftEntry`.`SID`) - JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id` - WHERE `ShiftEntry`.`UID`=" . sql_escape($user['UID']) . " - AND `Shifts`.`end` < " . sql_escape(time()) . " - AND `ShiftEntry`.`freeloaded` = 0 - ORDER BY `Shifts`.`end` - "); +function ShiftEntries_finished_by_user($user) +{ + return DB::select(' + SELECT * + FROM `ShiftEntry` + JOIN `Shifts` ON (`Shifts`.`SID` = `ShiftEntry`.`SID`) + JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id` + WHERE `ShiftEntry`.`UID` = ? + AND `Shifts`.`end` < ? + AND `ShiftEntry`.`freeloaded` = 0 + ORDER BY `Shifts`.`end` + ', + [ + $user['UID'], + time(), + ] + ); } /** * Returns all shift entries in given shift for given angeltype. * - * @param int $shift_id - * @param int $angeltype_id + * @param int $shift_id + * @param int $angeltype_id + * @return array */ -function ShiftEntries_by_shift_and_angeltype($shift_id, $angeltype_id) { - $result = sql_select(" - SELECT * - FROM `ShiftEntry` - WHERE `SID`=" . sql_escape($shift_id) . " - AND `TID`=" . sql_escape($angeltype_id) . " - "); - if ($result === false) { - engelsystem_error("Unable to load shift entries."); - } - return $result; +function ShiftEntries_by_shift_and_angeltype($shift_id, $angeltype_id) +{ + $result = DB::select(' + SELECT * + FROM `ShiftEntry` + WHERE `SID` = ? + AND `TID` = ? + ', + [ + $shift_id, + $angeltype_id, + ] + ); + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load shift entries.'); + } + return $result; } /** * Returns all freeloaded shifts for given user. + * + * @param array $user + * @return array */ -function ShiftEntries_freeloaded_by_user($user) { - return sql_select("SELECT * - FROM `ShiftEntry` - WHERE `freeloaded` = 1 - AND `UID`=" . sql_escape($user['UID'])); +function ShiftEntries_freeloaded_by_user($user) +{ + return DB::select(' + SELECT * + FROM `ShiftEntry` + WHERE `freeloaded` = 1 + AND `UID` = ? + ', + [ + $user['UID'] + ] + ); } - -?> diff --git a/includes/model/ShiftSignupState.php b/includes/model/ShiftSignupState.php index 393023ee..9b3de496 100644 --- a/includes/model/ShiftSignupState.php +++ b/includes/model/ShiftSignupState.php @@ -6,112 +6,136 @@ namespace Engelsystem; * BO to represent if there are free slots on a shift for a given angeltype * and if signup for a given user is possible (or not, because of collisions, etc.) */ -class ShiftSignupState { - - /** - * Shift has free places - */ - const FREE = 'FREE'; - - /** - * Shift collides with users shifts - */ - const COLLIDES = 'COLLIDES'; - - /** - * User cannot join because of a restricted angeltype or user is not in the angeltype - */ - const ANGELTYPE = 'ANGELTYPE'; - - /** - * Shift is full - */ - const OCCUPIED = 'OCCUPIED'; - - /** - * User is admin and can do what he wants. - */ - const ADMIN = 'ADMIN'; - - /** - * Shift has already ended, no signup - */ - const SHIFT_ENDED = 'SHIFT_ENDED'; - - /** - * User is already signed up - */ - const SIGNED_UP = 'SIGNED_UP'; - - private $state; - - private $freeEntries; - - public function __construct($state, $free_entries) { - $this->state = $state; - $this->freeEntries = $free_entries; - } - - /** - * Combine this state with another state from the same shift. - * - * @param ShiftSignupState $shiftSignupState - * The other state to combine - */ - public function combineWith(ShiftSignupState $shiftSignupState) { - $this->freeEntries += $shiftSignupState->getFreeEntries(); - - if ($this->valueForState($shiftSignupState->state) > $this->valueForState($this->state)) { - $this->state = $shiftSignupState->state; +class ShiftSignupState +{ + /** + * Shift has free places + */ + const FREE = 'FREE'; + + /** + * Shift collides with users shifts + */ + const COLLIDES = 'COLLIDES'; + + /** + * User cannot join because of a restricted angeltype or user is not in the angeltype + */ + const ANGELTYPE = 'ANGELTYPE'; + + /** + * Shift is full + */ + const OCCUPIED = 'OCCUPIED'; + + /** + * User is admin and can do what he wants. + */ + const ADMIN = 'ADMIN'; + + /** + * Shift has already ended, no signup + */ + const SHIFT_ENDED = 'SHIFT_ENDED'; + + /** + * User is already signed up + */ + const SIGNED_UP = 'SIGNED_UP'; + + /** @var string */ + private $state; + + /** @var int */ + private $freeEntries; + + /** + * ShiftSignupState constructor. + * + * @param string $state + * @param int $free_entries + */ + public function __construct($state, $free_entries) + { + $this->state = $state; + $this->freeEntries = $free_entries; } - } - - private function valueForState($state) { - switch ($state) { - case ShiftSignupState::SHIFT_ENDED: - return 100; - - case ShiftSignupState::SIGNED_UP: - return 90; - - case ShiftSignupState::FREE: - return 80; - - case ShiftSignupState::ANGELTYPE: - case ShiftSignupState::COLLIDES: - return 70; - - case ShiftSignupState::OCCUPIED: - case ShiftSignupState::ADMIN: - return 60; + + /** + * Combine this state with another state from the same shift. + * + * @param ShiftSignupState $shiftSignupState + * The other state to combine + */ + public function combineWith(ShiftSignupState $shiftSignupState) + { + $this->freeEntries += $shiftSignupState->getFreeEntries(); + + if ($this->valueForState($shiftSignupState->state) > $this->valueForState($this->state)) { + $this->state = $shiftSignupState->state; + } } - } - - /** - * Returns true, if signup is allowed - */ - public function isSignupAllowed() { - switch ($this->state) { - case ShiftSignupState::FREE: - case ShiftSignupState::ADMIN: - return true; + + /** + * @param string $state + * @return int + */ + private function valueForState($state) + { + switch ($state) { + case ShiftSignupState::SHIFT_ENDED: + return 100; + + case ShiftSignupState::SIGNED_UP: + return 90; + + case ShiftSignupState::FREE: + return 80; + + case ShiftSignupState::ANGELTYPE: + case ShiftSignupState::COLLIDES: + return 70; + + case ShiftSignupState::OCCUPIED: + case ShiftSignupState::ADMIN: + return 60; + default: + return 0; + } + } + + /** + * Returns true, if signup is allowed + * + * @return bool + */ + public function isSignupAllowed() + { + switch ($this->state) { + case ShiftSignupState::FREE: + case ShiftSignupState::ADMIN: + return true; + } + return false; } - return false; - } - - /** - * Return the shift signup state - */ - public function getState() { - return $this->state; - } - - /** - * How many places are free in this shift for the angeltype? - */ - public function getFreeEntries() { - return $this->freeEntries; - } -} -?>
\ No newline at end of file + /** + * Return the shift signup state + * + * @return string + */ + public function getState() + { + return $this->state; + } + + /** + * How many places are free in this shift for the angeltype? + * + * @return int + */ + public function getFreeEntries() + { + return $this->freeEntries; + } +} diff --git a/includes/model/ShiftTypes_model.php b/includes/model/ShiftTypes_model.php index 89704a65..96a823d4 100644 --- a/includes/model/ShiftTypes_model.php +++ b/includes/model/ShiftTypes_model.php @@ -1,69 +1,105 @@ <?php +use Engelsystem\Database\DB; + /** * Delete a shift type. + * * @param int $shifttype_id + * @return bool */ -function ShiftType_delete($shifttype_id) { - return sql_query("DELETE FROM `ShiftTypes` WHERE `id`='" . sql_escape($shifttype_id) . "'"); +function ShiftType_delete($shifttype_id) +{ + return DB::delete('DELETE FROM `ShiftTypes` WHERE `id`=?', [$shifttype_id]); } /** * Update a shift type. * - * @param int $shifttype_id - * @param string $name - * @param int $angeltype_id - * @param string $description + * @param int $shifttype_id + * @param string $name + * @param int $angeltype_id + * @param string $description + * @return bool */ -function ShiftType_update($shifttype_id, $name, $angeltype_id, $description) { - return sql_query("UPDATE `ShiftTypes` SET - `name`='" . sql_escape($name) . "', - `angeltype_id`=" . sql_null($angeltype_id) . ", - `description`='" . sql_escape($description) . "' - WHERE `id`='" . sql_escape($shifttype_id) . "'"); +function ShiftType_update($shifttype_id, $name, $angeltype_id, $description) +{ + DB::update(' + UPDATE `ShiftTypes` SET + `name`=?, + `angeltype_id`=?, + `description`=? + WHERE `id`=? + ', + [ + $name, + $angeltype_id, + $description, + $shifttype_id, + ] + ); + + return DB::getStm()->errorCode() == '00000'; } /** * Create a shift type. * - * @param string $name - * @param int $angeltype_id - * @param string $description - * @return new shifttype id + * @param string $name + * @param int $angeltype_id + * @param string $description + * @return int|false new shifttype id */ -function ShiftType_create($name, $angeltype_id, $description) { - $result = sql_query("INSERT INTO `ShiftTypes` SET - `name`='" . sql_escape($name) . "', - `angeltype_id`=" . sql_null($angeltype_id) . ", - `description`='" . sql_escape($description) . "'"); - if ($result === false) { - return false; - } - return sql_id(); +function ShiftType_create($name, $angeltype_id, $description) +{ + $result = DB::insert(' + INSERT INTO `ShiftTypes` (`name`, `angeltype_id`, `description`) + VALUES(?, ?, ?) + ', + [ + $name, + $angeltype_id, + $description + ] + ); + + if ($result === false) { + return false; + } + + return DB::getPdo()->lastInsertId(); } /** * Get a shift type by id. * - * @param int $shifttype_id + * @param int $shifttype_id + * @return array|null */ -function ShiftType($shifttype_id) { - $shifttype = sql_select("SELECT * FROM `ShiftTypes` WHERE `id`='" . sql_escape($shifttype_id) . "'"); - if ($shifttype === false) { - engelsystem_error('Unable to load shift type.'); - } - if ($shifttype == null) { - return null; - } - return $shifttype[0]; +function ShiftType($shifttype_id) +{ + $shifttype = DB::select('SELECT * FROM `ShiftTypes` WHERE `id`=?', [$shifttype_id]); + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load shift type.'); + } + if (empty($shifttype)) { + return null; + } + return array_shift($shifttype); } /** * Get all shift types. + * + * @return array|false */ -function ShiftTypes() { - return sql_select("SELECT * FROM `ShiftTypes` ORDER BY `name`"); -} +function ShiftTypes() +{ + $result = DB::select('SELECT * FROM `ShiftTypes` ORDER BY `name`'); -?>
\ No newline at end of file + if (DB::getStm()->errorCode() != '00000') { + return false; + } + + return $result; +} diff --git a/includes/model/ShiftsFilter.php b/includes/model/ShiftsFilter.php index ff1f5d40..47ef50d7 100644 --- a/includes/model/ShiftsFilter.php +++ b/includes/model/ShiftsFilter.php @@ -7,115 +7,170 @@ namespace Engelsystem; * * @author msquare */ -class ShiftsFilter { - - /** - * How long can the time interval be? - */ - const MAX_DURATION = 86400; - // one day - - /** - * Shift is completely full. - */ - const FILLED_FILLED = 1; - - /** - * Shift has some free slots. - */ - const FILLED_FREE = 0; - - /** - * Has the user "user shifts admin" privilege? - * - * @var boolean - */ - private $userShiftsAdmin; - - private $filled = []; - - private $rooms = []; - - private $types = []; - - private $startTime = null; - - private $endTime = null; - - public function __construct($user_shifts_admin, $rooms, $types) { - $this->user_shifts_admin = $user_shifts_admin; - $this->rooms = $rooms; - $this->types = $types; - - $this->filled = [ - ShiftsFilter::FILLED_FREE - ]; - - if ($user_shifts_admin) { - $this->filled[] = ShiftsFilter::FILLED_FILLED; +class ShiftsFilter +{ + /** + * How long can the time interval be? + * 86400 = one day + */ + const MAX_DURATION = 86400; + + /** + * Shift is completely full. + */ + const FILLED_FILLED = 1; + + /** + * Shift has some free slots. + */ + const FILLED_FREE = 0; + + /** + * Has the user "user shifts admin" privilege? + * + * @var boolean + */ + private $userShiftsAdmin; + + /** @var int[] */ + private $filled = []; + + /** @var int[] */ + private $rooms = []; + + /** @var int[] */ + private $types = []; + + /** @var int unix timestamp */ + private $startTime = null; + + /** @var int unix timestamp */ + private $endTime = null; + + /** + * ShiftsFilter constructor. + * + * @param bool $user_shifts_admin + * @param int[] $rooms + * @param int[] $types + */ + public function __construct($user_shifts_admin, $rooms, $types) + { + $this->user_shifts_admin = $user_shifts_admin; + $this->rooms = $rooms; + $this->types = $types; + + $this->filled = [ + ShiftsFilter::FILLED_FREE + ]; + + if ($user_shifts_admin) { + $this->filled[] = ShiftsFilter::FILLED_FILLED; + } } - } - public function getStartTime() { - return $this->startTime; - } + /** + * @return int unix timestamp + */ + public function getStartTime() + { + return $this->startTime; + } - public function setStartTime($startTime) { - $this->startTime = $startTime; - } + /** + * @param int $startTime unix timestamp + */ + public function setStartTime($startTime) + { + $this->startTime = $startTime; + } - public function getEndTime() { - return $this->endTime; - } + /** + * @return int unix timestamp + */ + public function getEndTime() + { + return $this->endTime; + } - public function setEndTime($endTime) { - if ($endTime - $this->startTime > ShiftsFilter::MAX_DURATION) { - $endTime = $this->startTime + ShiftsFilter::MAX_DURATION; + /** + * @param int $endTime unix timestamp + */ + public function setEndTime($endTime) + { + if ($endTime - $this->startTime > ShiftsFilter::MAX_DURATION) { + $endTime = $this->startTime + ShiftsFilter::MAX_DURATION; + } + $this->endTime = $endTime; } - $this->endTime = $endTime; - } - - public function getTypes() { - if (count($this->types) == 0) { - return [ - 0 - ]; + + /** + * @return int[] + */ + public function getTypes() + { + if (count($this->types) == 0) { + return [0]; + } + return $this->types; } - return $this->types; - } - - public function setTypes($types) { - $this->types = $types; - } - - public function getRooms() { - if (count($this->rooms) == 0) { - return [ - 0 - ]; + + /** + * @param int[] $types + */ + public function setTypes($types) + { + $this->types = $types; } - return $this->rooms; - } - public function setRooms($rooms) { - $this->rooms = $rooms; - } + /** + * @return int[] + */ + public function getRooms() + { + if (count($this->rooms) == 0) { + return [0]; + } + return $this->rooms; + } - public function isUserShiftsAdmin() { - return $this->userShiftsAdmin; - } + /** + * @param int[] $rooms + */ + public function setRooms($rooms) + { + $this->rooms = $rooms; + } - public function setUserShiftsAdmin($userShiftsAdmin) { - $this->userShiftsAdmin = $userShiftsAdmin; - } + /** + * @return bool + */ + public function isUserShiftsAdmin() + { + return $this->userShiftsAdmin; + } - public function getFilled() { - return $this->filled; - } + /** + * @param bool $userShiftsAdmin + */ + public function setUserShiftsAdmin($userShiftsAdmin) + { + $this->userShiftsAdmin = $userShiftsAdmin; + } - public function setFilled($filled) { - $this->filled = $filled; - } -} + /** + * @return int[] + */ + public function getFilled() + { + return $this->filled; + } -?>
\ No newline at end of file + /** + * @param int[] $filled + */ + public function setFilled($filled) + { + $this->filled = $filled; + } +} diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index 29156423..b0269362 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -1,432 +1,641 @@ <?php + +use Engelsystem\Database\DB; use Engelsystem\ShiftsFilter; use Engelsystem\ShiftSignupState; -function Shifts_by_room($room) { - $result = sql_select("SELECT * FROM `Shifts` WHERE `RID`=" . sql_escape($room['RID']) . " ORDER BY `start`"); - if ($result === false) { - engelsystem_error("Unable to load shifts."); - } - return $result; +/** + * @param array $room + * @return array + */ +function Shifts_by_room($room) +{ + $result = DB::select('SELECT * FROM `Shifts` WHERE `RID`=? ORDER BY `start`', [$room['RID']]); + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load shifts.'); + } + return $result; } -function Shifts_by_ShiftsFilter(ShiftsFilter $shiftsFilter) { - $SQL = "SELECT * FROM ( - SELECT DISTINCT `Shifts`.*, `ShiftTypes`.`name`, `Room`.`Name` as `room_name` +/** + * @param ShiftsFilter $shiftsFilter + * @return array[] + */ +function Shifts_by_ShiftsFilter(ShiftsFilter $shiftsFilter) +{ + $sql = 'SELECT * FROM ( + SELECT DISTINCT `Shifts`.*, `ShiftTypes`.`name`, `Room`.`Name` AS `room_name` FROM `Shifts` JOIN `Room` USING (`RID`) JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id` JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id`=`Shifts`.`SID` - WHERE `Shifts`.`RID` IN (" . implode(',', $shiftsFilter->getRooms()) . ") - AND `start` BETWEEN " . $shiftsFilter->getStartTime() . " AND " . $shiftsFilter->getEndTime() . " - AND `NeededAngelTypes`.`angel_type_id` IN (" . implode(',', $shiftsFilter->getTypes()) . ") + WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ') + AND `start` BETWEEN ? AND ? + AND `NeededAngelTypes`.`angel_type_id` IN (' . implode(',', $shiftsFilter->getTypes()) . ') AND `NeededAngelTypes`.`count` > 0 AND `Shifts`.`PSID` IS NULL - + UNION - - SELECT DISTINCT `Shifts`.*, `ShiftTypes`.`name`, `Room`.`Name` as `room_name` + + SELECT DISTINCT `Shifts`.*, `ShiftTypes`.`name`, `Room`.`Name` AS `room_name` FROM `Shifts` JOIN `Room` USING (`RID`) JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id` JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id`=`Shifts`.`RID` - WHERE `Shifts`.`RID` IN (" . implode(',', $shiftsFilter->getRooms()) . ") - AND `start` BETWEEN " . $shiftsFilter->getStartTime() . " AND " . $shiftsFilter->getEndTime() . " - AND `NeededAngelTypes`.`angel_type_id` IN (" . implode(',', $shiftsFilter->getTypes()) . ") + WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ') + AND `start` BETWEEN ? AND ? + AND `NeededAngelTypes`.`angel_type_id` IN (' . implode(',', $shiftsFilter->getTypes()) . ') AND `NeededAngelTypes`.`count` > 0 - AND NOT `Shifts`.`PSID` IS NULL) as tmp_shifts - - ORDER BY `start`"; - $result = sql_select($SQL); - if ($result === false) { - engelsystem_error("Unable to load shifts by filter."); - } - return $result; + AND NOT `Shifts`.`PSID` IS NULL) AS tmp_shifts + + ORDER BY `start`'; + $result = DB::select( + $sql, + [ + $shiftsFilter->getStartTime(), + $shiftsFilter->getEndTime(), + $shiftsFilter->getStartTime(), + $shiftsFilter->getEndTime(), + ] + ); + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load shifts by filter.'); + } + return $result; } -function NeededAngeltypes_by_ShiftsFilter(ShiftsFilter $shiftsFilter) { - $SQL = "SELECT `NeededAngelTypes`.*, `Shifts`.`SID`, `AngelTypes`.`id`, `AngelTypes`.`name`, `AngelTypes`.`restricted`, `AngelTypes`.`no_self_signup` +/** + * @param ShiftsFilter $shiftsFilter + * @return array[] + */ +function NeededAngeltypes_by_ShiftsFilter(ShiftsFilter $shiftsFilter) +{ + $sql = ' + SELECT + `NeededAngelTypes`.*, + `Shifts`.`SID`, + `AngelTypes`.`id`, + `AngelTypes`.`name`, + `AngelTypes`.`restricted`, + `AngelTypes`.`no_self_signup` FROM `Shifts` JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id`=`Shifts`.`SID` JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id` - WHERE `Shifts`.`RID` IN (" . implode(',', $shiftsFilter->getRooms()) . ") - AND `start` BETWEEN " . $shiftsFilter->getStartTime() . " AND " . $shiftsFilter->getEndTime() . " + WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ') + AND `start` BETWEEN ? AND ? AND `Shifts`.`PSID` IS NULL UNION - SELECT `NeededAngelTypes`.*, `Shifts`.`SID`, `AngelTypes`.`id`, `AngelTypes`.`name`, `AngelTypes`.`restricted`, `AngelTypes`.`no_self_signup` + SELECT + `NeededAngelTypes`.*, + `Shifts`.`SID`, + `AngelTypes`.`id`, + `AngelTypes`.`name`, + `AngelTypes`.`restricted`, + `AngelTypes`.`no_self_signup` FROM `Shifts` JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id`=`Shifts`.`RID` JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id` - WHERE `Shifts`.`RID` IN (" . implode(',', $shiftsFilter->getRooms()) . ") - AND `start` BETWEEN " . $shiftsFilter->getStartTime() . " AND " . $shiftsFilter->getEndTime() . " - AND NOT `Shifts`.`PSID` IS NULL"; - $result = sql_select($SQL); - if ($result === false) { - engelsystem_error("Unable to load needed angeltypes by filter."); - } - return $result; + WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ') + AND `start` BETWEEN ? AND ? + AND NOT `Shifts`.`PSID` IS NULL'; + $result = DB::select( + $sql, + [ + $shiftsFilter->getStartTime(), + $shiftsFilter->getEndTime(), + $shiftsFilter->getStartTime(), + $shiftsFilter->getEndTime(), + ] + ); + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load needed angeltypes by filter.'); + } + return $result; } -function NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype) { - $result = sql_select("SELECT `NeededAngelTypes`.*, `Shifts`.`SID`, `AngelTypes`.`id`, `AngelTypes`.`name`, `AngelTypes`.`restricted`, `AngelTypes`.`no_self_signup` - FROM `Shifts` - JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id`=`Shifts`.`SID` - JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id` - WHERE `Shifts`.`SID`=" . sql_escape($shift['SID']) . " - AND `AngelTypes`.`id`=" . sql_escape($angeltype['id']) . " - AND `Shifts`.`PSID` IS NULL - - UNION - - SELECT `NeededAngelTypes`.*, `Shifts`.`SID`, `AngelTypes`.`id`, `AngelTypes`.`name`, `AngelTypes`.`restricted`, `AngelTypes`.`no_self_signup` - FROM `Shifts` - JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id`=`Shifts`.`RID` - JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id` - WHERE `Shifts`.`SID`=" . sql_escape($shift['SID']) . " - AND `AngelTypes`.`id`=" . sql_escape($angeltype['id']) . " - AND NOT `Shifts`.`PSID` IS NULL"); - if ($result === false) { - engelsystem_error("Unable to load needed angeltypes by filter."); - } - if (count($result) == 0) { - return null; - } - return $result[0]; +/** + * @param array $shift + * @param array $angeltype + * @return array|null + */ +function NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype) +{ + $result = DB::select(' + SELECT + `NeededAngelTypes`.*, + `Shifts`.`SID`, + `AngelTypes`.`id`, + `AngelTypes`.`name`, + `AngelTypes`.`restricted`, + `AngelTypes`.`no_self_signup` + FROM `Shifts` + JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id`=`Shifts`.`SID` + JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id` + WHERE `Shifts`.`SID`=? + AND `AngelTypes`.`id`=? + AND `Shifts`.`PSID` IS NULL + + UNION + + SELECT + `NeededAngelTypes`.*, + `Shifts`.`SID`, + `AngelTypes`.`id`, + `AngelTypes`.`name`, + `AngelTypes`.`restricted`, + `AngelTypes`.`no_self_signup` + FROM `Shifts` + JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id`=`Shifts`.`RID` + JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id` + WHERE `Shifts`.`SID`=? + AND `AngelTypes`.`id`=? + AND NOT `Shifts`.`PSID` IS NULL + ', + [ + $shift['SID'], + $angeltype['id'], + $shift['SID'], + $angeltype['id'] + ] + ); + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load needed angeltypes by filter.'); + } + if (empty($result)) { + return null; + } + return $result[0]; } -function ShiftEntries_by_ShiftsFilter(ShiftsFilter $shiftsFilter) { - $SQL = "SELECT `User`.`Nick`, `User`.`email`, `User`.`email_shiftinfo`, `User`.`Sprache`, `User`.`Gekommen`, `ShiftEntry`.`UID`, `ShiftEntry`.`TID`, `ShiftEntry`.`SID`, `ShiftEntry`.`Comment`, `ShiftEntry`.`freeloaded` +/** + * @param ShiftsFilter $shiftsFilter + * @return array + */ +function ShiftEntries_by_ShiftsFilter(ShiftsFilter $shiftsFilter) +{ + $sql = ' + SELECT + `User`.`Nick`, + `User`.`email`, + `User`.`email_shiftinfo`, + `User`.`Sprache`, + `User`.`Gekommen`, + `ShiftEntry`.`UID`, + `ShiftEntry`.`TID`, + `ShiftEntry`.`SID`, + `ShiftEntry`.`Comment`, + `ShiftEntry`.`freeloaded` FROM `Shifts` JOIN `ShiftEntry` ON `ShiftEntry`.`SID`=`Shifts`.`SID` JOIN `User` ON `ShiftEntry`.`UID`=`User`.`UID` - WHERE `Shifts`.`RID` IN (" . implode(',', $shiftsFilter->getRooms()) . ") - AND `start` BETWEEN " . $shiftsFilter->getStartTime() . " AND " . $shiftsFilter->getEndTime() . " - ORDER BY `Shifts`.`start`"; - $result = sql_select($SQL); - if ($result === false) { - engelsystem_error("Unable to load shift entries by filter."); - } - return $result; + WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ') + AND `start` BETWEEN ? AND ? + ORDER BY `Shifts`.`start`'; + $result = DB::select( + $sql, + [ + $shiftsFilter->getStartTime(), + $shiftsFilter->getEndTime(), + ] + ); + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load shift entries by filter.'); + } + return $result; } /** * Check if a shift collides with other shifts (in time). * - * @param Shift $shift - * @param array<Shift> $shifts + * @param array $shift + * @param array $shifts + * @return bool */ -function Shift_collides($shift, $shifts) { - foreach ($shifts as $other_shift) { - if ($shift['SID'] != $other_shift['SID']) { - if (! ($shift['start'] >= $other_shift['end'] || $shift['end'] <= $other_shift['start'])) { - return true; - } - } - } - return false; +function Shift_collides($shift, $shifts) +{ + foreach ($shifts as $other_shift) { + if ($shift['SID'] != $other_shift['SID']) { + if (!($shift['start'] >= $other_shift['end'] || $shift['end'] <= $other_shift['start'])) { + return true; + } + } + } + return false; } /** * Returns the number of needed angels/free shift entries for an angeltype. + * + * @param array $needed_angeltype + * @param array[] $shift_entries + * @return int */ -function Shift_free_entries($needed_angeltype, $shift_entries) { - $taken = 0; - foreach ($shift_entries as $shift_entry) { - if ($shift_entry['freeloaded'] == 0) { - $taken ++; - } - } - return max(0, $needed_angeltype['count'] - $taken); +function Shift_free_entries($needed_angeltype, $shift_entries) +{ + $taken = 0; + foreach ($shift_entries as $shift_entry) { + if ($shift_entry['freeloaded'] == 0) { + $taken++; + } + } + return max(0, $needed_angeltype['count'] - $taken); } /** * Check if shift signup is allowed from the end users point of view (no admin like privileges) * - * @param Shift $shift - * The shift - * @param AngelType $angeltype - * The angeltype to which the user wants to sign up - * @param array<Shift> $user_shifts - * List of the users shifts - * @param boolean $angeltype_supporter - * True, if the user has angeltype supporter rights for the angeltype, which enables him to sign somebody up for the shift. + * @param array $user + * @param array $shift The shift + * @param array $angeltype The angeltype to which the user wants to sign up + * @param array|null $user_angeltype + * @param array|null $user_shifts List of the users shifts + * @param array $needed_angeltype + * @param array[] $shift_entries + * @return ShiftSignupState */ -function Shift_signup_allowed_angel($user, $shift, $angeltype, $user_angeltype, $user_shifts, $needed_angeltype, $shift_entries) { - $free_entries = Shift_free_entries($needed_angeltype, $shift_entries); - - if ($user['Gekommen'] == 0) { - return new ShiftSignupState(ShiftSignupState::SHIFT_ENDED, $free_entries); - } - - if ($user_shifts == null) { - $user_shifts = Shifts_by_user($user); - } - - $signed_up = false; - foreach ($user_shifts as $user_shift) { - if ($user_shift['SID'] == $shift['SID']) { - $signed_up = true; - break; - } - } - - if ($signed_up) { - // you cannot join if you already singed up for this shift - return new ShiftSignupState(ShiftSignupState::SIGNED_UP, $free_entries); - } - - if (time() > $shift['start']) { - // you can only join if the shift is in future - return new ShiftSignupState(ShiftSignupState::SHIFT_ENDED, $free_entries); - } - if ($free_entries == 0) { - // you cannot join if shift is full - return new ShiftSignupState(ShiftSignupState::OCCUPIED, $free_entries); - } - - if ($user_angeltype == null) { - $user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype); - } - - if ($user_angeltype == null || ($angeltype['no_self_signup'] == 1 && $user_angeltype != null) || ($angeltype['restricted'] == 1 && $user_angeltype != null && ! isset($user_angeltype['confirm_user_id']))) { - // you cannot join if user is not of this angel type - // you cannot join if you are not confirmed - // you cannot join if angeltype has no self signup - - return new ShiftSignupState(ShiftSignupState::ANGELTYPE, $free_entries); - } - - if (Shift_collides($shift, $user_shifts)) { - // you cannot join if user alread joined a parallel or this shift - return new ShiftSignupState(ShiftSignupState::COLLIDES, $free_entries); - } - - // Hooray, shift is free for you! - return new ShiftSignupState(ShiftSignupState::FREE, $free_entries); +function Shift_signup_allowed_angel( + $user, + $shift, + $angeltype, + $user_angeltype, + $user_shifts, + $needed_angeltype, + $shift_entries +) { + $free_entries = Shift_free_entries($needed_angeltype, $shift_entries); + + if ($user['Gekommen'] == 0) { + return new ShiftSignupState(ShiftSignupState::SHIFT_ENDED, $free_entries); + } + + if ($user_shifts == null) { + $user_shifts = Shifts_by_user($user); + } + + $signed_up = false; + foreach ($user_shifts as $user_shift) { + if ($user_shift['SID'] == $shift['SID']) { + $signed_up = true; + break; + } + } + + if ($signed_up) { + // you cannot join if you already singed up for this shift + return new ShiftSignupState(ShiftSignupState::SIGNED_UP, $free_entries); + } + + if (time() > $shift['start']) { + // you can only join if the shift is in future + return new ShiftSignupState(ShiftSignupState::SHIFT_ENDED, $free_entries); + } + if ($free_entries == 0) { + // you cannot join if shift is full + return new ShiftSignupState(ShiftSignupState::OCCUPIED, $free_entries); + } + + if ($user_angeltype == null) { + $user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype); + } + + if ( + $user_angeltype == null + || ($angeltype['no_self_signup'] == 1 && $user_angeltype != null) + || ($angeltype['restricted'] == 1 && $user_angeltype != null && !isset($user_angeltype['confirm_user_id'])) + ) { + // you cannot join if user is not of this angel type + // you cannot join if you are not confirmed + // you cannot join if angeltype has no self signup + + return new ShiftSignupState(ShiftSignupState::ANGELTYPE, $free_entries); + } + + if (Shift_collides($shift, $user_shifts)) { + // you cannot join if user alread joined a parallel or this shift + return new ShiftSignupState(ShiftSignupState::COLLIDES, $free_entries); + } + + // Hooray, shift is free for you! + return new ShiftSignupState(ShiftSignupState::FREE, $free_entries); } /** * Check if an angeltype supporter can sign up a user to a shift. + * + * @param array $needed_angeltype + * @param array[] $shift_entries + * @return ShiftSignupState */ -function Shift_signup_allowed_angeltype_supporter($angeltype, $needed_angeltype, $shift_entries) { - $free_entries = Shift_free_entries($needed_angeltype, $shift_entries); - if ($free_entries == 0) { - return new ShiftSignupState(ShiftSignupState::OCCUPIED, $free_entries); - } - - return new ShiftSignupState(ShiftSignupState::FREE, $free_entries); +function Shift_signup_allowed_angeltype_supporter($needed_angeltype, $shift_entries) +{ + $free_entries = Shift_free_entries($needed_angeltype, $shift_entries); + if ($free_entries == 0) { + return new ShiftSignupState(ShiftSignupState::OCCUPIED, $free_entries); + } + + return new ShiftSignupState(ShiftSignupState::FREE, $free_entries); } /** * Check if an admin can sign up a user to a shift. * - * @param Shift $shift - * The shift - * @param AngelType $angeltype - * The angeltype to which the user wants to sign up + * @param array $needed_angeltype + * @param array[] $shift_entries + * @return ShiftSignupState */ -function Shift_signup_allowed_admin($angeltype, $needed_angeltype, $shift_entries) { - $free_entries = Shift_free_entries($needed_angeltype, $shift_entries); - - if ($free_entries == 0) { - // User shift admins may join anybody in every shift - return new ShiftSignupState(ShiftSignupState::ADMIN, $free_entries); - } - - return new ShiftSignupState(ShiftSignupState::FREE, $free_entries); +function Shift_signup_allowed_admin($needed_angeltype, $shift_entries) +{ + $free_entries = Shift_free_entries($needed_angeltype, $shift_entries); + + if ($free_entries == 0) { + // User shift admins may join anybody in every shift + return new ShiftSignupState(ShiftSignupState::ADMIN, $free_entries); + } + + return new ShiftSignupState(ShiftSignupState::FREE, $free_entries); } /** * Check if an angel can sign up for given shift. * - * @param Shift $shift - * The shift - * @param AngelType $angeltype - * The angeltype to which the user wants to sign up - * @param array<Shift> $user_shifts - * List of the users shifts + * @param array $signup_user + * @param array $shift The shift + * @param array $angeltype The angeltype to which the user wants to sign up + * @param array|null $user_angeltype + * @param array|null $user_shifts List of the users shifts + * @param array $needed_angeltype + * @param array[] $shift_entries + * @return ShiftSignupState */ -function Shift_signup_allowed($signup_user, $shift, $angeltype, $user_angeltype = null, $user_shifts = null, $needed_angeltype, $shift_entries) { - global $user, $privileges; - - if (in_array('user_shifts_admin', $privileges)) { - return Shift_signup_allowed_admin($angeltype, $needed_angeltype, $shift_entries); - } - - if (in_array('shiftentry_edit_angeltype_supporter', $privileges) && User_is_AngelType_supporter($user, $angeltype)) { - return Shift_signup_allowed_angeltype_supporter($angeltype, $needed_angeltype, $shift_entries); - } - - return Shift_signup_allowed_angel($signup_user, $shift, $angeltype, $user_angeltype, $user_shifts, $needed_angeltype, $shift_entries); +function Shift_signup_allowed( + $signup_user, + $shift, + $angeltype, + $user_angeltype, + $user_shifts, + $needed_angeltype, + $shift_entries +) { + global $user, $privileges; + + if (in_array('user_shifts_admin', $privileges)) { + return Shift_signup_allowed_admin($needed_angeltype, $shift_entries); + } + + if ( + in_array('shiftentry_edit_angeltype_supporter', $privileges) + && User_is_AngelType_supporter($user, $angeltype) + ) { + return Shift_signup_allowed_angeltype_supporter($needed_angeltype, $shift_entries); + } + + return Shift_signup_allowed_angel( + $signup_user, + $shift, + $angeltype, + $user_angeltype, + $user_shifts, + $needed_angeltype, + $shift_entries + ); } /** * Delete a shift by its external id. + * + * @param int $shift_psid + * @return bool */ -function Shift_delete_by_psid($shift_psid) { - return sql_query("DELETE FROM `Shifts` WHERE `PSID`='" . sql_escape($shift_psid) . "'"); +function Shift_delete_by_psid($shift_psid) +{ + DB::delete('DELETE FROM `Shifts` WHERE `PSID`=?', [$shift_psid]); + + if (DB::getStm()->errorCode() != '00000') { + return false; + } + + return true; } /** * Delete a shift. + * + * @param int $shift_id + * @return bool */ -function Shift_delete($shift_id) { - mail_shift_delete(Shift($shift_id)); - - $result = sql_query("DELETE FROM `Shifts` WHERE `SID`='" . sql_escape($shift_id) . "'"); - if ($result === false) { - engelsystem_error('Unable to delete shift.'); - } - return $result; +function Shift_delete($shift_id) +{ + mail_shift_delete(Shift($shift_id)); + + $result = DB::delete('DELETE FROM `Shifts` WHERE `SID`=?', [$shift_id]); + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to delete shift.'); + } + return $result; } /** * Update a shift. + * + * @param array $shift + * @return bool */ -function Shift_update($shift) { - global $user; - $shift['name'] = ShiftType($shift['shifttype_id'])['name']; - mail_shift_change(Shift($shift['SID']), $shift); - - return sql_query("UPDATE `Shifts` SET - `shifttype_id`='" . sql_escape($shift['shifttype_id']) . "', - `start`='" . sql_escape($shift['start']) . "', - `end`='" . sql_escape($shift['end']) . "', - `RID`='" . sql_escape($shift['RID']) . "', - `title`=" . sql_null($shift['title']) . ", - `URL`=" . sql_null($shift['URL']) . ", - `PSID`=" . sql_null($shift['PSID']) . ", - `edited_by_user_id`='" . sql_escape($user['UID']) . "', - `edited_at_timestamp`=" . time() . " - WHERE `SID`='" . sql_escape($shift['SID']) . "'"); +function Shift_update($shift) +{ + global $user; + $shift['name'] = ShiftType($shift['shifttype_id'])['name']; + mail_shift_change(Shift($shift['SID']), $shift); + + return (bool)DB::update(' + UPDATE `Shifts` SET + `shifttype_id` = ?, + `start` = ?, + `end` = ?, + `RID` = ?, + `title` = ?, + `URL` = ?, + `PSID` = ?, + `edited_by_user_id` = ?, + `edited_at_timestamp` = ? + WHERE `SID` = ? + ', + [ + $shift['shifttype_id'], + $shift['start'], + $shift['end'], + $shift['RID'], + $shift['title'], + $shift['URL'], + $shift['PSID'], + $user['UID'], + time(), + $shift['SID'] + ] + ); } /** * Update a shift by its external id. + * + * @param array $shift + * @return bool|null */ -function Shift_update_by_psid($shift) { - $shift_source = sql_select("SELECT `SID` FROM `Shifts` WHERE `PSID`=" . $shift['PSID']); - if ($shift_source === false) { - return false; - } - if (count($shift_source) == 0) { - return null; - } - $shift['SID'] = $shift_source[0]['SID']; - return Shift_update($shift); +function Shift_update_by_psid($shift) +{ + $shift_source = DB::select('SELECT `SID` FROM `Shifts` WHERE `PSID`=?', [$shift['PSID']]); + if (DB::getStm()->errorCode() != '00000') { + return false; + } + + if (empty($shift_source)) { + return null; + } + + $shift['SID'] = $shift_source[0]['SID']; + return Shift_update($shift); } /** * Create a new shift. * - * @return new shift id or false + * @param array $shift + * @return int|false shift id or false */ -function Shift_create($shift) { - global $user; - $result = sql_query("INSERT INTO `Shifts` SET - `shifttype_id`='" . sql_escape($shift['shifttype_id']) . "', - `start`='" . sql_escape($shift['start']) . "', - `end`='" . sql_escape($shift['end']) . "', - `RID`='" . sql_escape($shift['RID']) . "', - `title`=" . sql_null($shift['title']) . ", - `URL`=" . sql_null($shift['URL']) . ", - `PSID`=" . sql_null($shift['PSID']) . ", - `created_by_user_id`='" . sql_escape($user['UID']) . "', - `created_at_timestamp`=" . time()); - if ($result === false) { - return false; - } - return sql_id(); +function Shift_create($shift) +{ + global $user; + DB::insert(' + INSERT INTO `Shifts` ( + `shifttype_id`, + `start`, + `end`, + `RID`, + `title`, + `URL`, + `PSID`, + `created_by_user_id`, + `created_at_timestamp` + ) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) + ', + [ + $shift['shifttype_id'], + $shift['start'], + $shift['end'], + $shift['RID'], + $shift['title'], + $shift['URL'], + $shift['PSID'], + $user['UID'], + time(), + ] + ); + if (DB::getStm()->errorCode() != '00000') { + return false; + } + return DB::getPdo()->lastInsertId(); } /** * Return users shifts. + * + * @param array $user + * @param bool $include_freeload_comments + * @return array */ -function Shifts_by_user($user, $include_freeload_comments = false) { - $result = sql_select(" - SELECT `ShiftTypes`.`id` as `shifttype_id`, `ShiftTypes`.`name`, - `ShiftEntry`.`id`, `ShiftEntry`.`SID`, `ShiftEntry`.`TID`, `ShiftEntry`.`UID`, `ShiftEntry`.`freeloaded`, `ShiftEntry`.`Comment`, - " . ($include_freeload_comments ? "`ShiftEntry`.`freeload_comment`, " : "") . " - `Shifts`.*, `Room`.* - FROM `ShiftEntry` - JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`) - JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`) - JOIN `Room` ON (`Shifts`.`RID` = `Room`.`RID`) - WHERE `UID`='" . sql_escape($user['UID']) . "' - ORDER BY `start` - "); - if ($result === false) { - engelsystem_error('Unable to load users shifts.'); - } - return $result; +function Shifts_by_user($user, $include_freeload_comments = false) +{ + $result = DB::select(' + SELECT `ShiftTypes`.`id` AS `shifttype_id`, `ShiftTypes`.`name`, + `ShiftEntry`.`id`, `ShiftEntry`.`SID`, `ShiftEntry`.`TID`, `ShiftEntry`.`UID`, `ShiftEntry`.`freeloaded`, `ShiftEntry`.`Comment`, + ' . ($include_freeload_comments ? '`ShiftEntry`.`freeload_comment`, ' : '') . ' + `Shifts`.*, `Room`.* + FROM `ShiftEntry` + JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`) + JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`) + JOIN `Room` ON (`Shifts`.`RID` = `Room`.`RID`) + WHERE `UID` = ? + ORDER BY `start` + ', + [ + $user['UID'] + ] + ); + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load users shifts.'); + } + return $result; } /** * Returns Shift by id. * - * @param $shift_id Shift - * ID + * @param int $shift_id Shift ID + * @return array|null */ -function Shift($shift_id) { - $shifts_source = sql_select(" +function Shift($shift_id) +{ + $shifts_source = DB::select(' SELECT `Shifts`.*, `ShiftTypes`.`name` - FROM `Shifts` + FROM `Shifts` JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`) - WHERE `SID`='" . sql_escape($shift_id) . "'"); - $shiftsEntry_source = sql_select("SELECT `id`, `TID` , `UID` , `freeloaded` FROM `ShiftEntry` WHERE `SID`='" . sql_escape($shift_id) . "'"); - - if ($shifts_source === false) { - engelsystem_error('Unable to load shift.'); - } - - if (empty($shifts_source)) { - return null; - } - - $result = $shifts_source[0]; - - $result['ShiftEntry'] = $shiftsEntry_source; - $result['NeedAngels'] = []; - - $temp = NeededAngelTypes_by_shift($shift_id); - foreach ($temp as $e) { - $result['NeedAngels'][] = [ - 'TID' => $e['angel_type_id'], - 'count' => $e['count'], - 'restricted' => $e['restricted'], - 'taken' => $e['taken'] - ]; - } - - return $result; + WHERE `SID`=?', [$shift_id]); + + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load shift.'); + } + + if (empty($shifts_source)) { + return null; + } + + $result = $shifts_source[0]; + + $shiftsEntry_source = DB::select(' + SELECT `id`, `TID` , `UID` , `freeloaded` + FROM `ShiftEntry` + WHERE `SID`=?', [$shift_id]); + + $result['ShiftEntry'] = $shiftsEntry_source; + $result['NeedAngels'] = []; + + $angelTypes = NeededAngelTypes_by_shift($shift_id); + foreach ($angelTypes as $type) { + $result['NeedAngels'][] = [ + 'TID' => $type['angel_type_id'], + 'count' => $type['count'], + 'restricted' => $type['restricted'], + 'taken' => $type['taken'] + ]; + } + + return $result; } /** * Returns all shifts with needed angeltypes and count of subscribed jobs. + * + * @return array|false */ -function Shifts() { - $shifts_source = sql_select(" - SELECT `ShiftTypes`.`name`, `Shifts`.*, `Room`.`RID`, `Room`.`Name` as `room_name` - FROM `Shifts` - JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`) - JOIN `Room` ON `Room`.`RID` = `Shifts`.`RID` - "); - if ($shifts_source === false) { - return false; - } - - foreach ($shifts_source as &$shift) { - $needed_angeltypes = NeededAngelTypes_by_shift($shift['SID']); - if ($needed_angeltypes === false) { - return false; - } - - $shift['angeltypes'] = $needed_angeltypes; - } - - return $shifts_source; -} +function Shifts() +{ + $shifts_source = DB::select(' + SELECT `ShiftTypes`.`name`, `Shifts`.*, `Room`.`RID`, `Room`.`Name` AS `room_name` + FROM `Shifts` + JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`) + JOIN `Room` ON `Room`.`RID` = `Shifts`.`RID` + '); + + if (DB::getStm()->errorCode() != '00000') { + return false; + } -?> + foreach ($shifts_source as &$shift) { + $needed_angeltypes = NeededAngelTypes_by_shift($shift['SID']); + $shift['angeltypes'] = $needed_angeltypes; + } + + return $shifts_source; +} diff --git a/includes/model/UserAngelTypes_model.php b/includes/model/UserAngelTypes_model.php index 11366cdf..82f390ee 100644 --- a/includes/model/UserAngelTypes_model.php +++ b/includes/model/UserAngelTypes_model.php @@ -1,5 +1,7 @@ <?php +use Engelsystem\Database\DB; + /** * User angeltypes model */ @@ -7,223 +9,283 @@ /** * Checks if a user joined an angeltype. * - * @param User $user - * The user to be checked - * @param Angeltype $angeltype - * The angeltype to be checked + * @param array $user The user to be checked + * @param array $angeltype The angeltype to be checked * @return boolean */ -function UserAngelType_exists($user, $angeltype) { - return sql_num_query(" - SELECT `id` +function UserAngelType_exists($user, $angeltype) +{ + return count(DB::select(' + SELECT `id` FROM `UserAngelTypes` - WHERE `UserAngelTypes`.`user_id`='" . sql_escape($user['UID']) . "' - AND `angeltype_id`='" . sql_escape($angeltype['id']) . "' - ") > 0; + WHERE `UserAngelTypes`.`user_id`=? + AND `angeltype_id`=? + ', [$user['UID'], $angeltype['id']])) > 0; } /** * List users angeltypes. * - * @param User $user + * @param array $user + * @return array|false */ -function User_angeltypes($user) { - $result = sql_select(" +function User_angeltypes($user) +{ + $result = DB::select(' SELECT `AngelTypes`.*, `UserAngelTypes`.`confirm_user_id`, `UserAngelTypes`.`supporter` FROM `UserAngelTypes` JOIN `AngelTypes` ON `UserAngelTypes`.`angeltype_id` = `AngelTypes`.`id` - WHERE `UserAngelTypes`.`user_id`='" . sql_escape($user['UID']) . "' - "); - if ($result === false) { - engelsystem_error("Unable to load user angeltypes."); - return false; - } - return $result; + WHERE `UserAngelTypes`.`user_id`=? + ', [$user['UID']]); + + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load user angeltypes.'); + return false; + } + + return $result; } /** * Gets unconfirmed user angeltypes for angeltypes of which the given user is a supporter. * - * @param User $user + * @param array $user + * @return array */ -function User_unconfirmed_AngelTypes($user) { - $result = sql_select(" - SELECT - `UserAngelTypes`.*, - `AngelTypes`.`name`, - count(`UnconfirmedMembers`.`user_id`) as `count` - FROM `UserAngelTypes` - JOIN `AngelTypes` ON `UserAngelTypes`.`angeltype_id`=`AngelTypes`.`id` - JOIN `UserAngelTypes` as `UnconfirmedMembers` ON `UserAngelTypes`.`angeltype_id`=`UnconfirmedMembers`.`angeltype_id` - WHERE `UserAngelTypes`.`user_id`='" . sql_escape($user['UID']) . "' - AND `UserAngelTypes`.`supporter`=TRUE - AND `AngelTypes`.`restricted`=TRUE - AND `UnconfirmedMembers`.`confirm_user_id` IS NULL - GROUP BY `UserAngelTypes`.`angeltype_id` - ORDER BY `AngelTypes`.`name`"); - if ($result === false) { - engelsystem_error("Unable to load user angeltypes."); - } - return $result; +function User_unconfirmed_AngelTypes($user) +{ + $result = DB::select(' + SELECT + `UserAngelTypes`.*, + `AngelTypes`.`name`, + count(`UnconfirmedMembers`.`user_id`) AS `count` + FROM `UserAngelTypes` + JOIN `AngelTypes` ON `UserAngelTypes`.`angeltype_id`=`AngelTypes`.`id` + JOIN `UserAngelTypes` AS `UnconfirmedMembers` ON `UserAngelTypes`.`angeltype_id`=`UnconfirmedMembers`.`angeltype_id` + WHERE `UserAngelTypes`.`user_id`=? + AND `UserAngelTypes`.`supporter`=TRUE + AND `AngelTypes`.`restricted`=TRUE + AND `UnconfirmedMembers`.`confirm_user_id` IS NULL + GROUP BY `UserAngelTypes`.`angeltype_id` + ORDER BY `AngelTypes`.`name` + ', [$user['UID']]); + + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load user angeltypes.'); + } + + return $result; } /** * Returns true if user is angeltype supporter or has privilege admin_user_angeltypes. * - * @param User $user - * @param AngelType $angeltype + * @param array $user + * @param array $angeltype + * @return bool */ -function User_is_AngelType_supporter(&$user, $angeltype) { - if(!isset($user['privileges'])) { - $user['privileges'] = privileges_for_user($user['UID']); - } - return (sql_num_query(" - SELECT `id` - FROM `UserAngelTypes` - WHERE `user_id`='" . sql_escape($user['UID']) . "' - AND `angeltype_id`='" . sql_escape($angeltype['id']) . "' - AND `supporter`=TRUE - LIMIT 1") > 0) || in_array('admin_user_angeltypes', $user['privileges']); +function User_is_AngelType_supporter(&$user, $angeltype) +{ + if (!isset($user['privileges'])) { + $user['privileges'] = privileges_for_user($user['UID']); + } + return (count(DB::select(' + SELECT `id` + FROM `UserAngelTypes` + WHERE `user_id`=? + AND `angeltype_id`=? + AND `supporter`=TRUE + LIMIT 1 + ', + [ + $user['UID'], + $angeltype['id'] + ] + )) > 0) + || in_array('admin_user_angeltypes', $user['privileges']); } /** * Add or remove supporter rights. * - * @param int $user_angeltype_id - * @param bool $supporter + * @param int $user_angeltype_id + * @param bool $supporter + * @return int */ -function UserAngelType_update($user_angeltype_id, $supporter) { - $result = sql_query(" +function UserAngelType_update($user_angeltype_id, $supporter) +{ + $result = DB::update(' UPDATE `UserAngelTypes` - SET `supporter`=" . sql_bool($supporter) . " - WHERE `id`='" . sql_escape($user_angeltype_id) . "' - LIMIT 1"); - if ($result === false) { - engelsystem_error("Unable to update supporter rights."); - } - return $result; + SET `supporter`=? + WHERE `id`=? + LIMIT 1 + ', [$supporter, $user_angeltype_id]); + + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to update supporter rights.'); + } + + return $result; } /** * Delete all unconfirmed UserAngelTypes for given Angeltype. * - * @param int $angeltype_id + * @param int $angeltype_id + * @return bool */ -function UserAngelTypes_delete_all($angeltype_id) { - $result = sql_query(" +function UserAngelTypes_delete_all($angeltype_id) +{ + DB::delete(' DELETE FROM `UserAngelTypes` - WHERE `angeltype_id`='" . sql_escape($angeltype_id) . "' - AND `confirm_user_id` IS NULL"); - if ($result === false) { - engelsystem_error("Unable to delete all unconfirmed users."); - } - return $result; + WHERE `angeltype_id`=? + AND `confirm_user_id` IS NULL + ', [$angeltype_id]); + + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to delete all unconfirmed users.'); + } + + return true; } /** * Confirm all unconfirmed UserAngelTypes for given Angeltype. * - * @param int $angeltype_id - * @param User $confirm_user + * @param int $angeltype_id + * @param array $confirm_user + * @return bool */ -function UserAngelTypes_confirm_all($angeltype_id, $confirm_user) { - $result = sql_query(" +function UserAngelTypes_confirm_all($angeltype_id, $confirm_user) +{ + $result = DB::update(' UPDATE `UserAngelTypes` - SET `confirm_user_id`='" . sql_escape($confirm_user['UID']) . "' - WHERE `angeltype_id`='" . sql_escape($angeltype_id) . "' - AND `confirm_user_id` IS NULL"); - if ($result === false) { - engelsystem_error("Unable to confirm all users."); - } - return $result; + SET `confirm_user_id`=? + WHERE `angeltype_id`=? + AND `confirm_user_id` IS NULL + ', [$confirm_user['UID'], $angeltype_id]); + + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to confirm all users.'); + } + + return (bool)$result; } /** * Confirm an UserAngelType with confirming user. * - * @param int $user_angeltype_id - * @param User $confirm_user + * @param int $user_angeltype_id + * @param array $confirm_user + * @return bool */ -function UserAngelType_confirm($user_angeltype_id, $confirm_user) { - $result = sql_query(" +function UserAngelType_confirm($user_angeltype_id, $confirm_user) +{ + $result = DB::update(' UPDATE `UserAngelTypes` - SET `confirm_user_id`='" . sql_escape($confirm_user['UID']) . "' - WHERE `id`='" . sql_escape($user_angeltype_id) . "' - LIMIT 1"); - if ($result === false) { - engelsystem_error("Unable to confirm user angeltype."); - } - return $result; + SET `confirm_user_id`=? + WHERE `id`=? + LIMIT 1', [$confirm_user['UID'], $user_angeltype_id]); + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to confirm user angeltype.'); + } + return (bool)$result; } /** * Delete an UserAngelType. * - * @param UserAngelType $user_angeltype + * @param array $user_angeltype + * @return bool */ -function UserAngelType_delete($user_angeltype) { - return sql_query(" - DELETE FROM `UserAngelTypes` - WHERE `id`='" . sql_escape($user_angeltype['id']) . "' - LIMIT 1"); +function UserAngelType_delete($user_angeltype) +{ + return (bool)DB::delete(' + DELETE FROM `UserAngelTypes` + WHERE `id`=? + LIMIT 1', [$user_angeltype['id']]); } /** * Create an UserAngelType. * - * @param User $user - * @param Angeltype $angeltype + * @param array $user + * @param array $angeltype + * @return int */ -function UserAngelType_create($user, $angeltype) { - $result = sql_query(" - INSERT INTO `UserAngelTypes` SET - `user_id`='" . sql_escape($user['UID']) . "', - `angeltype_id`='" . sql_escape($angeltype['id']) . "'"); - if ($result === false) { - engelsystem_error("Unable to create user angeltype."); - } - return sql_id(); +function UserAngelType_create($user, $angeltype) +{ + DB::insert(' + INSERT INTO `UserAngelTypes` (`user_id`, `angeltype_id`) + VALUES (?, ?) + ', + [ + $user['UID'], + $angeltype['id'] + ] + ); + + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to create user angeltype.'); + } + + return DB::getPdo()->lastInsertId(); } /** * Get an UserAngelType by its id. * - * @param int $user_angeltype_id + * @param int $user_angeltype_id + * @return array|null */ -function UserAngelType($user_angeltype_id) { - $angeltype = sql_select(" +function UserAngelType($user_angeltype_id) +{ + $angeltype = DB::select(' SELECT * FROM `UserAngelTypes` - WHERE `id`='" . sql_escape($user_angeltype_id) . "' - LIMIT 1"); - if ($angeltype === false) { - engelsystem_error("Unable to load user angeltype."); - } - if (count($angeltype) == 0) { - return null; - } - return $angeltype[0]; + WHERE `id`=? + LIMIT 1', [$user_angeltype_id]); + + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load user angeltype.'); + } + + if (empty($angeltype)) { + return null; + } + + return $angeltype[0]; } /** * Get an UserAngelType by user and angeltype. * - * @param User $user - * @param Angeltype $angeltype + * @param array $user + * @param array $angeltype + * @return array|null */ -function UserAngelType_by_User_and_AngelType($user, $angeltype) { - $angeltype = sql_select(" - SELECT * - FROM `UserAngelTypes` - WHERE `user_id`='" . sql_escape($user['UID']) . "' - AND `angeltype_id`='" . sql_escape($angeltype['id']) . "' - LIMIT 1"); - if ($angeltype === false) { - engelsystem_error("Unable to load user angeltype."); - } - if (count($angeltype) == 0) { - return null; - } - return $angeltype[0]; +function UserAngelType_by_User_and_AngelType($user, $angeltype) +{ + $angeltype = DB::select(' + SELECT * + FROM `UserAngelTypes` + WHERE `user_id`=? + AND `angeltype_id`=? + LIMIT 1 + ', + [ + $user['UID'], + $angeltype['id'] + ] + ); + + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load user angeltype.'); + } + + if (empty($angeltype)) { + return null; + } + + return array_shift($angeltype); } -?>
\ No newline at end of file diff --git a/includes/model/UserDriverLicenses_model.php b/includes/model/UserDriverLicenses_model.php index afc44575..5ff4df35 100644 --- a/includes/model/UserDriverLicenses_model.php +++ b/includes/model/UserDriverLicenses_model.php @@ -1,104 +1,152 @@ <?php +use Engelsystem\Database\DB; + /** * Returns a new empty UserDriverLicense * FIXME entity object needed + * + * @return array */ -function UserDriverLicense_new() { - return [ - 'user_id' => null, - 'has_car' => false, - 'has_license_car' => false, - 'has_license_3_5t_transporter' => false, - 'has_license_7_5t_truck' => false, - 'has_license_12_5t_truck' => false, - 'has_license_forklift' => false - ]; +function UserDriverLicense_new() +{ + return [ + 'user_id' => null, + 'has_car' => false, + 'has_license_car' => false, + 'has_license_3_5t_transporter' => false, + 'has_license_7_5t_truck' => false, + 'has_license_12_5t_truck' => false, + 'has_license_forklift' => false + ]; } /** * Is it valid? * - * @param UserDriverLicense $user_driver_license - * The UserDriverLicense to check + * @param array $user_driver_license The UserDriverLicense to check * @return boolean */ -function UserDriverLicense_valid($user_driver_license) { - return $user_driver_license['has_license_car'] || $user_driver_license['has_license_3_5t_transporter'] || $user_driver_license['has_license_7_5t_truck'] || $user_driver_license['has_license_12_5t_truck'] || $user_driver_license['has_license_forklift']; +function UserDriverLicense_valid($user_driver_license) +{ + return $user_driver_license['has_car'] + || $user_driver_license['has_license_car'] + || $user_driver_license['has_license_3_5t_transporter'] + || $user_driver_license['has_license_7_5t_truck'] + || $user_driver_license['has_license_12_5t_truck'] + || $user_driver_license['has_license_forklift']; } /** * Get a users driver license information * - * @param int $user_id - * The users id + * @param int $user_id The users id + * @return array|false|null */ -function UserDriverLicense($user_id) { - $user_driver_license = sql_select("SELECT * FROM `UserDriverLicenses` WHERE `user_id`='" . sql_escape($user_id) . "'"); - if ($user_driver_license === false) { - engelsystem_error('Unable to load user driver license.'); - return false; - } - if (count($user_driver_license) > 0) { - return $user_driver_license[0]; - } - return null; +function UserDriverLicense($user_id) +{ + $user_driver_license = DB::select(' + SELECT * + FROM `UserDriverLicenses` + WHERE `user_id`=?', [$user_id]); + + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load user driver license.'); + return false; + } + + if (empty($user_driver_license)) { + return null; + } + + return array_shift($user_driver_license); } /** * Create a user's driver license entry * - * @param UserDriverLicense $user_driver_license - * The UserDriverLicense to create + * @param array $user_driver_license The UserDriverLicense to create + * @param array $user + * @return array */ -function UserDriverLicenses_create($user_driver_license, $user) { - $user_driver_license['user_id'] = $user['UID']; - $result = sql_query(" - INSERT INTO `UserDriverLicenses` SET - `user_id`=" . sql_escape($user_driver_license['user_id']) . ", - `has_car`=" . sql_bool($user_driver_license['has_car']) . ", - `has_license_car`=" . sql_bool($user_driver_license['has_license_car']) . ", - `has_license_3_5t_transporter`=" . sql_bool($user_driver_license['has_license_3_5t_transporter']) . ", - `has_license_7_5t_truck`=" . sql_bool($user_driver_license['has_license_7_5t_truck']) . ", - `has_license_12_5t_truck`=" . sql_bool($user_driver_license['has_license_12_5t_truck']) . ", - `has_license_forklift`=" . sql_bool($user_driver_license['has_license_forklift'])); - if ($result === false) { - engelsystem_error('Unable to create user driver license'); - } - return $user_driver_license; +function UserDriverLicenses_create($user_driver_license, $user) +{ + $user_driver_license['user_id'] = $user['UID']; + DB::insert(' + INSERT INTO `UserDriverLicenses` ( + `user_id`, + `has_car`, + `has_license_car`, + `has_license_3_5t_transporter`, + `has_license_7_5t_truck`, + `has_license_12_5t_truck`, + `has_license_forklift` + ) + VALUES (?, ?, ?, ?, ?, ?, ?) + ', + [ + $user_driver_license['user_id'], + (bool)$user_driver_license['has_car'], + (bool)$user_driver_license['has_license_car'], + (bool)$user_driver_license['has_license_3_5t_transporter'], + (bool)$user_driver_license['has_license_7_5t_truck'], + (bool)$user_driver_license['has_license_12_5t_truck'], + (bool)$user_driver_license['has_license_forklift'], + ] + ); + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to create user driver license'); + } + + return $user_driver_license; } /** * Update a user's driver license entry * - * @param UserDriverLicense $user_driver_license - * The UserDriverLicense to update + * @param array $user_driver_license The UserDriverLicense to update + * @return bool */ -function UserDriverLicenses_update($user_driver_license) { - $result = sql_query("UPDATE `UserDriverLicenses` SET - `has_car`=" . sql_bool($user_driver_license['has_car']) . ", - `has_license_car`=" . sql_bool($user_driver_license['has_license_car']) . ", - `has_license_3_5t_transporter`=" . sql_bool($user_driver_license['has_license_3_5t_transporter']) . ", - `has_license_7_5t_truck`=" . sql_bool($user_driver_license['has_license_7_5t_truck']) . ", - `has_license_12_5t_truck`=" . sql_bool($user_driver_license['has_license_12_5t_truck']) . ", - `has_license_forklift`=" . sql_bool($user_driver_license['has_license_forklift']) . " - WHERE `user_id`='" . sql_escape($user_driver_license['user_id']) . "'"); - if ($result === false) { - engelsystem_error("Unable to update user driver license information"); - } - return $result; +function UserDriverLicenses_update($user_driver_license) +{ + $result = DB::update(' + UPDATE `UserDriverLicenses` + SET + `has_car`=?, + `has_license_car`=?, + `has_license_3_5t_transporter`=?, + `has_license_7_5t_truck`=?, + `has_license_12_5t_truck`=?, + `has_license_forklift`=? + WHERE `user_id`=? + ', + [ + (bool)$user_driver_license['has_car'], + (bool)$user_driver_license['has_license_car'], + (bool)$user_driver_license['has_license_3_5t_transporter'], + (bool)$user_driver_license['has_license_7_5t_truck'], + (bool)$user_driver_license['has_license_12_5t_truck'], + (bool)$user_driver_license['has_license_forklift'], + $user_driver_license['user_id'], + ] + ); + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to update user driver license information'); + } + return $result; } /** * Delete a user's driver license entry * - * @param int $user_id + * @param int $user_id + * @return bool */ -function UserDriverLicenses_delete($user_id) { - $result = sql_query("DELETE FROM `UserDriverLicenses` WHERE `user_id`=" . sql_escape($user_id)); - if ($result === false) { - engelsystem_error("Unable to remove user driver license information"); - } - return $result; +function UserDriverLicenses_delete($user_id) +{ + $result = DB::delete('DELETE FROM `UserDriverLicenses` WHERE `user_id`=?', [$user_id]); + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to remove user driver license information'); + } + return $result; } -?>
\ No newline at end of file diff --git a/includes/model/UserGroups_model.php b/includes/model/UserGroups_model.php index 766f402f..d4baf638 100644 --- a/includes/model/UserGroups_model.php +++ b/includes/model/UserGroups_model.php @@ -1,17 +1,22 @@ <?php +use Engelsystem\Database\DB; + /** * Returns users groups - * @param User $user + * + * @param array $user + * @return array */ -function User_groups($user) { - return sql_select(" - SELECT `Groups`.* - FROM `UserGroups` - JOIN `Groups` ON `Groups`.`UID`=`UserGroups`.`group_id` - WHERE `UserGroups`.`uid`='" . sql_escape($user['UID']) . "' - ORDER BY `UserGroups`.`group_id` - "); +function User_groups($user) +{ + return DB::select(' + SELECT `Groups`.* + FROM `UserGroups` + JOIN `Groups` ON `Groups`.`UID`=`UserGroups`.`group_id` + WHERE `UserGroups`.`uid`=? + ORDER BY `UserGroups`.`group_id` + ', + [$user['UID']] + ); } - -?>
\ No newline at end of file diff --git a/includes/model/User_model.php b/includes/model/User_model.php index 3ebd3bf9..2913c1a1 100644 --- a/includes/model/User_model.php +++ b/includes/model/User_model.php @@ -1,4 +1,6 @@ <?php + +use Engelsystem\Database\DB; use Engelsystem\ValidationResult; /** @@ -8,164 +10,292 @@ use Engelsystem\ValidationResult; /** * Delete a user * - * @param int $user_id + * @param int $user_id + * @return bool */ -function User_delete($user_id) { - return sql_query("DELETE FROM `User` WHERE `UID`='" . sql_escape($user_id) . "'"); +function User_delete($user_id) +{ + DB::delete('DELETE FROM `User` WHERE `UID`=?', [$user_id]); + + return DB::getStm()->errorCode() == '00000'; } /** * Update user. * - * @param User $user + * @param array $user + * @return bool */ -function User_update($user) { - return sql_query("UPDATE `User` SET - `Nick`='" . sql_escape($user['Nick']) . "', - `Name`='" . sql_escape($user['Name']) . "', - `Vorname`='" . sql_escape($user['Vorname']) . "', - `Alter`='" . sql_escape($user['Alter']) . "', - `Telefon`='" . sql_escape($user['Telefon']) . "', - `DECT`='" . sql_escape($user['DECT']) . "', - `Handy`='" . sql_escape($user['Handy']) . "', - `email`='" . sql_escape($user['email']) . "', - `email_shiftinfo`=" . sql_bool($user['email_shiftinfo']) . ", - `email_by_human_allowed`=" . sql_bool($user['email_by_human_allowed']) . ", - `jabber`='" . sql_escape($user['jabber']) . "', - `Size`='" . sql_escape($user['Size']) . "', - `Gekommen`='" . sql_escape($user['Gekommen']) . "', - `Aktiv`='" . sql_escape($user['Aktiv']) . "', - `force_active`=" . sql_bool($user['force_active']) . ", - `Tshirt`='" . sql_escape($user['Tshirt']) . "', - `color`='" . sql_escape($user['color']) . "', - `Sprache`='" . sql_escape($user['Sprache']) . "', - `Hometown`='" . sql_escape($user['Hometown']) . "', - `got_voucher`='" . sql_escape($user['got_voucher']) . "', - `arrival_date`='" . sql_escape($user['arrival_date']) . "', - `planned_arrival_date`='" . sql_escape($user['planned_arrival_date']) . "', - `planned_departure_date`=" . sql_null($user['planned_departure_date']) . " - WHERE `UID`='" . sql_escape($user['UID']) . "'"); +function User_update($user) +{ + return (bool)DB::update(' + UPDATE `User` SET + `Nick`=?, + `Name`=?, + `Vorname`=?, + `Alter`=?, + `Telefon`=?, + `DECT`=?, + `Handy`=?, + `email`=?, + `email_shiftinfo`=?, + `email_by_human_allowed`=?, + `jabber`=?, + `Size`=?, + `Gekommen`=?, + `Aktiv`=?, + `force_active`=?, + `Tshirt`=?, + `color`=?, + `Sprache`=?, + `Hometown`=?, + `got_voucher`=?, + `arrival_date`=?, + `planned_arrival_date`=?, + `planned_departure_date`=? + WHERE `UID`=? + ', + [ + $user['Nick'], + $user['Name'], + $user['Vorname'], + $user['Alter'], + $user['Telefon'], + $user['DECT'], + $user['Handy'], + $user['email'], + (bool)$user['email_shiftinfo'], + (bool)$user['email_by_human_allowed'], + $user['jabber'], + $user['Size'], + $user['Gekommen'], + $user['Aktiv'], + (bool)$user['force_active'], + $user['Tshirt'], + $user['color'], + $user['Sprache'], + $user['Hometown'], + $user['got_voucher'], + $user['arrival_date'], + $user['planned_arrival_date'], + $user['planned_departure_date'], + $user['UID'], + ] + ); } /** * Counts all forced active users. + * + * @return int */ -function User_force_active_count() { - return sql_select_single_cell("SELECT COUNT(*) FROM `User` WHERE `force_active` = 1"); +function User_force_active_count() +{ + $result = DB::select('SELECT COUNT(*) FROM `User` WHERE `force_active` = 1'); + $result = array_shift($result); + + if (empty($result)) { + return 0; + } + + return (int)array_shift($result); } -function User_active_count() { - return sql_select_single_cell("SELECT COUNT(*) FROM `User` WHERE `Aktiv` = 1"); +/** + * @return int + */ +function User_active_count() +{ + $result = DB::select('SELECT COUNT(*) FROM `User` WHERE `Aktiv` = 1'); + $result = array_shift($result); + + if (empty($result)) { + return 0; + } + + return (int)array_shift($result); } -function User_got_voucher_count() { - return sql_select_single_cell("SELECT SUM(`got_voucher`) FROM `User`"); +/** + * @return int + */ +function User_got_voucher_count() +{ + $result = DB::select('SELECT SUM(`got_voucher`) FROM `User`'); + $result = array_shift($result); + + if (empty($result)) { + return 0; + } + + return (int)array_shift($result); } -function User_arrived_count() { - return sql_select_single_cell("SELECT COUNT(*) FROM `User` WHERE `Gekommen` = 1"); +/** + * @return int + */ +function User_arrived_count() +{ + $result = DB::select('SELECT COUNT(*) FROM `User` WHERE `Gekommen` = 1'); + $result = array_shift($result); + + if (empty($result)) { + return 0; + } + + return (int)array_shift($result); } -function User_tshirts_count() { - return sql_select_single_cell("SELECT COUNT(*) FROM `User` WHERE `Tshirt` = 1"); +/** + * @return int + */ +function User_tshirts_count() +{ + $result = DB::select('SELECT COUNT(*) FROM `User` WHERE `Tshirt` = 1'); + $result = array_shift($result); + + if (empty($result)) { + return 0; + } + + return (int)array_shift($result); } /** * Returns all column names for sorting in an array. + * + * @return array */ -function User_sortable_columns() { - return [ - 'Nick', - 'Name', - 'Vorname', - 'Alter', - 'DECT', - 'email', - 'Size', - 'Gekommen', - 'Aktiv', - 'force_active', - 'Tshirt', - 'lastLogIn' - ]; +function User_sortable_columns() +{ + return [ + 'Nick', + 'Name', + 'Vorname', + 'Alter', + 'DECT', + 'email', + 'Size', + 'Gekommen', + 'Aktiv', + 'force_active', + 'Tshirt', + 'lastLogIn' + ]; } /** * Get all users, ordered by Nick by default or by given param. * - * @param string $order_by + * @param string $order_by + * @return array|false */ -function Users($order_by = 'Nick') { - return sql_select("SELECT * FROM `User` ORDER BY `" . sql_escape($order_by) . "` ASC"); +function Users($order_by = 'Nick') +{ + $result = DB::select(sprintf(' + SELECT * + FROM `User` + ORDER BY `%s` ASC + ', + trim(DB::getPdo()->quote($order_by), '\'') + )); + + if (DB::getStm()->errorCode() != '00000') { + return false; + } + + return $result; } /** * Returns true if user is freeloader * - * @param User $user + * @param array $user + * @return bool */ -function User_is_freeloader($user) { - global $max_freeloadable_shifts, $user; - - return count(ShiftEntries_freeloaded_by_user($user)) >= $max_freeloadable_shifts; +function User_is_freeloader($user) +{ + global $user; + + return count(ShiftEntries_freeloaded_by_user($user)) >= config('max_freeloadable_shifts'); } /** * Returns all users that are not member of given angeltype. * - * @param Angeltype $angeltype + * @param array $angeltype Angeltype + * @return array */ -function Users_by_angeltype_inverted($angeltype) { - $result = sql_select(" - 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`"); - if ($result === false) { - engelsystem_error("Unable to load users."); - } - return $result; +function Users_by_angeltype_inverted($angeltype) +{ + $result = DB::select(' + SELECT `User`.* + FROM `User` + LEFT JOIN `UserAngelTypes` + ON (`User`.`UID`=`UserAngelTypes`.`user_id` AND `angeltype_id`=?) + WHERE `UserAngelTypes`.`id` IS NULL + ORDER BY `Nick` + ', + [ + $angeltype['id'] + ] + ); + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load users.'); + } + return $result; } /** * Returns all members of given angeltype. * - * @param Angeltype $angeltype + * @param array $angeltype + * @return array */ -function Users_by_angeltype($angeltype) { - $result = sql_select(" - SELECT - `User`.*, - `UserAngelTypes`.`id` as `user_angeltype_id`, - `UserAngelTypes`.`confirm_user_id`, - `UserAngelTypes`.`supporter`, - `UserDriverLicenses`.* - FROM `User` - JOIN `UserAngelTypes` ON `User`.`UID`=`UserAngelTypes`.`user_id` - LEFT JOIN `UserDriverLicenses` ON `User`.`UID`=`UserDriverLicenses`.`user_id` - WHERE `UserAngelTypes`.`angeltype_id`='" . sql_escape($angeltype['id']) . "' - ORDER BY `Nick`"); - if ($result === false) { - engelsystem_error("Unable to load members."); - } - return $result; +function Users_by_angeltype($angeltype) +{ + $result = DB::select(' + SELECT + `User`.*, + `UserAngelTypes`.`id` AS `user_angeltype_id`, + `UserAngelTypes`.`confirm_user_id`, + `UserAngelTypes`.`supporter`, + `UserDriverLicenses`.* + FROM `User` + JOIN `UserAngelTypes` ON `User`.`UID`=`UserAngelTypes`.`user_id` + LEFT JOIN `UserDriverLicenses` ON `User`.`UID`=`UserDriverLicenses`.`user_id` + WHERE `UserAngelTypes`.`angeltype_id`=? + ORDER BY `Nick` + ', + [ + $angeltype['id'] + ] + ); + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load members.'); + } + return $result; } /** * Returns User id array + * + * @return array */ -function User_ids() { - return sql_select("SELECT `UID` FROM `User`"); +function User_ids() +{ + return DB::select('SELECT `UID` FROM `User`'); } /** * Strip unwanted characters from a users nick. * - * @param string $nick + * @param string $nick + * @return string */ -function User_validate_Nick($nick) { - return preg_replace("/([^a-z0-9üöäß. _+*-]{1,})/ui", '', $nick); +function User_validate_Nick($nick) +{ + return preg_replace('/([^\wüöäß. +*-]{1,})/ui', '', $nick); } /** @@ -175,9 +305,10 @@ function User_validate_Nick($nick) { * The email address to validate * @return ValidationResult */ -function User_validate_mail($mail) { - $mail = strip_item($mail); - return new ValidationResult(check_email($mail), $mail); +function User_validate_mail($mail) +{ + $mail = strip_item($mail); + return new ValidationResult(check_email($mail), $mail); } /** @@ -187,41 +318,42 @@ function User_validate_mail($mail) { * Jabber-ID to validate * @return ValidationResult */ -function User_validate_jabber($jabber) { - $jabber = strip_item($jabber); - if ($jabber == '') { - // Empty is ok - return new ValidationResult(true, ''); - } - return new ValidationResult(check_email($jabber), $jabber); +function User_validate_jabber($jabber) +{ + $jabber = strip_item($jabber); + if ($jabber == '') { + // Empty is ok + return new ValidationResult(true, ''); + } + return new ValidationResult(check_email($jabber), $jabber); } /** * Validate the planned arrival date * - * @param int $planned_arrival_date - * Unix timestamp + * @param int $planned_arrival_date Unix timestamp * @return ValidationResult */ -function User_validate_planned_arrival_date($planned_arrival_date) { - if ($planned_arrival_date == null) { - // null is not okay - return new ValidationResult(false, time()); - } - $event_config = EventConfig(); - if ($event_config == null) { - // Nothing to validate against +function User_validate_planned_arrival_date($planned_arrival_date) +{ + if ($planned_arrival_date == null) { + // null is not okay + return new ValidationResult(false, time()); + } + $event_config = EventConfig(); + if ($event_config == null) { + // Nothing to validate against + return new ValidationResult(true, $planned_arrival_date); + } + if (isset($event_config['buildup_start_date']) && $planned_arrival_date < $event_config['buildup_start_date']) { + // Planned arrival can not be before buildup start date + return new ValidationResult(false, $event_config['buildup_start_date']); + } + if (isset($event_config['teardown_end_date']) && $planned_arrival_date > $event_config['teardown_end_date']) { + // Planned arrival can not be after teardown end date + return new ValidationResult(false, $event_config['teardown_end_date']); + } return new ValidationResult(true, $planned_arrival_date); - } - if (isset($event_config['buildup_start_date']) && $planned_arrival_date < $event_config['buildup_start_date']) { - // Planned arrival can not be before buildup start date - return new ValidationResult(false, $event_config['buildup_start_date']); - } - if (isset($event_config['teardown_end_date']) && $planned_arrival_date > $event_config['teardown_end_date']) { - // Planned arrival can not be after teardown end date - return new ValidationResult(false, $event_config['teardown_end_date']); - } - return new ValidationResult(true, $planned_arrival_date); } /** @@ -233,45 +365,51 @@ function User_validate_planned_arrival_date($planned_arrival_date) { * Unix timestamp * @return ValidationResult */ -function User_validate_planned_departure_date($planned_arrival_date, $planned_departure_date) { - if ($planned_departure_date == null) { - // null is okay - return new ValidationResult(true, null); - } - if ($planned_arrival_date > $planned_departure_date) { - // departure cannot be before arrival - return new ValidationResult(false, $planned_arrival_date); - } - $event_config = EventConfig(); - if ($event_config == null) { - // Nothing to validate against +function User_validate_planned_departure_date($planned_arrival_date, $planned_departure_date) +{ + if ($planned_departure_date == null) { + // null is okay + return new ValidationResult(true, null); + } + if ($planned_arrival_date > $planned_departure_date) { + // departure cannot be before arrival + return new ValidationResult(false, $planned_arrival_date); + } + $event_config = EventConfig(); + if ($event_config == null) { + // Nothing to validate against + return new ValidationResult(true, $planned_departure_date); + } + if (isset($event_config['buildup_start_date']) && $planned_departure_date < $event_config['buildup_start_date']) { + // Planned arrival can not be before buildup start date + return new ValidationResult(false, $event_config['buildup_start_date']); + } + if (isset($event_config['teardown_end_date']) && $planned_departure_date > $event_config['teardown_end_date']) { + // Planned arrival can not be after teardown end date + return new ValidationResult(false, $event_config['teardown_end_date']); + } return new ValidationResult(true, $planned_departure_date); - } - if (isset($event_config['buildup_start_date']) && $planned_departure_date < $event_config['buildup_start_date']) { - // Planned arrival can not be before buildup start date - return new ValidationResult(false, $event_config['buildup_start_date']); - } - if (isset($event_config['teardown_end_date']) && $planned_departure_date > $event_config['teardown_end_date']) { - // Planned arrival can not be after teardown end date - return new ValidationResult(false, $event_config['teardown_end_date']); - } - return new ValidationResult(true, $planned_departure_date); } /** * Returns user by id. * - * @param $user_id UID + * @param int $user_id UID + * @return array|null */ -function User($user_id) { - $user_source = sql_select("SELECT * FROM `User` WHERE `UID`='" . sql_escape($user_id) . "' LIMIT 1"); - if ($user_source === false) { - engelsystem_error("Unable to load user."); - } - if (count($user_source) > 0) { - return $user_source[0]; - } - return null; +function User($user_id) +{ + $user_source = DB::select('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$user_id]); + + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load user.'); + } + + if (empty($user_source)) { + return null; + } + + return array_shift($user_source); } /** @@ -279,96 +417,138 @@ function User($user_id) { * * @param string $api_key * User api key - * @return Matching user, null or false on error + * @return array|null Matching user, null on error */ -function User_by_api_key($api_key) { - $user = sql_select("SELECT * FROM `User` WHERE `api_key`='" . sql_escape($api_key) . "' LIMIT 1"); - if ($user === false) { - engelsystem_error("Unable to find user by api key."); - } - if (count($user) == 0) { - return null; - } - return $user[0]; +function User_by_api_key($api_key) +{ + $user = DB::select('SELECT * FROM `User` WHERE `api_key`=? LIMIT 1', [$api_key]); + + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to find user by api key.'); + } + + if (empty($user)) { + return null; + } + + return $user[0]; } /** * Returns User by email. * - * @param string $email - * @return Matching user, null or false on error + * @param string $email + * @return array|null 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) { - engelsystem_error("Unable to load user."); - } - if (count($user) == 0) { - return null; - } - return $user[0]; +function User_by_email($email) +{ + $user = DB::select('SELECT * FROM `User` WHERE `email`=? LIMIT 1', [$email]); + + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load user.'); + } + + if (empty($user)) { + return null; + } + + return array_shift($user); } /** * Returns User by password token. * - * @param string $token - * @return Matching user, null or false on error + * @param string $token + * @return array|null 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) { - engelsystem_error("Unable to load user."); - } - if (count($user) == 0) { - return null; - } - return $user[0]; +function User_by_password_recovery_token($token) +{ + $user = DB::select('SELECT * FROM `User` WHERE `password_recovery_token`=? LIMIT 1', [$token]); + + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to load user.'); + } + + if (empty($user)) { + return null; + } + + return array_shift($user); } /** * Generates a new api key for given user. * - * @param User $user + * @param array $user + * @param bool $log + * @return bool */ -function User_reset_api_key(&$user, $log = true) { - $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"); - if ($result === false) { - return false; - } - if ($log) { - engelsystem_log(sprintf("API key resetted (%s).", User_Nick_render($user))); - } +function User_reset_api_key(&$user, $log = true) +{ + $user['api_key'] = md5($user['Nick'] . time() . rand()); + DB::update(' + UPDATE `User` + SET `api_key`=? + WHERE `UID`=? + LIMIT 1 + ', + [ + $user['api_key'], + $user['UID'] + ] + ); + if (DB::getStm()->errorCode() != '00000') { + return false; + } + + if ($log) { + engelsystem_log(sprintf('API key resetted (%s).', User_Nick_render($user))); + } + + return true; } /** * Generates a new password recovery token for given user. * - * @param User $user + * @param array $user + * @return string */ -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) { - engelsystem_error("Unable to generate password recovery token."); - } - engelsystem_log("Password recovery for " . User_Nick_render($user) . " started."); - return $user['password_recovery_token']; +function User_generate_password_recovery_token(&$user) +{ + $user['password_recovery_token'] = md5($user['Nick'] . time() . rand()); + DB::update(' + UPDATE `User` + SET `password_recovery_token`=? + WHERE `UID`=? + LIMIT 1 + ', + [ + $user['password_recovery_token'], + $user['UID'], + ] + ); + if (DB::getStm()->errorCode() != '00000') { + engelsystem_error('Unable to generate password recovery token.'); + } + engelsystem_log('Password recovery for ' . User_Nick_render($user) . ' started.'); + return $user['password_recovery_token']; } -function User_get_eligable_voucher_count(&$user) { - global $voucher_settings; - - $shifts_done = count(ShiftEntries_finished_by_user($user)); - - $earned_vouchers = $user['got_voucher'] - $voucher_settings['initial_vouchers']; - $elegible_vouchers = $shifts_done / $voucher_settings['shifts_per_voucher'] - $earned_vouchers; - if ($elegible_vouchers < 0) { - return 0; - } - - return $elegible_vouchers; -} +/** + * @param array $user + * @return float + */ +function User_get_eligable_voucher_count(&$user) +{ + $voucher_settings = config('voucher_settings'); + $shifts_done = count(ShiftEntries_finished_by_user($user)); -?> + $earned_vouchers = $user['got_voucher'] - $voucher_settings['initial_vouchers']; + $elegible_vouchers = $shifts_done / $voucher_settings['shifts_per_voucher'] - $earned_vouchers; + if ($elegible_vouchers < 0) { + return 0; + } + + return $elegible_vouchers; +} diff --git a/includes/model/ValidationResult.php b/includes/model/ValidationResult.php index 0fc24161..7f88b432 100644 --- a/includes/model/ValidationResult.php +++ b/includes/model/ValidationResult.php @@ -6,37 +6,41 @@ namespace Engelsystem; * BO that represents the result of an entity attribute validation. * It contains the validated value and a bool for validation success. */ -class ValidationResult { +class ValidationResult +{ + /** @var bool */ + private $valid; - private $valid; + /** @var mixed */ + private $value; - private $value; + /** + * @param boolean $valid Is the value valid? + * @param mixed $value The validated value + */ + public function __construct($valid, $value) + { + $this->valid = $valid; + $this->value = $value; + } - /** - * Constructor. - * - * @param boolean $valid - * Is the value valid? - * @param * $value - * The validated value - */ - public function __construct($valid, $value) { - $this->valid = $valid; - $this->value = $value; - } + /** + * Is the value valid? + * + * @return bool + */ + public function isValid() + { + return $this->valid; + } - /** - * Is the value valid? - */ - public function isValid() { - return $this->valid; - } - - /** - * The parsed/validated value. - */ - public function getValue() { - return $this->value; - } + /** + * The parsed/validated value. + * + * @return mixed + */ + public function getValue() + { + return $this->value; + } } -?>
\ No newline at end of file |