Notify.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace common\models;
  3. use common\components\notify\Parser;
  4. use common\modules\book\models\Book;
  5. use common\modules\book\models\BookChapter;
  6. use common\modules\user\behaviors\UserBehavior;
  7. use yii\behaviors\TimestampBehavior;
  8. use yii\helpers\Html;
  9. use yii\helpers\Json;
  10. /**
  11. * This is the model class for table "{{%notify}}".
  12. *
  13. * @property integer $id
  14. * @property integer $from_uid
  15. * @property integer $to_uid
  16. * @property string $content
  17. * @property integer $created_at
  18. * @property integer $category_id
  19. * @property string $extra
  20. */
  21. class Notify extends \yii\db\ActiveRecord
  22. {
  23. /* @var Parser */
  24. public $parser;
  25. /**
  26. * @inheritdoc
  27. */
  28. public static function tableName()
  29. {
  30. return '{{%notify}}';
  31. }
  32. /**
  33. * @inheritdoc
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['to_uid', 'from_uid', 'category_id'], 'required'],
  39. [['from_uid', 'to_uid', 'read'], 'integer'],
  40. [['from_uid'], 'default', 'value' => 0], // 0是系统信息
  41. [['extra'], 'string']
  42. ];
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'id' => 'ID',
  51. 'from_uid' => 'From Uid',
  52. 'to_uid' => 'To Uid',
  53. 'category_id' => '通知类型',
  54. 'read' => '是否读过',
  55. 'created_at' => 'Created At',
  56. ];
  57. }
  58. public function behaviors()
  59. {
  60. return [
  61. [
  62. 'class' => TimestampBehavior::className(),
  63. 'updatedAtAttribute' => false
  64. ],
  65. UserBehavior::className()
  66. ];
  67. }
  68. public function getCategory()
  69. {
  70. return $this->hasOne(NotifyCategory::className(), ['id' => 'category_id']);
  71. }
  72. public function getTitle()
  73. {
  74. $item = [
  75. 'from' => $this->from,
  76. 'to' => $this->to,
  77. 'extra' => Json::decode($this->extra),
  78. 'text' => $this->category->title
  79. ];
  80. $this->parser = new Parser();
  81. return $this->parser->parse($item);
  82. }
  83. public function getEntity()
  84. {
  85. $extra = Json::decode($this->extra);
  86. switch ($this->category_id) {
  87. case NotifyCategory::REWARD:
  88. return Html::a($extra['article_title'], ['/article/view', 'id' => $extra['article_id']]);
  89. break;
  90. case NotifyCategory::UP_ARTICLE:
  91. return Html::a($extra['entity_title'], ['/article/view', 'id' => $extra['entity_id']]);
  92. break;
  93. case NotifyCategory::UP_COMMENT:
  94. if ($extra['entity'] == 'common\\models\\Article') {
  95. return Html::a($extra['comment_title'], ['/article/view', 'id' => $extra['entity_id'], '#' => 'comment-' . $extra['comment_id']]);
  96. } else {
  97. return Html::a($extra['comment_title'], ['/suggest/index', 'id' => $extra['entity_id'], '#' => 'comment-' . $extra['comment_id']]);
  98. }
  99. break;
  100. case NotifyCategory::FAVOURITE:
  101. return Html::a($extra['entity_title'], ['/article/view', 'id' => $extra['entity_id']]);
  102. break;
  103. case NotifyCategory::COMMENT_ARTICLE:
  104. return Html::a($extra['entity_title'], ['/article/view', 'id' => $extra['entity_id']]);
  105. break;
  106. case NotifyCategory::COMMENT_SUGGEST:
  107. return Html::a($extra['entity_title'], ['/suggest/view', 'id' => $extra['entity_id']]);
  108. break;
  109. default:
  110. return '';
  111. break;
  112. }
  113. }
  114. public function getContent()
  115. {
  116. $item = [
  117. 'from' => $this->from,
  118. 'to' => $this->to,
  119. 'extra' => Json::decode($this->extra),
  120. 'text' => $this->category->content
  121. ];
  122. $this->parser = new Parser();
  123. return $this->parser->parse($item);
  124. }
  125. public function getLink()
  126. {
  127. $extra = Json::decode($this->extra);
  128. switch ($this->category_id) {
  129. case NotifyCategory::SUGGEST:
  130. return ['/suggest/view', 'id' => $extra['entity_id']];
  131. break;
  132. case NotifyCategory::MESSAGE:
  133. return ['/message/index'];
  134. break;
  135. case NotifyCategory::COMMENT_ARTICLE:
  136. return ['/article/view', 'id' => $extra['entity_id'], '#' => 'comment-' . $extra['comment_id']];
  137. break;
  138. case NotifyCategory::COMMENT_SUGGEST:
  139. return ['/suggest/view', 'id' => $extra['entity_id'], '#' => 'comment-' . $extra['comment_id']];
  140. break;
  141. case NotifyCategory::REPLY:
  142. if ($extra['entity'] == 'common\\models\\Article') {
  143. return ['/article/view', 'id' => $extra['entity_id'], '#' => 'comment-' . $extra['comment_id']];
  144. }else if ($extra['entity'] == 'common\\modules\\book\\models\\Book') {
  145. return ['/book/view', 'id' => $extra['entity_id'], '#' => 'comment-' . $extra['comment_id']];
  146. }else if ($extra['entity'] == 'common\\modules\\book\\models\\BookChapter') {
  147. return ['/book/chapter', 'id' => $extra['entity_id'], '#' => 'comment-' . $extra['comment_id']];
  148. } else if ($extra['entity'] == 'common\\models\\Suggest') {
  149. return ['/suggest/view', 'id' => $extra['entity_id'], '#' => 'comment-' . $extra['comment_id']];
  150. }
  151. break;
  152. default:
  153. return '';
  154. break;
  155. }
  156. }
  157. public function afterSave($insert, $changedAttributes)
  158. {
  159. parent::afterSave($insert, $changedAttributes);
  160. if ($insert) {
  161. if ($this->to->isConfirmed) {
  162. \Yii::$app->mailer->compose()
  163. ->setTo($this->to->email)
  164. ->setSubject($this->from->username . ' ' . $this->getTitle())
  165. ->setTextBody($this->getContent())
  166. ->send();
  167. }
  168. }
  169. }
  170. }