SurveyNew.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_new}}".
  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 SurveyNew extends \yii\db\ActiveRecord
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function tableName()
  23. {
  24. return '{{%survey_new}}';
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function rules()
  30. {
  31. return [
  32. [['content', 'title'], 'required'],
  33. [['content'], 'string'],
  34. [['filter'], 'string'],
  35. [['cengci'],'string'],
  36. ['markdown', 'default', 'value' => $this->getIsMarkdown()],
  37. [['use_layout'], 'in', 'range' => [0, 1]],
  38. [['title'], '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. ];
  73. }
  74. public function attributeHints()
  75. {
  76. return [
  77. 'name' => '(影响url)'
  78. ];
  79. }
  80. public function behaviors()
  81. {
  82. return [
  83. TimestampBehavior::className(),
  84. [
  85. 'class' => MetaBehavior::className(),
  86. ],
  87. [
  88. 'class' => CommentBehavior::className()
  89. ]
  90. ];
  91. }
  92. public function getMetaData()
  93. {
  94. $model = $this->getMetaModel();
  95. $title = $model->title ? : $this->title;
  96. $description = $model->description ? : StringHelper::truncate(strip_tags($this->content), 150);
  97. return [$title, $description, $model->keywords];
  98. }
  99. }