/** * Process the user registration, triggered by a registration form * * @param Form $form */ private function processUserRegistration($form, Event $event) { if (!$this->config->get('plugins.login.enabled')) { throw new \RuntimeException($this->grav['language']->translate('PLUGIN_LOGIN.PLUGIN_LOGIN_DISABLED')); } if (!$this->config->get('plugins.login.user_registration.enabled')) { throw new \RuntimeException($this->grav['language']->translate('PLUGIN_LOGIN.USER_REGISTRATION_DISABLED')); } $data = []; $username = $form->value('username'); $data['username'] = $username; if (file_exists($this->grav['locator']->findResource('account://' . $username . YAML_EXT))) { $this->grav->fireEvent('onFormValidationError', new Event(['form' => $form, 'message' => $this->grav['language']->translate(['PLUGIN_LOGIN.USERNAME_NOT_AVAILABLE', $username])])); $event->stopPropagation(); return; } if ($this->config->get('plugins.login.user_registration.options.validate_password1_and_password2', false)) { if ($form->value('password1') != $form->value('password2')) { $this->grav->fireEvent('onFormValidationError', new Event(['form' => $form, 'message' => $this->grav['language']->translate('PLUGIN_LOGIN.PASSWORDS_DO_NOT_MATCH')])); $event->stopPropagation(); return; } $data['password'] = $form->value('password1'); } $fields = $this->config->get('plugins.login.user_registration.fields', []); foreach ($fields as $field) { // Process value of field if set in the page process.register_user $default_values = $this->config->get('plugins.login.user_registration.default_values'); if ($default_values) { foreach ($default_values as $key => $param) { $values = explode(',', $param); if ($key == $field) { $data[$field] = $values; } } } if (!isset($data[$field]) && $form->value($field)) { $data[$field] = $form->value($field); } } if ($this->config->get('plugins.login.user_registration.options.validate_password1_and_password2', false)) { unset($data['password1']); unset($data['password2']); } if ($this->config->get('plugins.login.user_registration.options.set_user_disabled', false)) { $data['state'] = 'disabled'; } else { $data['state'] = 'enabled'; } $user = $this->login->register($data); if ($this->config->get('plugins.login.user_registration.options.send_activation_email', false)) { $this->login->sendActivationEmail($user); } else { if ($this->config->get('plugins.login.user_registration.options.send_welcome_email', false)) { $this->login->sendWelcomeEmail($user); } if ($this->config->get('plugins.login.user_registration.options.send_notification_email', false)) { $this->login->sendNotificationEmail($user); } } $redirect = $this->config->get('plugins.login.user_registration.redirect_after_registration', false); if ($redirect) { $this->grav->redirect($redirect); } }
/** * Process a form * * Currently available processing tasks: * * - fillWithCurrentDateTime * * @param Form $form * * @return bool */ protected function process($form) { foreach ($form->fields as $field) { if (isset($field['process'])) { if (isset($field['process']['fillWithCurrentDateTime']) && $field['process']['fillWithCurrentDateTime']) { $form->setData($field['name'], gmdate('D, d M Y H:i:s', time())); } } } }
/** * Validate a form * * @param Form $form * @return bool */ protected function validate($form) { foreach ($form->fields as $field) { if (isset($field['validate']) && isset($field['validate']['required']) && $field['validate']['required']) { if (!$form->value($field['name'])) { return false; } } } return true; }