Example #1
0
 protected static function localFormat($value)
 {
     if (trim($value) !== '') {
         $value = strtr($value, array('.' => \de\toxa\txf\_L('DECIMAL_SEP', null, 1, '.')));
     }
     return $value;
 }
Example #2
0
 public function validate($input, $property, model_editor $editor)
 {
     if ($input === null) {
         if ($this->isMandatory) {
             throw new \InvalidArgumentException(\de\toxa\txf\_L('This information is required.'));
         }
     }
     return true;
 }
Example #3
0
 /**
  * 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'));
 }
Example #4
0
 public function validate($input, $property, model_editor $editor)
 {
     parent::validate($input, $property, $editor);
     if ($input != '') {
         if (!mail::isValidAddress($input)) {
             throw new \InvalidArgumentException(\de\toxa\txf\_L('This is not a valid e-mail address.'));
         }
     }
     return true;
 }
Example #5
0
 public function validate($input, $property, model_editor $editor)
 {
     parent::validate($input, $property, $editor);
     $items = preg_split('/[' . preg_quote($this->separator) . ']/', $input);
     if ($this->minCount > 0 && $this->minCount > count($items)) {
         throw new \InvalidArgumentException(\de\toxa\txf\_L('Provide additional information here!'));
     }
     if ($this->maxCount > 0 && $this->maxCount < count($items)) {
         throw new \InvalidArgumentException(\de\toxa\txf\_L('Provide less information here!'));
     }
     return true;
 }
Example #6
0
File: url.php Project: cepharum/txf
 public function validate($input, $property, model_editor $editor)
 {
     parent::validate($input, $property, $editor);
     if ($input != '') {
         if (!url::isFile($input)) {
             throw new \InvalidArgumentException(\de\toxa\txf\_L('This is not a valid URL.'));
         }
         if ($this->absolute && url::isRelative($input)) {
             throw new \InvalidArgumentException(\de\toxa\txf\_L('This URL must be absolute. Include scheme e.g. http://www.example.com/!'));
         }
     }
     return true;
 }
Example #7
0
 public function validate($input, $property, model_editor $editor)
 {
     if ($input === null) {
         if ($this->isMandatory) {
             throw new \InvalidArgumentException(\de\toxa\txf\_L('This information is required.'));
         }
         return true;
     }
     $parts = explode(' ', $input);
     if (count($parts) == 2) {
         if (preg_match('/^[+-]?\\d+\\.\\d{2}$/', $parts[0])) {
             return true;
         }
     }
     throw new \InvalidArgumentException(\de\toxa\txf\_L('Your input is invalid.'));
 }
Example #8
0
 /**
  * Processes input of widget updating its internal state.
  *
  * @throws http_exception on trying to use widget without authorization
  * @return $this current instance
  */
 public function processInput()
 {
     if (!$this->isUserAuthorized()) {
         throw new http_exception(403, \de\toxa\txf\_L('You must not manage users!'));
     }
     $provider = user::getProvider();
     if (!$provider instanceof sql_user) {
         throw new http_exception(400, \de\toxa\txf\_L('This manager is suitable for managing SQL-based users, only!'));
     }
     list($action, $userId) = $this->detectMode();
     if ($this->isListing()) {
         $this->getBrowser()->processInput();
     } else {
         switch ($action) {
             case 'edit':
             case 'add':
                 $this->processInputOnEditing($provider, $userId);
                 break;
             case 'delete':
                 if ($userId === user::current()->getID()) {
                     throw new http_exception(403, \de\toxa\txf\_L('Deleting current user account rejected.'));
                 }
                 user::load($userId)->delete();
                 txf::redirectTo($this->getUrls()->list);
                 break;
             default:
                 // TODO implement all else actions (lock, unlock, ...)
                 txf::redirectTo($this->getUrls()->list);
         }
     }
     return $this;
 }
Example #9
0
 public function performDelete()
 {
     if (!user::current()->isAuthenticated()) {
         throw new http_exception(403);
     }
     $this->prepareControl();
     try {
         $this->getSelectedItem()->delete();
     } catch (datasource_exception $e) {
         view::flash(\de\toxa\txf\_L('Failed deleting selected item.'), 'error');
     } catch (\RuntimeException $e) {
         view::flash(\de\toxa\txf\_L('Selected item does not exist (anymore).'), 'error');
     }
     txf::redirectTo($this->getUrls()->list);
 }
Example #10
0
 public function formatValue($name, $value, model_editor $editor, model_editor_field $field)
 {
     return $this->options->exists($value) ? $this->options->value($value) : \de\toxa\txf\_L('-');
 }
Example #11
0
 public function renderStatic(html_form $form, $name, $input, $label, model_editor $editor, model_editor_field $field)
 {
     $classes = implode(' ', array_filter(array($this->class, 'option')));
     $form->setRow($name, $label, markup::inline($input ? \de\toxa\txf\_L('yes') : \de\toxa\txf\_L('no'), 'static'), null, null, $classes);
     return $this;
 }
Example #12
0
 /**
  * Associates editor instance with single item of related model.
  *
  * Providing null is available for convenience, too. In that case editor is
  * actually kept unassociated with a particular item of model, but returns
  * true here nevertheless. It's intended to call selectItem( null ) in case
  * of trying to edit model instance that does not exist, yet. This way
  * callers won't have to test for existing item themselves.
  *
  * It is possible to provide item instance instead of item's ID here. In
  * that case editor is switching to use provided item's data source further
  * one.
  *
  * Selecting item is available once, only. This method is throwing exception
  * on trying to select another item.
  *
  * @param mixed $id ID or instance of item to associate with editor
  * @return bool true on success, false on error
  * @throws \LogicException on trying to re-associate editor
  */
 public function selectItem($id)
 {
     if ($this->item) {
         throw new \LogicException(\de\toxa\txf\_L('Editor is already operating on model instance.'));
     }
     if ($id !== null) {
         if (is_object($id) && $this->class->isInstance($id)) {
             $this->item = $id;
         } else {
             $this->item = $this->class->getMethod('select')->invoke(null, $this->datasource, $id);
         }
         if (!$this->item) {
             return false;
         }
         $this->datasource = $this->item->source();
         if (!$this->datasource) {
             throw new \RuntimeException('item is not associated with data source');
         }
         foreach ($this->fields as $field) {
             /** @var model_editor_field $field */
             $field->type()->onSelectingItem($this, $this->item, $field);
         }
     }
     return true;
 }
Example #13
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;
 }
Example #14
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;
 }
Example #15
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;
 }
Example #16
0
 public function validate($input, $property, model_editor $editor)
 {
     if ($input === null) {
         if ($this->isMandatory) {
             throw new \InvalidArgumentException(\de\toxa\txf\_L('This information is required.'));
         }
     } else {
         $text = $this->limitWithoutHtml ? preg_replace('/\\s+/', '', strip_tags($input)) : $input;
         if ($this->minLength > 0 && mb_strlen($text) < $this->minLength) {
             throw new \InvalidArgumentException(\de\toxa\txf\_L('Your input is too short.'));
         }
         if ($this->maxLength > 0 && mb_strlen($text) > $this->maxLength) {
             throw new \InvalidArgumentException(\de\toxa\txf\_L('Your input is too long.'));
         }
         if ($this->pattern && $input !== null) {
             if (is_array($this->pattern)) {
                 $match = preg_match($this->pattern[0], $this->pattern[1] ? preg_replace('/\\s+/', '', $input) : $input);
             } else {
                 $match = preg_match($this->pattern, $input);
             }
             if (!$match) {
                 throw new \InvalidArgumentException(\de\toxa\txf\_L('Your input is invalid.'));
             }
         }
         if (preg_match('#<(script|object|iframe|style|link)[\\s/>]#i', $input)) {
             throw new \InvalidArgumentException(\de\toxa\txf\_L('This input contains invalid HTML code.'));
         }
     }
     return true;
 }
Example #17
0
 /**
  * Creates query for browsing model's items stored in provided datasource.
  *
  * @param connection $source datasource containing model's items, omit for using default datasource
  * @param string $alias optional name of alias to explicitly use on model's data set in retrieved query
  * @return query query for listing items of current model
  */
 public static function browse(connection $source = null, $alias = null)
 {
     if ($source === null) {
         $source = datasource::getDefault();
     }
     if (!$source instanceof connection) {
         throw new \InvalidArgumentException(\de\toxa\txf\_L('missing link to datasource'));
     }
     static::updateSchema($source);
     $setName = preg_replace('/\\s+/', ' ', trim(static::$set_prefix . static::$set));
     if ($alias) {
         $parts = explode(' ', $setName);
         if (!is_string($alias)) {
             throw new \InvalidArgumentException('invalid type of alias');
         }
         $alias = explode(' ', preg_replace('/\\s+/', ' ', trim($alias)));
         $setName = $parts[0] . ' ' . $alias[0];
     }
     return $source->createQuery($setName);
 }
Example #18
0
 /**
  * Retrieves code for embedding widget in current view.
  *
  * @throws http_exception on trying to access missing page
  * @return string code embeddable in view
  */
 public function getCode()
 {
     if ($this->mayEdit && $this->wantEdit) {
         return markup::h2(\de\toxa\txf\_L('Edit Page')) . $this->getEditor()->render();
     } else {
         $page = $this->getPage();
         if (!$page) {
             if ($this->mayEdit) {
                 return markup::block(markup::emphasize(\de\toxa\txf\_L('Selected page does not exist, yet!')), 'missing-page-content') . ($this->editorUrl ? $this->getLinkToEditor() : '');
             }
             throw new http_exception(404);
         }
         $content = markup::block($page->content, 'page-content');
         if ($this->editorUrl && $this->mayEdit) {
             $content .= $this->getLinkToEditor();
         }
         return $content;
     }
 }
Example #19
0
 public static function label($count = 1)
 {
     return \de\toxa\txf\_L('page', 'pages', $count);
 }