Talent.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace common\models\article;
  3. use common\behaviors\DynamicFormBehavior;
  4. use common\behaviors\XsBehavior;
  5. use common\models\Article;
  6. use yii\helpers\HtmlPurifier;
  7. use yii\helpers\StringHelper;
  8. /**
  9. * This is the model class for table "{{%article_data}}".
  10. *
  11. * @property int $id
  12. * @property string $intro
  13. * @property integer $markdown
  14. */
  15. class Talent extends \yii\db\ActiveRecord
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public static function tableName()
  21. {
  22. return '{{%article_talent}}';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function rules()
  28. {
  29. return [
  30. [['company'], 'required'],
  31. [['company', 'position'], 'string', 'max' => 255],
  32. [['id', 'markdown'], 'integer'],
  33. ['markdown', 'default', 'value' => $this->getIsMarkdown()],
  34. [['intro'], 'string'],
  35. ];
  36. }
  37. /**
  38. * 没有指定markdown情况下默认编辑器是否为markdown
  39. * @return int
  40. */
  41. public function getIsMarkdown()
  42. {
  43. return \Yii::$app->config->get('article_editor_type') == 'markdown' ? 1 : 0;
  44. }
  45. public function getProcessedIntro()
  46. {
  47. return $this->markdown ? \yii\helpers\Markdown::process($this->intro) : $this->intro;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function attributeLabels()
  53. {
  54. return [
  55. 'id' => 'ID',
  56. 'intro' => '研究领域',
  57. 'company' => '单位',
  58. 'position' => '职务',
  59. // 'chain' => '产业链',
  60. 'markdown' => '是否markdown格式'
  61. ];
  62. }
  63. public function attributeHints()
  64. {
  65. return [
  66. 'intro' => '摘要不填时默认截取内容前150个字符'
  67. ];
  68. }
  69. public function behaviors()
  70. {
  71. $behaviors = [];
  72. if (\Yii::$app->config->get('SEARCH_ENGINE') == 'xunsearch') {
  73. $behaviors[] = XsBehavior::className();
  74. }
  75. $behaviors[] = [
  76. 'class' => DynamicFormBehavior::className(),
  77. 'formAttributes' => [
  78. 'company' => [
  79. 'type' => 'text'
  80. ],
  81. 'position' => [
  82. 'type' => 'text'
  83. ],
  84. // 'chain' => [
  85. // 'type' => 'text'
  86. // ],
  87. 'intro' => [
  88. 'type' => 'textarea',
  89. // 'options' => function ($model) {
  90. // return ['widgetOptions' => $model->isNewRecord ? ['type' => config('article_editor_type')] : ['isMarkdown' => $model->markdown]];
  91. // }
  92. ],
  93. ]
  94. ];
  95. return $behaviors;
  96. }
  97. // 发布新文章后(摘要为空的话根据内容生成摘要)
  98. public function afterSave($insert, $changedAttributes)
  99. {
  100. $article = Article::findOne(['id' => $this->id]);
  101. if (!empty($article)) {
  102. if (empty($article->description)) {
  103. $article->description = $this->generateDesc($this->getProcessedIntro());
  104. $article->save();
  105. }
  106. }
  107. }
  108. // 摘要生成方式
  109. private function generateDesc($intro)
  110. {
  111. if (empty($intro)) {
  112. return '';
  113. }
  114. return StringHelper::truncate(preg_replace('/\s+/', ' ', strip_tags($intro)), 150);
  115. }
  116. }