Firm.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 Firm extends \yii\db\ActiveRecord
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public static function tableName()
  21. {
  22. return '{{%article_firm}}';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function rules()
  28. {
  29. return [
  30. [['id', 'markdown'], 'integer'],
  31. ['markdown', 'default', 'value' => $this->getIsMarkdown()],
  32. [['intro'], 'string'],
  33. ];
  34. }
  35. /**
  36. * 没有指定markdown情况下默认编辑器是否为markdown
  37. * @return int
  38. */
  39. public function getIsMarkdown()
  40. {
  41. return \Yii::$app->config->get('article_editor_type') == 'markdown' ? 1 : 0;
  42. }
  43. public function getProcessedIntro()
  44. {
  45. return $this->markdown ? \yii\helpers\Markdown::process($this->intro) : $this->intro;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => 'ID',
  54. 'intro' => '简介',
  55. 'principal' => '秘书长',
  56. 'markdown' => '是否markdown格式'
  57. ];
  58. }
  59. public function attributeHints()
  60. {
  61. return [
  62. 'intro' => '摘要不填时默认截取内容前150个字符'
  63. ];
  64. }
  65. public function behaviors()
  66. {
  67. $behaviors = [];
  68. if (\Yii::$app->config->get('SEARCH_ENGINE') == 'xunsearch') {
  69. $behaviors[] = XsBehavior::className();
  70. }
  71. $behaviors[] = [
  72. 'class' => DynamicFormBehavior::className(),
  73. 'formAttributes' => [
  74. 'principal' => [
  75. 'type' => 'text'
  76. ],
  77. 'intro' => [
  78. 'type' => 'textarea',
  79. // 'options' => function ($model) {
  80. // return ['widgetOptions' => $model->isNewRecord ? ['type' => config('article_editor_type')] : ['isMarkdown' => $model->markdown]];
  81. // }
  82. ],
  83. ]
  84. ];
  85. return $behaviors;
  86. }
  87. // 发布新文章后(摘要为空的话根据内容生成摘要)
  88. public function afterSave($insert, $changedAttributes)
  89. {
  90. $article = Article::findOne(['id' => $this->id]);
  91. if (!empty($article)) {
  92. if (empty($article->description)) {
  93. $article->description = $this->generateDesc($this->getProcessedIntro());
  94. $article->save();
  95. }
  96. }
  97. }
  98. // 摘要生成方式
  99. private function generateDesc($intro)
  100. {
  101. if (empty($intro)) {
  102. return '';
  103. }
  104. return StringHelper::truncate(preg_replace('/\s+/', ' ', strip_tags($intro)), 150);
  105. }
  106. }