added nicer overview and delete function
This commit is contained in:
parent
0f13bdea27
commit
ddf13b2413
@ -57,9 +57,13 @@ class Model {
|
||||
|
||||
public function save()
|
||||
{
|
||||
$newgen = false;
|
||||
if (!$this->id)
|
||||
{
|
||||
$this->id = gen_ulid();
|
||||
if (!$this->validate())
|
||||
$newgen = true;
|
||||
}
|
||||
if (!$this->validate($newgen))
|
||||
return false;
|
||||
foreach($this->dbFields as $field=>$options)
|
||||
{
|
||||
@ -111,7 +115,7 @@ class Model {
|
||||
/**
|
||||
* @param array $data
|
||||
*/
|
||||
private function validate()
|
||||
private function validate($newgen=false)
|
||||
{
|
||||
if (!$this->dbFields)
|
||||
return true;
|
||||
@ -133,8 +137,8 @@ class Model {
|
||||
if (is_array($value))
|
||||
continue;
|
||||
|
||||
if (isset($desc[0]))
|
||||
$type = $desc[0];
|
||||
// if (isset($desc[0]))
|
||||
// $type = $desc[0];
|
||||
if (in_array('required',$options))
|
||||
$required = true;
|
||||
|
||||
@ -149,8 +153,11 @@ class Model {
|
||||
$this->data[$key] = $value;
|
||||
}
|
||||
|
||||
if($options['default']!==null && $value===null)
|
||||
if($options['default']!==null && $newgen === true && !$value)
|
||||
{
|
||||
$value = $options['default'];
|
||||
$this->data[$key] = $value;
|
||||
}
|
||||
|
||||
if ($required && strlen($value) == 0) {
|
||||
throw new Exception($this->dbTable . "." . $key . " is required");
|
||||
|
@ -47,7 +47,7 @@ class Page
|
||||
$this->menu_priority = 1;
|
||||
}
|
||||
|
||||
function addSubmenuItem($text, $action, $icon = '', $classes = '')
|
||||
function addSubmenuItem($text, $action='', $icon = '', $classes = '')
|
||||
{
|
||||
$active = $GLOBALS['url'][0];
|
||||
if (!$active) $active = 'index';
|
||||
|
@ -12,8 +12,8 @@ class Dog extends Model
|
||||
'size' => ['type' => 'text'], //in cm
|
||||
'birthday' => ['type' => 'text'],
|
||||
'agility_size' => ['type' => 'text'], //S,M,I,L
|
||||
'photo' => ['type' => 'text'],
|
||||
'active' => ['type' => 'int', 'default' => 1]
|
||||
'photo' => ['type' => 'text', 'default' => 'https://pictshare.net/1ch3e5.png'],
|
||||
'active' => ['type' => 'bool', 'default' => 1]
|
||||
);
|
||||
|
||||
function isMyDog($dog = false)
|
||||
|
@ -33,6 +33,16 @@ class User extends Model {
|
||||
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)
|
||||
{
|
||||
$keys = $this->redis->keys($this->dbTable.':*');
|
||||
|
@ -15,7 +15,7 @@ class Dogs extends Page {
|
||||
$this->addSubmenuItem('Hund hinzufügen','/dogs/add','fas fa-plus-circle');
|
||||
if($_SESSION['user']->data['dogs'] && count($_SESSION['user']->data['dogs']) > 0)
|
||||
{
|
||||
$this->addSubmenuItem('divider','');
|
||||
$this->addSubmenuItem('divider');
|
||||
foreach($_SESSION['user']->data['dogs'] as $dogid)
|
||||
{
|
||||
$dog = new Dog();
|
||||
@ -23,7 +23,6 @@ class Dogs extends Page {
|
||||
$this->addSubmenuItem($dog->data['name'],'/dogs/overview/'.$dogid,'fas fa-dog');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function index() {
|
||||
@ -67,6 +66,20 @@ class Dogs extends Page {
|
||||
$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() {
|
||||
if($_REQUEST['submit'])
|
||||
{
|
||||
@ -74,10 +87,11 @@ class Dogs extends Page {
|
||||
$name = $_REQUEST['name'];
|
||||
$kennel_name = $_REQUEST['kennel_name'];
|
||||
$dog_breed = $_REQUEST['dog_breed'];
|
||||
$dog_size = $_REQUEST['dog_size'];
|
||||
$dog_size = intval($_REQUEST['dog_size']);
|
||||
$dog_birthday = $_REQUEST['dog_birthday'];
|
||||
$agi_height_category = $_REQUEST['agi_height_category'];
|
||||
$newphoto = false;
|
||||
$active = intval($_REQUEST['agi_active']);
|
||||
|
||||
if($_FILES['photo'])
|
||||
{
|
||||
@ -142,6 +156,7 @@ class Dogs extends Page {
|
||||
$dog->size = $dog_size;
|
||||
$dog->birthday = $dog_birthday;
|
||||
$dog->agility_size = $agi_height_category;
|
||||
$dog->active = $active;
|
||||
if($newphoto)
|
||||
$dog->photo = $newphoto;
|
||||
|
||||
|
@ -10,8 +10,9 @@
|
||||
<p class="card-text">
|
||||
|
||||
<ul>
|
||||
<li>Rasse: <?= escape($dogdata['breed'])?:'Nicht eingetragen'; ?></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['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; ?>
|
||||
@ -25,8 +26,8 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-8">
|
||||
Second Column
|
||||
<div class="col-8" id="sitemain">
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
@ -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>
|
||||
<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_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>
|
||||
<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">
|
||||
|
@ -1,15 +1,23 @@
|
||||
<h1">Meine Hunde</h1>
|
||||
<h1>Meine Hunde</h1>
|
||||
|
||||
|
||||
<figure>
|
||||
<table>
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<?php foreach (array_keys($doggos[0]) as $key) : ?>
|
||||
<th>
|
||||
<?= $key ?>
|
||||
Foto
|
||||
</th>
|
||||
<th>
|
||||
Name
|
||||
</th>
|
||||
<th>
|
||||
Geburstag
|
||||
</th>
|
||||
|
||||
<th>
|
||||
Aktiv
|
||||
</th>
|
||||
<?php endforeach; ?>
|
||||
|
||||
<th>
|
||||
Bearbeiten
|
||||
@ -23,17 +31,27 @@
|
||||
<?php foreach ($doggos as $dog) : ?>
|
||||
|
||||
<tr>
|
||||
<?php foreach (array_keys($dog) as $key) : ?>
|
||||
<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>
|
||||
<?php endforeach; ?>
|
||||
|
||||
<td>
|
||||
<button hx-get="/dogs/edit/<?= $dog['id'] ?>" hx-push-url="/dogs/edit/<?= $dog['id'] ?>" hx-target="#main" >Bearbeiten</button>
|
||||
</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>
|
||||
</tr>
|
||||
|
||||
|
Reference in New Issue
Block a user