added nicer overview and delete function

This commit is contained in:
Chris 2023-10-26 17:50:54 +02:00
parent 0f13bdea27
commit ddf13b2413
8 changed files with 81 additions and 23 deletions

View File

@ -57,9 +57,13 @@ class Model {
public function save() public function save()
{ {
$newgen = false;
if (!$this->id) if (!$this->id)
{
$this->id = gen_ulid(); $this->id = gen_ulid();
if (!$this->validate()) $newgen = true;
}
if (!$this->validate($newgen))
return false; return false;
foreach($this->dbFields as $field=>$options) foreach($this->dbFields as $field=>$options)
{ {
@ -111,7 +115,7 @@ class Model {
/** /**
* @param array $data * @param array $data
*/ */
private function validate() private function validate($newgen=false)
{ {
if (!$this->dbFields) if (!$this->dbFields)
return true; return true;
@ -133,8 +137,8 @@ class Model {
if (is_array($value)) if (is_array($value))
continue; continue;
if (isset($desc[0])) // if (isset($desc[0]))
$type = $desc[0]; // $type = $desc[0];
if (in_array('required',$options)) if (in_array('required',$options))
$required = true; $required = true;
@ -149,8 +153,11 @@ class Model {
$this->data[$key] = $value; $this->data[$key] = $value;
} }
if($options['default']!==null && $value===null) if($options['default']!==null && $newgen === true && !$value)
{
$value = $options['default']; $value = $options['default'];
$this->data[$key] = $value;
}
if ($required && strlen($value) == 0) { if ($required && strlen($value) == 0) {
throw new Exception($this->dbTable . "." . $key . " is required"); throw new Exception($this->dbTable . "." . $key . " is required");

View File

@ -47,7 +47,7 @@ class Page
$this->menu_priority = 1; $this->menu_priority = 1;
} }
function addSubmenuItem($text, $action, $icon = '', $classes = '') function addSubmenuItem($text, $action='', $icon = '', $classes = '')
{ {
$active = $GLOBALS['url'][0]; $active = $GLOBALS['url'][0];
if (!$active) $active = 'index'; if (!$active) $active = 'index';

View File

@ -12,8 +12,8 @@ class Dog extends Model
'size' => ['type' => 'text'], //in cm 'size' => ['type' => 'text'], //in cm
'birthday' => ['type' => 'text'], 'birthday' => ['type' => 'text'],
'agility_size' => ['type' => 'text'], //S,M,I,L 'agility_size' => ['type' => 'text'], //S,M,I,L
'photo' => ['type' => 'text'], 'photo' => ['type' => 'text', 'default' => 'https://pictshare.net/1ch3e5.png'],
'active' => ['type' => 'int', 'default' => 1] 'active' => ['type' => 'bool', 'default' => 1]
); );
function isMyDog($dog = false) function isMyDog($dog = false)

View File

@ -33,6 +33,16 @@ class User extends Model {
unset($_SESSION['user']); unset($_SESSION['user']);
} }
function removeDog($dogid)
{
if(!$this->id) return false;
if (in_array($dogid, $this->data['dogs']))
{
$this->data['dogs'] = array_diff($this->data['dogs'],[$dogid]);
$this->save();
}
}
function getAll($filtered = true) function getAll($filtered = true)
{ {
$keys = $this->redis->keys($this->dbTable.':*'); $keys = $this->redis->keys($this->dbTable.':*');

View File

@ -15,7 +15,7 @@ class Dogs extends Page {
$this->addSubmenuItem('Hund hinzufügen','/dogs/add','fas fa-plus-circle'); $this->addSubmenuItem('Hund hinzufügen','/dogs/add','fas fa-plus-circle');
if($_SESSION['user']->data['dogs'] && count($_SESSION['user']->data['dogs']) > 0) if($_SESSION['user']->data['dogs'] && count($_SESSION['user']->data['dogs']) > 0)
{ {
$this->addSubmenuItem('divider',''); $this->addSubmenuItem('divider');
foreach($_SESSION['user']->data['dogs'] as $dogid) foreach($_SESSION['user']->data['dogs'] as $dogid)
{ {
$dog = new Dog(); $dog = new Dog();
@ -23,7 +23,6 @@ class Dogs extends Page {
$this->addSubmenuItem($dog->data['name'],'/dogs/overview/'.$dogid,'fas fa-dog'); $this->addSubmenuItem($dog->data['name'],'/dogs/overview/'.$dogid,'fas fa-dog');
} }
} }
} }
function index() { function index() {
@ -67,6 +66,20 @@ class Dogs extends Page {
$this->set('template', 'edit.html'); $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');
}
function edit() { function edit() {
if($_REQUEST['submit']) if($_REQUEST['submit'])
{ {
@ -74,10 +87,11 @@ class Dogs extends Page {
$name = $_REQUEST['name']; $name = $_REQUEST['name'];
$kennel_name = $_REQUEST['kennel_name']; $kennel_name = $_REQUEST['kennel_name'];
$dog_breed = $_REQUEST['dog_breed']; $dog_breed = $_REQUEST['dog_breed'];
$dog_size = $_REQUEST['dog_size']; $dog_size = intval($_REQUEST['dog_size']);
$dog_birthday = $_REQUEST['dog_birthday']; $dog_birthday = $_REQUEST['dog_birthday'];
$agi_height_category = $_REQUEST['agi_height_category']; $agi_height_category = $_REQUEST['agi_height_category'];
$newphoto = false; $newphoto = false;
$active = intval($_REQUEST['agi_active']);
if($_FILES['photo']) if($_FILES['photo'])
{ {
@ -142,6 +156,7 @@ class Dogs extends Page {
$dog->size = $dog_size; $dog->size = $dog_size;
$dog->birthday = $dog_birthday; $dog->birthday = $dog_birthday;
$dog->agility_size = $agi_height_category; $dog->agility_size = $agi_height_category;
$dog->active = $active;
if($newphoto) if($newphoto)
$dog->photo = $newphoto; $dog->photo = $newphoto;

View File

@ -10,8 +10,9 @@
<p class="card-text"> <p class="card-text">
<ul> <ul>
<li>Rasse: <?= escape($dogdata['breed'])?:'Nicht eingetragen'; ?></li>
<li>Alter: <?= date_diff(date_create($dogdata['birthday']), date_create('now'))->y ?></li> <li>Alter: <?= date_diff(date_create($dogdata['birthday']), date_create('now'))->y ?></li>
<?php if($dogdata['breed']): ?><li>Rasse: <?= escape($dogdata['breed']); ?></li> <?php endif; ?>
<?php if($dogdata['kennel_name']): ?><li>Zuchtname: <?= escape($dogdata['kennel_name']); ?></li> <?php endif; ?> <?php if($dogdata['kennel_name']): ?><li>Zuchtname: <?= escape($dogdata['kennel_name']); ?></li> <?php endif; ?>
<?php if($dogdata['size']): ?><li>Größe: <?= escape($dogdata['size']); ?> cm</li> <?php endif; ?> <?php if($dogdata['size']): ?><li>Größe: <?= escape($dogdata['size']); ?> cm</li> <?php endif; ?>
<?php if($dogdata['agility_size']): ?><li>Agility Größe: <?= escape($dogdata['agility_size']); ?></li> <?php endif; ?> <?php if($dogdata['agility_size']): ?><li>Agility Größe: <?= escape($dogdata['agility_size']); ?></li> <?php endif; ?>
@ -25,8 +26,8 @@
</div> </div>
</div> </div>
</div> </div>
<div class="col-8"> <div class="col-8" id="sitemain">
Second Column
</div> </div>
</div> </div>

View File

@ -23,6 +23,13 @@
<label for="dog_birthday" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Geburtstag des Hundes</label> <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> <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>
<div>
<label for="agi_active" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Hund ist noch aktiv im Sport?</label>
<select id="agi_active" name="agi_active" 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="1" <?= $dogdata['active']=='1'?'selected':''; ?>>Ja</option>
<option value="0" <?= $dogdata['active']=='0'?'selected':''; ?>>Nein</option>
</select>
</div>
<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> <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"> <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">

View File

@ -1,15 +1,23 @@
<h1">Meine Hunde</h1> <h1>Meine Hunde</h1>
<figure> <figure>
<table> <table class="table">
<thead> <thead>
<tr> <tr>
<?php foreach (array_keys($doggos[0]) as $key) : ?>
<th> <th>
<?= $key ?> Foto
</th>
<th>
Name
</th>
<th>
Geburstag
</th>
<th>
Aktiv
</th> </th>
<?php endforeach; ?>
<th> <th>
Bearbeiten Bearbeiten
@ -23,17 +31,27 @@
<?php foreach ($doggos as $dog) : ?> <?php foreach ($doggos as $dog) : ?>
<tr> <tr>
<?php foreach (array_keys($dog) as $key) : ?>
<td> <td>
<?= $dog[$key] ?> <img src="<?= $dog['photo'] ?>/50x50/forcesize">
</td>
<td>
<?= escape($dog['name']); ?>
</td>
<td>
<?= escape($dog['birthday']); ?>
</td>
<td>
<?= escape($dog['active'])?'Ja':'Nein'; ?>
</td> </td>
<?php endforeach; ?>
<td> <td>
<button hx-get="/dogs/edit/<?= $dog['id'] ?>" hx-push-url="/dogs/edit/<?= $dog['id'] ?>" hx-target="#main" >Bearbeiten</button> <button hx-get="/dogs/edit/<?= $dog['id'] ?>" hx-push-url="/dogs/edit/<?= $dog['id'] ?>" hx-target="#main" >Bearbeiten</button>
</td> </td>
<td> <td>
<button hx-get="/dogs/delete/<?= $dog['id'] ?>" hx-target="#main" >Löschen</button> <button hx-get="/dogs/delete/<?= $dog['id'] ?>" hx-target="#main" hx-confirm="Bist du sicher, dass du <?= escape($dog['name']); ?> löschen willst">Löschen</button>
</td> </td>
</tr> </tr>