<?php

class Profile extends Page {

    function index()
    {
        $this->set('user', $_SESSION['user']->data);
        $this->set('template', 'profile.html');
    }

    function edit()
    {
        if($_REQUEST['submit']=='true')
        {
            $error = false;

            $_SESSION['user']->data['firstname'] = trim($_REQUEST['firstname']);
            $_SESSION['user']->data['lastname'] = trim($_REQUEST['lastname']);
            //$_SESSION['user']->data['email'] = $_REQUEST['email'];
            $_SESSION['user']->data['birthday'] = $_REQUEST['birthday'];
            $_SESSION['user']->data['club'] = trim($_REQUEST['club']);
            //$_SESSION['user']->data['timezone'] = $_REQUEST['timezone'];

            if(!strtotime($_SESSION['user']->data['birthday']))
                $error = 'Das Geburstdatum ist ungültig. Bitte die Eingabe prüfen';
            
            $newphoto = false;
            if($_FILES['photo'])
            {
                $photo = $_FILES['photo'];
                $photo_name = $photo['name'];
                $photo_tmp_name = $photo['tmp_name'];
                $photo_size = $photo['size'];
                $photo_error = $photo['error'];
                $photo_type = $photo['type'];

                $allowed = ['jpg','jpeg','png','gif'];
                $photo_ext = strtolower(end(explode('.', $photo_name)));

                if(in_array($photo_ext, $allowed))
                {
                    if($photo_error === 0)
                    {
                        if($photo_size < 10000000)
                        {
                            $answer = pictshareUploadImage($photo_tmp_name);
                            if($answer['status']=='ok' && in_array($answer['filetype'],['jpeg','png','gif']))
                                $newphoto = $answer['url'];
                            else
                                return partial('error.html', ['errorTitle' => 'Error', 'errorMessage' => 'Fehler beim CDN Upload: '.json_encode($answer,true)]);
                        }
                        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']);
            }

            if($newphoto)
                $_SESSION['user']->data['photo'] = $newphoto;

            try{
                $_SESSION['user']->save();
            }
            catch(Exception $e)
            {
                return partial('error.html', ['errorMessage' => 'Fehler beim Speichern des Profils: '.$e->getMessage()]);
            }
            
            $this->redirect('/profile');
        }

        $this->set('user', $_SESSION['user']->data);
        $this->set('template', 'edit_profile.html');
    }

    function maySeeThisPage() {
        if($_SESSION['user']) //wenn eingeloggt, kein problem
            return true;
        else return false;
    }

}