2023-10-26 17:42:33 +02:00
< ? php
class Tournaments extends Page {
function setMenu ()
{
$this -> menu_text = 'Turniere' ;
$this -> menu_image = 'far fa-medal' ;
$this -> menu_priority = 1 ;
}
2023-10-26 20:26:28 +02:00
function setSubmenu ()
{
$this -> addSubmenuItem ( 'Übersicht' , '/tournaments' , 'far fa-list-alt' );
$this -> addSubmenuItem ( 'Turnier anlegen' , '/tournaments/add' , 'fas fa-calendar-plus' );
2023-10-26 21:18:32 +02:00
if ( $_SESSION [ 'user' ] -> data [ 'tournaments' ] && count ( $_SESSION [ 'user' ] -> data [ 'tournaments' ]) > 0 )
2023-10-26 17:42:33 +02:00
{
2023-10-26 20:26:28 +02:00
$this -> addSubmenuItem ( 'divider' );
2023-11-20 20:24:04 +01:00
$counter = 0 ;
2023-10-26 20:26:28 +02:00
foreach ( $_SESSION [ 'user' ] -> data [ 'tournaments' ] as $tid )
2023-10-26 17:42:33 +02:00
{
2023-10-26 20:26:28 +02:00
$t = new Tournament ();
$t -> load ( $tid );
2023-10-31 10:02:32 +01:00
$this -> addSubmenuItem ( $t -> data [ 'name' ], '/tournaments/event/' . $tid , 'fas fa-calendar-star' );
2023-11-20 20:24:04 +01:00
$counter ++ ;
if ( $counter === 5 ) {
break ;
}
2023-10-26 17:42:33 +02:00
}
}
}
2023-10-26 20:26:28 +02:00
function add ()
2023-10-26 17:42:33 +02:00
{
2023-12-23 18:20:50 +01:00
$t = new Tournament ();
$t -> name = $_REQUEST [ 'tournament_name' ];
$t -> date = date ( " d.m.Y " , strtotime ( $_REQUEST [ 'tournament_date' ]));
$t -> duration = intval ( $_REQUEST [ 'tournament_duration' ]);
$t -> referee = $_REQUEST [ 'tournament_referee' ];
$t -> text = $_REQUEST [ 'tournament_text' ];
$t -> url = $_REQUEST [ 'tournament_url' ];
$t -> logo = $_REQUEST [ 'tournament_logo' ];
$t -> run = $_REQUEST [ 'tournament_run' ];
$t -> run = $_REQUEST [ 'tournament_run' ];
$t -> ranking = $_REQUEST [ 'tournament_ranking' ];
$t -> number = $_REQUEST [ 'tournament_number' ];
$t -> participant = $_REQUEST [ 'tournament_participant' ];
$t -> dog = $_REQUEST [ 'tournament_dog' ];
$t -> club = $_REQUEST [ 'tournament_club' ];
$t -> faults = $_REQUEST [ 'tournament_faults' ];
$t -> refusals = $_REQUEST [ 'tournament_refusals' ];
$t -> time = $_REQUEST [ 'tournament_time' ];
$t -> points = $_REQUEST [ 'tournament_points' ];
$t -> speed = $_REQUEST [ 'tournament_speed' ];
$this -> set ( 'tournamentdata' , $t -> data );
2023-10-26 20:26:28 +02:00
$this -> set ( 'template' , 'edit_tournament.html' );
2023-12-23 18:20:50 +01:00
2023-10-26 20:26:28 +02:00
}
2023-12-23 18:20:50 +01:00
2023-10-26 20:26:28 +02:00
function index () {
2023-11-03 19:04:50 +01:00
$events = $_SESSION [ 'user' ] -> data [ 'tournaments' ];
$tournaments = [];
foreach ( $events as $key => $eventid )
{
//var_dump($dogid);
$event = new Tournament ();
try {
$event -> load ( $eventid );
}
catch ( Exception $e )
{
error_log ( " Event $eventid not found. Deleting from user " );
unset ( $_SESSION [ 'user' ] -> data [ 'tournament' ][ $key ]);
$_SESSION [ 'user' ] -> save ();
continue ;
}
if ( $event -> data )
$tournaments [] = array_merge ( $event -> data ,[ 'id' => $eventid ]);
}
if ( count ( $_SESSION [ 'user' ] -> data [ 'tournaments' ]) > 0 )
{
$this -> set ( 'tournaments' , $tournaments );
$this -> set ( 'template' , 'tournaments.html' );
}
2023-10-26 17:42:33 +02:00
}
2023-10-26 21:48:04 +02:00
function manage ()
{
$action = $this -> params [ 0 ];
$tid = $this -> params [ 1 ];
$t = new Tournament ();
if ( $t -> exists ( $tid ))
$t -> load ( $tid );
else
return partial ( 'error.html' , [ 'errorTitle' => 'Error' , 'errorMessage' => 'Dieses Turnier existiert nicht' ]);
switch ( $action )
{
case 'join' :
if ( in_array ( $tid , $_SESSION [ 'user' ] -> data [ 'tournaments' ]))
return partial ( 'error.html' , [ 'errorTitle' => 'Error' , 'errorMessage' => 'Du bist bereits für dieses Turnier angemeldet' ]);
else
{
2023-10-27 11:52:31 +02:00
$t -> joinUser ( $tid );
2023-10-26 21:48:04 +02:00
return partial ( 'success.html' , [ 'successTitle' => 'Erfolgreich' , 'successMessage' => 'Du wurdest erfolgreich für dieses Turnier angemeldet' ]);
}
case 'leave' :
if ( ! in_array ( $tid , $_SESSION [ 'user' ] -> data [ 'tournaments' ]))
return partial ( 'error.html' , [ 'errorTitle' => 'Error' , 'errorMessage' => 'Du bist für dieses Turnier nicht angemeldet' ]);
else
{
2023-10-27 11:52:31 +02:00
$t -> removeUser ( $tid );
2023-10-26 21:48:04 +02:00
return partial ( 'success.html' , [ 'successTitle' => 'Erfolgreich' , 'successMessage' => 'Du wurdest erfolgreich für dieses Turnier abgemeldet' ]);
}
}
}
function leave ()
{
}
2023-10-26 21:18:32 +02:00
function event ()
{
$tid = $this -> params [ 0 ];
2023-10-26 21:33:29 +02:00
$t = new Tournament ();
if ( $t -> exists ( $tid ))
$t -> load ( $tid );
else
return partial ( 'error.html' , [ 'errorTitle' => 'Error' , 'errorMessage' => 'Dieses Turnier existiert nicht' ]);
2023-10-26 21:18:32 +02:00
2023-10-26 21:32:37 +02:00
$this -> set ( 'admin' , $t -> amIAdmin ( $tid ));
$this -> set ( 'joined' , $t -> isMyEvent ( $tid ));
2023-10-26 21:18:32 +02:00
$this -> set ( 'tournament_id' , $tid );
2023-10-27 11:52:31 +02:00
$this -> set ( 'admins' , $t -> getAdmins ( $tid ));
$this -> set ( 'members' , $t -> getMembers ( $tid ));
2023-10-26 21:18:32 +02:00
$this -> set ( 'tdata' , $t -> data );
$this -> set ( 'template' , 'event.html' );
}
function edit ()
{
if ( $_REQUEST [ 'submit' ])
{
2023-12-23 18:20:50 +01:00
var_dump ( $_REQUEST );
2023-10-26 21:18:32 +02:00
$id = $_REQUEST [ 'tournament_id' ];
$name = $_REQUEST [ 'tournament_name' ];
$date = $_REQUEST [ 'tournament_date' ];
$tournament_referee = $_REQUEST [ 'tournament_referee' ];
$tournament_duration = intval ( $_REQUEST [ 'tournament_duration' ]);
$tournament_url = $_REQUEST [ 'tournament_url' ];
$tournament_text = $_REQUEST [ 'tournament_text' ];
$newlogo = false ;
if ( $_FILES [ 'logo' ])
{
$logo = $_FILES [ 'logo' ];
$logo_name = $logo [ 'name' ];
$logo_tmp_name = $logo [ 'tmp_name' ];
$logo_size = $logo [ 'size' ];
$logo_error = $logo [ 'error' ];
$logo_type = $logo [ 'type' ];
$allowed = [ 'jpg' , 'jpeg' , 'png' , 'gif' ];
$logo_ext = strtolower ( end ( explode ( '.' , $logo_name )));
$logo_name = $name . '.' . $logo_ext ;
$logo_path = 'uploads/' . $logo_name ;
if ( in_array ( $logo_ext , $allowed ))
{
if ( $logo_error === 0 )
{
if ( $logo_size < 10000000 )
{
$answer = pictshareUploadImage ( $logo_tmp_name );
if ( $answer [ 'status' ] == 'ok' && in_array ( $answer [ 'filetype' ],[ 'jpeg' , 'png' , 'gif' ]))
$newlogo = $answer [ 'url' ];
}
else
return partial ( 'error.html' , [ 'errorTitle' => 'Error' , 'errorMessage' => 'Die Datei ist zu groß. Bitte eine kleinere Datei hochladen' ]);
}
else
return partial ( 'error.html' , [ 'errorTitle' => 'Error' , 'errorMessage' => 'Beim Upload der Datei ist ein Fehler aufgetreten' ]);
}
else
return partial ( 'error.html' , [ 'errorTitle' => 'Error' , 'errorMessage' => 'Dieser Dateityp ist nicht erlaubt. Bitte nur jpg, jpeg oder png Dateien hochladen' ]);
}
$error = false ;
if ( ! $name || ! $date )
$error = 'Bitte zumindest Name und Datum angeben' ;
else if ( ! strtotime ( $date ))
$error = 'Das Datumm ist ungültig. Bitte die Eingabe prüfen' ;
if ( $error ){
$this -> set ( 'errorMessage' , $error );
$this -> set ( 'template' , '/templates/partials/error.html' );
return ;
}
else
{
$t = new Tournament ();
2023-11-02 18:16:22 +01:00
2023-10-26 21:18:32 +02:00
if ( $id )
2023-10-26 21:32:37 +02:00
{
2023-11-02 18:16:22 +01:00
if ( $t -> exists ( $id ))
$t -> load ( $id );
else
return partial ( 'error.html' , [ 'errorTitle' => 'Error' , 'errorMessage' => 'Dieses Turnier existiert nicht' ]);
2023-10-26 21:32:37 +02:00
if ( ! in_array ( $_SESSION [ 'user' ] -> id , $t -> data [ 'admins' ]))
return partial ( 'error.html' , [ 'errorTitle' => 'Error' , 'errorMessage' => 'Du bist nicht berechtigt, dieses Turnier zu bearbeiten' ]);
else
$t -> load ( $id );
}
2023-10-26 21:18:32 +02:00
$t -> name = $name ;
$t -> date = $date ;
$t -> duration = $tournament_duration ;
$t -> referee = $tournament_referee ;
$t -> text = $tournament_text ;
$t -> url = $tournament_url ;
2023-10-27 11:52:31 +02:00
if ( ! $t -> data [ 'admins' ] || ! is_array ( $t -> data [ 'admins' ] || count ( $t -> data [ 'admins' ]) == 0 ))
$t -> data [ 'admins' ] = [];
2023-10-26 21:32:37 +02:00
if ( ! in_array ( $_SESSION [ 'user' ] -> id , $t -> data [ 'admins' ]))
$t -> data [ 'admins' ][] = $_SESSION [ 'user' ] -> id ;
2023-10-26 21:18:32 +02:00
if ( $newlogo )
$t -> logo = $newlogo ;
try
{
$tid = $t -> save ();
}
catch ( Exception $e )
{
$this -> set ( 'template' , '/templates/partials/error.html' );
$this -> set ( 'errorTitle' , 'Error' );
$this -> set ( 'errorMessage' , $e -> getMessage ());
return ;
}
2023-11-03 12:37:44 +01:00
//var_dump($_SESSION['user']->data['tournaments']);
2023-10-26 21:18:32 +02:00
if ( ! is_array ( $_SESSION [ 'user' ] -> data [ 'tournaments' ]) || ! in_array ( $tid , $_SESSION [ 'user' ] -> data [ 'tournaments' ])) // new t!
{
$_SESSION [ 'user' ] -> data [ 'tournaments' ][] = $tid ;
try
{
$_SESSION [ 'user' ] -> save ();
}
catch ( Exception $e )
{
$this -> set ( 'template' , '/templates/partials/error.html' );
$this -> set ( 'errorTitle' , 'Error' );
$this -> set ( 'errorMessage' , $e -> getMessage ());
return ;
}
$this -> redirect ( '/tournaments/event/' . $tid );
}
else
{
$this -> set ( 'template' , '/templates/partials/success.html' );
$this -> set ( 'successMessage' , " Daten erfolgreich gespeichert " );
return ;
}
}
}
else
{
$id = $this -> params [ 0 ];
$t = new Tournament ();
$t -> load ( $id );
$this -> set ( 'tournamentdata' , $t -> data );
$this -> set ( 'tournamentid' , $t -> id );
$this -> set ( 'template' , 'edit_tournament.html' );
}
}
2023-10-26 17:42:33 +02:00
function maySeeThisPage () {
if ( $_SESSION [ 'user' ]) //wenn eingeloggt, kein problem
return true ;
else return false ;
}
}