First approach to port user classes to abstract MVC classes as well
This commit is contained in:
@@ -1,207 +1,106 @@
|
||||
<?php
|
||||
|
||||
use Joomla\CMS\MVC\Controller\BaseController;
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Router\Route;
|
||||
|
||||
// No direct access.
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
class ClubsControllerUser extends BaseController
|
||||
class ClubsControllerUser extends AbstractClubsController
|
||||
{
|
||||
|
||||
function new()
|
||||
{
|
||||
$app = Factory::getApplication();
|
||||
$input = $app->input;
|
||||
$u = ClubsUser::createUser();
|
||||
|
||||
// Fetch the posted data
|
||||
$user = $input->post->getCmd('user');
|
||||
$pwd = $input->post->getCmd('pwd');
|
||||
$pwdConfirm = $input->post->getCmd('pwd-confirm');
|
||||
$name = $input->post->getString('name');
|
||||
$address = $input->post->getString('address');
|
||||
$city = $input->post->getString('city');
|
||||
$mail = $input->post->getString('mail');
|
||||
$phone = $input->post->getString('phone');
|
||||
$mobile = $input->post->getString('mobile');
|
||||
|
||||
// Check the input data
|
||||
$error = false;
|
||||
|
||||
if(empty($user))
|
||||
{
|
||||
$app->enqueueMessage("Es muss ein Benutzername angegeben werden.", 'error');
|
||||
$error = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(! $this->checkUserName(trim($user)))
|
||||
{
|
||||
$app->enqueueMessage('Username ' . $user . ' ist nicht gültig.', 'error');
|
||||
$error = true;
|
||||
}
|
||||
else
|
||||
$u->setUser($user);
|
||||
}
|
||||
|
||||
$pwderr = false;
|
||||
if(isset($pwd))
|
||||
{
|
||||
if(trim($pwd) != trim($pwdConfirm))
|
||||
{
|
||||
$app->enqueueMessage('Die Passwörter stimmen nicht überein.', 'error');
|
||||
$error = true;
|
||||
$pwderr = true;
|
||||
}
|
||||
|
||||
if(! $u->checkPassword(trim($pwd)))
|
||||
{
|
||||
$app->enqueueMessage('Das Passwort ist nicht zulässig.', 'error');
|
||||
$error = true;
|
||||
$pwderr = true;
|
||||
}
|
||||
|
||||
if(! $pwderr)
|
||||
$u->setPassword(trim($pwd));
|
||||
}
|
||||
|
||||
// Check existence of the other fields
|
||||
$fields = array('name'=>'Bürgerlicher Name', 'address'=>'Adresse', 'city'=>"Stadt", 'mail'=>'E-Mail');
|
||||
foreach ($fields as $f => $fname)
|
||||
{
|
||||
$fvalue = $$f;
|
||||
if(! isset($fvalue) || empty(trim($fvalue)))
|
||||
{
|
||||
$app->enqueueMessage("Das Feld $fname ist obligatorisch.", 'error');
|
||||
$error = true;
|
||||
}
|
||||
}
|
||||
|
||||
$u->setName($name);
|
||||
$u->setAddress($address);
|
||||
$u->setCity($city);
|
||||
$u->setMail($mail);
|
||||
$u->setPhone($phone);
|
||||
$u->setMobile($mobile);
|
||||
|
||||
if($error)
|
||||
{
|
||||
$data = array();
|
||||
foreach(array('user', 'name', 'address', 'city', 'mail', 'phone', 'mobile') as $i)
|
||||
$data[$i] = $$i;
|
||||
|
||||
$urldata = urlencode(json_encode($data));
|
||||
$this->setRedirect(Route::_('index.php?option=com_clubs&view=user&id=new&data=' . $urldata, false));
|
||||
return;
|
||||
}
|
||||
|
||||
// Do the actual work
|
||||
$u->save();
|
||||
$this->setRedirect(Route::_('index.php?option=com_clubs&view=users', false));
|
||||
}
|
||||
|
||||
function change()
|
||||
{
|
||||
$app = Factory::getApplication();
|
||||
$input = $app->input;
|
||||
$id = (int) $input->post->getInt('id');
|
||||
$u = ClubsUser::loadUser((int) $id);
|
||||
|
||||
// Fetch the posted data
|
||||
$user = $input->post->getCmd('user');
|
||||
$pwd = $input->post->getCmd('pwd');
|
||||
$pwdConfirm = $input->post->getCmd('pwd-confirm');
|
||||
$name = $input->post->getString('name');
|
||||
$address = $input->post->getString('address');
|
||||
$city = $input->post->getString('city');
|
||||
$mail = $input->post->getString('mail');
|
||||
$phone = $input->post->getString('phone');
|
||||
$mobile = $input->post->getString('mobile');
|
||||
|
||||
// Check the input data
|
||||
$error = false;
|
||||
|
||||
if(! empty($user))
|
||||
{
|
||||
if(! $this->checkUserName(trim($user), $id))
|
||||
{
|
||||
$app->enqueueMessage('Username ' . $user . ' ist nicht gültig.', 'error');
|
||||
$error = true;
|
||||
}
|
||||
|
||||
$u->setUser($user);
|
||||
}
|
||||
|
||||
if(isset($pwd))
|
||||
{
|
||||
if(trim($pwd) != trim($pwdConfirm))
|
||||
{
|
||||
$app->enqueueMessage('Die Passwörter stimmen nicht überein.', 'error');
|
||||
$error = true;
|
||||
}
|
||||
|
||||
if(! empty(trim($pwd)))
|
||||
{
|
||||
if(! $u->checkPassword(trim($pwd)))
|
||||
{
|
||||
$app->enqueueMessage('Das Passwort ist nicht zulässig.', 'error');
|
||||
$error = true;
|
||||
}
|
||||
|
||||
$u->setPassword(trim($pwd));
|
||||
}
|
||||
}
|
||||
|
||||
// Check existence of the other fields
|
||||
$fields = array('name'=>'Bürgerlicher Name', 'address'=>'Adresse', 'city'=>"Stadt", 'mail'=>'E-Mail');
|
||||
foreach ($fields as $f => $fname)
|
||||
{
|
||||
$fvalue = $$f;
|
||||
if(! isset($fvalue) || empty(trim($fvalue)))
|
||||
{
|
||||
$app->enqueueMessage("Das Feld $fname ist obligatorisch.", 'error');
|
||||
$error = true;
|
||||
}
|
||||
}
|
||||
|
||||
$u->setName($name);
|
||||
$u->setAddress($address);
|
||||
$u->setCity($city);
|
||||
$u->setMail($mail);
|
||||
$u->setPhone($phone);
|
||||
$u->setMobile($mobile);
|
||||
|
||||
if($error)
|
||||
{
|
||||
$data = array();
|
||||
foreach(array('user', 'name', 'address', 'city', 'mail', 'phone', 'mobile') as $i)
|
||||
$data[$i] = $$i;
|
||||
|
||||
$urldata = urlencode(json_encode($data));
|
||||
$this->setRedirect(Route::_('index.php?option=com_clubs&view=user&id=' . $id . '&data=' . $urldata, false));
|
||||
return;
|
||||
}
|
||||
|
||||
// Do the actual work
|
||||
$u->save();
|
||||
$this->setRedirect(Route::_('index.php?option=com_clubs&view=users', false));
|
||||
}
|
||||
|
||||
function delete()
|
||||
{
|
||||
$app = Factory::getApplication();
|
||||
$id = $app->input->get->getInt('id');
|
||||
$app->enqueueMessage("Removal of user with id $id.");
|
||||
$user = ClubsUser::loadUser($id);
|
||||
$user->delete();
|
||||
$this->setRedirect(Route::_('index.php?option=com_clubs&view=users', false));
|
||||
}
|
||||
|
||||
private function checkUserName($username, $id = -1)
|
||||
{
|
||||
return ClubsUser::isUserNameFree($username, $id);
|
||||
}
|
||||
|
||||
protected function getNameOfElement()
|
||||
{
|
||||
return 'user';
|
||||
}
|
||||
|
||||
protected function getDataMapping()
|
||||
{
|
||||
return array(
|
||||
'user'=>array('required'=>true, 'name'=>'Benutzername', 'filter'=>'cmd'),
|
||||
'pwd'=>array('required'=>false, 'name'=>'Passwort', 'filter'=>'string', 'setter'=>'setPassword'),
|
||||
'pwdConfirm'=>array('required'=>false, 'name'=>'Passwortwiederholung', 'filter'=>'string', 'setter'=>null),
|
||||
'name'=>array('required'=>true, 'name'=>'Benutzername', 'filter'=>'string'),
|
||||
'address'=>array('required'=>true, 'name'=>'Adresse', 'filter'=>'string'),
|
||||
'city'=>array('required'=>true, 'name'=>'Stadt', 'filter'=>'string'),
|
||||
'mail'=>array('required'=>true, 'name'=>'E-Mail', 'filter'=>'string'),
|
||||
'phone'=>array('required'=>false, 'name'=>'Telefonnummer', 'filter'=>'string'),
|
||||
'mobile'=>array('required'=>false, 'name'=>'Handynummer', 'filter'=>'string')
|
||||
|
||||
);
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @see AbstractClubsController::checkData()
|
||||
*/
|
||||
protected function checkDataIsValid($values, $isNew)
|
||||
{
|
||||
if(! parent::checkDataIsValid($values, $isNew))
|
||||
return false;
|
||||
|
||||
// TODO Auto-generated method stub
|
||||
if(isset($values['pwd']))
|
||||
{
|
||||
$pwd = $values['pwd'];
|
||||
$pwdConfirm = $values['pwdConfirm'];
|
||||
|
||||
if(trim($pwd) != trim($pwdConfirm))
|
||||
{
|
||||
Factory::getApplication()->enqueueMessage('Die Passwörter stimmen nicht überein.', 'error');
|
||||
return false;
|
||||
}
|
||||
|
||||
if(! ClubsUser::checkPasswordStrength(trim($pwd)))
|
||||
{
|
||||
Factory::getApplication()->enqueueMessage('Das Passwort ist zu schwach.', 'error');
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if($isNew)
|
||||
{
|
||||
Factory::getApplication()->enqueueMessage('Für einen neuen Benutzer muss ein Passwort vergeben werden.', 'error');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if(! $this->checkUserName(trim($values['user'])))
|
||||
{
|
||||
Factory::getApplication()->enqueueMessage('Username ' . $$values['user'] . ' ist nicht gültig.', 'error');
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @see AbstractClubsController::filterPrePacking()
|
||||
*/
|
||||
protected function filterPrePacking(&$values)
|
||||
{
|
||||
parent::filterPrePacking($values);
|
||||
unset($values['pwd']);
|
||||
unset($values['pwdConfirm']);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @see AbstractClubsController::applyData()
|
||||
*/
|
||||
public function applyData($obj, $values)
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
parent::applyData($obj, $values);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user