Replaced more sql queries and old data

This commit is contained in:
Igor Scheller 2018-10-14 18:24:42 +02:00 committed by msquare
parent 57932be428
commit d7aea575ff
10 changed files with 89 additions and 179 deletions

View File

@ -215,9 +215,9 @@ function user_controller()
); );
foreach ($shift['needed_angeltypes'] as &$needed_angeltype) { foreach ($shift['needed_angeltypes'] as &$needed_angeltype) {
$needed_angeltype['users'] = DB::select(' $needed_angeltype['users'] = DB::select('
SELECT `ShiftEntry`.`freeloaded`, `User`.* SELECT `ShiftEntry`.`freeloaded`, `users`.*
FROM `ShiftEntry` FROM `ShiftEntry`
JOIN `User` ON `ShiftEntry`.`UID`=`User`.`UID` JOIN `users` ON `ShiftEntry`.`UID`=`users`.`id`
WHERE `ShiftEntry`.`SID` = ? WHERE `ShiftEntry`.`SID` = ?
AND `ShiftEntry`.`TID` = ? AND `ShiftEntry`.`TID` = ?
', ',

View File

@ -64,7 +64,8 @@ function mail_shift_change($old_shift, $new_shift)
$message .= $new_room['Name'] . "\n"; $message .= $new_room['Name'] . "\n";
foreach ($users as $user) { foreach ($users as $user) {
if ($user['email_shiftinfo']) { $user = User::find($user->id);
if ($user->settings->email_shiftinfo) {
engelsystem_email_to_user( engelsystem_email_to_user(
$user, $user,
__('Your Shift has changed'), __('Your Shift has changed'),
@ -91,7 +92,8 @@ function mail_shift_delete($shift)
$message .= $room['Name'] . "\n"; $message .= $room['Name'] . "\n";
foreach ($users as $user) { foreach ($users as $user) {
if ($user['email_shiftinfo']) { $user = User::find($user->id);
if ($user->settings->email_shiftinfo) {
engelsystem_email_to_user($user, __('Your Shift was deleted'), $message, true); engelsystem_email_to_user($user, __('Your Shift was deleted'), $message, true);
} }
} }

View File

@ -43,10 +43,10 @@ function Message_send($receiver_user_id, $text)
if ( if (
($text != '' && is_numeric($receiver_user_id)) ($text != '' && is_numeric($receiver_user_id))
&& count(DB::select(' && count(DB::select('
SELECT `UID` SELECT `id`
FROM `User` FROM `users`
WHERE `UID` = ? WHERE `id` = ?
AND NOT `UID` = ? AND NOT `id` = ?
LIMIT 1 LIMIT 1
', [$receiver_user_id, $user->id])) > 0 ', [$receiver_user_id, $user->id])) > 0
) { ) {

View File

@ -48,11 +48,7 @@ function ShiftEntries_by_shift($shift_id)
{ {
return DB::select(' return DB::select('
SELECT SELECT
`User`.`Nick`, `users`.*,
`User`.`email`,
`User`.`email_shiftinfo`,
`User`.`Sprache`,
`User`.`Gekommen`,
`ShiftEntry`.`UID`, `ShiftEntry`.`UID`,
`ShiftEntry`.`TID`, `ShiftEntry`.`TID`,
`ShiftEntry`.`SID`, `ShiftEntry`.`SID`,
@ -60,7 +56,7 @@ function ShiftEntries_by_shift($shift_id)
`ShiftEntry`.`Comment`, `ShiftEntry`.`Comment`,
`ShiftEntry`.`freeloaded` `ShiftEntry`.`freeloaded`
FROM `ShiftEntry` FROM `ShiftEntry`
JOIN `User` ON `ShiftEntry`.`UID`=`User`.`UID` JOIN `users` ON `ShiftEntry`.`UID`=`users`.`id`
JOIN `AngelTypes` ON `ShiftEntry`.`TID`=`AngelTypes`.`id` JOIN `AngelTypes` ON `ShiftEntry`.`TID`=`AngelTypes`.`id`
WHERE `ShiftEntry`.`SID` = ?', WHERE `ShiftEntry`.`SID` = ?',
[$shift_id] [$shift_id]

View File

@ -235,11 +235,7 @@ function ShiftEntries_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
{ {
$sql = ' $sql = '
SELECT SELECT
`User`.`Nick`, users.*
`User`.`email`,
`User`.`email_shiftinfo`,
`User`.`Sprache`,
`User`.`Gekommen`,
`ShiftEntry`.`UID`, `ShiftEntry`.`UID`,
`ShiftEntry`.`TID`, `ShiftEntry`.`TID`,
`ShiftEntry`.`SID`, `ShiftEntry`.`SID`,
@ -247,7 +243,7 @@ function ShiftEntries_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
`ShiftEntry`.`freeloaded` `ShiftEntry`.`freeloaded`
FROM `Shifts` FROM `Shifts`
JOIN `ShiftEntry` ON `ShiftEntry`.`SID`=`Shifts`.`SID` JOIN `ShiftEntry` ON `ShiftEntry`.`SID`=`Shifts`.`SID`
JOIN `User` ON `ShiftEntry`.`UID`=`User`.`UID` JOIN `users` ON `ShiftEntry`.`UID`=`users`.`id`
WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ') WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ')
AND `start` BETWEEN ? AND ? AND `start` BETWEEN ? AND ?
ORDER BY `Shifts`.`start`'; ORDER BY `Shifts`.`start`';
@ -685,25 +681,3 @@ function Shift($shift_id)
return $result; return $result;
} }
/**
* Returns all shifts with needed angeltypes and count of subscribed jobs.
*
* @return array
*/
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`
');
foreach ($shifts_source as &$shift) {
$needed_angeltypes = NeededAngelTypes_by_shift($shift['SID']);
$shift['angeltypes'] = $needed_angeltypes;
}
return $shifts_source;
}

View File

@ -244,36 +244,24 @@ function admin_user()
if (in_array('admin_active', $privileges)) { if (in_array('admin_active', $privileges)) {
$force_active = $request->input('force_active'); $force_active = $request->input('force_active');
} }
$sql = ' if($user_source->settings->email_human){
UPDATE `User` SET $user_source->email = $request->postData('eemail');
`Nick` = ?, }
`Name` = ?, $user_source->name = User_validate_Nick($request->postData('eNick'));
`Vorname` = ?, $user_source->save();
`Handy` = ?, $user_source->personalData->first_name = $request->postData('eVorname');
`DECT` = ?, $user_source->personalData->last_name = $request->postData('eName');
' . ($user_source->settings->email_human $user_source->personalData->shirt_size = $request->postData('eSize');
? '`email` = ' . DB::getPdo()->quote($request->postData('eemail')) . ',' $user_source->personalData->save();
: '') . ' $user_source->contact->mobile = $request->postData('eHandy');
`Size` = ?, $user_source->contact->dect = $request->postData('eDECT');
`Gekommen`= ?, $user_source->contact->save();
`Aktiv`= ?, $user_source->state->arrived = $request->postData('eGekommen');
`force_active`= ?, $user_source->state->active = $request->postData('eAktiv');
`Tshirt` = ? $user_source->state->force_active = $force_active;
WHERE `UID` = ? $user_source->state->got_shirt = $request->postData('eTshirt');
LIMIT 1'; $user_source->state->save();
DB::update($sql, [
User_validate_Nick($request->postData('eNick')),
$request->postData('eName'),
$request->postData('eVorname'),
$request->postData('eHandy'),
$request->postData('eDECT'),
$request->postData('eSize'),
$request->postData('eGekommen'),
$request->postData('eAktiv'),
$force_active,
$request->postData('eTshirt'),
$user_id,
]);
engelsystem_log( engelsystem_log(
'Updated user: ' . $request->postData('eNick') . ', ' . $request->postData('eSize') 'Updated user: ' . $request->postData('eNick') . ', ' . $request->postData('eSize')
. ', arrived: ' . $request->postData('eVorname') . ', arrived: ' . $request->postData('eVorname')

View File

@ -2,6 +2,10 @@
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Database\DB; use Engelsystem\Database\DB;
use Engelsystem\Models\User\Contact;
use Engelsystem\Models\User\PersonalData;
use Engelsystem\Models\User\Settings;
use Engelsystem\Models\User\State;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
/** /**
@ -36,7 +40,7 @@ function logout_title()
function guest_register() function guest_register()
{ {
global $privileges; global $privileges;
$user = Auth()->user(); $authUser = Auth()->user();
$tshirt_sizes = config('tshirt_sizes'); $tshirt_sizes = config('tshirt_sizes');
$enable_tshirt_size = config('enable_tshirt_size'); $enable_tshirt_size = config('enable_tshirt_size');
$min_password_length = config('min_password_length'); $min_password_length = config('min_password_length');
@ -48,16 +52,11 @@ function guest_register()
$nick = ''; $nick = '';
$lastName = ''; $lastName = '';
$preName = ''; $preName = '';
$age = 0;
$tel = '';
$dect = ''; $dect = '';
$mobile = ''; $mobile = '';
$mail = ''; $mail = '';
$email_shiftinfo = false; $email_shiftinfo = false;
$email_by_human_allowed = false; $email_by_human_allowed = false;
$jabber = '';
$hometown = '';
$comment = '';
$tshirt_size = ''; $tshirt_size = '';
$password_hash = ''; $password_hash = '';
$selected_angel_types = []; $selected_angel_types = [];
@ -72,7 +71,7 @@ function guest_register()
} }
} }
if (!in_array('register', $privileges) || (!$user && !config('registration_enabled'))) { if (!in_array('register', $privileges) || (!$authUser && !config('registration_enabled'))) {
error(__('Registration is disabled.')); error(__('Registration is disabled.'));
return page_with_title(register_title(), [ return page_with_title(register_title(), [
@ -85,7 +84,7 @@ function guest_register()
if ($request->has('nick') && strlen(User_validate_Nick($request->input('nick'))) > 1) { if ($request->has('nick') && strlen(User_validate_Nick($request->input('nick'))) > 1) {
$nick = User_validate_Nick($request->input('nick')); $nick = User_validate_Nick($request->input('nick'));
if (count(DB::select('SELECT `UID` FROM `User` WHERE `Nick`=? LIMIT 1', [$nick])) > 0) { if (User::whereName($nick)->count() > 0) {
$valid = false; $valid = false;
$msg .= error(sprintf(__('Your nick "%s" already exists.'), $nick), true); $msg .= error(sprintf(__('Your nick "%s" already exists.'), $nick), true);
} }
@ -116,14 +115,6 @@ function guest_register()
$email_by_human_allowed = true; $email_by_human_allowed = true;
} }
if ($request->has('jabber') && strlen(strip_request_item('jabber')) > 0) {
$jabber = strip_request_item('jabber');
if (!check_email($jabber)) {
$valid = false;
$msg .= error(__('Please check your jabber account information.'), true);
}
}
if ($enable_tshirt_size) { if ($enable_tshirt_size) {
if ($request->has('tshirt_size') && isset($tshirt_sizes[$request->input('tshirt_size')])) { if ($request->has('tshirt_size') && isset($tshirt_sizes[$request->input('tshirt_size')])) {
$tshirt_size = $request->input('tshirt_size'); $tshirt_size = $request->input('tshirt_size');
@ -173,12 +164,6 @@ function guest_register()
if ($request->has('prename')) { if ($request->has('prename')) {
$preName = strip_request_item('prename'); $preName = strip_request_item('prename');
} }
if ($request->has('age') && preg_match('/^\d{1,4}$/', $request->input('age'))) {
$age = strip_request_item('age');
}
if ($request->has('tel')) {
$tel = strip_request_item('tel');
}
if ($request->has('dect')) { if ($request->has('dect')) {
if (strlen(strip_request_item('dect')) <= 5) { if (strlen(strip_request_item('dect')) <= 5) {
$dect = strip_request_item('dect'); $dect = strip_request_item('dect');
@ -190,88 +175,71 @@ function guest_register()
if ($request->has('mobile')) { if ($request->has('mobile')) {
$mobile = strip_request_item('mobile'); $mobile = strip_request_item('mobile');
} }
if ($request->has('hometown')) {
$hometown = strip_request_item('hometown');
}
if ($request->has('comment')) {
$comment = strip_request_item_nl('comment');
}
if ($valid) { if ($valid) {
DB::insert(' $user = new User([
INSERT INTO `User` ( 'name' => $nick,
`color`, 'password' => $password_hash,
`Nick`, 'email' => $mail,
`Vorname`, 'api_key' => '',
`Name`, 'last_login_at' => null,
`Alter`, ]);
`Telefon`, $user->save();
`DECT`,
`Handy`, $contact = new Contact([
`email`, 'dect' => $dect,
`email_shiftinfo`, 'mobile' => $mobile,
`email_by_human_allowed`, ]);
`jabber`, $contact->user()
`Size`, ->associate($user)
`Passwort`, ->save();
`kommentar`,
`Hometown`, $personalData = new PersonalData([
`CreateDate`, 'first_name' => $preName,
`Sprache`, 'last_name' => $lastName,
`arrival_date`, 'shirt_size' => $tshirt_size,
`planned_arrival_date`, 'planned_arrival_date' => Carbon::createFromTimestamp($planned_arrival_date),
`force_active`, ]);
`lastLogIn`, $personalData->user()
`api_key`, ->associate($user)
`got_voucher` ->save();
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?, NULL, ?, FALSE, 0, "", 0) $settings = new Settings([
', 'language' => $session->get('locale'),
[ 'theme' => config('theme'),
config('theme'), 'email_human' => $email_by_human_allowed,
$nick, 'email_shiftinfo' => $email_shiftinfo,
$preName, ]);
$lastName, $settings->user()
$age, ->associate($user)
$tel, ->save();
$dect,
$mobile, (new State())->user()
$mail, ->associate($user)
(int)$email_shiftinfo, ->save();
(int)$email_by_human_allowed,
$jabber,
$tshirt_size,
$password_hash,
$comment,
$hometown,
$session->get('locale'),
$planned_arrival_date,
]
);
// Assign user-group and set password // Assign user-group and set password
$user_id = DB::getPdo()->lastInsertId(); DB::insert('INSERT INTO `UserGroups` (`uid`, `group_id`) VALUES (?, -20)', [$user->id]);
DB::insert('INSERT INTO `UserGroups` (`uid`, `group_id`) VALUES (?, -20)', [$user_id]); set_password($user->id, $request->postData('password'));
set_password($user_id, $request->postData('password'));
// Assign angel-types // Assign angel-types
$user_angel_types_info = []; $user_angel_types_info = [];
foreach ($selected_angel_types as $selected_angel_type_id) { foreach ($selected_angel_types as $selected_angel_type_id) {
DB::insert( DB::insert(
'INSERT INTO `UserAngelTypes` (`user_id`, `angeltype_id`, `supporter`) VALUES (?, ?, FALSE)', 'INSERT INTO `UserAngelTypes` (`user_id`, `angeltype_id`, `supporter`) VALUES (?, ?, FALSE)',
[$user_id, $selected_angel_type_id] [$user->id, $selected_angel_type_id]
); );
$user_angel_types_info[] = $angel_types[$selected_angel_type_id]; $user_angel_types_info[] = $angel_types[$selected_angel_type_id];
} }
engelsystem_log( engelsystem_log(
'User ' . User_Nick_render(User::find($user_id)) 'User ' . User_Nick_render($user)
. ' signed up as: ' . join(', ', $user_angel_types_info) . ' signed up as: ' . join(', ', $user_angel_types_info)
); );
success(__('Angel registration successful!')); success(__('Angel registration successful!'));
// User is already logged in - that means a supporter has registered an angel. Return to register page. // User is already logged in - that means a supporter has registered an angel. Return to register page.
if ($user) { if ($authUser) {
redirect(page_link_to('register')); redirect(page_link_to('register'));
} }
@ -369,11 +337,7 @@ function guest_register()
div('col-sm-4', [ div('col-sm-4', [
form_text('mobile', __('Mobile'), $mobile) form_text('mobile', __('Mobile'), $mobile)
]), ]),
div('col-sm-4', [
form_text('tel', __('Phone'), $tel)
])
]), ]),
form_text('jabber', __('Jabber'), $jabber),
div('row', [ div('row', [
div('col-sm-6', [ div('col-sm-6', [
form_text('prename', __('First name'), $preName) form_text('prename', __('First name'), $preName)
@ -382,14 +346,6 @@ function guest_register()
form_text('lastname', __('Last name'), $lastName) form_text('lastname', __('Last name'), $lastName)
]) ])
]), ]),
div('row', [
div('col-sm-3', [
form_text('age', __('Age'), $age)
]),
div('col-sm-9', [
form_text('hometown', __('Hometown'), $hometown)
])
]),
form_info(entry_required() . ' = ' . __('Entry required!')) form_info(entry_required() . ' = ' . __('Entry required!'))
]) ])
]), ]),

View File

@ -1,6 +1,8 @@
<?php <?php
use Engelsystem\Database\DB; use Engelsystem\Database\DB;
use Engelsystem\Models\User\State;
use Engelsystem\Models\User\User;
function guest_stats() function guest_stats()
{ {
@ -11,15 +13,8 @@ function guest_stats()
if (!empty($apiKey) && $request->input('api_key') == $apiKey) { if (!empty($apiKey) && $request->input('api_key') == $apiKey) {
$stats = []; $stats = [];
list($user_count) = DB::select('SELECT count(*) AS `user_count` FROM `User`'); $stats['user_count'] = User::all()->count();
$stats['user_count'] = $user_count['user_count']; $stats['arrived_user_count'] = State::whereArrived(true)->count();
list($arrived_user_count) = DB::select('
SELECT count(*) AS `user_count`
FROM `User`
WHERE `Gekommen`=1
');
$stats['arrived_user_count'] = $arrived_user_count['user_count'];
$done_shifts_seconds = DB::selectOne(' $done_shifts_seconds = DB::selectOne('
SELECT SUM(`Shifts`.`end` - `Shifts`.`start`) SELECT SUM(`Shifts`.`end` - `Shifts`.`start`)

View File

@ -26,7 +26,7 @@ function user_myshifts()
$request->has('id') $request->has('id')
&& in_array('user_shifts_admin', $privileges) && in_array('user_shifts_admin', $privileges)
&& preg_match('/^\d{1,}$/', $request->input('id')) && preg_match('/^\d{1,}$/', $request->input('id'))
&& count(DB::select('SELECT `UID` FROM `User` WHERE `UID`=?', [$request->input('id')])) > 0 && User::find($request->input('id'))
) { ) {
$shift_entry_id = $request->input('id'); $shift_entry_id = $request->input('id');
} else { } else {

View File

@ -212,7 +212,6 @@ function Users_view(
$tshirts_count, $tshirts_count,
$voucher_count $voucher_count
) { ) {
$usersList = []; $usersList = [];
foreach ($users as $user) { foreach ($users as $user) {
$u = []; $u = [];