From ddf13b241312be87f5fe61e4e5f67014b6a544a9 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 26 Oct 2023 17:50:54 +0200 Subject: [PATCH] added nicer overview and delete function --- web/inc/classes/Model.class.php | 17 +++++++++++----- web/inc/classes/Page.class.php | 2 +- web/models/Dog.model.php | 4 ++-- web/models/User.model.php | 10 +++++++++ web/pages/dogs/controller.php | 21 ++++++++++++++++--- web/pages/dogs/dog.html | 7 ++++--- web/pages/dogs/edit.html | 7 +++++++ web/pages/dogs/list.html | 36 ++++++++++++++++++++++++--------- 8 files changed, 81 insertions(+), 23 deletions(-) diff --git a/web/inc/classes/Model.class.php b/web/inc/classes/Model.class.php index 0ae19be..8980de0 100644 --- a/web/inc/classes/Model.class.php +++ b/web/inc/classes/Model.class.php @@ -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"); diff --git a/web/inc/classes/Page.class.php b/web/inc/classes/Page.class.php index f90e185..f1627df 100644 --- a/web/inc/classes/Page.class.php +++ b/web/inc/classes/Page.class.php @@ -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'; diff --git a/web/models/Dog.model.php b/web/models/Dog.model.php index 9822205..9b8b4b4 100644 --- a/web/models/Dog.model.php +++ b/web/models/Dog.model.php @@ -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) diff --git a/web/models/User.model.php b/web/models/User.model.php index f90bab9..3bf6848 100644 --- a/web/models/User.model.php +++ b/web/models/User.model.php @@ -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.':*'); diff --git a/web/pages/dogs/controller.php b/web/pages/dogs/controller.php index 7912c84..669559b 100644 --- a/web/pages/dogs/controller.php +++ b/web/pages/dogs/controller.php @@ -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; diff --git a/web/pages/dogs/dog.html b/web/pages/dogs/dog.html index 0ce0a3b..9021252 100644 --- a/web/pages/dogs/dog.html +++ b/web/pages/dogs/dog.html @@ -10,8 +10,9 @@