| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- <?php
- /**
- * Author: lf
- * Blog: https://blog.feehi.com
- * Email: job@feehi.com
- * Created at: 2016-10-16 17:15
- */
- namespace common\models;
- use Yii;
- use common\helpers\Util;
- use common\models\meta\ArticleMetaImages;
- use common\models\meta\ArticleMetaLike;
- use common\models\meta\ArticleMetaTag;
- use feehi\cdn\TargetAbstract;
- use common\libs\Constants;
- use yii\behaviors\TimestampBehavior;
- use yii\web\UploadedFile;
- /**
- * This is the model class for table "{{%article}}".
- *
- * @property integer $id
- * @property integer $cid
- * @property integer $type
- * @property string $title
- * @property string $sub_title
- * @property string $summary
- * @property string $thumb
- * @property string $seo_title
- * @property string $seo_keywords
- * @property string $seo_description
- * @property integer $status
- * @property integer $sort
- * @property integer $author_id
- * @property string $author_name
- * @property integer $scan_count
- * @property integer $comment_count
- * @property integer $can_comment
- * @property integer $visibility
- * @property string $password
- * @property integer $flag_headline
- * @property integer $flag_recommend
- * @property integer $flag_slide_show
- * @property integer $flag_special_recommend
- * @property integer $flag_roll
- * @property integer $flag_bold
- * @property integer $flag_picture
- * @property string $template
- * @property integer $created_at
- * @property integer $updated_at
- *
- * @property ArticleContent $articleContent
- * @property Category $category
- */
- class Article extends \yii\db\ActiveRecord
- {
- const ARTICLE = 0;
- const SINGLE_PAGE = 2;
- const ARTICLE_PUBLISHED = 1;
- const ARTICLE_DRAFT = 0;
- /**
- * @var string
- */
- public $tag = '';
- /**
- * 需要截取的文章缩略图尺寸
- */
- public static $thumbSizes = [
- ["w"=>220, "h"=>150],//首页文章列表
- ["w"=>168, "h"=>112],//精选导读
- ["w"=>185, "h"=>110],//文章详情下边图片推荐
- ["w"=>125, "h"=>86],//热门推荐
- ];
- /**
- * @var array
- */
- public $images;
- public function behaviors()
- {
- return [
- TimestampBehavior::className(),
- ];
- }
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%article}}';
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['cid', 'type', 'status', 'sort', 'author_id', 'can_comment', 'visibility'], 'integer'],
- [['cid', 'sort', 'author_id'], 'compare', 'compareValue' => 0, 'operator' => '>='],
- [['title', 'status'], 'required'],
- [['can_comment'], 'default', 'value' => Constants::YesNo_Yes],
- [['visibility'], 'default', 'value' => Constants::ARTICLE_VISIBILITY_PUBLIC],
- [['thumb'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg, jpeg, gif, webp'],
- [['images'], 'safe'],
- [['created_at', 'updated_at'], 'safe'],
- [
- [
- 'title',
- 'sub_title',
- 'summary',
- 'seo_title',
- 'seo_keywords',
- 'seo_description',
- 'author_name',
- 'tag',
- 'template'
- ],
- 'string',
- 'max' => 255
- ],
- [
- [
- 'flag_headline',
- 'flag_recommend',
- 'flag_slide_show',
- 'flag_special_recommend',
- 'flag_roll',
- 'flag_bold',
- 'flag_picture',
- 'status',
- 'can_comment'
- ],
- 'in',
- 'range' => [0, 1]
- ],
- [['visibility'], 'in', 'range' => array_keys(Constants::getArticleVisibility())],
- [['type'], 'default', 'value'=>self::ARTICLE, 'on'=>'article'],
- [['type'], 'default', 'value'=>self::SINGLE_PAGE, 'on'=>'page'],
- [['password'], 'string', 'max'=>20],
- ['cid', 'default', 'value'=>0]
- ];
- }
- /**
- * @inheritdoc
- */
- public function scenarios()
- {
- return [
- 'article' => [
- 'cid',
- 'type',
- 'title',
- 'sub_title',
- 'summary',
- 'thumb',
- 'seo_title',
- 'seo_keywords',
- 'seo_description',
- 'status',
- 'sort',
- 'author_id',
- 'author_name',
- 'created_at',
- 'updated_at',
- 'scan_count',
- 'comment_count',
- 'can_comment',
- 'visibility',
- 'tag',
- 'flag_headline',
- 'flag_recommend',
- 'flag_slide_show',
- 'flag_special_recommend',
- 'flag_roll',
- 'flag_bold',
- 'flag_picture',
- 'password',
- 'images',
- 'template'
- ],
- 'page' => [
- 'type',
- 'title',
- 'sub_title',
- 'summary',
- 'seo_title',
- 'seo_keywords',
- 'seo_description',
- 'status',
- 'can_comment',
- 'visibility',
- 'tag',
- 'sort',
- 'images',
- 'template'
- ],
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => Yii::t('app', 'ID'),
- 'cid' => Yii::t('app', 'Category Id'),
- 'type' => Yii::t('app', 'Type'),
- 'title' => Yii::t('app', 'Title'),
- 'sub_title' => Yii::t('app', 'Sub Title'),
- 'summary' => Yii::t('app', 'Summary'),
- 'thumb' => Yii::t('app', 'Thumb'),
- 'seo_title' => Yii::t('app', 'Seo Title'),
- 'seo_keywords' => Yii::t('app', 'Seo Keyword'),
- 'seo_description' => Yii::t('app', 'Seo Description'),
- 'status' => Yii::t('app', 'Status'),
- 'can_comment' => Yii::t('app', 'Can Comment'),
- 'visibility' => Yii::t('app', 'Visibility'),
- 'sort' => Yii::t('app', 'Sort'),
- 'tag' => Yii::t('app', 'Tag'),
- 'author_id' => Yii::t('app', 'Author Id'),
- 'author_name' => Yii::t('app', 'Author'),
- 'created_at' => Yii::t('app', 'Created At'),
- 'updated_at' => Yii::t('app', 'Updated At'),
- 'flag_headline' => Yii::t('app', 'Is Headline'),
- 'flag_recommend' => Yii::t('app', 'Is Recommend'),
- 'flag_special_recommend' => Yii::t('app', 'Is Special Recommend'),
- 'flag_slide_show' => Yii::t('app', 'Is Slide Show'),
- 'flag_roll' => Yii::t('app', 'Is Roll'),
- 'flag_bold' => Yii::t('app', 'Is Bold'),
- 'flag_picture' => Yii::t('app', 'Is Picture'),
- 'template' => Yii::t('app', 'Article Template'),
- 'password' => Yii::t('app', 'Password'),
- 'scan_count' => Yii::t('app', 'Scan Count'),
- 'comment_count' => Yii::t('app', 'Comment Count'),
- 'category' => Yii::t('app', 'Category'),
- 'images' => Yii::t('app', 'Article Images'),
- ];
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getCategory()
- {
- return $this->hasOne(Category::className(), ['id' => 'cid']);
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getArticleContent()
- {
- return $this->hasOne(ArticleContent::className(), ['aid' => 'id']);
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getArticleLikes()
- {
- $tempModel = new ArticleMetaLike();
- return $this->hasMany(ArticleMetaLike::className(), ['aid' => 'id'])->where(['key'=>$tempModel->keyName]);
- }
- public function getArticleTags()
- {
- $tempModel = new ArticleMetaTag();
- return $this->hasMany(ArticleMetaLike::className(), ['aid' => 'id'])->where(['key'=>$tempModel->keyName]);
- }
- /**
- * @inheritdoc
- */
- public function afterValidate()
- {
- if($this->visibility == Constants::ARTICLE_VISIBILITY_SECRET){//加密文章需要设置密码
- if( empty( $this->password ) ){
- $this->addError('password', Yii::t('app', "Secret article must set a password"));
- }
- }
- parent::afterValidate();
- }
- /**
- * @inheritdoc
- */
- public function beforeSave($insert)
- {
- $insert = $this->getIsNewRecord();
- Util::handleModelSingleFileUpload($this, 'thumb', $insert, '@thumb', ['thumbSizes'=>self::$thumbSizes]);
- $this->seo_keywords = str_replace(',', ',', $this->seo_keywords);
- if ($insert) {
- $this->author_id = Yii::$app->getUser()->getIdentity()->getId();
- $this->author_name = Yii::$app->getUser()->getIdentity()->username;
- }
- $this->type = self::ARTICLE;
- if( $this->getScenario() === 'page' ){
- $this->type = self::SINGLE_PAGE;
- }
- if ($this->thumb) {
- /** @var TargetAbstract $cdn */
- $cdn = Yii::$app->get('cdn');
- $this->thumb = str_replace($cdn->host, '', $this->thumb);
- }
- return parent::beforeSave($insert);
- }
- /**
- * @inheritdoc
- */
- public function afterSave($insert, $changedAttributes)
- {
- $articleMetaTag = new ArticleMetaTag();
- $articleMetaTag->setArticleTags($this->id, $this->tag);
- $articleMetaTag = new ArticleMetaImages();
- $articleMetaTag->setImages($this->id, $this->images);
- parent::afterSave($insert, $changedAttributes);
- }
- /**
- * @inheritdoc
- */
- public function beforeDelete()
- {
- if( !empty( $this->thumb ) ){
- Util::deleteThumbnails(Yii::getAlias('@frontend/web') . $this->thumb, self::$thumbSizes, true);
- }
- Comment::deleteAll(['aid' => $this->id]);
- return parent::beforeDelete();
- }
- /**
- * @inheritdoc
- */
- public function afterFind()
- {
- if ($this->thumb) {
- /** @var TargetAbstract $cdn */
- $cdn = Yii::$app->get('cdn');
- $this->thumb = $cdn->getCdnUrl($this->thumb);
- }
- $articleMetaImagesModel = new ArticleMetaImages();
- $this->images = $articleMetaImagesModel->getImagesByArticle($this->id);
- parent::afterFind();
- }
- /**
- * @return integer
- */
- public function getArticleLikeCount()
- {
- return $this->getArticleLikes()->count('id');
- }
- public function beforeValidate()
- {
- if ($this->thumb !== "0") {//为0表示需要删除图片,Util::handleModelSingleFileUpload()会有判断删除图片
- $this->thumb = UploadedFile::getInstance($this, "thumb");
- }
- return parent::beforeValidate();
- }
- public function getThumbUrlBySize($width='', $height='')
- {
- if( empty($width) || empty($height) ){
- return $this->thumb;
- }
- if( empty($this->thumb) ){//未配图
- return $this->thumb = '/static/images/' . rand(1, 10) . '.jpg';
- }
- static $str = null;
- if( $str === null ) {
- $str = "";
- foreach (self::$thumbSizes as $temp){
- $str .= $temp['w'] . 'x' . $temp['h'] . '---';
- }
- }
- if( strpos($str, $width . 'x' . $height) !== false ){
- $dotPosition = strrpos($this->thumb, '.');
- $thumbExt = "@" . $width . 'x' . $height;
- if( $dotPosition === false ){
- return $this->thumb . $thumbExt;
- }else{
- return substr_replace($this->thumb,$thumbExt, $dotPosition, 0);
- }
- }
- return Yii::$app->getRequest()->getBaseUrl() . '/timthumb.php?' . http_build_query(['src'=>$this->thumb, 'h'=>$height, 'w'=>$width, 'zc'=>0]);
- }
-
- }
|