Started work on backend with self-written models

This commit is contained in:
Christian Wolf 2019-04-15 15:40:10 +02:00
parent 5c93c0b74c
commit 9c407e750f
7 changed files with 399 additions and 1 deletions

View File

@ -1,5 +1,17 @@
<?php
// No direct access.
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Controller\BaseController;
defined('_JEXEC') or die;
JLoader::discover('Clubs', JPATH_ROOT . '/administrator/components/com_clubs/mymodels');
$controller = BaseController::getInstance("Clubs");
$input = Factory::getApplication()->input;
$task = $input->getCmd("task", "display");
$controller->execute($task);
$controller->redirect();

View File

@ -8,4 +8,5 @@ defined('_JEXEC') or die;
class ClubsController extends BaseController
{
protected $default_view = 'users';
}

329
src/admin/mymodels/user.php Normal file
View File

@ -0,0 +1,329 @@
<?php
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Factory;
// No direct access.
defined('_JEXEC') or die;
class ClubsUser
{
protected $dbo;
protected $id;
protected $user;
protected $hash;
protected $name;
protected $address;
protected $city;
protected $mail;
protected $phone;
protected $mobile;
/**
* @return string
*/
public function getMail()
{
return $this->mail;
}
/**
* @param string $mail
*/
public function setMail(string $mail)
{
$this->mail = $mail;
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getUser()
{
return $this->user;
}
/**
* @return string
*/
public function getHash()
{
return $this->hash;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return string
*/
public function getAddress()
{
return $this->address;
}
/**
* @return string
*/
public function getCity()
{
return $this->city;
}
/**
* @return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* @return string
*/
public function getMobile()
{
return $this->mobile;
}
/**
* @param string $user
*/
public function setUser(string $user)
{
$this->user = $user;
}
/**
* @param string $hash
*/
public function setPassword(string $password)
{
$this->hash = password_hash($password, PASSWORD_DEFAULT);
}
public function isPasswordValid(string $password)
{
$valid = password_verify($password, $this->hash);
if($valid)
{
$this->checkHash($password);
}
return $valid;
}
/**
* @param string $name
*/
public function setName(string $name)
{
$this->name = $name;
}
/**
* @param string $address
*/
public function setAddress(string $address)
{
$this->address = $address;
}
/**
* @param string $city
*/
public function setCity(string $city)
{
$this->city = $city;
}
/**
* @param string $phone
*/
public function setPhone(string $phone)
{
$this->phone = $phone;
}
/**
* @param string $mobile
*/
public function setMobile(string $mobile)
{
$this->mobile = $mobile;
}
protected function __construct()
{
$this->dbo = Factory::getDbo();
}
public static function loadUsers()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->select('*')
->from('#__club_users');
$dbo->setQuery($q);
$dbo->execute();
$list = $dbo->loadAssocList('id');
$ret = array();
foreach($list as $u)
{
$uo = new ClubsUser($dbo);
$uo->loadData($u);
$ret[] = $uo;
}
return $ret;
}
protected function loadData(array $data)
{
$this->id = $data['id'];
$this->user = $data['user'];
$this->hash = $data['password'];
$this->name = $data['name'];
$this->address = $data['address'];
$this->city = $data['city'];
$this->mail = $data['mail'];
$this->phone = isset($data['phone']) ? $data['phone'] : null;
$this->mobile = isset($data['mobile']) ? $data['mobile'] : null;
}
public static function loadUser(int $id)
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->select('*')->from('#__club_users')->where('id=' . (int) $id);
$dbo->setQuery($q);
$dbo->execute();
$row = $dbo->loadAssoc();
if($row == null)
{
throw new Exception("No user found.");
// TODO
}
$user = new ClubsUser();
$user->loadData($row);
return $user;
}
public static function createUser()
{
$user = new ClubsUser();
$user->id = 'new';
return $user;
}
public function save()
{
if($this->id === 'new')
$this->insertUser();
else
$this->updateUser();
}
private function insertUser()
{
$q = $this->dbo->getQuery(true);
$vuser = $q->q($this->user);
$vpassword = $q->q($this->hash);
$vname = $q->q($this->name);
$vaddress = $q->q($this->address);
$vcity = $q->q($this->city);
$vmail = $q->q($this->mail);
$vphone = empty($this->phone) ? 'NULL' : $q->q($this->phone);
$vmobile = empty($this->mobile) ? 'NULL' : $q->q($this->mobile);
$q->insert('#__club_users')
->columns(array('user', 'password', 'name', 'address', 'city', 'mail', 'phone', 'mobile'))
->values("$vuser, $vpassword, $vname, $vaddress, $vcity, $vmail, $vphone, $vmobile")
;
$this->dbo->transactionStart();
$this->dbo->setQuery($q);
$this->dbo->execute();
$this->id = $this->dbo->insertid();
$this->dbo->transactionCommit();
}
private function updateUser()
{
$q = $this->dbo->getQuery(true);
$vuser = $q->q($this->user);
$vpassword = $q->q($this->hash);
$vname = $q->q($this->name);
$vaddress = $q->q($this->address);
$vcity = $q->q($this->city);
$vmail = $q->q($this->mail);
$vphone = empty($this->phone) ? 'NULL' : $q->q($this->phone);
$vmobile = empty($this->mobile) ? 'NULL' : $q->q($this->mobile);
$q->update('#__club_users')
->set(array(
"user=$vuser",
"password=$vpassword",
"name=$vname",
"address = $vaddress",
"city=$vcity",
"mail=$vmail",
"phone=$vphone",
"mobile=$vmobile"
))
->where("id=". (int) $this->id)
;
$this->dbo->setQuery($q);
$this->dbo->execute();
}
private function checkHash(string $password)
{
if($this->id === 'new')
return;
if(password_needs_rehash($this->hash, PASSWORD_DEFAULT))
{
$this->hash = password_hash($password, PASSWORD_DEFAULT);
$q = $this->dbo->getQuery(true);
$q->update('#__club_users')->set('password=' . $q->q($this->hash))->where('id=' . (int) $this->id);
$this->dbo->setQuery($q);
$this->dbo->execute();
}
}
public function delete()
{
if($this->id === 'new')
return;
$q = $this->dbo->getQuery(true);
$q->delete('#__club_users')
->where('id=' . (int) $this->id);
$this->dbo->setQuery($q);
$this->dbo->execute();
}
}

View File

@ -0,0 +1,32 @@
<?php
use Joomla\CMS\Router\Route;
// No direct access.
defined('_JEXEC') or die;
?>
<table width='100%' class='table table-stiped, table-hover'>
<thead>
<tr>
<th></th>
<th width='30%'>Benutzername</th>
<th width='30%'>Ort</th>
<th width='30%'>E-Mail</th>
<th width='5%'>id</th>
</tr>
</thead>
<?php foreach($this->users as $user): ?>
<?php $link = Route::_('index.php?option=com_clubs&view=user&id=' . $user->getId()); ?>
<tr>
<td></td>
<td><a href='<?php echo $link; ?>'><?php echo htmlentities($user->getName()); ?></a></td>
<td><?php echo htmlentities($user->getCity()); ?></td>
<td><?php echo htmlentities($user->getMail()); ?></td>
<td><?php echo htmlentities($user->getId()); ?></td>
</tr>
<?php endforeach; ?>
</table>

View File

@ -0,0 +1,22 @@
<?php
use Joomla\CMS\MVC\View\HtmlView;
use Joomla\CMS\Toolbar\ToolbarHelper;
// No direct access.
defined('_JEXEC') or die;
// JLoader::register("ClubsModelUser", JPATH_ROOT . "/administrator/components/com_clubs/models/user.php");
class ClubsViewUsers extends HtmlView
{
function display($tpl = null)
{
$this->users = ClubsUser::loadUsers();
ToolbarHelper::title('Club-Management');
parent::display($tpl);
}
}

View File

@ -6,6 +6,8 @@ use Joomla\CMS\Factory;
// No direct access.
defined('_JEXEC') or die;
JLoader::discover('Clubs', JPATH_ROOT . '/administrator/components/com_clubs/mymodels');
$controller = BaseController::getInstance("Clubs");
$input = Factory::getApplication()->input;

View File

@ -10,7 +10,7 @@ defined('_JEXEC') or die;
class ClubsViewClub extends HtmlView
{
public function display(string $tpl = null)
public function display($tpl = null)
{
// FIXME Insert code from DB
$this->clubid = 43;