man kann hunde anlegen yay
All checks were successful
Build and push / Pulling repo on server (push) Successful in 18s

This commit is contained in:
Chris 2023-10-22 23:05:24 +02:00
parent 890905b435
commit f7a7c3457b
19 changed files with 314 additions and 83 deletions

View File

@ -5,7 +5,8 @@ https://gitea.haschek.at/Crispi/dogstats
## Techstack
- [HTMX](https://htmx.org/docs/)
- [Tailwind](https://flowbite.com/docs/components/)
- [Tailwind](https://flowbite.com/docs/components/accordion/)
- [Font Awesome 5 Pro](https://fontawesome.com/v5/search)
## Start Dev

View File

@ -57,34 +57,54 @@ class Model {
public function save()
{
if (!$this->id)
$this->id = gen_ulid();
if (!$this->validate())
return false;
foreach($this->dbFields as $field=>$options)
{
if(isset($this->data[$field]))
$this->redis->hset($this->dbTable.':'.$this->id,$field,$this->data[$field]);
{
error_log($field.' -> '.$options['type']);
if($options['type']=='array')
$GLOBALS['redis']->hset($this->dbTable.':'.$this->id,$field,json_encode($this->data[$field]));
else
{
error_log("hset $this->dbTable:$this->id $field {$this->data[$field]}");
$GLOBALS['redis']->hset($this->dbTable.':'.$this->id,$field,$this->data[$field]);
}
}
}
return $this->id;
}
public function load($id)
{
$this->id = $id;
if(!$this->redis->exists($this->dbTable.':'.$this->id))
if(!$GLOBALS['redis']->exists($this->dbTable.':'.$this->id))
return false;
$keys = array_keys($this->dbFields);
foreach($keys as $key)
{
$value = $this->redis->hget($this->dbTable.':'.$this->id,$key);
$value = $GLOBALS['redis']->hget($this->dbTable.':'.$this->id,$key);
if($value!==NULL) //we'll leave null values
switch($this->dbFields[$key]['type'])
{
case 'int': $value = intval($value);break;
case 'bool': $value = boolval($value);break;
case 'float': $value = floatval($value);break;
case 'double': $value = doubleval($value);break;
}
switch($this->dbFields[$key]['type'])
{
case 'int': $value = intval($value);break;
case 'bool': $value = boolval($value);break;
case 'float': $value = floatval($value);break;
case 'double': $value = doubleval($value);break;
case 'array':
$value = json_decode($value,true);
if($value===null)
$value = [];
break;
}
$this->data[$key] = $value;
}
@ -148,6 +168,10 @@ class Model {
throw new Exception("$type validation failed");
}
break;
case 'array':
if(!is_array($value))
throw new Exception('Dogs validation failed');
break;
case 'csv':
case "text":
$regexp = null;
@ -180,7 +204,7 @@ class Model {
function delete()
{
return $this->redis->del($this->dbTable.':'.$this->id);
return $GLOBALS['redis']->del($this->dbTable.':'.$this->id);
}
function gen_shorthash(){

View File

@ -30,7 +30,7 @@ function includeManagement()
{
$redis = new Redis();
try{
$redis->pconnect(REDIS_SERVER, REDIS_PORT);
$redis->connect(REDIS_SERVER, REDIS_PORT);
if (defined('REDIS_PASS') && REDIS_PASS)
$redis->auth(REDIS_PASS);
if (defined('REDIS_PREFIX') && REDIS_PREFIX)

View File

@ -1,5 +1,5 @@
<?php
session_start();
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(__FILE__));
@ -10,6 +10,8 @@ define('ROOT', dirname(__FILE__));
include_once(ROOT.DS.'inc'.DS.'core.php');
includeManagement();
session_start();
if($_SERVER['HTTP_HX_REQUEST']!='true')
{
include(ROOT.DS.'templates'.DS.'mainpage.html');

17
web/models/Dog.model.php Normal file
View File

@ -0,0 +1,17 @@
<?php
class Dog extends Model {
protected $dbTable = "dogs";
protected $dbFields = Array (
'uuid' => ['type'=>'text','required','unique','autoValMethod'=>'gen_ulid'],
'registered' => ['type'=>'datetime','required','unique','autoValMethod'=>'getDateTime'],
'name' => ['type'=>'text'],
'kennel_name' => ['type'=>'text'],
'breed' => ['type'=>'text'],
'size' => ['type'=>'text'], //in cm
'birthday' => ['type'=>'int'], //unix timestamp
'agility_size' => ['type'=>'int'], //S,M,I,L
'active' => ['type'=>'int','default'=>1]
);
}

View File

@ -12,6 +12,7 @@ class User extends Model {
'last_login' => ['type'=>'datetime','required','unique','autoValMethod'=>'getDateTime'],
'token' => ['type'=>'text','required','unique','autoValMethod'=>'uuid4'],
'timezone' => ['type'=>'int'],
'dogs' => ['type'=> 'array','default'=>[]],
'active' => ['type'=>'int','default'=>0]
);
protected $hidden = ['password','token'];

View File

@ -53,6 +53,10 @@ class Admin extends Page {
foreach($_REQUEST as $key => $value)
{
if($key == 'email') continue;
if($key=='dogs')
{
$value = json_decode(html_entity_decode($value),true);
}
$u->$key = $value;
}
@ -61,12 +65,12 @@ class Admin extends Page {
}
catch(Exception $e)
{
$this->set('message', $e->getMessage());
$this->set('errorMessage', $e->getMessage());
$this->set('template', '/templates/partials/error.html');
return;
}
$this->set('message', 'Speichern erfolgreich');
$this->set('successMessage', 'Speichern erfolgreich');
$this->set('template', '/templates/partials/success.html');
}

View File

@ -22,7 +22,7 @@
<label for="<?= $key ?>" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
<?= $key ?>
</label>
<input type="text" name="<?= $key ?>" id="<?= $key ?>" value="<?= $userdata[$key] ?>" class="bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
<input type="text" name="<?= $key ?>" id="<?= $key ?>" value="<?= is_array($userdata[$key])?escape(json_encode($userdata[$key])):$userdata[$key] ?>" class="bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
</div>
<?php endforeach; ?>
</div>

View File

@ -1,16 +0,0 @@
<?php
class Dog extends Page {
function setMenu()
{
$this->menu_text = 'Dog';
$this->menu_image = 'far fa-dog';
$this->menu_priority = 0;
}
function index() {
$this->set('template', 'dog.html');
}
}

View File

@ -1,32 +0,0 @@
<div class="container mx-auto">
<h1 class="text-4xl md:text-5xl">Hund hinzufügen/bearbeiten</h1>
<form>
<div>
<label for="name" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Name</label>
<input type="text" id="name" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Max" required>
</div>
<div>
<label for="kennel_name" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Zuchtname</label>
<input type="text" id="kennel_name" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Zuchtname" required>
</div>
<div>
<label for="dog_breed" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Rasse</label>
<input type="text" id="dog_breed" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Flowbite" required>
</div>
<div>
<label for="dog_size" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Größe des Hundes (in cm)</label>
<input type="number" id="dog_size" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="" required>
</div>
<div>
<label for="agi_height_category" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Agility Größenklasse</label>
<select id="agi_height_category" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
<option value="S" selected>S</option>
<option value="M">M</option>
<option value="I">I</option>
<option value="L">L</option>
</select>
</div>
<button type="submit" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Submit</button>
</form>
</div>

View File

@ -0,0 +1,147 @@
<?php
class Dogs extends Page {
function setMenu()
{
$this->menu_text = 'Meine Hunde';
$this->menu_image = 'far fa-dog';
$this->menu_priority = 0;
}
function setSubmenu()
{
$this->addSubmenuItem('Hunde anzeigen','/dogs','far fa-list-alt');
$this->addSubmenuItem('Hund hinzufügen','/dogs/add','fas fa-plus-circle');
}
function index() {
$dogs = $_SESSION['user']->data['dogs'];
$doggos = [];
foreach($dogs as $dogid)
{
$dog = new Dog();
try{
$dog->load($dogid);
}
catch(Exception $e)
{
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 edit() {
if($_REQUEST['submit'])
{
$id = $_REQUEST['dog_id'];
$name = $_REQUEST['name'];
$kennel_name = $_REQUEST['kennel_name'];
$dog_breed = $_REQUEST['dog_breed'];
$dog_size = $_REQUEST['dog_size'];
$dog_birthday = $_REQUEST['dog_birthday'];
$agi_height_category = $_REQUEST['agi_height_category'];
$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;
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');
}
}
function maySeeThisPage() {
if($_SESSION['user']) //wenn eingeloggt, kein problem
return true;
else return false;
}
}

38
web/pages/dogs/edit.html Normal file
View File

@ -0,0 +1,38 @@
<div class="container mx-auto">
<h1 class="text-4xl md:text-5xl">Hund hinzufügen/bearbeiten</h1>
<form hx-post="/dogs/edit" hx-target="#response">
<input type="hidden" name="dog_id" value="<?= $dogid; ?>">
<div>
<label for="name" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Name</label>
<input type="text" value="<?= $dogdata['name']; ?>" id="name" name="name" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Max" required>
</div>
<div>
<label for="kennel_name" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Zuchtname</label>
<input type="text" value="<?= $dogdata['kennel_name']; ?>" id="kennel_name" name="kennel_name" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Zuchtname">
</div>
<div>
<label for="dog_breed" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Rasse</label>
<input type="text" value="<?= $dogdata['breed']; ?>" id="dog_breed" name="dog_breed" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Flowbite">
</div>
<div>
<label for="dog_size" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Größe des Hundes (in cm)</label>
<input type="number" value="<?= $dogdata['size']; ?>" id="dog_size" name="dog_size" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="">
</div>
<div>
<label for="dog_birthday" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Geburtstag des Hundes</label>
<input type="date" value="<?= $dogdata['birthday']; ?>" id="dog_birthday" name="dog_birthday" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="" required>
</div>
<div>
<label for="agi_height_category" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Agility Größenklasse</label>
<select id="agi_height_category" name="agi_height_category" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
<option value="S" <?= $dogdata['agility_size']=='S'?'selected':''; ?>>S</option>
<option value="M" <?= $dogdata['agility_size']=='M'?'selected':''; ?>>M</option>
<option value="I" <?= $dogdata['agility_size']=='I'?'selected':''; ?>>I</option>
<option value="L" <?= $dogdata['agility_size']=='L'?'selected':''; ?>>L</option>
</select>
</div>
<button type="submit" name="submit" value="true" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Submit</button>
</form>
<div id="response"></div>
</div>

48
web/pages/dogs/list.html Normal file
View File

@ -0,0 +1,48 @@
<h1 class="text-2xl p-6 text-center">Meine Hunde</h1>
<div class="container flex justify-center mx-auto">
<div class="flex flex-col">
<div class="w-full">
<div class="border-b border-gray-200 shadow">
<table class="divide-y divide-gray-300 ">
<thead class="bg-gray-50">
<tr>
<?php foreach (array_keys($doggos[0]) as $key) : ?>
<th class="px-6 py-2 text-xs text-gray-500">
<?= $key ?>
</th>
<?php endforeach; ?>
<th class="px-6 py-2 text-xs text-gray-500">
Bearbeiten
</th>
<th class="px-6 py-2 text-xs text-gray-500">
Löschen
</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-300">
<?php foreach ($doggos as $dog) : ?>
<tr class="whitespace-nowrap">
<?php foreach (array_keys($dog) as $key) : ?>
<td class="px-6 py-4 text-sm text-gray-500">
<?= $dog[$key] ?>
</td>
<?php endforeach; ?>
<td class="px-6 py-4">
<button hx-get="/dogs/edit/<?= $dog['id'] ?>" hx-push-url="/dogs/edit/<?= $dog['id'] ?>" hx-target="#main" class="px-4 py-1 text-sm text-indigo-600 bg-indigo-200 rounded-full">Bearbeiten</button>
</td>
<td class="px-6 py-4">
<button hx-get="/dogs/delete/<?= $dog['id'] ?>" hx-target="#main" class="px-4 py-1 text-sm text-red-400 bg-red-200 rounded-full">Löschen</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>

View File

@ -43,7 +43,7 @@
<tr class="whitespace-nowrap">
<?php foreach (array_keys($user) as $key) : ?>
<td class="px-6 py-4 text-sm text-gray-500">
<?= $user[$key] ?>
<?= is_array($user[$key])?json_encode($user[$key]):$user[$key] ?>
</td>
<?php endforeach; ?>

View File

@ -15,7 +15,6 @@ class Register extends Page {
$hash = password_hash($password, PASSWORD_DEFAULT);
$u = new User();
$err = false;
@ -35,8 +34,8 @@ class Register extends Page {
if($err)
{
$this->set('template', '/templates/partials/error.html');
$this->set('title', 'Error');
$this->set('message', $err);
$this->set('errorTitle', 'Error');
$this->set('errorMessage', $err);
return;
}
else
@ -52,22 +51,22 @@ class Register extends Page {
catch(Exception $e)
{
$this->set('template', '/templates/partials/error.html');
$this->set('title', 'Error');
$this->set('message', $e->getMessage());
$this->set('errorTitle', 'Error');
$this->set('errorMessage', $e->getMessage());
return;
}
//$this->redirect('/register/success');
return;
}
return print_r(['email'=>$email,'password'=>$password,'password2'=>$password2], true);
//return print_r(['email'=>$email,'password'=>$password,'password2'=>$password2], true);
}
function success()
{
$this->set('template', '/templates/partials/success.html');
$this->set('title', 'Success');
$this->set('message', 'You have successfully registered. Activate your account from the Link in your email');
$this->set('successTitle', 'Success');
$this->set('successMessage', 'You have successfully registered. Activate your account from the Link in your email');
}
function test()

View File

@ -4,9 +4,7 @@
<h1 class="text-xl font-bold leading-tight tracking-tight text-gray-900 md:text-2xl dark:text-white">
Register
</h1>
<div id="response" class="text-gray-900 dark:text-white">
<?= $hello ?>
</div>
<div id="response" class="text-gray-900 dark:text-white"></div>
<form class="space-y-4 md:space-y-6" hx-post="/register/validate" hx-target="#response">
<div>
<label for="email" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Your email</label>

View File

@ -1,4 +1,4 @@
<div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4" role="alert">
<p class="font-bold"><?= $title ?></p>
<p><?= $message ?></p>
<p class="font-bold"><?= $errorTitle ?></p>
<p><?= $errorMessage ?></p>
</div>

View File

@ -1,4 +1,4 @@
<div class="bg-blue-100 border-l-4 border-blue-500 text-blue-700 p-4" role="alert">
<p class="font-bold"><?= $title ?></p>
<p><?= $message ?></p>
<p class="font-bold"><?= $infoTitle ?></p>
<p><?= $infoMessage ?></p>
</div>

View File

@ -1,4 +1,4 @@
<div class="bg-green-100 border-l-4 border-green-500 text-green-700 p-4" role="alert">
<p class="font-bold"><?= $title ?></p>
<p><?= $message ?></p>
<p class="font-bold"><?= $successTitle ?></p>
<p><?= $successMessage ?></p>
</div>