Advertisement
Guest User

Untitled

a guest
Jul 7th, 2018
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.37 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Forms;
  4.  
  5. use Nette\Application\UI\Form,
  6.     App\Model\Utils\StringUtils,
  7.     App\Model\Dao\AppUser,
  8.     App\Model\UserManager;
  9.  
  10. /**
  11.  * Factory for registration form
  12.  * @author Michal Rost
  13.  * @since 6.4.2015
  14.  */
  15. class RegisterFormFactory extends AbstractFormFactory {
  16.  
  17.   private $userObject;
  18.   private $userManager;
  19.   private $db;
  20.  
  21.   const MIN_NAME = 3;
  22.   const MAX_NAME = 25;
  23.  
  24.   /**
  25.    * Creates register form factory
  26.    * @param UserManager $um user manager
  27.    * @param \DibiConnection $db database
  28.    */
  29.   public function __construct(UserManager $um, \DibiConnection $db) {
  30.     $this->userManager = $um;
  31.     $this->db = $db;
  32.   }
  33.  
  34.   /**
  35.    * Creates register form
  36.    * @param array defaultData
  37.    * @param AppUser $user (object for user editation)
  38.    * @return Form
  39.    */
  40.   public function create($defaultData = array(), AppUser $user = null) {
  41.    
  42.     // Strings
  43.     $minName = 'Minumum length of name is ' . self::MIN_NAME . ' characters';
  44.     $maxName = 'Maximum length of name is ' . self::MAX_NAME . ' characters';
  45.    
  46.     // Form (empty is valid email field, email is fake for robots)
  47.     $form = new Form;
  48.     if ($user == null) {
  49.       $form->addGroup("Basic info");
  50.       $form->addText('name', 'Name: ')->setRequired('Enter your name');
  51.       $form->addText('empty', 'E-mail:')->setType('email');
  52.       $form->addText('email');
  53.     } else {
  54.       $form->addHidden('userid', $user->getId());
  55.     }
  56.     $form->addGroup("Password");
  57.     $form->addPassword('passwd1', 'Password:');
  58.     $form->addPassword('passwd2', 'Password again:');
  59.     $form->addGroup("Tell us more");
  60.     $form->addText('homepage', 'Your website:');
  61.     $form->addText('paypal', 'PayPal e-mail:');
  62.     $form->addTextArea('descr', 'About yourself:');
  63.    
  64.     // Set user's data for editation
  65.     if ($user != null) {
  66.       $form['homepage']->setDefaultValue($user->getHomepage());
  67.       $form['paypal']->setDefaultValue($user->getPaypal());
  68.       $form['descr']->setDefaultValue($user->getDescription());
  69.     } else {
  70.       $form['name']->addRule(Form::MIN_LENGTH, $minName, self::MIN_NAME);
  71.       $form['name']->addRule(Form::MAX_LENGTH, $maxName, self::MAX_NAME);
  72.       $form['passwd1']->setRequired('Enter password');
  73.       $form['passwd2']->setRequired('Repeat password');
  74.       $form['email']->setAttribute('style', 'display:none')->addRule(~$form::FILLED);
  75.       $form['empty']->setRequired('E-mail is required');
  76.       $form['empty']->addRule(Form::EMAIL, 'Invalid e-mail');
  77.       $form['passwd2']->addConditionOn($form["passwd1"], Form::FILLED)
  78.           ->addRule(Form::EQUAL, "Passwords must match", $form["passwd1"]);
  79.     }
  80.    
  81.     // Set default data
  82.     if ($user == null && count($defaultData) > 0) {
  83.       $form['name']->setDefaultValue($defaultData['name']);
  84.       $form['empty']->setDefaultValue($defaultData['email']);
  85.       $form->addHidden('fbid', $defaultData['fbid']);
  86.       $form->addHidden('gpid', $defaultData['gpid']);
  87.     }
  88.  
  89.     // Triggers
  90.     $form->onSuccess[] = array($this, 'formSucceeded');
  91.     $form->onValidate[] = array($this, 'formValidation');
  92.  
  93.     // Create submit button
  94.     $form->addSubmit('regButton', $user == null ? 'Register' : 'Save');
  95.     return $this->updateRenderer($form);
  96.   }
  97.  
  98.   /**
  99.    * Checks registration form validity
  100.    * @param Form $form
  101.    */
  102.   public function formValidation(Form $form) {
  103.  
  104.     // Helper variables
  105.     $values = $form->getValues();
  106.     $email = isset($values['empty']) ? $values['empty'] : null;
  107.     $passwd1 = $values['passwd1'];
  108.     $passwd2 = $values['passwd2'];
  109.     $homepage = $values['homepage'];
  110.     $editation = isset($values['userid']);
  111.  
  112.     // Check email is set
  113.     if (!$editation && strlen(StringUtils::simplify($email)) == 0) {
  114.       $form['empty']->addError('Please fill your e-mail');
  115.     }
  116.  
  117.     // Check if email has valid format
  118.     if (!$editation && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  119.       $form['empty']->addError('Invalid e-mail format.');
  120.     }
  121.  
  122.     // Check email is unique
  123.     $user = (new AppUser())->setDb($this->db);
  124.     if (!$editation && $user->loadByEmail($email)->isPopulated()) {
  125.       $form['empty']->addError('Given email is already registered.');
  126.     }
  127.  
  128.     // Check password is set
  129.     if (!$editation && strlen(StringUtils::simplify($passwd1)) == 0) {
  130.       $form['passwd1']->addError('Fill your password');
  131.     }
  132.  
  133.     // Check passwords match
  134.     if ($passwd1 !== $passwd2) {
  135.       $form['passwd2']->addError('Both passwords must match.');
  136.     }
  137.    
  138.     // Check homepage URL validity, if set
  139.     if ($homepage && $homepage !== "") {
  140.       if (!filter_var($homepage, FILTER_VALIDATE_URL)) {
  141.         $form['homepage']->addError('Must be a valid URL (including http://).');    
  142.       }
  143.     }
  144.   }
  145.  
  146.   /**
  147.    * Registers user
  148.    * @param Form $form
  149.    * @param type $values
  150.    */
  151.   public function formSucceeded($form, $values) {
  152.     $this->db->begin();    
  153.     try {
  154.      
  155.       // Decide about creation and editation
  156.       $editation = isset($values['userid']);      
  157.       $home = $values->homepage;
  158.       $paypal = $values->paypal;
  159.       $desc = $values->descr;
  160.      
  161.       // Create/change password
  162.       if (isset($values['passwd1']) && strlen($values->passwd1) > 0) {
  163.         $salt = StringUtils::randomString(6);
  164.         $passwd = $this->userManager->encryptPasswd($values->passwd1, $salt);
  165.       } else {
  166.         $salt = null;
  167.         $passwd = null;
  168.       }
  169.      
  170.       // Check email availability
  171.       $user = (new AppUser())->setDb($this->db);
  172.       if (!$editation && $user->loadByEmail($values->empty)->isPopulated()) {
  173.         $email = $values->empty;
  174.         throw new \Exception("E-mail '$email' is already registered!");
  175.       }
  176.      
  177.       // Update/Create user
  178.       if ($editation) {
  179.         $id = $values->userid;
  180.         $user->load($id)->update($desc, $home, $paypal, $passwd, $salt);
  181.       } else {        
  182.         $name = $values->name;
  183.         $mail = $values->empty;
  184.         $fbid = isset($values->fbid) ? $values->fbid : null;
  185.         $gpid = isset($values->gpid) ? $values->gpid : null;        
  186.         $user->create($name, $mail, $passwd, $salt, $desc, $home, $fbid, $gpid,
  187.             $paypal);
  188.       }
  189.     } catch (\Exception $e) {
  190.       $this->db->rollback();
  191.       $form->addError($e->getMessage());      
  192.     }
  193.     $this->db->commit();
  194.   }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement