Policy.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace common\models;
  3. use common\behaviors\CommentBehavior;
  4. use common\behaviors\MetaBehavior;
  5. use yii\behaviors\TimestampBehavior;
  6. use yii\helpers\HtmlPurifier;
  7. use yii\helpers\StringHelper;
  8. /**
  9. * This is the model class for table "{{%policy}}".
  10. *
  11. * @property int $id
  12. * @property int $use_layout
  13. * @property string $content
  14. * @property string $title
  15. * @property string $summary
  16. * @property string $cengci
  17. * @property integer $markdown
  18. * @property string $type
  19. * @property string $estate
  20. */
  21. class Policy extends \yii\db\ActiveRecord
  22. {
  23. const TYPE_SURVEY = 'survey';//政策匹配
  24. const TYPE_SEARCH = 'search';//政策查询
  25. public static function getTypeList()
  26. {
  27. return [
  28. self::TYPE_SURVEY => '匹配(仅显示在匹配列表中)',
  29. self::TYPE_SEARCH => '查询(仅供政策搜索查询)',
  30. ];
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public static function tableName()
  36. {
  37. return '{{%policy}}';
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function rules()
  43. {
  44. return [
  45. [['content', 'title', 'summary', 'type','cengci', 'estate'], 'required'],
  46. [['content'], 'string'],
  47. [['filter','cengci'], 'string'],
  48. ['markdown', 'default', 'value' => $this->getIsMarkdown()],
  49. [['use_layout'], 'in', 'range' => [0, 1]],
  50. [['title'], 'string', 'max' => 50],
  51. [['author'], 'string', 'max' => 50],
  52. ['content', 'filterHtml']
  53. ];
  54. }
  55. public function filterHtml($attribute)
  56. {
  57. $this->$attribute = HtmlPurifier::process($this->$attribute, function ($config) {
  58. $config->set('HTML.Allowed', 'p[style],a[href|title],img[src|title|alt|class],h3,ul,ol,li,span,b,strong,hr,code,table,tbody,tr,td');
  59. // 清除空标签
  60. $config->set('AutoFormat.RemoveEmpty', true);
  61. });
  62. }
  63. /**
  64. * 没有指定markdown情况下默认编辑器是否为markdown
  65. * @return int
  66. */
  67. public function getIsMarkdown()
  68. {
  69. return \Yii::$app->config->get('article_editor_type') == 'markdown' ? 1 : 0;
  70. }
  71. public function getProcessedContent()
  72. {
  73. return $this->markdown ? \yii\helpers\Markdown::process($this->content) : $this->content;
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function attributeLabels()
  79. {
  80. return [
  81. 'id' => 'ID',
  82. 'use_layout' => '是否使用布局',
  83. 'summary' => '摘要',
  84. 'content' => '详情',
  85. 'title' => '标题',
  86. 'author'=>'作者',
  87. 'type'=>'类型',//类型:0 匹配,1 查询
  88. 'estate'=>'产业',
  89. 'created_at'=>'发布时间'
  90. ];
  91. }
  92. public function attributeHints()
  93. {
  94. return [
  95. 'name' => '(影响url)'
  96. ];
  97. }
  98. public function behaviors()
  99. {
  100. return [
  101. TimestampBehavior::className(),
  102. [
  103. 'class' => MetaBehavior::className(),
  104. ],
  105. [
  106. 'class' => CommentBehavior::className()
  107. ]
  108. ];
  109. }
  110. public function getMetaData()
  111. {
  112. $model = $this->getMetaModel();
  113. $title = $model->title ? : $this->title;
  114. $author = $model->author ? : $this->author;
  115. $description = $model->description ? : StringHelper::truncate(strip_tags($this->content), 150);
  116. return [$title, $author, $description, $model->keywords];
  117. }
  118. }