Created
June 22, 2015 10:11
-
-
Save cornernote/0ae3ae86d358b32b6d1d to your computer and use it in GitHub Desktop.
email errors in yii2-audit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace bedezign\yii2\audit\commands; | |
use bedezign\yii2\audit\models\AuditEntry; | |
use bedezign\yii2\audit\models\AuditError; | |
use Yii; | |
use yii\helpers\Html; | |
/** | |
* Task runner commands for Auditing. | |
* | |
* @package bedezign\yii2\audit\commands | |
*/ | |
class AuditController extends \yii\console\Controller | |
{ | |
/** | |
* Email errors to support email. | |
*/ | |
public function actionErrorEmail() | |
{ | |
$batch = AuditError::find() | |
->andWhere(['>', 'error_count', 0]) | |
->andWhere(['emailed' => 0]) | |
->batch(); | |
$email = Yii::$app->params['supportEmail']; | |
// TODO - get absolute url | |
//$request = Yii::$app->request; | |
//$baseUrl = $request->getHostInfo() . $request->getBaseUrl(); | |
foreach ($batch as $auditErrors) { | |
/** @var AuditError $model */ | |
foreach ($auditErrors as $model) { | |
$params = [ | |
':message' => $model->message, | |
':file' => $model->file, | |
':line' => $model->line, | |
':link' => Html::a(Yii::t('audit', 'view audit entry'), ['auditing/default/view', 'id' => $model->audit_id]), | |
]; | |
Yii::$app->mail->compose() | |
->setFrom([$email => 'Audit :: ' . Yii::$app->name]) | |
->setTo($email) | |
->setSubject('Audit Error in Audit Entry #' . $model->id) | |
->setTextBody(Yii::t('app', ':message' . "\n" . 'in :file on line :line.' . "\n" . ':link.', $params)) | |
->setHtmlBody(Yii::t('app', '<b>:message</b><br />in <i>:file</i> on line <i>:line</i>.<br/>:link.', $params)) | |
->send(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment