Page.php 2.7 KB

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