loaing of model via arbitrary field and nice home page
All checks were successful
Build and push / Pulling repo on server (push) Successful in 3s

This commit is contained in:
Chris 2023-10-29 17:37:28 +01:00
parent fea847afd2
commit 4516486b8e
5 changed files with 120 additions and 7 deletions

View File

@ -53,3 +53,32 @@ main>.container {
.tooltipp:hover:after { .tooltipp:hover:after {
opacity: 1; opacity: 1;
} }
/* XDEBUG */
.xdebug-error th
{
font-family:monospace;
font-weight:normal;
font-size:15px;
padding: 6px 6px 6px 6px;
border:1px solid black;
background: #FFCC99;
color:#000000;
}
.xdebug-error > tr:first-child > th:first-child,
.xdebug-error > tbody > tr:first-child > th:first-child
{
line-height:1.6em;
padding: 10px 10px 10px 10px;
border:1px solid #000000;
background: #000000;
color:#FFFFFF;
}
.xdebug-error td
{
font-size:14px;
padding: 6px 6px 6px 6px;
border:1px solid green;
background: #D1FFE8;
color:#000000;
}

View File

@ -9,7 +9,7 @@ class Model {
{ {
//redis //redis
if ($GLOBALS['redis']) if ($GLOBALS['redis'])
$this->redis = $GLOBALS['redis']; $GLOBALS['redis'] = $GLOBALS['redis'];
} }
/** /**
@ -55,6 +55,13 @@ class Model {
return $data; return $data;
} }
public function getField($field,$id=false)
{
if(!$id)
$id = $this->id;
return $GLOBALS['redis']->hget($this->dbTable.':'.$id,$field);
}
public function save() public function save()
{ {
$newgen = false; $newgen = false;
@ -84,9 +91,31 @@ class Model {
return $this->id; return $this->id;
} }
public function load($id) /**
* @param $value The value to search for
* @param string $field The field to search in. HAS to be marked as unique otherwise will throw error
*/
public function load($value,$field='id')
{ {
$this->id = $id; if($field!='id')
{
//sanity check. Check if $field is marked as unique
if(!in_array('unique',$this->dbFields[$field]))
throw new Exception($field.' is not unique');
//we need to find the id first
$keys = $GLOBALS['redis']->keys($this->dbTable.':*');
foreach($keys as $key){
$id = end(explode(':',$key));
$thisval = $GLOBALS['redis']->hget($this->dbTable.':'.$id,$field);
if($thisval==$value)
{
$this->id = $id;
break;
}
}
}
else
$this->id = $value;
if(!$GLOBALS['redis']->exists($this->dbTable.':'.$this->id)) if(!$GLOBALS['redis']->exists($this->dbTable.':'.$this->id))
throw new Exception($this->dbTable.':'.$this->id.' not found'); throw new Exception($this->dbTable.':'.$this->id.' not found');
$keys = array_keys($this->dbFields); $keys = array_keys($this->dbFields);
@ -95,7 +124,7 @@ class Model {
{ {
$value = $GLOBALS['redis']->hget($this->dbTable.':'.$this->id,$key); $value = $GLOBALS['redis']->hget($this->dbTable.':'.$this->id,$key);
if($value!==NULL) //we'll leave null values if($value!==NULL) //we'll leave null values alone
switch($this->dbFields[$key]['type']) switch($this->dbFields[$key]['type'])
{ {
case 'int': $value = intval($value);break; case 'int': $value = intval($value);break;

View File

@ -5,7 +5,7 @@ class User extends Model {
protected $dbFields = Array ( protected $dbFields = Array (
'uuid' => ['type'=>'text','required','unique','autoValMethod'=>'gen_ulid'], 'uuid' => ['type'=>'text','required','unique','autoValMethod'=>'gen_ulid'],
'password' => ['type'=>'text'], 'password' => ['type'=>'text'],
'registered' => ['type'=>'datetime','required','unique','autoValMethod'=>'getDateTime'], 'registered' => ['type'=>'datetime','required','autoValMethod'=>'getDateTime'],
'email' => ['type'=>'email','unique'], 'email' => ['type'=>'email','unique'],
'firstname' => ['type'=>'text'], 'firstname' => ['type'=>'text'],
'lastname' => ['type'=>'text'], 'lastname' => ['type'=>'text'],
@ -14,6 +14,7 @@ class User extends Model {
'timezone' => ['type'=>'int'], 'timezone' => ['type'=>'int'],
'dogs' => ['type'=> 'array','default'=>[]], 'dogs' => ['type'=> 'array','default'=>[]],
'tournaments' => ['type'=> 'array','default'=>[]], 'tournaments' => ['type'=> 'array','default'=>[]],
'photo' => ['type'=>'text','default'=>'https://pictshare.net/pj7vzx.jpg'],
'active' => ['type'=>'int','default'=>0] 'active' => ['type'=>'int','default'=>0]
); );
protected $hidden = ['password','token']; protected $hidden = ['password','token'];
@ -46,7 +47,7 @@ class User extends Model {
function getAll($filtered = true) function getAll($filtered = true)
{ {
$keys = $this->redis->keys($this->dbTable.':*'); $keys = $GLOBALS['redis']->keys($this->dbTable.':*');
$users = []; $users = [];
foreach($keys as $key) foreach($keys as $key)
{ {

View File

@ -15,6 +15,13 @@ class Demo extends Page{
//$this->addSubmenuItem('Hund hinzufügen','/dogs/add','fas fa-plus-circle'); //$this->addSubmenuItem('Hund hinzufügen','/dogs/add','fas fa-plus-circle');
} }
function test()
{
$u = new User();
$u->load('Chris','firstname');
var_dump($u->data);
}
function index() function index()
{ {
$this->set('successTitle','Erfolgreich'); $this->set('successTitle','Erfolgreich');

View File

@ -16,6 +16,53 @@
</div> </div>
<h1>Admin stuff</h1> <h1>Admin stuff</h1>
<div class="container text-center">
<div class="row">
<?php foreach ($userdata as $user) : ?>
<div class="m-3 col">
<div class="card">
<img src="<?= $user['photo']?:'https://pictshare.net/pj7vzx.jpg' ?>" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title"><?= escape($user['firstname'].' '.$user['lastname']) ?></h5>
<p class="card-text"><?= escape($user['email']) ?></p>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">
<h6>uuid</h6>
<span class="badge rounded-pill text-bg-secondary"><?= escape($user['uuid']); ?></span>
</li>
<li class="list-group-item">
<h6>Registered</h6>
<?= escape($user['registered']); ?>
</li>
<li class="list-group-item">
<h6>last_login</h6>
<?= escape($user['last_login']); ?>
</li>
<li class="list-group-item">
<h6>Active</h6>
<?= $user['active']?'yes':'no'; ?>
</li>
<li class="list-group-item">
<h6>Dogs</h6>
<?php foreach($user['dogs'] as $dog): ?>
<a href="/dogs/<?= $dog ?>" class="btn btn-secondary"><?= escape((new Dog())->getField('name',$dog)); ?></a>
<?php endforeach; ?>
</li>
</ul>
<div class="card-body">
<button class="btn btn-primary" name="email" value="<?= $user['email'] ?>" hx-post="/admin/loginas/">Login</button>
<button class="btn btn-primary"name="email" value="<?= $user['email'] ?>" hx-post="/admin/edituser/" hx-target="#main">Edit</button>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<!--
<figure class="table-responsive"> <figure class="table-responsive">
<table class="table" role="grid"> <table class="table" role="grid">
<thead> <thead>
@ -58,4 +105,4 @@
<?php endforeach; ?> <?php endforeach; ?>
</tbody> </tbody>
</table> </table>
</figure> </figure> -->