Survey.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 "{{%survey}}".
  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 Survey extends \yii\db\ActiveRecord
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function tableName()
  23. {
  24. return '{{%survey}}';
  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. ['content', 'filterHtml']
  39. ];
  40. }
  41. public function filterHtml($attribute)
  42. {
  43. $this->$attribute = HtmlPurifier::process($this->$attribute, function ($config) {
  44. $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');
  45. // 清除空标签
  46. $config->set('AutoFormat.RemoveEmpty', true);
  47. });
  48. }
  49. /**
  50. * 没有指定markdown情况下默认编辑器是否为markdown
  51. * @return int
  52. */
  53. public function getIsMarkdown()
  54. {
  55. return \Yii::$app->config->get('article_editor_type') == 'markdown' ? 1 : 0;
  56. }
  57. public function getProcessedContent()
  58. {
  59. return $this->markdown ? \yii\helpers\Markdown::process($this->content) : $this->content;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function attributeLabels()
  65. {
  66. return [
  67. 'id' => 'ID',
  68. 'use_layout' => '是否使用布局',
  69. 'content' => '内容',
  70. 'title' => '标题'
  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. }