Question.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace app\admin\model\ask;
  3. use addons\ask\library\Askdown;
  4. use addons\ask\library\FulltextSearch;
  5. use addons\ask\model\Taggable;
  6. use addons\ask\model\User;
  7. use think\Model;
  8. use traits\model\SoftDelete;
  9. class Question extends Model
  10. {
  11. use SoftDelete;
  12. // 表名
  13. protected $name = 'ask_question';
  14. // 自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'int';
  16. // 定义时间戳字段名
  17. protected $createTime = 'createtime';
  18. protected $updateTime = 'updatetime';
  19. protected $deleteTime = 'deletetime';
  20. protected $auto = ['content_fmt'];
  21. // 追加属性
  22. protected $append = [
  23. 'url',
  24. 'flag_text',
  25. 'rewardtime_text',
  26. 'deletetime_text',
  27. 'status_text'
  28. ];
  29. protected static $config = [];
  30. protected static function init()
  31. {
  32. $config = get_addon_config('ask');
  33. self::$config = $config;
  34. self::afterDelete(function ($row) use ($config) {
  35. $data = $row->withTrashed()->where('id', $row->id)->find();
  36. if ($data) {
  37. //删除相关回答
  38. $answerList = Answer::where('question_id', $row['id'])->select();
  39. foreach ($answerList as $index => $item) {
  40. $item->delete();
  41. }
  42. //删除相关评论
  43. $commentList = Comment::where('type', 'question')->where('source_id', $row['id'])->select();
  44. foreach ($commentList as $index => $item) {
  45. $item->delete();
  46. }
  47. User::decrease('questions', 1, $row->user_id);
  48. //更新Tags信息
  49. Tag::where('questions', '>', '0')->where('id', 'in', $row->tags_ids)->setDec("questions");
  50. //减少积分
  51. $config['score']['postquestion'] && \app\common\model\User::score(-$config['score']['postquestion'], $row->user_id, '删除问题');
  52. if ($config['searchtype'] == 'xunsearch') {
  53. FulltextSearch::del($row);
  54. }
  55. } else {
  56. //删除相关回答
  57. $answerList = Answer::where('question_id', $row['id'])->select();
  58. foreach ($answerList as $index => $item) {
  59. $item->delete(true);
  60. }
  61. //删除相关评论
  62. $commentList = Comment::where('type', 'question')->where('source_id', $row['id'])->select();
  63. foreach ($commentList as $index => $item) {
  64. $item->delete(true);
  65. }
  66. //删除相关标签
  67. $tagableList = Taggable::where('type', 'question')->where('source_id', $row['id'])->select();
  68. foreach ($tagableList as $index => $item) {
  69. $item->delete(true);
  70. }
  71. }
  72. });
  73. self::afterUpdate(function ($row) use ($config) {
  74. $changedData = $row->getChangedData();
  75. if (array_key_exists( 'deletetime', $changedData) && is_null($changedData['deletetime'])) {
  76. //更新Tags信息
  77. Tag::where('id', 'in', $row->tags_ids)->setInc("questions");
  78. User::increase('questions', 1, $row->user_id);
  79. $config['score']['postquestion'] && \app\common\model\User::score($config['score']['postquestion'], $row->user_id, '恢复问题');
  80. if ($config['searchtype'] == 'xunsearch') {
  81. FulltextSearch::update($row);
  82. }
  83. }
  84. });
  85. }
  86. public function getUrlAttr($value, $data)
  87. {
  88. return addon_url('ask/question/show', [':id' => $data['id']], static::$config['urlsuffix']);
  89. }
  90. public function getTagsAttr($value, $data)
  91. {
  92. if (isset($this->data['tags'])) {
  93. return $this->data['tags'];
  94. }
  95. $tags = Tag::getTags('question', $data['id']);
  96. return $tags;
  97. }
  98. public function getTagsIdsAttr($value, $data)
  99. {
  100. if (isset($this->data['tags_ids'])) {
  101. return $this->data['tags_ids'];
  102. }
  103. $tagsArr = [];
  104. $tagsList = $this->getTagsAttr($value, $data);
  105. foreach ($tagsList as $index => $item) {
  106. $tagsArr[] = $item->id;
  107. }
  108. return $tagsArr;
  109. }
  110. public function setContentFmtAttr($data)
  111. {
  112. $content = Askdown::instance()->format($this->data['content']);
  113. return $content;
  114. }
  115. public function getFlagList()
  116. {
  117. return ['index' => __('Index'), 'hot' => __('Hot'), 'recommend' => __('Recommend'), 'top' => __('Top')];
  118. }
  119. public function getStatusList()
  120. {
  121. return ['normal' => __('Normal'), 'hidden' => __('Hidden'), 'solved' => __('Solved'), 'closed' => __('Closed')];
  122. }
  123. public function getFlagTextAttr($value, $data)
  124. {
  125. $value = $value ? $value : (isset($data['flag']) ? $data['flag'] : '');
  126. $valueArr = explode(',', $value);
  127. $list = $this->getFlagList();
  128. return implode(',', array_intersect_key($list, array_flip($valueArr)));
  129. }
  130. public function getRewardtimeTextAttr($value, $data)
  131. {
  132. $value = $value ? $value : (isset($data['rewardtime']) ? $data['rewardtime'] : '');
  133. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  134. }
  135. public function getDeletetimeTextAttr($value, $data)
  136. {
  137. $value = $value ? $value : (isset($data['deletetime']) ? $data['deletetime'] : '');
  138. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  139. }
  140. public function getStatusTextAttr($value, $data)
  141. {
  142. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  143. $list = $this->getStatusList();
  144. return isset($list[$value]) ? $list[$value] : '';
  145. }
  146. protected function setFlagAttr($value)
  147. {
  148. return is_array($value) ? implode(',', $value) : $value;
  149. }
  150. protected function setRewardtimeAttr($value)
  151. {
  152. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  153. }
  154. protected function setDeletetimeAttr($value)
  155. {
  156. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  157. }
  158. public function user()
  159. {
  160. return $this->belongsTo("\app\common\model\User", 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
  161. }
  162. public function category()
  163. {
  164. return $this->belongsTo("Category", 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
  165. }
  166. }