Skip to content
Merged
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
35 changes: 29 additions & 6 deletions backend/modules/content/controllers/EmailTemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function behaviors()
'class' => \yii\filters\AccessControl::class,
'rules' => [
'00filtered-actions' => [
'actions' => ['update', 'delete','adhoc-mail'],
'actions' => ['update', 'delete', 'adhoc-mail'],
'allow' => true,
'roles' => ['@'],
]
Expand Down Expand Up @@ -71,8 +71,18 @@ public function actionCreate()
{
$model = new EmailTemplate();

if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$player = Player::find()->one();
try {
if ($player !== null) {
$contentHtml = $this->renderPhpContent("?>" . $model->html, ['user' => $player]);
$contentTxt = $this->renderPhpContent("?>" . $model->txt, ['user' => $player]);
}
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} catch (\Throwable $e) {
Yii::$app->session->setFlash('error', 'Failed to save the html and txt templates. ' . $e->getMessage());
}
}

return $this->render('create', [
Expand All @@ -93,7 +103,10 @@ public function actionAdhocMail()
$successes = $failures = 0;
foreach (Player::find()->where(['status' => 10])->orderBy(['id' => SORT_ASC])->all() as $p) {
//public function mail($subject, $html, $txt, $headers = [])
if ($p->mail($model->title, $model->html, $model->txt))
$contentHtml = $this->renderPhpContent("?>" . $model->html, ['user' => $player]);
$contentTxt = $this->renderPhpContent("?>" . $model->txt, ['user' => $player]);

if ($p->mail($model->title, $contentHtml, $contentTxt))
$successes++;
else {
$failures--;
Expand Down Expand Up @@ -122,8 +135,18 @@ public function actionUpdate($id)
{
$model = $this->findModel($id);

if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$player = Player::find()->one();
try {
if ($player !== null) {
$contentHtml = $this->renderPhpContent("?>" . $model->html, ['user' => $player]);
$contentTxt = $this->renderPhpContent("?>" . $model->txt, ['user' => $player]);
}
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} catch (\Throwable $e) {
Yii::$app->session->setFlash('error', 'Failed to save the html and txt templates. ' . $e->getMessage());
}
}

return $this->render('update', [
Expand Down
101 changes: 51 additions & 50 deletions backend/modules/content/models/EmailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace app\modules\content\models;

use Throwable;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;
Expand All @@ -17,57 +18,57 @@
*/
class EmailTemplate extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'email_template';
}
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'email_template';
}

public function behaviors()
{
return [
[
'class' => TimestampBehavior::class,
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => new Expression('NOW()'),
],
];
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['html', 'txt'], 'string'],
[['name'], 'string', 'max' => 32],
[['title'], 'string', 'max' => 255],
];
}
public function behaviors()
{
return [
[
'class' => TimestampBehavior::class,
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => new Expression('NOW()'),
],
];
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['html', 'txt'], 'string'],
[['name'], 'string', 'max' => 32],
[['title'], 'string', 'max' => 255],
];
}

/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'name' => Yii::t('app', 'Name'),
'title' => Yii::t('app', 'Title'),
'html' => Yii::t('app', 'Html'),
'txt' => Yii::t('app', 'Txt'),
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'name' => Yii::t('app', 'Name'),
'title' => Yii::t('app', 'Title'),
'html' => Yii::t('app', 'Html'),
'txt' => Yii::t('app', 'Txt'),
];
}

/**
* {@inheritdoc}
* @return EmailTemplateQuery the active query used by this AR class.
*/
public static function find()
{
return new EmailTemplateQuery(get_called_class());
}
/**
* {@inheritdoc}
* @return EmailTemplateQuery the active query used by this AR class.
*/
public static function find()
{
return new EmailTemplateQuery(get_called_class());
}
}