setTemplate('contact.form', 'internalmail'); $this->setKey('contactForm'); $this->useBlocksRotation(); $this->captcha_key .= md5(static::class); } public function data() { $data = parent::data(); $data['guest'] = User::guest(); if (! $this->guestAllowed && $data['guest']) { return _t('', 'Log in to send a message'); } $data['hasCaptcha'] = $this->hasCaptcha(); $data['attachments_on'] = $this->attachments_on(); return $data; } public function onWriteFormSubmitAction() { $this->iframeResponse = true; if (!$this->isRequestValid()) { $this->errors->reloadPage(); } return $this->submit(); } public function validate($data = []) { do { $data = parent::validate($data); if (empty($data['receiver'])) { $this->errors->impossible(); break; } if (! isset($data['author'])) { $data['author'] = User::id(); } if (empty($data['author'])) { if ($this->guestAllowed) { if ($this->hasCaptcha() && ! $this->isCaptchaValid($this->captcha_key)) { if ($this->errors->no()) { $this->errors->impossible(); } break; } $data['email'] = $this->input->postget('email', [TYPE_NOTAGS, 'len' => 150]); if (! $this->input->isEmail($data['email'])) { $this->errors->set(_t('', 'Incorrect email address'), 'email'); break; } } else { $this->errors->set(_t('', 'Log in to send a message')); break; } } $data['message'] = $this->input->post('message', [TYPE_TEXT, 'len' => 1000]); if (mb_strlen($data['message']) < 10) { $this->errors->set(_t('users', 'Message is too short'), 'message'); break; } # проверяем получателя $receiver = Users::model()->userData($data['receiver'], ['activated', 'blocked']); if (empty($receiver) || $receiver['blocked'] || User::isCurrent($data['receiver'])) { $this->errors->reloadPage(); break; } if ($this->tooManyRequests('users-write-form', 15)) { break; } $data['attach'] = InternalMail::attachUpload('attach', array_map('trim', explode(',', $this->attachmentsTypes))); } while (false); $this->trigger('afterValidateWriteForm', ['data' => & $data]); return $data; } public function submit() { $data = $this->validate(); if ($this->errors->no()) { $this->submitAdd($data); } return parent::submit(); } public function submitAdd($data) { do { if (empty($data['receiver'])) { $this->errors->impossible(); break; } if (empty($data['author']) && ! empty($data['email'])) { $data['author'] = $this->submitAddUserRegister($data['email']); } unset($data['email']); if ($this->errors->any()) { break; } if (empty($data['author']) || $data['author'] == $data['receiver']) { $this->errors->impossible(); break; } if (empty($data['message'])) { $this->errors->impossible(); break; } $this->trigger('submitAddBeforeSave', ['data' => & $data]); if ($this->errors->any()) { break; } # отправляем сообщение $author = $data['author']; $receiver = $data['receiver']; $message = $data['message']; unset($data['author'], $data['receiver'], $data['message']); InternalMail::model()->sendMessage( $author, $receiver, $message, $data ); if ($this->errors->no()) { $this->respond('success_message', _t('users', 'The message was sent successfully')); } } while (false); } public function submitAddUserRegister($email) { $userData = Users::model()->userDataByFilter(['email' => $email], [ 'user_id','blocked','blocked_reason', ]); if (empty($userData)) { # создаем новый аккаунт (неактивированный) $userData = Users::userRegister(['email' => $email]); if (!empty($userData['user_id'])) { return $userData['user_id']; } else { # ошибка регистрации $this->errors->reloadPage(); } } else { if ($userData['blocked']) { $this->errors->set(_t('users', 'This account has been blocked due to: [reason]', [ 'reason' => $userData['blocked_reason'] ])); } elseif (User::guest()) { # Prevent sending message from another user account (knowing email) $this->errors->set(_t('', 'Log in to send a message')); return 0; } else { return $userData['user_id']; } } return 0; } public function attachments_on() { if ($this->attachments_on instanceof Closure) { return ($this->attachments_on)(); } if (is_null($this->attachments_on)) { if (User::guest()) { return false; } return InternalMail::attachmentsEnabled(); } return $this->attachments_on; } public function hasCaptcha() { if (User::guest()) { return Captcha::enabled($this->captcha_key); } return false; } public function settingsForm($form) { $form->select('guestAllowed', _t('@users', 'Guest'), $this->guestAllowed) ->option(1, _t('@', 'allowed')) ->option(0, _t('@', 'Disallowed')) ; $form->select('captcha', _t('@', 'Captcha'), $this->captcha, function () { return Captcha::actionDriverOptions(); }) ->visibleIf('guestAllowed', 1) ->sysAdmin('captcha.enabled.' . $this->captcha_key); $form->text('attachmentsTypes', _t('@', 'Allowed attachment types'), $this->attachmentsTypes, false) ->tip(_t('@', 'Comma separated')); } }