From 0449bd404a72fa41b0761454a009760bcd415d9b Mon Sep 17 00:00:00 2001 From: Pantelis Roditis Date: Tue, 3 Mar 2026 22:36:14 +0200 Subject: [PATCH] add throwable catch so that we can stop saving the templates if there is a syntax error. --- .../controllers/EmailTemplateController.php | 35 ++++-- .../modules/content/models/EmailTemplate.php | 101 +++++++++--------- 2 files changed, 80 insertions(+), 56 deletions(-) diff --git a/backend/modules/content/controllers/EmailTemplateController.php b/backend/modules/content/controllers/EmailTemplateController.php index ad3e3f88b..9bd7c2236 100644 --- a/backend/modules/content/controllers/EmailTemplateController.php +++ b/backend/modules/content/controllers/EmailTemplateController.php @@ -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' => ['@'], ] @@ -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', [ @@ -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--; @@ -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', [ diff --git a/backend/modules/content/models/EmailTemplate.php b/backend/modules/content/models/EmailTemplate.php index 2cb314981..fb8919bf6 100644 --- a/backend/modules/content/models/EmailTemplate.php +++ b/backend/modules/content/models/EmailTemplate.php @@ -2,6 +2,7 @@ namespace app\modules\content\models; +use Throwable; use Yii; use yii\behaviors\TimestampBehavior; use yii\db\Expression; @@ -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()); + } }