diff --git a/app/forms/UserSettings.php b/app/forms/UserSettings.php new file mode 100644 index 0000000..ebc7f5c --- /dev/null +++ b/app/forms/UserSettings.php @@ -0,0 +1,235 @@ +setValidation(new \Phalcon\Validation()); + + // Id + $id = new Text('id', array( + 'class' => 'form-control', + 'readonly' => '', + )); + $id->addValidator(new IdenticalValidator([ + 'accepted' => $this->getEntity()->getId(), + ])); + + $id->setLabel('ID'); + $this->add($id); + + // Username + $username = new Text('username', array( + 'class' => 'form-control', + 'placeholder' => 'Username', + )); + + $username->setLabel('Username'); + + $username->addValidator(new AlnumValidator()); + + $validator = new UniquenessValidator(array( + 'model' => new UserModel(), + 'message' => 'The username already exists.', + 'attribute' => 'username', + 'except' => [ $this->getEntity()->getUsername() ] + )); + + $username->addValidator($validator); + + $this->add($username); + + // Name + $name = new Text('name', array( + 'class' => 'form-control', + 'placeholder' => 'Name', + )); + + $name->setLabel('Name'); + $name->addValidator(new AlphaValidator([ + 'allowSpace' => true, + 'allowEmpty' => true, + ])); + + $this->add($name); + + // Email + $email = new Text('email', array( + 'class' => 'form-control', + 'placeholder' => 'Email', + 'readonly' => '', + )); + + $email->addValidator(new IdenticalValidator([ + 'accepted' => $this->getEntity()->getEmail(), + ])); + + $email->setLabel('Email'); + + $this->add($email); + + // Passwords + $this->_passwords(); + + // Submit + $submit = new Submit('Save', array('class' => 'button button-default')); + $this->add($submit); + } + + /** + * Password section + */ + protected function _passwords() + { + $current_pw = $this->getEntity()->getPassword(); + + // Current + if (strlen($current_pw) > 0) { + $current = new Password('passwordCurrent', array( + 'class' => 'form-control', + )); + $current->setLabel('Current password'); + $this->add($current); + } + + // New + $new = new Password('passwordNew', array( + 'class' => 'form-control', + )); + $new->setLabel('New password'); + $this->add($new); + + // Confirm + $confirm = new Password('passwordConfirm', array( + 'class' => 'form-control', + )); + $confirm->setLabel('Confirm'); + $this->add($confirm); + + // Validation + $validation = $this->getValidation(); + + if (strlen($current_pw) > 0) { + $validation->add('passwordCurrent', new CallbackValidator([ + 'callback' => function($data) { + $new_pw = $data['passwordNew']; + if (strlen($new_pw) > 0) { + $value = $data['passwordCurrent']; + $hash = $this->getEntity()->getPassword(); + + // Only fail if there is a password and they did not match. + if (strlen($hash) > 0 && password_verify($value, $hash) === false) { + return false; + } + } + return true; + }, + 'message' => 'Password is not valid.' + ])); + } + + $validation->add('passwordNew', new StringLengthValidator([ + 'allowEmpty' => true, + 'min' => 8, + 'messageMinimum' => 'Password must be atleast 8 characters long', + ])); + + $validation->add('passwordConfirm', new ConfirmationValidator([ + 'message' => 'Passwords does not match', + 'with' => 'passwordNew', + ])); + } + + public function renderDecorated($name, $opt = []) + { + $options = [ + 'label-class' => 'control-label', + 'class' => 'col-sm-10', + 'message' => '' + ]; + + $ele = $this->get($name); + + if (isset($opt['label-length'])) { + $length = (int) $opt['label-length']; + } else { + $length = 2; + } + $options['label-class'] .= ' col-sm-' . $length; + + if (isset($opt['length'])) { + + $len = $opt['length']; + + if ($len === 'full') { + $options['class'] = ''; + } else { + $options['class'] = 'col-sm-' . $opt['length']; + } + } + + if ($ele->hasMessages()) { + $options['class'] .= ' has-error'; + $options['message'] = $ele->getMessages()->current(); + } + + return $this->_render($ele, $options); + } + + protected function _render(FormElement $ele, $opt) + { + $xhtml = ''; + + if (strlen($ele->getLabel()) > 0) { + + $xhtml .= sprintf( + '', + $opt['label-class'], $ele->getName(), $ele->getLabel()); + } + + $xhtml .= '