PedigreeCompany.php 3.0 KB

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