Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Forms;
- use Nette\Application\UI\Form,
- App\Model\Utils\StringUtils,
- App\Model\Dao\AppUser,
- App\Model\UserManager;
- /**
- * Factory for registration form
- * @author Michal Rost
- * @since 6.4.2015
- */
- class RegisterFormFactory extends AbstractFormFactory {
- private $userObject;
- private $userManager;
- private $db;
- const MIN_NAME = 3;
- const MAX_NAME = 25;
- /**
- * Creates register form factory
- * @param UserManager $um user manager
- * @param \DibiConnection $db database
- */
- public function __construct(UserManager $um, \DibiConnection $db) {
- $this->userManager = $um;
- $this->db = $db;
- }
- /**
- * Creates register form
- * @param array defaultData
- * @param AppUser $user (object for user editation)
- * @return Form
- */
- public function create($defaultData = array(), AppUser $user = null) {
- // Strings
- $minName = 'Minumum length of name is ' . self::MIN_NAME . ' characters';
- $maxName = 'Maximum length of name is ' . self::MAX_NAME . ' characters';
- // Form (empty is valid email field, email is fake for robots)
- $form = new Form;
- if ($user == null) {
- $form->addGroup("Basic info");
- $form->addText('name', 'Name: ')->setRequired('Enter your name');
- $form->addText('empty', 'E-mail:')->setType('email');
- $form->addText('email');
- } else {
- $form->addHidden('userid', $user->getId());
- }
- $form->addGroup("Password");
- $form->addPassword('passwd1', 'Password:');
- $form->addPassword('passwd2', 'Password again:');
- $form->addGroup("Tell us more");
- $form->addText('homepage', 'Your website:');
- $form->addText('paypal', 'PayPal e-mail:');
- $form->addTextArea('descr', 'About yourself:');
- // Set user's data for editation
- if ($user != null) {
- $form['homepage']->setDefaultValue($user->getHomepage());
- $form['paypal']->setDefaultValue($user->getPaypal());
- $form['descr']->setDefaultValue($user->getDescription());
- } else {
- $form['name']->addRule(Form::MIN_LENGTH, $minName, self::MIN_NAME);
- $form['name']->addRule(Form::MAX_LENGTH, $maxName, self::MAX_NAME);
- $form['passwd1']->setRequired('Enter password');
- $form['passwd2']->setRequired('Repeat password');
- $form['email']->setAttribute('style', 'display:none')->addRule(~$form::FILLED);
- $form['empty']->setRequired('E-mail is required');
- $form['empty']->addRule(Form::EMAIL, 'Invalid e-mail');
- $form['passwd2']->addConditionOn($form["passwd1"], Form::FILLED)
- ->addRule(Form::EQUAL, "Passwords must match", $form["passwd1"]);
- }
- // Set default data
- if ($user == null && count($defaultData) > 0) {
- $form['name']->setDefaultValue($defaultData['name']);
- $form['empty']->setDefaultValue($defaultData['email']);
- $form->addHidden('fbid', $defaultData['fbid']);
- $form->addHidden('gpid', $defaultData['gpid']);
- }
- // Triggers
- $form->onSuccess[] = array($this, 'formSucceeded');
- $form->onValidate[] = array($this, 'formValidation');
- // Create submit button
- $form->addSubmit('regButton', $user == null ? 'Register' : 'Save');
- return $this->updateRenderer($form);
- }
- /**
- * Checks registration form validity
- * @param Form $form
- */
- public function formValidation(Form $form) {
- // Helper variables
- $values = $form->getValues();
- $email = isset($values['empty']) ? $values['empty'] : null;
- $passwd1 = $values['passwd1'];
- $passwd2 = $values['passwd2'];
- $homepage = $values['homepage'];
- $editation = isset($values['userid']);
- // Check email is set
- if (!$editation && strlen(StringUtils::simplify($email)) == 0) {
- $form['empty']->addError('Please fill your e-mail');
- }
- // Check if email has valid format
- if (!$editation && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $form['empty']->addError('Invalid e-mail format.');
- }
- // Check email is unique
- $user = (new AppUser())->setDb($this->db);
- if (!$editation && $user->loadByEmail($email)->isPopulated()) {
- $form['empty']->addError('Given email is already registered.');
- }
- // Check password is set
- if (!$editation && strlen(StringUtils::simplify($passwd1)) == 0) {
- $form['passwd1']->addError('Fill your password');
- }
- // Check passwords match
- if ($passwd1 !== $passwd2) {
- $form['passwd2']->addError('Both passwords must match.');
- }
- // Check homepage URL validity, if set
- if ($homepage && $homepage !== "") {
- if (!filter_var($homepage, FILTER_VALIDATE_URL)) {
- $form['homepage']->addError('Must be a valid URL (including http://).');
- }
- }
- }
- /**
- * Registers user
- * @param Form $form
- * @param type $values
- */
- public function formSucceeded($form, $values) {
- $this->db->begin();
- try {
- // Decide about creation and editation
- $editation = isset($values['userid']);
- $home = $values->homepage;
- $paypal = $values->paypal;
- $desc = $values->descr;
- // Create/change password
- if (isset($values['passwd1']) && strlen($values->passwd1) > 0) {
- $salt = StringUtils::randomString(6);
- $passwd = $this->userManager->encryptPasswd($values->passwd1, $salt);
- } else {
- $salt = null;
- $passwd = null;
- }
- // Check email availability
- $user = (new AppUser())->setDb($this->db);
- if (!$editation && $user->loadByEmail($values->empty)->isPopulated()) {
- $email = $values->empty;
- throw new \Exception("E-mail '$email' is already registered!");
- }
- // Update/Create user
- if ($editation) {
- $id = $values->userid;
- $user->load($id)->update($desc, $home, $paypal, $passwd, $salt);
- } else {
- $name = $values->name;
- $mail = $values->empty;
- $fbid = isset($values->fbid) ? $values->fbid : null;
- $gpid = isset($values->gpid) ? $values->gpid : null;
- $user->create($name, $mail, $passwd, $salt, $desc, $home, $fbid, $gpid,
- $paypal);
- }
- } catch (\Exception $e) {
- $this->db->rollback();
- $form->addError($e->getMessage());
- }
- $this->db->commit();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement