Beispiel #1
0
 public function normalize($input, $property, model_editor $editor)
 {
     if ($this->isReadOnly) {
         return null;
     }
     // don't normalize input provided by editor, but separately read input
     // from multiple included fields
     $field = $editor->propertyToField($property);
     $amount = trim(input::vget("{$field}_amount"));
     $currency = trim(input::vget("{$field}_currency"));
     // consider missing monetary input if amount is empty
     if ($amount === '') {
         return null;
     }
     if (array_key_exists($currency, static::$currencies)) {
         // normalize entered amount according to provided set of supported notations
         foreach (static::$amountNotations as $amountPattern) {
             if (preg_match($amountPattern, $amount, $matches)) {
                 return sprintf('%s%s.%02d %s', $matches[1] == '-' ? '-' : '', strtr($matches[2], array('.' => '', ',' => '')), $matches[3], $currency);
             }
         }
     }
     // provide amount w/o currency as fallback (causing validation failure)
     return $amount;
 }
Beispiel #2
0
 public function render(html_form $form, $name, $input, $label, model_editor $editor, model_editor_field $field)
 {
     if ($this->isReadOnly) {
         return $this->renderStatic($form, $name, $input, $label, $editor, $field);
     }
     $available = array_merge(array('0' => \de\toxa\txf\_L('-')), $this->getSelectableOptions());
     $values = array_pad($input, $this->selectorCount, null);
     if (\de\toxa\txf\input::vget($name . '_cmdActionAddSelector')) {
         $values[] = null;
     }
     if (count($values) > $this->maxCount) {
         array_splice($values, $this->maxCount);
     }
     $selectors = array_map(function ($value) use($name, $available) {
         return markup::selector($name . '[]', $available, $value);
     }, $values);
     $classes = implode(' ', array_filter(array($this->class, 'related')));
     $form->setRow($name, $label, implode("\n", $selectors), $this->isMandatory, $this->hint, null, $classes);
     if (count($selectors) < $this->maxCount) {
         $form->setRowCode($name, markup::button($name . '_cmdActionAddSelector', '1', \de\toxa\txf\_L('Add Entry'), \de\toxa\txf\_L('Click this button to add another selector for choosing related information.'), 'actionAddSelector'));
     }
     return $this;
 }
Beispiel #3
0
 /**
  * Processes input while editing/adding user record.
  *
  * @param sql_user $provider provider used on creating new user record
  * @param int|false $userId ID of user to edit, false/0 on adding new user
  * @return sql_user|null edited or created user, null if creating user failed
  */
 protected function processInputOnEditing($provider, $userId)
 {
     if ($userId) {
         $user = user::load($userId);
         $userData = array('id' => $user->getID(), 'loginname' => $user->getLoginName(), 'name' => $user->getName(), 'email' => $user->getProperty('email'));
     } else {
         $user = null;
         $userData = array();
     }
     $form = $this->getForm($userData);
     if ($form->hasInput()) {
         if (input::vget('submit') == 'cancel') {
             txf::redirectTo($this->getUrls()->list);
         }
         /*
          * read in and normalize all provided information on user
          */
         $loginName = $user ? $userData['loginname'] : trim(input::vget('loginname'));
         $name = trim(input::vget('name'));
         $email = trim(input::vget('email'));
         $passwordA = trim(input::vget('password'));
         $passwordB = trim(input::vget('repeat'));
         /*
          * validate all information on user
          */
         if ($loginName === '') {
             $form->setRowError('loginname', \de\toxa\txf\_L('Provide login name of user!'));
         } else {
             if (strlen($loginName) > 64) {
                 $form->setRowError('loginname', \de\toxa\txf\_L('Provided login name is too long!'));
             }
         }
         if ($name && strlen($name) > 128) {
             $form->setRowError('loginname', \de\toxa\txf\_L('Provided full name is too long!'));
         }
         if ($email) {
             if (strlen($name) > 128) {
                 $form->setRowError('loginname', \de\toxa\txf\_L('Provided mail address is too long!'));
             } else {
                 if (!\de\toxa\txf\mail::isValidAddress($email)) {
                     $form->setRowError('email', \de\toxa\txf\_L('Provided mail address is invalid!'));
                 }
             }
         }
         // validate optionally provided password
         if (!$user || $passwordA || $passwordB) {
             if ($passwordA === '' || $passwordB === '') {
                 if ($user) {
                     $form->setRowError('password', \de\toxa\txf\_l('Provide new password twice for excluding typos.'));
                 } else {
                     $form->setRowError('password', \de\toxa\txf\_l('Provide password of new user and repeat for excluding typos.'));
                 }
             } else {
                 if ($passwordA !== $passwordB) {
                     $form->setRowError('password', \de\toxa\txf\_L('Doubly entered passwords don\'t match.'));
                 } else {
                     try {
                         if (is_callable($this->passwordValidator)) {
                             call_user_func($this->passwordValidator, $passwordA);
                         } else {
                             $this->passwordValidatorDefault($passwordA);
                         }
                     } catch (\InvalidArgumentException $e) {
                         $form->setRowError('password', $e->getMessage());
                     }
                 }
             }
         }
         /*
          * save changes to datasource
          */
         $hasError = $form->hasAnyRowError();
         if (!$hasError) {
             exception::enterSensitive();
             if ($user) {
                 try {
                     $user->datasource()->transaction()->wrap(function (datasource\connection $conn) use($user, $name, $email, $passwordA) {
                         $user->setProperty('name', $name);
                         $user->setProperty('email', $email);
                         if (trim($passwordA) !== '') {
                             $user->changePassword($passwordA);
                             if ($user->getUUID() === user::current()->getUUID()) {
                                 try {
                                     user::current()->authenticate($passwordA);
                                 } catch (unauthorized_exception $e) {
                                     view::flash(\de\toxa\txf\_L('Updating current session for using changed password failed. Probably you need to login, again.'), 'error');
                                 }
                             }
                         }
                         view::flash(\de\toxa\txf\_L('Successfully changed information on selected user.'));
                         return true;
                     });
                 } catch (\Exception $e) {
                     $hasError = true;
                     view::flash(\de\toxa\txf\_L('Failed to save information on user in datasource.'), 'error');
                 }
             } else {
                 try {
                     $user = $provider->create(array('loginname' => $loginName, 'name' => $name, 'password' => $passwordA, 'email' => $email, 'lock' => ''));
                     view::flash(\de\toxa\txf\_L('Successfully created new user.'));
                 } catch (\Exception $e) {
                     $hasError = true;
                     view::flash(\de\toxa\txf\_L('Failed to create new user record in datasource.'), 'error');
                 }
             }
             exception::leaveSensitive();
         }
         if (!$hasError) {
             txf::redirectTo($this->getUrls()->list);
         }
     }
     return $user;
 }
Beispiel #4
0
 /**
  * Retrieves format to use on processing request and creating response.
  *
  * @return string
  */
 public function getFormat()
 {
     if ($this->format === null) {
         $this->getAction();
         $this->format = input::vget('format', \de\toxa\txf\_1($this->format, 'html'));
     }
     return $this->format;
 }
Beispiel #5
0
 /**
  * Renders editor with fields providing controls for editing properties of
  * item in editor.
  *
  * @return string
  * @throws \LogicException on trying to render editable view of editor unless editing has been enabled
  */
 public function renderEditable()
 {
     if (!$this->isEditable()) {
         throw new \LogicException(\de\toxa\txf\_L('Model editor is not enabled.'));
     }
     $form = $this->form();
     if ($this->item) {
         $form->setHidden('id', $this->item->getReflection()->getMethod("serializeId")->invoke(null, $this->item->id()));
     }
     if (!array_key_exists('_referrer', $this->fields)) {
         $form->setHidden('_referrer', input::vget('_referrer'));
     }
     $fixed = array();
     foreach ($this->fields as $property => $field) {
         /** @var model_editor_field $field */
         if (!count($this->enabled) || !@$this->enabled[$property]) {
             $label = $field->label();
             $type = $field->type();
             $name = $this->propertyToField($property);
             $input = $field->isCustom() ? null : $this->getValue($property, false, $type);
             if ($this->isFixedValue($property)) {
                 $fixed[$property] = $input;
                 $type->renderStatic($form, $name, $input, $label, $this, $field);
             } else {
                 $type->render($form, $name, $input, $label, $this, $field);
                 if (array_key_exists($property, $this->errors)) {
                     $form->setRowError($name, $this->errors[$property]);
                 }
             }
         }
     }
     if (count($fixed)) {
         $form->setHidden('_fix', $fixed);
     }
     // compile buttons to show at end of editor
     if (!$this->item || $this->may['edit']) {
         $form->setButtonRow('_cmd', $this->item ? \de\toxa\txf\_L('Save') : \de\toxa\txf\_L('Create'), 'save');
     }
     $form->setButtonRow('_cmd', \de\toxa\txf\_L('Cancel'), 'cancel');
     if ($this->item && $this->may['delete']) {
         $form->setButtonRow('_cmd', \de\toxa\txf\_L('Delete'), 'delete');
     }
     if ($this->sortingOrder) {
         $form->setSortingOrder($this->sortingOrder);
     }
     // return HTML code of editor
     return $form->getCode();
 }
Beispiel #6
0
 /**
  * Processes input of widget updating its internal state.
  *
  * @return $this current instance
  */
 public function processInput()
 {
     if (user::current()->isAuthenticated()) {
         view::flash(\de\toxa\txf\_L('You are logged in, already.'));
         $this->redirect();
     }
     $form = $this->getForm();
     if ($form->hasInput()) {
         if (input::vget('submit') == 'cancel') {
             $this->redirect();
         }
         $username = input::vget('name');
         if ($username) {
             try {
                 user::setCurrent(user::load($username), input::vget('token'));
                 $this->redirect();
             } catch (unauthorized_exception $ex) {
                 if ($ex->isAccountLocked()) {
                     if ($this->resendUnlockMailUrl) {
                         view::flash(sprintf(\de\toxa\txf\_L('Your account is locked! <a href="%s">Resend unlock mail now.</a>'), sprintf($this->resendUnlockMailUrl, $ex->getUser()->getID())), 'error');
                     } else {
                         view::flash(sprintf(\de\toxa\txf\_L('Your account is locked!')), 'error');
                     }
                 } else {
                     sleep(3);
                     if ($ex->isUserNotFound()) {
                         view::flash(\de\toxa\txf\_L('User does not exist.'), 'error');
                     } else {
                         view::flash(\de\toxa\txf\_L('Authentication failed.'), 'error');
                     }
                 }
             }
         } else {
             view::flash(\de\toxa\txf\_L('Provide login name and password!'));
         }
     } else {
         $session =& txf::session();
         $referrer = input::vget('referrer');
         $session['referrer'] = url::isRelative($referrer) ? $referrer : null;
     }
     return $this;
 }
Beispiel #7
0
 /**
  * Processes input of widget updating its internal state.
  *
  * @return $this current instance
  */
 public function processInput()
 {
     if (!user::current()->isAuthenticated()) {
         view::flash(\de\toxa\txf\_L('You must be logged in.'));
         $this->redirect();
     }
     $form = $this->getForm();
     if ($form->hasInput()) {
         if (input::vget('submit') == 'cancel') {
             $this->redirect();
         }
         $passwordOld = trim(input::vget('old'));
         $passwordNewA = trim(input::vget('new'));
         $passwordNewB = trim(input::vget('repeat'));
         if ($passwordOld === '') {
             $form->setRowError('old', \de\toxa\txf\_L('Provide current password!'));
         }
         if ($passwordNewA === '' || $passwordNewB === '') {
             $form->setRowError('new', \de\toxa\txf\_l('Provide new password twice for excluding typos.'));
         } else {
             if ($passwordNewA !== $passwordNewB) {
                 $form->setRowError('new', \de\toxa\txf\_L('Doubly entered passwords don\'t match.'));
             } else {
                 try {
                     if (is_callable($this->passwordValidator)) {
                         call_user_func($this->passwordValidator, $passwordNewA);
                     } else {
                         $this->passwordValidatorDefault($passwordNewA);
                     }
                 } catch (\InvalidArgumentException $e) {
                     $form->setRowError('new', $e->getMessage());
                 }
             }
         }
         exception::enterSensitive();
         if (!$form->hasAnyRowError()) {
             try {
                 $user = user::load(user::current()->getID());
                 try {
                     $user->authenticate($passwordOld);
                 } catch (unauthorized_exception $e) {
                     $form->setRowError('old', \de\toxa\txf\_L('Authenticating request using old password failed.'));
                 }
             } catch (unauthorized_exception $e) {
                 $form->setRowError('old', \de\toxa\txf\_L('Current user isn\'t available.'));
             }
         }
         $hasError = false;
         if (!$form->hasAnyRowError()) {
             try {
                 user::current()->changePassword($passwordNewA);
                 view::flash(\de\toxa\txf\_L('Password has been changed successfully.'));
                 try {
                     user::current()->authenticate($passwordNewA);
                 } catch (unauthorized_exception $e) {
                     view::flash(\de\toxa\txf\_L('Updating current session for using changed password failed. Probably you need to login, again.'), 'error');
                 }
             } catch (\RuntimeException $e) {
                 $hasError = true;
                 view::flash(\de\toxa\txf\_L('Your input is okay, but changing password failed nevertheless.'), 'error');
             }
         }
         exception::leaveSensitive();
         if (!$hasError && !$form->hasAnyRowError()) {
             $this->redirect();
         }
     } else {
         $session =& txf::session();
         $referrer = input::vget('referrer');
         $session['referrer'] = url::isRelative($referrer) ? $referrer : null;
     }
     return $this;
 }
Beispiel #8
-1
 /**
  * Processes input of widget updating its internal state.
  *
  * @return widget current instance
  */
 public function processInput()
 {
     if (user::current()->isAuthenticated()) {
         user::dropCurrent();
     }
     view::flash(\de\toxa\txf\_L('You logged out successfully.'));
     $referrer = input::vget('referrer');
     $referrer = url::isRelative($referrer) ? $referrer : null;
     txf::redirectTo(\de\toxa\txf\_1($referrer, 'home'));
 }