fcnNames = array(); $this->registerAll(); } abstract protected function registerAll(); protected function registerFcn($fcnName, $methodName) { $this->fcnNames[$fcnName] = $methodName; } public function handle($fcnName, $user, $post) { if(empty($this->fcnNames[$fcnName])) throw new Exception(); $method = $this->fcnNames[$fcnName]; $this->$method($user, $post); } protected function checkLen($value, $len, $msg) { if(strlen($value) < $len) throw new InvalidUserDataException($msg); } } class ClubsHelperControllerUser extends AbstractClubsHelperController { /** * @param string $fcnName * @param CommonClubsModelUser $user * @param JInput $post * @throws Exception * @return string */ public function parse($fcnName, $user, $post) { switch($fcnName) { case 'name': $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; default: throw new Exception(); } return "index.php?option=com_clubs&view=part&type=user.$fcnName"; } protected function registerAll() { $this->registerFcn('name', 'editName'); $this->registerFcn('username', 'editUsername'); $this->registerFcn('address', 'editAddress'); $this->registerFcn('city', 'editCity'); $this->registerFcn('mail', 'editMail'); $this->registerFcn('phone', 'editPhone'); $this->registerFcn('mobile', 'editMobile'); } protected function editName($user, $post) { $name = trim($post->getString('value')); $this->checkLen($name, 5, "Der Name muss mindestens 5 Zeichen lang sein."); $user->setName($name); $user->save(); } protected function editUsername($user, $post) { $username = $post->getString('value'); $validator = new ClubsHelperValidator(); $res = $validator->validateUsername($user, $username); $this->checkLen($username, 5, 'Der Benutzername muss mindestens 5 Zeichen lang sein.'); 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(); } } protected function editAddress($user, $post) { $address = trim($post->getString('value')); $this->checkLen($address, 10, 'Die Adresse muss mindestens 10 Zeichen lang sein.'); $validator = new ClubsHelperValidator(); $addr = ''; $valid = $validator->validateAddress($address, $addr); if(! $valid) throw new InvalidUserDataException('Die Adresse muss mindestens aus 2 Zeilen bestehen.'); $user->setAddress($addr); $user->save(); } protected 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(); } protected function editMail($user, $post) { $mail = trim($post->getString('value')); $this->checkLen($mail, 8, "Die Mail-Adresse muss aus mindestens 8 Zeichen bestehen."); $validator = new ClubsHelperValidator(); if(! $validator->validateMail($mail)) throw new InvalidUserDataException('Die Mail-Adresse hat kein gültiges Format.'); $user->setMail($mail); $user->save(); } protected 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(); } protected 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(); } } class ClubsHelperControllerClub extends AbstractClubsHelperController { private $id; /** * @var CommonClubsModelClub */ private $club; public function __construct($id) { parent::__construct(); $this->id = $id; $factory = new CommonClubsModelFactoryClub(); $this->club = $factory->loadById($id, false); } protected function registerAll() { $this->registerFcn('name', 'editName'); $this->registerFcn('address', 'editAddress'); $this->registerFcn('city', 'editCity'); $this->registerFcn('homepage', 'editHomepage'); $this->registerFcn('mail', 'editMail'); $this->registerFcn('iban', 'editIban'); $this->registerFcn('bic', 'editBic'); $this->registerFcn('offers', 'editOffers'); $this->registerFcn('place', 'editPlace'); } protected function editName($user, $post) { $name = trim($post->getString('value')); $this->checkLen($name, 6, 'Der Name des Vereins muss mindestens 6 Zeichen lang sein.'); // TODO ACL needed $this->club->setName($name); $this->club->save(); } protected function editAddress($user, $post) { $address = trim($post->getString('value')); $this->checkLen($address, 10, 'Die Adresse muss mindestens 10 Zeichen lang sein.'); $validator = new ClubsHelperValidator(); $addr = ''; $valid = $validator->validateAddress($address, $addr); if(! $valid) throw new InvalidUserDataException('Die Adresse muss mindestens aus 2 Zeilen bestehen.'); // TODO ACL needed $this->club->setAddress($addr); $this->club->save(); } protected function editCity($user, $post) { $city = trim($post->getString('value')); $this->checkLen($city, 4, 'Die Stadt muss aus mindestens 4 Zeichen bestehen.'); // TODO ACL needed $this->club->setCity($city); $this->club->save(); } protected function editHomepage($user, $post) { $homepage = trim($post->getString('value')); $validator = new ClubsHelperValidator(); if(!$validator->validateHomepage($homepage)) throw new InvalidUserDataException('Die URL muss mit "http(s)://domain.tld" beginnen.'); // TODO ACL needed $this->club->setHomepage($homepage); $this->club->save(); } protected function editMail($user, $post) { $mail = trim($post->getString('value')); $this->checkLen($mail, 8, "Die Mail-Adresse muss aus mindestens 8 Zeichen bestehen."); $validator = new ClubsHelperValidator(); if(! $validator->validateMail($mail)) throw new InvalidUserDataException('Die Mail-Adresse hat kein gültiges Format.'); $this->club->setMail($mail); $this->club->save(); } protected function editIban($user, $post) { $iban = trim($post->getString('value')); $this->checkLen($iban, 10, 'Die IBAN muss aus mindestens 10 Zeichen bestehen.'); $validator = new ClubsHelperValidator(); $formattedIban = null; if(! $validator->validateIban($iban, $formattedIban)) throw new InvalidUserDataException('Die IBAN ist nicht korrekt.'); // TODO ACL needed $this->club->setIban($iban); $this->club->save(); } protected function editBic($user, $post) { $bic = trim($post->getString('value')); $this->checkLen($bic, 6, 'Die BIC muss aus mindestens 6 Zeichen bestehen.'); // TODO ACL needed $this->club->setBic($bic); $this->club->save(); } protected function editOffers($user, $post) { $ids = $post->getInt('offers'); // TODO ACL needed try { $this->club->setOfferIds($ids); } catch(ElementNotFoundException $ex) { throw new InvalidUserDataException('Es gab ein Problem mit der Eingabe. Bitte Seite neu laden und Daten erneut eintragen.'); } } protected function editPlace($user, $post) { $factory = new CommonClubsModelFactoryPlace(); $place = $factory->loadById($this->id); $name = trim($post->getString('name')); $address = $post->getString('address'); $area = $post->getInt('area'); $validator = new ClubsHelperValidator(); $this->checkLen($name, 5, "Der Name des Saals muss mindestens aus 5 Zeichen bestehen."); $this->checkLen($address, 10, 'Die Adresse muss mindestens 10 Zeichen lang sein.'); $addr = ''; if(!$validator->validateAddress($address, $addr)) { throw new InvalidUserDataException('Die Adresse muss mindestens aus 2 Zeilen bestehen.'); } if($area < 0) throw new InvalidUserDataException('Die Fläche darf nicht negativ sein.'); if($area == 0) $area = null; if($area > 2500) throw new InvalidUserDataException('Die Fläche erscheint zu groß zu sein. Wenden Sie sich bitte ggf an den Support.'); // TODO ACL needed $place->setName($name); $place->setAddress($addr); $place->setArea($area); $place->save(); } } 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) { $partname = $post->getCmd('partname'); if(! preg_match('/.*\..*/', $partname)) throw new Exception(); list($objname, $fcnName) = explode('.', $partname, 2); switch($objname) { case 'user': $obj = new ClubsHelperControllerUser(); $obj->handle($fcnName, $user, $post); break; case 'club': $clubid = $post->getInt('id', -1); $obj = new ClubsHelperControllerClub($clubid); $obj->handle($fcnName, $user, $post); // TODO User!?!?! break; default: throw new Exception(); } $id = (int) $post->getInt('id', -1); $addId = ''; if($id != -1) $addId = "&id=$id"; return "index.php?option=com_clubs&view=part&type=$partname$addId"; } }