From 9c407e750f096b82a61a4cabc83f96576f871cba Mon Sep 17 00:00:00 2001 From: Christian Wolf Date: Mon, 15 Apr 2019 15:40:10 +0200 Subject: [PATCH] Started work on backend with self-written models --- src/admin/clubs.php | 12 + src/admin/controller.php | 1 + src/admin/mymodels/user.php | 329 +++++++++++++++++++++++++ src/admin/views/users/tmpl/default.php | 32 +++ src/admin/views/users/view.html.php | 22 ++ src/site/clubs.php | 2 + src/site/views/club/view.html.php | 2 +- 7 files changed, 399 insertions(+), 1 deletion(-) create mode 100644 src/admin/mymodels/user.php create mode 100644 src/admin/views/users/tmpl/default.php create mode 100644 src/admin/views/users/view.html.php diff --git a/src/admin/clubs.php b/src/admin/clubs.php index aea8f99..0fc46f6 100644 --- a/src/admin/clubs.php +++ b/src/admin/clubs.php @@ -1,5 +1,17 @@ input; + +$task = $input->getCmd("task", "display"); + +$controller->execute($task); +$controller->redirect(); diff --git a/src/admin/controller.php b/src/admin/controller.php index 134a89f..1316210 100644 --- a/src/admin/controller.php +++ b/src/admin/controller.php @@ -8,4 +8,5 @@ defined('_JEXEC') or die; class ClubsController extends BaseController { + protected $default_view = 'users'; } diff --git a/src/admin/mymodels/user.php b/src/admin/mymodels/user.php new file mode 100644 index 0000000..2ad9733 --- /dev/null +++ b/src/admin/mymodels/user.php @@ -0,0 +1,329 @@ +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(); + } +} diff --git a/src/admin/views/users/tmpl/default.php b/src/admin/views/users/tmpl/default.php new file mode 100644 index 0000000..fcd060c --- /dev/null +++ b/src/admin/views/users/tmpl/default.php @@ -0,0 +1,32 @@ + + + + + + + + + + + + +users as $user): ?> +getId()); ?> + + + + + + + + +
BenutzernameOrtE-Mailid
getName()); ?>getCity()); ?>getMail()); ?>getId()); ?>
+ + diff --git a/src/admin/views/users/view.html.php b/src/admin/views/users/view.html.php new file mode 100644 index 0000000..4897d47 --- /dev/null +++ b/src/admin/views/users/view.html.php @@ -0,0 +1,22 @@ +users = ClubsUser::loadUsers(); + + ToolbarHelper::title('Club-Management'); + + parent::display($tpl); + } + +} diff --git a/src/site/clubs.php b/src/site/clubs.php index 638e005..9342ee0 100644 --- a/src/site/clubs.php +++ b/src/site/clubs.php @@ -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; diff --git a/src/site/views/club/view.html.php b/src/site/views/club/view.html.php index 0736ce3..6f0f790 100644 --- a/src/site/views/club/view.html.php +++ b/src/site/views/club/view.html.php @@ -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;