Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 62 |
| MessageForm | |
0.00% |
0 / 1 |
|
0.00% |
0 / 5 |
156.00 | |
0.00% |
0 / 62 |
| rules | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 9 |
|||
| attributeLabels | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 5 |
|||
| beforeValidate | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 7 |
|||
| sendMessage | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 24 |
|||
| sendEmail | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 17 |
|||
| <?php | |
| namespace backend\models; | |
| use Yii; | |
| use common\models\Message; | |
| use common\models\User; | |
| use yii\base\Model; | |
| use yii\web\UploadedFile; | |
| /** | |
| * MessageForm is the model behind the contact form. | |
| */ | |
| class MessageForm extends Model | |
| { | |
| public $receivers; | |
| public $subject; | |
| public $body; | |
| public $attachments; | |
| public $verifyCode; | |
| /** | |
| * @inheritdoc | |
| */ | |
| public function rules() | |
| { | |
| return [ | |
| // receivers, subject and body are required | |
| [['receivers', 'subject', 'body'], 'required'], | |
| // each receiver is integer | |
| ['receivers', 'each', 'rule' => ['integer']], | |
| // verifyCode needs to be entered correctly | |
| ['verifyCode', 'captcha'], | |
| [['subject'], 'string', 'max' => 150], | |
| [['attachments'], 'file', 'maxFiles' => 10], | |
| ]; | |
| } | |
| /** | |
| * @inheritdoc | |
| */ | |
| public function attributeLabels() | |
| { | |
| return [ | |
| 'verifyCode' => Yii::t('app', 'Verification Code'), | |
| ]; | |
| } | |
| /** | |
| * @inheritdoc | |
| */ | |
| public function beforeValidate() | |
| { | |
| if (parent::beforeValidate()) { | |
| $this->attachments = UploadedFile::getInstances($this, 'attachments'); | |
| return true; | |
| } | |
| return false; | |
| } | |
| /** | |
| * Sends an message to the specified user using the information collected by this model. | |
| * | |
| * @return boolean whether all messages were sent | |
| */ | |
| public function sendMessage() | |
| { | |
| $sender = Yii::$app->user; | |
| $users = User::find()->where(['id' => $this->receivers])->all(); | |
| $transaction = Yii::$app->db->beginTransaction(); | |
| try { | |
| foreach($users as $user){ | |
| $message = new Message(); | |
| $message->sender_user_id = $sender->id; | |
| $message->reciver_user_id = $user->id; | |
| $message->subject = $this->subject; | |
| $message->body = $this->body; | |
| $message->save(); | |
| } | |
| $transaction->commit(); | |
| } catch(\Exception $e) { | |
| $transaction->rollBack(); | |
| return false; | |
| } | |
| $result = true; | |
| foreach($users as $user){ | |
| $result &= $this->sendEmail($user); | |
| } | |
| return $result; | |
| } | |
| /** | |
| * Sends an email to the specified email address using the information collected by this model. | |
| * | |
| * @param string $email the target email address | |
| * @return boolean whether the email was sent | |
| */ | |
| private function sendEmail($user) | |
| { | |
| $message = Yii::$app->mailer->compose(); | |
| $fromEmail = \Yii::$app->params['adminEmail']; | |
| $sourceEmail = $message->getFrom(); | |
| if (!empty($sourceEmail)) $fromEmail = key($sourceEmail); | |
| $message = $message->setFrom([$fromEmail => \Yii::$app->name . ' robot']) | |
| ->setReplyTo([\Yii::$app->params['adminEmail'] => \Yii::$app->name . ' robot']) | |
| ->setTo($user->email) | |
| ->setSubject($this->subject) | |
| ->setTextBody($this->body); | |
| if (is_array($this->attachments)) { | |
| foreach ($this->attachments as $attachmentFile) { | |
| $message->attach($attachmentFile->tempName, ['fileName' => $attachmentFile->name, 'contentType' => $attachmentFile->type]); | |
| } | |
| } | |
| return $message->send(); | |
| } | |
| } |