2016-06-16 02:24:13 +05:30
|
|
|
<?php
|
2019-12-29 20:29:58 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-06-16 02:24:13 +05:30
|
|
|
namespace api\models;
|
|
|
|
|
|
|
|
use api\models\base\ApiForm;
|
2018-04-18 02:17:25 +05:30
|
|
|
use common\helpers\Error as E;
|
2017-11-01 22:26:23 +05:30
|
|
|
use common\models\Account;
|
2019-12-29 20:29:58 +05:30
|
|
|
use Webmozart\Assert\Assert;
|
2016-06-16 02:24:13 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\base\InvalidConfigException;
|
|
|
|
|
|
|
|
class FeedbackForm extends ApiForm {
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public mixed $subject = null;
|
2016-06-16 02:24:13 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public mixed $email = null;
|
2016-06-16 02:24:13 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public mixed $type = null;
|
2016-06-16 02:24:13 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public mixed $message = null;
|
2016-06-16 02:24:13 +05:30
|
|
|
|
2019-12-29 20:29:58 +05:30
|
|
|
public function rules(): array {
|
2016-06-16 02:24:13 +05:30
|
|
|
return [
|
2019-12-29 20:29:58 +05:30
|
|
|
[['subject'], 'required', 'message' => E::SUBJECT_REQUIRED],
|
|
|
|
[['email'], 'required', 'message' => E::EMAIL_REQUIRED],
|
|
|
|
[['message'], 'required', 'message' => E::MESSAGE_REQUIRED],
|
2016-06-16 02:24:13 +05:30
|
|
|
[['subject'], 'string', 'max' => 255],
|
2016-06-17 02:02:23 +05:30
|
|
|
[['email'], 'email', 'message' => E::EMAIL_INVALID],
|
2016-06-16 02:24:13 +05:30
|
|
|
[['message'], 'string', 'max' => 65535],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
/**
|
|
|
|
* @throws InvalidConfigException
|
|
|
|
*/
|
2018-04-18 02:17:25 +05:30
|
|
|
public function sendMessage(): bool {
|
2016-06-16 02:24:13 +05:30
|
|
|
if (!$this->validate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
/** @var \yii\symfonymailer\Mailer $mailer */
|
2016-06-16 02:24:13 +05:30
|
|
|
$mailer = Yii::$app->mailer;
|
2016-10-07 20:15:06 +05:30
|
|
|
$supportEmail = Yii::$app->params['supportEmail'];
|
|
|
|
if (!$supportEmail) {
|
2024-12-02 15:40:55 +05:30
|
|
|
throw new InvalidConfigException('Please specify supportEmail value in the app params');
|
2016-06-16 02:24:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
$account = $this->getAccount();
|
2024-12-02 15:40:55 +05:30
|
|
|
/** @var \yii\symfonymailer\Message $message */
|
2017-04-25 05:10:35 +05:30
|
|
|
$message = $mailer->compose('@common/emails/views/feedback', [
|
2016-06-16 02:24:13 +05:30
|
|
|
'model' => $this,
|
|
|
|
'account' => $account,
|
|
|
|
]);
|
|
|
|
$message
|
2016-10-07 20:15:06 +05:30
|
|
|
->setTo($supportEmail)
|
2024-12-02 15:40:55 +05:30
|
|
|
->setFrom([$this->email => $account?->username ?? $this->email])
|
2016-06-16 02:24:13 +05:30
|
|
|
->setSubject($this->subject);
|
|
|
|
|
2019-12-29 20:29:58 +05:30
|
|
|
Assert::true($message->send(), 'Unable send feedback email.');
|
2016-06-16 02:24:13 +05:30
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-01 22:26:23 +05:30
|
|
|
protected function getAccount(): ?Account {
|
|
|
|
$identity = Yii::$app->user->identity;
|
|
|
|
if ($identity === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $identity->getAccount();
|
2016-06-16 02:24:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|