AdminLog.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2017-03-15 21:16
  7. */
  8. namespace common\models;
  9. use Yii;
  10. use yii\behaviors\TimestampBehavior;
  11. /**
  12. * This is the model class for table "{{%admin_log}}".
  13. *
  14. * @property integer $id
  15. * @property string $route
  16. * @property string $description
  17. * @property integer $created_at
  18. * @property integer $user_id
  19. */
  20. class AdminLog extends \yii\db\ActiveRecord
  21. {
  22. /**
  23. * @inheritdoc
  24. */
  25. public static function tableName()
  26. {
  27. return '{{%admin_log}}';
  28. }
  29. public function beforeSave($insert)
  30. {
  31. if($insert){
  32. $this->created_at = time();
  33. }
  34. return parent::beforeSave($insert);
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['description'], 'string'],
  43. [['created_at', 'user_id'], 'integer'],
  44. [['route'], 'string', 'max' => 255]
  45. ];
  46. }
  47. /**
  48. * @inheritdoc
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => Yii::t('app', 'ID'),
  54. 'route' => Yii::t('app', 'Route'),
  55. 'description' => Yii::t('app', 'Description'),
  56. 'created_at' => Yii::t('app', 'Created At'),
  57. 'user_id' => Yii::t('app', 'Admin User Id'),
  58. ];
  59. }
  60. /**
  61. * @return \yii\db\ActiveQuery
  62. */
  63. public function getUser()
  64. {
  65. return $this->hasOne(AdminUser::className(), ['id' => 'user_id']);
  66. }
  67. public function afterFind()
  68. {
  69. $this->description = str_replace([
  70. '{{%ADMIN_USER%}}',
  71. '{{%BY%}}',
  72. '{{%CREATED%}}',
  73. '{{%UPDATED%}}',
  74. '{{%DELETED%}}',
  75. '{{%ID%}}',
  76. '{{%RECORD%}}'
  77. ], [
  78. Yii::t('app', 'Admin user'),
  79. Yii::t('app', 'through'),
  80. Yii::t('app', 'created'),
  81. Yii::t('app', 'updated'),
  82. Yii::t('app', 'deleted'),
  83. Yii::t('app', 'id'),
  84. Yii::t('app', 'record')
  85. ], $this->description);
  86. $this->description = preg_replace_callback('/\(created_at\) : (\d{1,10})=>(\d{1,10})/', function ($matches) {
  87. return str_replace([$matches[1], $matches[2]], [Yii::$app->getFormatter()->asDate((int)$matches[1]), Yii::$app->getFormatter()->asDate((int)$matches[2])], $matches[0]);
  88. }, $this->description);
  89. $this->description = preg_replace_callback('/\(updated_at\) : (\d{1,10})=>(\d{1,10})/', function ($matches) {
  90. return str_replace([$matches[1], $matches[2]], [Yii::$app->getFormatter()->asDate((int)$matches[1]), Yii::$app->getFormatter()->asDate((int)$matches[2])], $matches[0]);
  91. }, $this->description);
  92. parent::afterFind();
  93. }
  94. /**
  95. * delete log not need to trigger after delete that lead to cycle call
  96. *
  97. * @return bool
  98. */
  99. public function afterDelete()
  100. {
  101. return false;
  102. }
  103. }