Policy.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace common\models;
  3. use common\behaviors\CommentBehavior;
  4. use common\behaviors\MetaBehavior;
  5. use common\behaviors\PositionBehavior;
  6. use yii\behaviors\TimestampBehavior;
  7. use yii\helpers\HtmlPurifier;
  8. use yii\helpers\StringHelper;
  9. /**
  10. * This is the model class for table "{{%policy}}".
  11. *
  12. * @property int $id
  13. * @property int $use_layout
  14. * @property string $content
  15. * @property string $title
  16. * @property string $summary
  17. * @property string $cengci
  18. * @property integer $markdown
  19. * @property string $type
  20. * @property string $estate
  21. * @property int $sort
  22. */
  23. class Policy extends \yii\db\ActiveRecord
  24. {
  25. const TYPE_SURVEY = 'survey';//政策匹配
  26. const TYPE_SEARCH = 'search';//政策查询
  27. public static function getTypeList()
  28. {
  29. return [
  30. self::TYPE_SURVEY => '匹配(人才匹配)',
  31. self::TYPE_SEARCH => '查询(政策查询)',
  32. ];
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static function tableName()
  38. {
  39. return '{{%policy}}';
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function rules()
  45. {
  46. return [
  47. [['content', 'title', 'summary', 'type', 'cengci', 'estate'], 'required'],
  48. [['sort'], 'integer'],
  49. [['content', 'filter', 'cengci'], 'string'],
  50. ['markdown', 'default', 'value' => $this->getIsMarkdown()],
  51. [['use_layout'], 'in', 'range' => [0, 1]],
  52. [['sort'], 'default', 'value' => 0],
  53. [['title'], 'string', 'max' => 50],
  54. [['author'], 'string', 'max' => 50],
  55. // ['content', 'filterHtml']
  56. ];
  57. }
  58. public function filterHtml($attribute)
  59. {
  60. $this->$attribute = HtmlPurifier::process($this->$attribute, function ($config) {
  61. $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');
  62. // 清除空标签
  63. $config->set('AutoFormat.RemoveEmpty', true);
  64. });
  65. }
  66. /**
  67. * 没有指定markdown情况下默认编辑器是否为markdown
  68. * @return int
  69. */
  70. public function getIsMarkdown()
  71. {
  72. return \Yii::$app->config->get('article_editor_type') == 'markdown' ? 1 : 0;
  73. }
  74. public function getProcessedContent()
  75. {
  76. return $this->markdown ? \yii\helpers\Markdown::process($this->content) : $this->content;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function attributeLabels()
  82. {
  83. return [
  84. 'id' => 'ID',
  85. 'use_layout' => '是否使用布局',
  86. 'summary' => '摘要',
  87. 'content' => '详情',
  88. 'title' => '标题',
  89. 'author' => '作者',
  90. 'type' => '类型',//类型:0 匹配,1 查询
  91. 'cengci' => '层次',
  92. 'estate' => '产业',
  93. 'created_at' => '发布时间',
  94. 'sort' => '排序',
  95. ];
  96. }
  97. public function attributeHints()
  98. {
  99. return [
  100. 'name' => '(影响url)'
  101. ];
  102. }
  103. public function behaviors()
  104. {
  105. return [
  106. TimestampBehavior::className(),
  107. [
  108. 'class' => MetaBehavior::className(),
  109. ],
  110. [
  111. 'class' => CommentBehavior::className()
  112. ],
  113. 'positionBehavior' => [
  114. 'class' => PositionBehavior::className(),
  115. 'positionAttribute' => 'sort',
  116. 'groupAttributes' => [
  117. 'estate'
  118. ],
  119. ],
  120. ];
  121. }
  122. public function getMetaData()
  123. {
  124. $model = $this->getMetaModel();
  125. $title = $model->title ?: $this->title;
  126. $author = $model->author ?: $this->author;
  127. $description = $model->description ?: StringHelper::truncate(strip_tags($this->content), 150);
  128. return [$title, $author, $description, $model->keywords];
  129. }
  130. public function getTypeText()
  131. {
  132. if (!empty($this->type)) {
  133. $keys = explode(',', $this->type);
  134. $list = self::getTypeList();
  135. if (count($keys) == 2) {
  136. return $list[$keys[0]] . ',' . $list[$keys[1]];
  137. }
  138. return $list[$keys[0]];
  139. }
  140. return '';
  141. }
  142. }