This repository has been archived on 2023-12-29. You can view files and clone it, but cannot push or open issues or pull requests.
dogstats/web/models/Tournament.model.php
Chris fea847afd2
All checks were successful
Build and push / Pulling repo on server (push) Successful in 3s
auto login when chosen so
2023-10-29 15:58:16 +01:00

122 lines
3.6 KiB
PHP

<?php
class Tournament extends Model
{
protected $dbTable = "tournaments";
protected $dbFields = array(
'registered' => ['type' => 'datetime', 'required', 'unique', 'autoValMethod' => 'getDateTime'],
'name' => ['type' => 'text', 'required'],
'date' => ['type' => 'datetime','required'],
'duration' => ['type' => 'int'], //in days
'text' => ['type' => 'text'],
'url' => ['type' => 'text'], //if there is one
'logo' => ['type' => 'text', 'default' => 'https://pictshare.net/prrnrk.jpg'],
'admins' => ['type'=> 'array', 'default'=>[]],
'members' => ['type'=> 'array', 'default'=>[]],
);
function joinUser($tournament = false)
{
if ($tournament == false)
$tournament = $this->id;
if (!$this->exists($tournament))
return false;
else {
if (!in_array($_SESSION['user']->id, $this->data['members']))
$this->data['members'][] = $_SESSION['user']->id;
if(!in_array($tournament,$_SESSION['user']->data['tournaments']))
{
$_SESSION['user']->data['tournaments'][] = $tournament;
$_SESSION['user']->save();
}
$this->save();
}
}
function removeUser($tournament = false)
{
if ($tournament == false)
$tournament = $this->id;
if (!$this->exists($tournament))
return false;
else {
if (in_array($_SESSION['user']->id, $this->data['members']))
$this->data['members'] = array_diff($this->data['members'],[$_SESSION['user']->id]);
if(in_array($tournament,$_SESSION['user']->data['tournaments']))
{
$_SESSION['user']->data['tournaments'] = array_diff($_SESSION['user']->data['tournaments'],[$tournament]);
$_SESSION['user']->save();
}
$this->save();
}
}
function isMyEvent($tournament = false)
{
if ($tournament == false)
$tournament = $this->id;
if (!$this->exists($tournament))
return false;
else {
if (in_array($tournament, $_SESSION['user']->data['tournaments']))
return true;
else return false;
}
}
function amIAdmin($tournament = false)
{
if ($tournament == false)
$tournament = $this->id;
if (!$this->exists($tournament))
return false;
else {
if (in_array($_SESSION['user']->id, $this->data['admins']))
return true;
else return false;
}
}
function getAdmins($tournament = false)
{
if ($tournament == false)
$tournament = $this->id;
if (!$this->exists($tournament))
return false;
else {
$admins = [];
foreach($this->data['admins'] as $admin)
{
$u = new User();
$u->load($admin);
$admins[] = $u->getDataFiltered();
}
return $admins;
}
}
function getMembers($tournament = false)
{
if ($tournament == false)
$tournament = $this->id;
if (!$this->exists($tournament))
return false;
else {
$members = [];
foreach($this->data['members'] as $member)
{
$u = new User();
$u->load($member);
$members[] = $u->getDataFiltered();
}
return $members;
}
}
}