Policy.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. 'cengci'=>'层次',
  89. 'estate'=>'产业',
  90. 'created_at'=>'发布时间'
  91. ];
  92. }
  93. public function attributeHints()
  94. {
  95. return [
  96. 'name' => '(影响url)'
  97. ];
  98. }
  99. public function behaviors()
  100. {
  101. return [
  102. TimestampBehavior::className(),
  103. [
  104. 'class' => MetaBehavior::className(),
  105. ],
  106. [
  107. 'class' => CommentBehavior::className()
  108. ]
  109. ];
  110. }
  111. public function getMetaData()
  112. {
  113. $model = $this->getMetaModel();
  114. $title = $model->title ? : $this->title;
  115. $author = $model->author ? : $this->author;
  116. $description = $model->description ? : StringHelper::truncate(strip_tags($this->content), 150);
  117. return [$title, $author, $description, $model->keywords];
  118. }
  119. public function getTypeText()
  120. {
  121. if (!empty($this->type)) {
  122. $keys = explode(',', $this->type);
  123. $list = self::getTypeList();
  124. if (count($keys) == 2) {
  125. return $list[$keys[0]] . ',' . $list[$keys[1]];
  126. }
  127. return $list[$keys[0]];
  128. }
  129. return '';
  130. }
  131. }