2019-06-18 16:44:44 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Joomla\CMS\MVC\Controller\BaseController;
|
|
|
|
use Joomla\CMS\Response\JsonResponse;
|
|
|
|
use Joomla\CMS\Factory;
|
|
|
|
|
|
|
|
// No direct access.
|
|
|
|
defined('_JEXEC') or die;
|
|
|
|
|
|
|
|
abstract class UserException extends Exception {}
|
|
|
|
class InvalidUserDataException extends UserException {}
|
|
|
|
|
2019-06-18 17:09:52 +02:00
|
|
|
class ClubsHelperControllerUser
|
|
|
|
{
|
2019-06-19 16:14:28 +02:00
|
|
|
/**
|
|
|
|
* @param string $fcnName
|
|
|
|
* @param CommonClubsModelUser $user
|
|
|
|
* @param JInput $post
|
|
|
|
* @throws Exception
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-06-18 17:09:52 +02:00
|
|
|
public function parse($fcnName, $user, $post)
|
|
|
|
{
|
|
|
|
switch($fcnName)
|
|
|
|
{
|
|
|
|
case 'name':
|
2019-06-19 16:14:28 +02:00
|
|
|
$this->editName($user, $post);
|
|
|
|
break;
|
|
|
|
case 'username':
|
|
|
|
$this->editUsername($user, $post);
|
|
|
|
break;
|
|
|
|
case 'address':
|
|
|
|
$this->editAddress($user, $post);
|
|
|
|
break;
|
|
|
|
case 'city':
|
|
|
|
$this->editCity($user, $post);
|
|
|
|
break;
|
|
|
|
case 'mail':
|
|
|
|
$this->editMail($user, $post);
|
|
|
|
break;
|
|
|
|
case 'phone':
|
|
|
|
$this->editPhone($user, $post);
|
|
|
|
break;
|
|
|
|
case 'mobile':
|
|
|
|
$this->editMobile($user, $post);
|
|
|
|
break;
|
2019-06-18 17:09:52 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Exception();
|
|
|
|
}
|
2019-06-19 16:14:28 +02:00
|
|
|
|
|
|
|
return "index.php?option=com_clubs&view=part&type=user.$fcnName";
|
2019-06-18 17:09:52 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 16:14:28 +02:00
|
|
|
private function checkLen($value, $len, $msg)
|
2019-06-18 17:09:52 +02:00
|
|
|
{
|
2019-06-19 16:14:28 +02:00
|
|
|
if(strlen($value) < 5)
|
|
|
|
throw new InvalidUserDataException($msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function editName($user, $post)
|
|
|
|
{
|
|
|
|
$name = trim($post->getString('value'));
|
|
|
|
$this->checkLen($name, 5, "Der Name muss mindestens 5 Zeichen lang sein.");
|
2019-06-18 17:09:52 +02:00
|
|
|
|
|
|
|
$user->setName($name);
|
|
|
|
$user->save();
|
2019-06-19 16:14:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function editUsername($user, $post)
|
|
|
|
{
|
|
|
|
$username = $post->getString('value');
|
|
|
|
$validator = new ClubsHelperValidator();
|
|
|
|
$res = $validator->validateUsername($user, $username);
|
|
|
|
|
|
|
|
switch($res)
|
|
|
|
{
|
|
|
|
case ClubsHelperValidator::USERNAME_VALID:
|
|
|
|
$user->setUsername($username);
|
|
|
|
$user->save();
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ClubsHelperValidator::USERNAME_NOT_FREE:
|
|
|
|
throw new InvalidUserDataException('Der Benutzername ist bereits vergeben.');
|
|
|
|
|
|
|
|
case ClubsHelperValidator::USERNAME_NOT_OK:
|
|
|
|
throw new InvalidUserDataException('Der Benutzername ist nicht korrekt formaitert. Er muss mit einem Buchstaben beginnen.');
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Exception();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function editAddress($user, $post)
|
|
|
|
{
|
|
|
|
$address = trim($post->getString('value'));
|
|
|
|
$this->checkLen($address, 10, 'Die Adresse muss mindestens 10 Zeichen lang sein.');
|
|
|
|
|
|
|
|
$parts = array_filter(explode("\n", str_replace("\r", "\n", $address)));
|
|
|
|
if(count($parts) < 2)
|
|
|
|
throw new InvalidUserDataException('Die Adresse muss mindestens aus 2 Zeilen bestehen.');
|
|
|
|
|
|
|
|
$addr = join("\n", $parts);
|
|
|
|
$user->setAddress($addr);
|
|
|
|
$user->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function editCity($user, $post)
|
|
|
|
{
|
|
|
|
$city = trim($post->getString('value'));
|
|
|
|
$this->checkLen($city, 4, 'Die Stadt muss aus mindestens 4 Zeichen bestehen.');
|
|
|
|
|
|
|
|
$user->setCity($city);
|
|
|
|
$user->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function editMail($user, $post)
|
|
|
|
{
|
|
|
|
$mail = trim($post->getString('value'));
|
|
|
|
$this->checkLen($mail, 8, "Die Mail-Adresse muss aus mindestens 8 Zeichen bestehen.");
|
|
|
|
if(! preg_match('/..*@..*\...*/', $mail))
|
|
|
|
throw new InvalidUserDataException('Die Mail-Adresse hat kein gültiges Format.');
|
|
|
|
|
|
|
|
$user->setMail($mail);
|
|
|
|
$user->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function editPhone($user, $post)
|
|
|
|
{
|
|
|
|
$phone = trim($post->getString('value'));
|
|
|
|
|
|
|
|
if(strlen($phone) == 0)
|
|
|
|
{
|
|
|
|
$user->setPhone(null);
|
|
|
|
$user->save();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$validator = new ClubsHelperValidator();
|
|
|
|
$phonePlain = '';
|
|
|
|
$phoneDigits = '';
|
|
|
|
$valid = $validator->validatePhoneNumber($phone, $phonePlain, $phoneDigits);
|
|
|
|
|
|
|
|
$this->checkLen($phoneDigits, 6, 'Die Telefonnummer muss mindestens 6 Ziffern umfassen.');
|
|
|
|
if(! $valid)
|
|
|
|
throw new InvalidUserDataException('Keine gültige Telefonnummer gegeben. Erlaubte Zeichen sind 0-9, -, + und Leerzeichen. Bitte Vorwahl mit angeben.');
|
|
|
|
|
|
|
|
$user->setPhone($phonePlain);
|
|
|
|
$user->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function editMobile($user, $post)
|
|
|
|
{
|
|
|
|
$mobile = trim($post->getString('value'));
|
|
|
|
|
|
|
|
if(strlen($mobile) == 0)
|
|
|
|
{
|
|
|
|
$user->setMobile(null);
|
|
|
|
$user->save();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$validator = new ClubsHelperValidator();
|
|
|
|
$mobilePlain = '';
|
|
|
|
$mobileDigits = '';
|
|
|
|
$valid = $validator->validatePhoneNumber($mobile, $mobilePlain, $mobileDigits);
|
|
|
|
|
|
|
|
$this->checkLen($mobileDigits, 6, 'Die Handynummer muss mindestens 6 Ziffern umfassen.');
|
|
|
|
if(! $valid)
|
|
|
|
throw new InvalidUserDataException('Keine gültige Handynummer gegeben. Erlaubte Zeichen sind 0-9, -, + und Leerzeichen. Bitte Vorwahl mit angeben.');
|
|
|
|
|
|
|
|
$user->setMobile($mobilePlain);
|
|
|
|
$user->save();
|
2019-06-18 17:09:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 16:44:44 +02:00
|
|
|
class ClubsControllerParts extends BaseController
|
|
|
|
{
|
|
|
|
|
|
|
|
public function edit()
|
|
|
|
{
|
|
|
|
$auth = new ClubsHelperAuth();
|
|
|
|
$user = $auth->getCurrentUser();
|
|
|
|
$app = Factory::getApplication();
|
|
|
|
$post = $app->input->post;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$ret = $this->callMethod($user, $post);
|
|
|
|
echo new JsonResponse($ret);
|
|
|
|
}
|
|
|
|
catch(UserException $e)
|
|
|
|
{
|
|
|
|
echo new JsonResponse($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param CommonClubsModelUser $user
|
|
|
|
* @param JInput $post
|
|
|
|
*/
|
|
|
|
private function callMethod($user, $post)
|
|
|
|
{
|
2019-06-18 17:09:52 +02:00
|
|
|
$partname = $post->getCmd('partname');
|
|
|
|
|
|
|
|
if(! preg_match('/.*\..*/', $partname))
|
|
|
|
throw new Exception();
|
|
|
|
|
|
|
|
list($objname, $fcnName) = explode('.', $partname, 2);
|
|
|
|
|
|
|
|
switch($objname)
|
2019-06-18 16:44:44 +02:00
|
|
|
{
|
2019-06-18 17:09:52 +02:00
|
|
|
case 'user':
|
|
|
|
$obj = new ClubsHelperControllerUser();
|
|
|
|
return $obj->parse($fcnName, $user, $post);
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Exception();
|
2019-06-18 16:44:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|