Backend\Modules\Users\Actions\Edit::validateForm PHP Метод

validateForm() приватный Метод

Validate the form
private validateForm ( )
    private function validateForm()
    {
        // is the form submitted?
        if ($this->frm->isSubmitted()) {
            // cleanup the submitted fields, ignore fields that were added by hackers
            $this->frm->cleanupFields();
            $fields = $this->frm->getFields();
            // email is present
            if (!$this->user->isGod()) {
                if ($fields['email']->isFilled(BL::err('EmailIsRequired'))) {
                    // is this an email-address
                    if ($fields['email']->isEmail(BL::err('EmailIsInvalid'))) {
                        // was this emailaddress deleted before
                        if (BackendUsersModel::emailDeletedBefore($fields['email']->getValue())) {
                            $fields['email']->addError(sprintf(BL::err('EmailWasDeletedBefore'), BackendModel::createURLForAction('UndoDelete', null, null, array('email' => $fields['email']->getValue()))));
                        } elseif (BackendUsersModel::existsEmail($fields['email']->getValue(), $this->id)) {
                            // email already exists
                            $fields['email']->addError(BL::err('EmailAlreadyExists'));
                        }
                    }
                }
            }
            // required fields
            if ($this->user->isGod() && $fields['email']->getValue() != '' && $this->user->getEmail() != $fields['email']->getValue()) {
                $fields['email']->addError(BL::err('CantChangeGodsEmail'));
            }
            if (!$this->user->isGod()) {
                $fields['email']->isEmail(BL::err('EmailIsInvalid'));
            }
            $fields['nickname']->isFilled(BL::err('NicknameIsRequired'));
            $fields['name']->isFilled(BL::err('NameIsRequired'));
            $fields['surname']->isFilled(BL::err('SurnameIsRequired'));
            $fields['interface_language']->isFilled(BL::err('FieldIsRequired'));
            $fields['date_format']->isFilled(BL::err('FieldIsRequired'));
            $fields['time_format']->isFilled(BL::err('FieldIsRequired'));
            $fields['number_format']->isFilled(BL::err('FieldIsRequired'));
            if ($this->allowUserRights) {
                $fields['groups']->isFilled(BL::err('FieldIsRequired'));
            }
            if (isset($fields['new_password']) && $fields['new_password']->isFilled()) {
                if ($fields['new_password']->getValue() !== $fields['confirm_password']->getValue()) {
                    $fields['confirm_password']->addError(BL::err('ValuesDontMatch'));
                }
            }
            // no errors?
            if ($this->frm->isCorrect()) {
                // build user-array
                $user['id'] = $this->id;
                if (!$this->user->isGod()) {
                    $user['email'] = $fields['email']->getValue(true);
                }
                if ($this->authenticatedUser->getUserId() != $this->record['id']) {
                    $user['active'] = $fields['active']->getActualValue();
                }
                // user is now de-activated, we now remove all sessions for this user so he is logged out immediately
                if (isset($user['active']) && $user['active'] === 'N' && $this->record['active'] !== $user['active']) {
                    // delete all sessions for user
                    BackendModel::get('database')->delete('users_sessions', 'user_id = ?', array($this->user->getUserId()));
                }
                // build settings-array
                $settings['nickname'] = $fields['nickname']->getValue();
                $settings['name'] = $fields['name']->getValue();
                $settings['surname'] = $fields['surname']->getValue();
                $settings['interface_language'] = $fields['interface_language']->getValue();
                $settings['date_format'] = $fields['date_format']->getValue();
                $settings['time_format'] = $fields['time_format']->getValue();
                $settings['datetime_format'] = $settings['date_format'] . ' ' . $settings['time_format'];
                $settings['number_format'] = $fields['number_format']->getValue();
                $settings['csv_split_character'] = $fields['csv_split_character']->getValue();
                $settings['csv_line_ending'] = $fields['csv_line_ending']->getValue();
                // @TODO remove this when the api is kicked out
                $settings['api_access'] = $this->allowUserRights ? (bool) $fields['api_access']->getChecked() : $this->record['settings']['api_access'];
                // update password (only if filled in)
                if (isset($fields['new_password']) && $fields['new_password']->isFilled()) {
                    $user['password'] = BackendAuthentication::getEncryptedString($fields['new_password']->getValue(), $this->record['settings']['password_key']);
                    // the password has changed
                    if ($this->record['password'] != $user['password']) {
                        // save the login timestamp in the user's settings
                        $lastPasswordChange = BackendUsersModel::getSetting($user['id'], 'current_password_change');
                        $settings['current_password_change'] = time();
                        if ($lastPasswordChange) {
                            $settings['last_password_change'] = $lastPasswordChange;
                        }
                        // save the password strength
                        $passwordStrength = BackendAuthentication::checkPassword($fields['new_password']->getValue());
                        $settings['password_strength'] = $passwordStrength;
                    }
                }
                // get user groups when allowed to edit
                if ($this->allowUserRights) {
                    // get selected groups
                    $groups = $fields['groups']->getChecked();
                }
                // has the user submitted an avatar?
                if ($fields['avatar']->isFilled()) {
                    // init vars
                    $avatarsPath = FRONTEND_FILES_PATH . '/backend_users/avatars';
                    // delete old avatar if it isn't the default-image
                    if ($this->record['settings']['avatar'] != 'no-avatar.jpg' && $this->record['settings']['avatar'] != '') {
                        $filesystem = new Filesystem();
                        $filesystem->remove($avatarsPath . '/source/' . $this->record['settings']['avatar']);
                        $filesystem->remove($avatarsPath . '/128x128/' . $this->record['settings']['avatar']);
                        $filesystem->remove($avatarsPath . '/64x64/' . $this->record['settings']['avatar']);
                        $filesystem->remove($avatarsPath . '/32x32/' . $this->record['settings']['avatar']);
                    }
                    // create new filename
                    $filename = mt_rand(0, 3) . '_' . $user['id'] . '.' . $fields['avatar']->getExtension();
                    // add into settings to update
                    $settings['avatar'] = $filename;
                    // resize (128x128)
                    $fields['avatar']->createThumbnail($avatarsPath . '/128x128/' . $filename, 128, 128, true, false, 100);
                    // resize (64x64)
                    $fields['avatar']->createThumbnail($avatarsPath . '/64x64/' . $filename, 64, 64, true, false, 100);
                    // resize (32x32)
                    $fields['avatar']->createThumbnail($avatarsPath . '/32x32/' . $filename, 32, 32, true, false, 100);
                }
                // save changes
                BackendUsersModel::update($user, $settings);
                // save groups
                if ($this->allowUserRights) {
                    BackendGroupsModel::insertMultipleGroups($this->id, $groups);
                }
                // trigger event
                BackendModel::triggerEvent($this->getModule(), 'after_edit', array('item' => $user));
                // can only edit own profile
                if (!BackendAuthentication::isAllowedAction('Index')) {
                    // everything is saved, so redirect to the edit page
                    $this->redirect(BackendModel::createURLForAction('Edit') . '&id=' . $this->id . '&report=edited&var=' . $settings['nickname']);
                } else {
                    // everything is saved, so redirect to the overview
                    $this->redirect(BackendModel::createURLForAction('Index') . '&report=edited&var=' . $settings['nickname'] . '&highlight=row-' . $user['id']);
                }
            }
        }
    }