2011-06-02 20:18:01 +02:00
< ? php
2014-12-06 18:37:34 +01:00
2018-09-25 17:33:31 +02:00
use Carbon\Carbon ;
2018-10-11 01:26:34 +02:00
use Engelsystem\Models\User\User ;
2017-01-21 13:58:53 +01:00
2017-01-03 03:22:48 +01:00
/**
* @ return string
*/
2017-01-02 03:57:23 +01:00
function settings_title ()
{
2018-08-29 21:55:32 +02:00
return __ ( 'Settings' );
2013-11-25 21:56:56 +01:00
}
2016-11-11 16:34:23 +01:00
/**
* Change user main attributes ( name , dates , etc . )
*
2018-10-11 01:26:34 +02:00
* @ param User $user_source The user
2017-01-03 03:22:48 +01:00
* @ param bool $enable_tshirt_size
* @ param array $tshirt_sizes
2018-10-11 01:26:34 +02:00
* @ return User
2016-11-11 16:34:23 +01:00
*/
2017-01-02 03:57:23 +01:00
function user_settings_main ( $user_source , $enable_tshirt_size , $tshirt_sizes )
{
$valid = true ;
2017-07-18 21:38:53 +02:00
$request = request ();
2017-01-02 15:43:36 +01:00
2017-07-18 21:38:53 +02:00
if ( $request -> has ( 'mail' )) {
$result = User_validate_mail ( $request -> input ( 'mail' ));
2018-10-11 01:26:34 +02:00
$user_source -> email = $result -> getValue ();
2017-01-02 15:43:36 +01:00
if ( ! $result -> isValid ()) {
2017-01-02 03:57:23 +01:00
$valid = false ;
2018-08-29 21:55:32 +02:00
error ( __ ( 'E-mail address is not correct.' ));
2017-01-02 03:57:23 +01:00
}
} else {
$valid = false ;
2018-08-29 21:55:32 +02:00
error ( __ ( 'Please enter your e-mail.' ));
2012-12-26 19:53:27 +01:00
}
2017-01-02 15:43:36 +01:00
2018-10-11 01:26:34 +02:00
$user_source -> settings -> email_shiftinfo = $request -> has ( 'email_shiftinfo' );
$user_source -> settings -> email_human = $request -> has ( 'email_by_human_allowed' );
2017-01-02 15:43:36 +01:00
2017-07-18 21:38:53 +02:00
if ( $request -> has ( 'tshirt_size' ) && isset ( $tshirt_sizes [ $request -> input ( 'tshirt_size' )])) {
2018-10-11 01:26:34 +02:00
$user_source -> personalData -> shirt_size = $request -> input ( 'tshirt_size' );
2017-01-02 03:57:23 +01:00
} elseif ( $enable_tshirt_size ) {
$valid = false ;
}
2017-01-02 15:43:36 +01:00
2020-09-06 23:27:00 +02:00
if ( $request -> has ( 'planned_arrival_date' ) && $request -> input ( 'planned_arrival_date' )) {
2017-07-18 21:38:53 +02:00
$tmp = parse_date ( 'Y-m-d H:i' , $request -> input ( 'planned_arrival_date' ) . ' 00:00' );
2017-01-02 03:57:23 +01:00
$result = User_validate_planned_arrival_date ( $tmp );
2018-10-11 01:26:34 +02:00
$user_source -> personalData -> planned_arrival_date = Carbon :: createFromTimestamp ( $result -> getValue ());
2017-01-02 15:43:36 +01:00
if ( ! $result -> isValid ()) {
2017-01-02 03:57:23 +01:00
$valid = false ;
2018-08-29 21:55:32 +02:00
error ( __ ( 'Please enter your planned date of arrival. It should be after the buildup start date and before teardown end date.' ));
2017-01-02 03:57:23 +01:00
}
2012-12-26 19:53:27 +01:00
}
2017-01-02 15:43:36 +01:00
2020-09-06 23:27:00 +02:00
if ( $request -> has ( 'planned_departure_date' ) && $request -> input ( 'planned_departure_date' )) {
2017-07-18 21:38:53 +02:00
$tmp = parse_date ( 'Y-m-d H:i' , $request -> input ( 'planned_departure_date' ) . ' 00:00' );
2018-10-17 01:30:10 +02:00
$plannedArrivalDate = $user_source -> personalData -> planned_arrival_date ;
$result = User_validate_planned_departure_date (
$plannedArrivalDate ? $plannedArrivalDate -> getTimestamp () : 0 ,
$tmp
);
2018-10-11 01:26:34 +02:00
$user_source -> personalData -> planned_departure_date = Carbon :: createFromTimestamp ( $result -> getValue ());
2017-01-02 15:43:36 +01:00
if ( ! $result -> isValid ()) {
2017-01-02 03:57:23 +01:00
$valid = false ;
2018-08-29 21:55:32 +02:00
error ( __ ( 'Please enter your planned date of departure. It should be after your planned arrival date and after buildup start date and before teardown end date.' ));
2017-01-02 03:57:23 +01:00
}
2015-07-15 21:53:19 +02:00
}
2017-01-02 15:43:36 +01:00
// Trivia
2019-12-26 19:07:51 +01:00
$pronoun = strip_request_item ( 'pronoun' , $user_source -> personalData -> pronoun );
if ( config ( 'enable_pronoun' ) && mb_strlen ( $pronoun ) <= 15 ) {
$user_source -> personalData -> pronoun = $pronoun ;
}
if ( config ( 'enable_user_name' )) {
2019-08-24 10:56:59 +02:00
$user_source -> personalData -> last_name = strip_request_item ( 'lastname' , $user_source -> personalData -> last_name );
$user_source -> personalData -> first_name = strip_request_item ( 'prename' , $user_source -> personalData -> first_name );
}
if ( config ( 'enable_dect' )) {
if ( strlen ( strip_request_item ( 'dect' )) <= 40 ) {
$user_source -> contact -> dect = strip_request_item ( 'dect' , $user_source -> contact -> dect );
} else {
$valid = false ;
error ( __ ( 'For dect numbers are only 40 digits allowed.' ));
}
2017-11-21 19:00:42 +01:00
}
2019-04-25 17:30:51 +02:00
$user_source -> contact -> mobile = strip_request_item ( 'mobile' , $user_source -> contact -> mobile );
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
if ( $valid ) {
2018-10-11 01:26:34 +02:00
$user_source -> save ();
$user_source -> contact -> save ();
$user_source -> personalData -> save ();
$user_source -> settings -> save ();
2017-08-29 23:06:46 +02:00
2018-08-29 21:55:32 +02:00
success ( __ ( 'Settings saved.' ));
2019-09-08 02:25:49 +02:00
throw_redirect ( page_link_to ( 'user_settings' ));
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
return $user_source ;
2016-11-11 16:34:23 +01:00
}
/**
* Change user password .
*
2018-10-11 01:26:34 +02:00
* @ param User $user_source The user
2016-11-11 16:34:23 +01:00
*/
2017-01-02 03:57:23 +01:00
function user_settings_password ( $user_source )
{
2017-07-18 21:38:53 +02:00
$request = request ();
2018-11-27 12:01:36 +01:00
$auth = auth ();
2017-01-02 15:43:36 +01:00
if (
2017-07-18 21:38:53 +02:00
! $request -> has ( 'password' )
2018-11-27 12:01:36 +01:00
|| ! $auth -> verifyPassword ( $user_source , $request -> postData ( 'password' ))
2017-01-02 15:43:36 +01:00
) {
2018-08-29 21:55:32 +02:00
error ( __ ( '-> not OK. Please try again.' ));
2017-08-29 16:21:25 +02:00
} elseif ( strlen ( $request -> postData ( 'new_password' )) < config ( 'min_password_length' )) {
2018-08-29 21:55:32 +02:00
error ( __ ( 'Your password is to short (please use at least 6 characters).' ));
2017-08-29 16:21:25 +02:00
} elseif ( $request -> postData ( 'new_password' ) != $request -> postData ( 'new_password2' )) {
2018-08-29 21:55:32 +02:00
error ( __ ( 'Your passwords don\'t match.' ));
2017-01-02 03:57:23 +01:00
} else {
2018-11-27 12:01:36 +01:00
$auth -> setPassword ( $user_source , $request -> postData ( 'new_password' ));
2018-08-29 21:55:32 +02:00
success ( __ ( 'Password saved.' ));
2017-01-02 03:57:23 +01:00
}
2019-09-08 02:25:49 +02:00
throw_redirect ( page_link_to ( 'user_settings' ));
2016-11-11 16:34:23 +01:00
}
/**
* Change user theme
*
2018-10-11 01:26:34 +02:00
* @ param User $user_source The user
2017-01-03 03:22:48 +01:00
* @ param array $themes List of available themes
2018-10-11 01:26:34 +02:00
* @ return User
2016-11-11 16:34:23 +01:00
*/
2017-01-02 03:57:23 +01:00
function user_settings_theme ( $user_source , $themes )
{
$valid = true ;
2017-07-18 21:38:53 +02:00
$request = request ();
2017-01-02 15:43:36 +01:00
2017-07-18 21:38:53 +02:00
if ( $request -> has ( 'theme' ) && isset ( $themes [ $request -> input ( 'theme' )])) {
2018-10-11 01:26:34 +02:00
$user_source -> settings -> theme = $request -> input ( 'theme' );
2017-01-02 03:57:23 +01:00
} else {
$valid = false ;
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
if ( $valid ) {
2018-10-11 01:26:34 +02:00
$user_source -> settings -> save ();
2017-01-02 15:43:36 +01:00
2018-08-29 21:55:32 +02:00
success ( __ ( 'Theme changed.' ));
2019-09-08 02:25:49 +02:00
throw_redirect ( page_link_to ( 'user_settings' ));
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
return $user_source ;
2016-11-11 16:34:23 +01:00
}
/**
* Change use locale
*
2018-10-11 01:26:34 +02:00
* @ param User $user_source The user
2017-01-03 03:22:48 +01:00
* @ param array $locales List of available locales
2018-10-11 01:26:34 +02:00
* @ return User
2016-11-11 16:34:23 +01:00
*/
2017-01-02 03:57:23 +01:00
function user_settings_locale ( $user_source , $locales )
{
$valid = true ;
2017-07-18 21:38:53 +02:00
$request = request ();
2017-08-30 19:57:01 +02:00
$session = session ();
2017-01-02 15:43:36 +01:00
2017-07-18 21:38:53 +02:00
if ( $request -> has ( 'language' ) && isset ( $locales [ $request -> input ( 'language' )])) {
2018-10-11 01:26:34 +02:00
$user_source -> settings -> language = $request -> input ( 'language' );
2017-01-02 03:57:23 +01:00
} else {
$valid = false ;
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
if ( $valid ) {
2018-10-11 01:26:34 +02:00
$user_source -> settings -> save ();
$session -> set ( 'locale' , $user_source -> settings -> language );
2017-01-02 15:43:36 +01:00
2017-01-03 14:12:17 +01:00
success ( 'Language changed.' );
2019-09-08 02:25:49 +02:00
throw_redirect ( page_link_to ( 'user_settings' ));
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
return $user_source ;
2016-11-11 16:34:23 +01:00
}
/**
* Main user settings page / controller
2017-01-03 03:22:48 +01:00
*
* @ return string
2016-11-11 16:34:23 +01:00
*/
2017-01-02 03:57:23 +01:00
function user_settings ()
{
2017-07-18 21:38:53 +02:00
$request = request ();
2018-09-25 17:33:31 +02:00
$config = config ();
2017-07-20 01:14:28 +02:00
$themes = config ( 'available_themes' );
2017-01-21 23:07:20 +01:00
$enable_tshirt_size = config ( 'enable_tshirt_size' );
$tshirt_sizes = config ( 'tshirt_sizes' );
$locales = config ( 'locales' );
2020-11-15 18:47:30 +01:00
$oauth2_providers = config ( 'oauth' );
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$buildup_start_date = null ;
$teardown_end_date = null ;
2018-09-25 17:33:31 +02:00
if ( $buildup = $config -> get ( 'buildup_start' )) {
/** @var Carbon $buildup */
$buildup_start_date = $buildup -> getTimestamp ();
}
if ( $teardown = $config -> get ( 'teardown_end' )) {
/** @var Carbon $teardown */
$teardown_end_date = $teardown -> getTimestamp ();
2016-09-29 10:53:17 +02:00
}
2017-01-02 15:43:36 +01:00
2018-10-11 01:26:34 +02:00
$user_source = auth () -> user ();
2018-11-20 16:02:03 +01:00
if ( $request -> hasPostData ( 'submit' )) {
2017-01-02 03:57:23 +01:00
$user_source = user_settings_main ( $user_source , $enable_tshirt_size , $tshirt_sizes );
2018-11-20 16:02:03 +01:00
} elseif ( $request -> hasPostData ( 'submit_password' )) {
2017-01-02 03:57:23 +01:00
user_settings_password ( $user_source );
2018-11-20 16:02:03 +01:00
} elseif ( $request -> hasPostData ( 'submit_theme' )) {
2017-01-02 03:57:23 +01:00
$user_source = user_settings_theme ( $user_source , $themes );
2018-11-20 16:02:03 +01:00
} elseif ( $request -> hasPostData ( 'submit_language' )) {
2017-01-02 03:57:23 +01:00
$user_source = user_settings_locale ( $user_source , $locales );
}
2017-01-02 15:43:36 +01:00
2017-01-03 14:12:17 +01:00
return User_settings_view (
$user_source ,
$locales ,
$themes ,
$buildup_start_date ,
$teardown_end_date ,
$enable_tshirt_size ,
2020-11-15 18:47:30 +01:00
$tshirt_sizes ,
$oauth2_providers
2017-01-03 14:12:17 +01:00
);
2011-06-02 20:18:01 +02:00
}