Policy.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 integer $markdown
  16. */
  17. class Policy extends \yii\db\ActiveRecord
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function tableName()
  23. {
  24. return '{{%policy}}';
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function rules()
  30. {
  31. return [
  32. [['content', 'title'], 'required'],
  33. [['content'], 'string'],
  34. [['filter'], 'string'],
  35. ['markdown', 'default', 'value' => $this->getIsMarkdown()],
  36. [['use_layout'], 'in', 'range' => [0, 1]],
  37. [['title'], 'string', 'max' => 50],
  38. [['author'], 'string', 'max' => 50],
  39. ['content', 'filterHtml']
  40. ];
  41. }
  42. public function filterHtml($attribute)
  43. {
  44. $this->$attribute = HtmlPurifier::process($this->$attribute, function ($config) {
  45. $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');
  46. // 清除空标签
  47. $config->set('AutoFormat.RemoveEmpty', true);
  48. });
  49. }
  50. /**
  51. * 没有指定markdown情况下默认编辑器是否为markdown
  52. * @return int
  53. */
  54. public function getIsMarkdown()
  55. {
  56. return \Yii::$app->config->get('article_editor_type') == 'markdown' ? 1 : 0;
  57. }
  58. public function getProcessedContent()
  59. {
  60. return $this->markdown ? \yii\helpers\Markdown::process($this->content) : $this->content;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function attributeLabels()
  66. {
  67. return [
  68. 'id' => 'ID',
  69. 'use_layout' => '是否使用布局',
  70. 'content' => '内容',
  71. 'title' => '标题',
  72. 'author'=>'作者',
  73. 'created_at'=>'发布时间'
  74. ];
  75. }
  76. public function attributeHints()
  77. {
  78. return [
  79. 'name' => '(影响url)'
  80. ];
  81. }
  82. public function behaviors()
  83. {
  84. return [
  85. TimestampBehavior::className(),
  86. [
  87. 'class' => MetaBehavior::className(),
  88. ],
  89. [
  90. 'class' => CommentBehavior::className()
  91. ]
  92. ];
  93. }
  94. public function getMetaData()
  95. {
  96. $model = $this->getMetaModel();
  97. $title = $model->title ? : $this->title;
  98. $author = $model->author ? : $this->author;
  99. $description = $model->description ? : StringHelper::truncate(strip_tags($this->content), 150);
  100. return [$title, $author, $description, $model->keywords];
  101. }
  102. }