Policy.php 3.4 KB

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