Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make it possible to use other mailers as well (for example mandrill) #202

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions modules/feedback/mail/en/answer.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php

use yii\helpers\ArrayHelper;

$this->title = $subject;
?>
<p><?= nl2br($feedback->answer_text) ?></p>
<p><?= $html_answer ?></p>
<br/>
<br/>
<hr>
<p><?= Yii::$app->formatter->asDatetime($feedback->time, 'medium') ?> you wrote:</p>
<p><?= $nice_date ?> you wrote:</p>
<p>
<?php foreach(explode("\n", $feedback->text) as $line) echo '> '.$line.'<br/>'; ?>
<?php foreach(explode("\n", ArrayHelper::getValue($feedback, 'text')) as $line) echo '> '.$line.'<br/>'; ?>
</p>
4 changes: 3 additions & 1 deletion modules/feedback/mail/en/new_feedback.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use yii\helpers\ArrayHelper;
use yii\helpers\Html;

$this->title = $subject;
?>
<p>User <b><?= $feedback->name ?></b> leaved message in your guestbook.</p>
<p>User <b><?= ArrayHelper::getValue($feedback, 'name') ?></b> left a message in your guestbook.</p>
<p>You can view it <?= Html::a('here', $link) ?>.</p>
9 changes: 6 additions & 3 deletions modules/feedback/mail/ru/answer.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php

use yii\helpers\ArrayHelper;

$this->title = $subject;
?>
<p><?= nl2br($feedback->answer_text) ?></p>
<p><?= $html_answer ?></p>
<br/>
<br/>
<hr>
<p><?= Yii::$app->formatter->asDatetime($feedback->time, 'medium') ?> Вы писали:</p>
<p><?= $nice_date ?> Вы писали:</p>
<p>
<?php foreach(explode("\n", $feedback->text) as $line) echo '> '.$line.'<br/>'; ?>
<?php foreach(explode("\n", ArrayHelper::getValue($feedback, 'text')) as $line) echo '> '.$line.'<br/>'; ?>
</p>
4 changes: 3 additions & 1 deletion modules/feedback/mail/ru/new_feedback.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

use yii\helpers\ArrayHelper;
use yii\helpers\Html;

$this->title = $subject;
?>
<p>Пользователь <b><?= $feedback->name ?></b> оставил сообщение в вашей гостевой книге.</p>
<p>Пользователь <b><?= ArrayHelper::getValue($feedback, 'name') ?></b> оставил сообщение в вашей гостевой книге.</p>
<p>Просмотреть его вы можете <?= Html::a('здесь', $link) ?>.</p>
<hr>
<p>Это автоматическое сообщение и на него не нужно отвечать.</p>
73 changes: 52 additions & 21 deletions modules/feedback/models/Feedback.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
<?php

namespace yii\easyii\modules\feedback\models;

use Yii;
use yii\behaviors\AttributeBehavior;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\easyii\behaviors\CalculateNotice;
use yii\easyii\helpers\Mail;
use yii\easyii\models\Setting;
use yii\easyii\validators\ReCaptchaValidator;
use yii\easyii\validators\EscapeValidator;
use yii\helpers\Url;

/**
* Class Feedback
* @package yii\easyii\modules\feedback\models
*
* @property int $status
* @property int $time
* @property string $name
* @property string $email
* @property string $phone
* @property string $title
* @property string $text
* @property string $answer_text
*/
class Feedback extends \yii\easyii\components\ActiveRecord
{
const STATUS_NEW = 0;
Expand All @@ -24,6 +41,11 @@ public static function tableName()
return 'easyii_feedback';
}

public function init()
{
$this->status = self::STATUS_NEW;
}

public function rules()
{
return [
Expand All @@ -33,31 +55,17 @@ public function rules()
['title', 'string', 'max' => 128],
['email', 'email'],
['phone', 'match', 'pattern' => '/^[\d\s-\+\(\)]+$/'],
['reCaptcha', ReCaptchaValidator::className(), 'when' => function($model){
['reCaptcha', ReCaptchaValidator::className(), 'when' => function ($model) {
return $model->isNewRecord && Yii::$app->getModule('admin')->activeModules['feedback']->settings['enableCaptcha'];
}],
];
}

public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
if($insert){
$this->ip = Yii::$app->request->userIP;
$this->time = time();
$this->status = self::STATUS_NEW;
}
return true;
} else {
return false;
}
}

public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);

if($insert){
if ($insert) {
$this->mailAdmin();
}
}
Expand All @@ -81,25 +89,43 @@ public function behaviors()
return [
'cn' => [
'class' => CalculateNotice::className(),
'callback' => function(){
'callback' => function () {
return self::find()->status(self::STATUS_NEW)->count();
}
]
],
[
'class' => AttributeBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['ip'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['ip'],
],
'value' => Yii::$app->request->userIP,
],
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'time',
'updatedAtAttribute' => false,
],
];
}

public function mailAdmin()
{
$settings = Yii::$app->getModule('admin')->activeModules['feedback']->settings;

if(!$settings['mailAdminOnNewFeedback']){
if (!$settings['mailAdminOnNewFeedback']) {
return false;
}
return Mail::send(
Setting::get('admin_email'),
$settings['subjectOnNewFeedback'],
$settings['templateOnNewFeedback'],
['feedback' => $this, 'link' => Url::to(['/admin/feedback/a/view', 'id' => $this->primaryKey], true)]
[
'feedback' => $this->getAttributes(),
'link' => Url::to(['/admin/feedback/a/view','id' => $this->primaryKey], true),
'nice_date' => Yii::$app->formatter->asDatetime($this->time, 'medium'),
'html_text' => nl2br($this->text),
]
);
}

Expand All @@ -111,7 +137,12 @@ public function sendAnswer()
$this->email,
$this->answer_subject,
$settings['answerTemplate'],
['feedback' => $this],
[
'feedback' => $this->getAttributes(),
'nice_date' => Yii::$app->formatter->asDatetime($this->time, 'medium'),
'html_answer' => nl2br($this->answer_text),
'html_text' => nl2br($this->text),
],
['replyTo' => Setting::get('admin_email')]
);
}
Expand Down