This repository has been archived on 2023-12-29. You can view files and clone it, but cannot push or open issues or pull requests.
dogstats/web/pages/dogs/controller.php

230 lines
7.8 KiB
PHP
Raw Permalink Normal View History

2023-10-22 23:05:24 +02:00
<?php
class Dogs extends Page {
function setMenu()
{
$this->menu_text = 'Meine Hunde';
$this->menu_image = 'far fa-dog';
2023-10-23 14:15:33 +02:00
$this->menu_priority = 1;
2023-10-22 23:05:24 +02:00
}
function setSubmenu()
{
$this->addSubmenuItem('Hunde anzeigen','/dogs','far fa-list-alt');
$this->addSubmenuItem('Hund hinzufügen','/dogs/add','fas fa-plus-circle');
2023-10-23 14:36:28 +02:00
if($_SESSION['user']->data['dogs'] && count($_SESSION['user']->data['dogs']) > 0)
{
$this->addSubmenuItem('divider');
2023-10-23 14:36:28 +02:00
foreach($_SESSION['user']->data['dogs'] as $dogid)
{
$dog = new Dog();
$dog->load($dogid);
$this->addSubmenuItem($dog->data['name'],'/dogs/overview/'.$dogid,'fas fa-dog');
}
}
2023-10-22 23:05:24 +02:00
}
function index() {
$dogs = $_SESSION['user']->data['dogs'];
$doggos = [];
2023-10-23 10:00:19 +02:00
foreach($dogs as $key => $dogid)
2023-10-22 23:05:24 +02:00
{
2023-10-23 10:00:19 +02:00
//var_dump($dogid);
2023-10-22 23:05:24 +02:00
$dog = new Dog();
try{
$dog->load($dogid);
}
catch(Exception $e)
{
2023-10-23 10:00:19 +02:00
error_log("Dog $dogid not found. Deleting from user");
unset($_SESSION['user']->data['dogs'][$key]);
$_SESSION['user']->save();
2023-10-22 23:05:24 +02:00
continue;
}
if($dog->data)
$doggos[] = array_merge($dog->data,['id'=>$dogid]);
}
//var_dump($doggos);
if(count($doggos) > 0)
{
$this->set('doggos',$doggos);
$this->set('template', 'list.html');
}
else
{
$this->set('template','/templates/partials/info.html');
$this->set('infoTitle', 'Keine Hunde gefunden');
$this->set('infoMessage', 'Du hast aktuell noch keine Hunde angelegt. Klicke im Menü auf "Hund hinzufügen" um einen Hund anzulegen.');
}
}
function add() {
$this->set('template', 'edit.html');
}
function delete(){
$dogid = $this->params[0];
$d = new Dog();
if(!$d->isMyDog($dogid))
return 'Not your dog :(';
$d->load($dogid);
$d->delete();
$_SESSION['user']->removeDog($dogid);
$this->redirect('/dogs');
}
2023-10-22 23:05:24 +02:00
function edit() {
if($_REQUEST['submit'])
{
$id = $_REQUEST['dog_id'];
$name = $_REQUEST['name'];
$kennel_name = $_REQUEST['kennel_name'];
$dog_breed = $_REQUEST['dog_breed'];
$dog_size = intval($_REQUEST['dog_size']);
2023-10-22 23:05:24 +02:00
$dog_birthday = $_REQUEST['dog_birthday'];
$agi_height_category = $_REQUEST['agi_height_category'];
$active = intval($_REQUEST['agi_active']);
2023-10-26 13:46:55 +02:00
2023-10-30 19:27:44 +01:00
$newphoto = false;
2023-10-26 13:46:55 +02:00
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'];
2023-10-26 21:18:32 +02:00
$allowed = ['jpg','jpeg','png','gif'];
2023-10-26 13:46:55 +02:00
$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'];
2023-10-27 12:06:09 +02:00
else
2023-10-27 12:08:56 +02:00
return partial('error.html', ['errorTitle' => 'Error', 'errorMessage' => 'Fehler beim CDN Upload: '.json_encode($answer,true)]);
2023-10-26 13:46:55 +02:00
}
else
2023-10-26 16:41:33 +02:00
return partial('error.html', ['errorTitle' => 'Error', 'errorMessage' => 'Die Datei ist zu groß. Bitte eine kleinere Datei hochladen']);
2023-10-26 13:46:55 +02:00
}
else
2023-10-26 16:41:33 +02:00
return partial('error.html', ['errorTitle' => 'Error', 'errorMessage' => 'Beim Upload der Datei ist ein Fehler aufgetreten']);
2023-10-26 13:46:55 +02:00
}
else
2023-10-26 16:41:33 +02:00
return partial('error.html', ['errorTitle' => 'Error', 'errorMessage' => 'Dieser Dateityp ist nicht erlaubt. Bitte nur jpg, jpeg oder png Dateien hochladen']);
2023-10-26 13:46:55 +02:00
}
2023-10-22 23:05:24 +02:00
$error = false;
if(!$name || !$dog_birthday )
$error = 'Bitte zumindest Name und Geburtsdatum angeben';
else if(!strtotime($dog_birthday))
$error = 'Das Geburstdatum ist ungültig. Bitte die Eingabe prüfen';
if($error){
$this->set('errorMessage', $error);
$this->set('template', '/templates/partials/error.html');
return;
}
else
{
$dog = new Dog();
if($id)
$dog->load($id);
$dog->name = $name;
$dog->kennel_name = $kennel_name;
$dog->breed = $dog_breed;
$dog->size = $dog_size;
$dog->birthday = $dog_birthday;
$dog->agility_size = $agi_height_category;
$dog->active = $active;
2023-10-26 13:46:55 +02:00
if($newphoto)
$dog->photo = $newphoto;
2023-10-22 23:05:24 +02:00
try
{
$dogid = $dog->save();
}
catch(Exception $e)
{
$this->set('template', '/templates/partials/error.html');
$this->set('errorTitle', 'Error');
$this->set('errorMessage', $e->getMessage());
return;
}
//var_dump($_SESSION['user']->data['dogs']);
if(!is_array($_SESSION['user']->data['dogs']) || !in_array($dogid, $_SESSION['user']->data['dogs'])) // new dog!
{
$_SESSION['user']->data['dogs'][] = $dogid;
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('/dogs');
}
else
{
$this->set('template', '/templates/partials/success.html');
$this->set('successMessage', "Daten erfolgreich gespeichert");
return;
}
}
}
else
{
//checken ob der den hund eh bearbeiten darf
$id = $this->params[0];
$dog = new Dog();
$dog->load($id);
$this->set('dogdata',$dog->data);
$this->set('dogid',$dog->id);
$this->set('template', 'edit.html');
}
}
2023-10-23 14:36:28 +02:00
function overview()
{
$dogid = $this->params[0];
$d = new Dog();
2023-11-01 19:53:36 +01:00
//if(!$d->isMyDog($dogid))
// return 'Not your dog :(';
2023-10-26 13:46:55 +02:00
$d->load($dogid);
2023-11-01 19:53:36 +01:00
$this->set('ismydog',$d->isMyDog($dogid));
2023-10-31 22:43:08 +01:00
$this->set('results', $d->getResults());
2023-10-26 13:46:55 +02:00
$this->set('dogdata', $d->data);
$this->set('dogid', $dogid);
2023-10-31 23:42:26 +01:00
$this->set('template', 'dog.html.php');
2023-10-23 14:36:28 +02:00
}
2023-10-22 23:05:24 +02:00
function maySeeThisPage() {
if($_SESSION['user']) //wenn eingeloggt, kein problem
return true;
else return false;
}
}