123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace common\models;
- use common\behaviors\CommentBehavior;
- use common\behaviors\MetaBehavior;
- use common\behaviors\PositionBehavior;
- use yii\behaviors\TimestampBehavior;
- use yii\helpers\HtmlPurifier;
- use yii\helpers\StringHelper;
- /**
- * This is the model class for table "{{%policy}}".
- *
- * @property int $id
- * @property int $use_layout
- * @property string $content
- * @property string $title
- * @property string $summary
- * @property string $cengci
- * @property integer $markdown
- * @property string $type
- * @property string $estate
- * @property int $sort
- */
- class Policy extends \yii\db\ActiveRecord
- {
- const TYPE_SURVEY = 'survey';//政策匹配
- const TYPE_SEARCH = 'search';//政策查询
- public static function getTypeList()
- {
- return [
- self::TYPE_SURVEY => '匹配(人才匹配)',
- self::TYPE_SEARCH => '查询(政策查询)',
- ];
- }
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%policy}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['content', 'title', 'summary', 'type', 'cengci', 'estate'], 'required'],
- [['sort'], 'integer'],
- [['content', 'filter', 'cengci'], 'string'],
- ['markdown', 'default', 'value' => $this->getIsMarkdown()],
- [['use_layout'], 'in', 'range' => [0, 1]],
- [['sort'], 'default', 'value' => 0],
- [['title'], 'string', 'max' => 50],
- [['author'], 'string', 'max' => 50],
- // ['content', 'filterHtml']
- ];
- }
- public function filterHtml($attribute)
- {
- $this->$attribute = HtmlPurifier::process($this->$attribute, function ($config) {
- $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');
- // 清除空标签
- $config->set('AutoFormat.RemoveEmpty', true);
- });
- }
- /**
- * 没有指定markdown情况下默认编辑器是否为markdown
- * @return int
- */
- public function getIsMarkdown()
- {
- return \Yii::$app->config->get('article_editor_type') == 'markdown' ? 1 : 0;
- }
- public function getProcessedContent()
- {
- return $this->markdown ? \yii\helpers\Markdown::process($this->content) : $this->content;
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'use_layout' => '是否使用布局',
- 'summary' => '摘要',
- 'content' => '详情',
- 'title' => '标题',
- 'author' => '作者',
- 'type' => '类型',//类型:0 匹配,1 查询
- 'cengci' => '层次',
- 'estate' => '产业',
- 'created_at' => '发布时间',
- 'sort' => '排序',
- ];
- }
- public function attributeHints()
- {
- return [
- 'name' => '(影响url)'
- ];
- }
- public function behaviors()
- {
- return [
- TimestampBehavior::className(),
- [
- 'class' => MetaBehavior::className(),
- ],
- [
- 'class' => CommentBehavior::className()
- ],
- 'positionBehavior' => [
- 'class' => PositionBehavior::className(),
- 'positionAttribute' => 'sort',
- 'groupAttributes' => [
- 'estate'
- ],
- ],
- ];
- }
- public function getMetaData()
- {
- $model = $this->getMetaModel();
- $title = $model->title ?: $this->title;
- $author = $model->author ?: $this->author;
- $description = $model->description ?: StringHelper::truncate(strip_tags($this->content), 150);
- return [$title, $author, $description, $model->keywords];
- }
- public function getTypeText()
- {
- if (!empty($this->type)) {
- $keys = explode(',', $this->type);
- $list = self::getTypeList();
- if (count($keys) == 2) {
- return $list[$keys[0]] . ',' . $list[$keys[1]];
- }
- return $list[$keys[0]];
- }
- return '';
- }
- }
|