engelsystem/includes/model/UserDriverLicenses_model.php

120 lines
3.9 KiB
PHP
Raw Normal View History

<?php
/**
* Returns a new empty UserDriverLicense
* FIXME entity object needed
2017-01-03 03:22:48 +01:00
*
* @return array
*/
2017-01-02 03:57:23 +01:00
function UserDriverLicense_new()
{
return [
2017-01-02 15:43:36 +01:00
'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?
*
2017-01-03 03:22:48 +01:00
* @param array $user_driver_license The UserDriverLicense to check
* @return boolean
*/
2017-01-02 03:57:23 +01:00
function UserDriverLicense_valid($user_driver_license)
{
2017-01-02 15:43:36 +01:00
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
*
2017-01-03 03:22:48 +01:00
* @param int $user_id The users id
* @return array|false|null
*/
2017-01-02 03:57:23 +01:00
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;
}
2017-01-03 03:22:48 +01:00
if (count($user_driver_license) == 0) {
return null;
2017-01-02 03:57:23 +01:00
}
2017-01-03 03:22:48 +01:00
return $user_driver_license[0];
}
/**
* Create a user's driver license entry
*
2017-01-03 03:22:48 +01:00
* @param array $user_driver_license The UserDriverLicense to create
* @param array $user
* @return array
*/
2017-01-02 03:57:23 +01:00
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']));
2017-01-02 03:57:23 +01:00
if ($result === false) {
engelsystem_error('Unable to create user driver license');
}
return $user_driver_license;
}
/**
* Update a user's driver license entry
*
2017-01-03 03:22:48 +01:00
* @param array $user_driver_license The UserDriverLicense to update
* @return mysqli_result
*/
2017-01-02 03:57:23 +01:00
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']) . "'");
2017-01-02 03:57:23 +01:00
if ($result === false) {
2017-01-03 14:12:17 +01:00
engelsystem_error('Unable to update user driver license information');
2017-01-02 03:57:23 +01:00
}
return $result;
}
/**
* Delete a user's driver license entry
*
2017-01-02 03:57:23 +01:00
* @param int $user_id
2017-01-03 03:22:48 +01:00
* @return mysqli_result
*/
2017-01-02 03:57:23 +01:00
function UserDriverLicenses_delete($user_id)
{
$result = sql_query("DELETE FROM `UserDriverLicenses` WHERE `user_id`=" . sql_escape($user_id));
if ($result === false) {
2017-01-03 14:12:17 +01:00
engelsystem_error('Unable to remove user driver license information');
2017-01-02 03:57:23 +01:00
}
return $result;
}