Completed
Push — master ( f5277c...2a14a8 )
by zacksleo
03:16
created

Post::getCategories()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace zacksleo\yii2\cms\models;
4
5
use Yii;
6
use yii\behaviors\TimestampBehavior;
7
use zacksleo\yii2\cms\Module;
8
use creocoder\taggable\TaggableBehavior;
9
use yii\helpers\Url;
10
use zacksleo\yii2\cms\models\queries\PostQuery;
11
12
/**
13
 * This is the model class for table "{{%news}}".
14
 *
15
 * @property integer $id
16
 * @property string $title
17
 * @property string $image
18
 * @property string $content
19
 * @property integer $active
20
 * @property string $source
21
 * @property integer $visits
22
 * @property integer $created_at
23
 * @property integer $updated_at
24
 * @property string $category_id
25
 * @property string $categories
26
 */
27
class Post extends \yii\db\ActiveRecord
28
{
29
    const STATUS_INACTIVE = 0;
30
    const STATUS_ACTIVE = 1;
31
32
    /**
33
     * @inheritdoc
34
     */
35 1
    public static function tableName()
36
    {
37 1
        return '{{%post}}';
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43 1
    public function rules()
44
    {
45
        return [
46 1
            [['title', 'content', 'active'], 'required'],
47 1
            [['content'], 'string'],
48 1
            [['active', 'visits', 'created_at', 'updated_at', 'category_id'], 'integer'],
49 1
            [['title', 'image', 'source', 'categories'], 'string', 'max' => 255],
50 1
            ['tagValues', 'safe'],
51 1
        ];
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function attributeLabels()
58
    {
59
        return [
60
            'id' => Module::t('cms', 'ID'),
61
            'title' => Module::t('cms', 'Title'),
62
            'image' => Module::t('cms', 'Image'),
63
            'content' => Module::t('cms', 'Content'),
64
            'active' => Module::t('cms', 'Active'),
65
            'source' => Module::t('cms', 'Source'),
66
            'visits' => Module::t('cms', 'Visits'),
67
            'category_id' => Module::t('cms', 'Category Id'),
68
            'categories' => Module::t('cms', 'Categories'),
69
            'created_at' => Module::t('cms', 'Created At'),
70
            'updated_at' => Module::t('cms', 'Updated At'),
71
            'tagValues' => '标签',
72
        ];
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78 1
    public function behaviors()
79
    {
80
        return [
81
            'timestamp' => [
82 1
                'class' => TimestampBehavior::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
83 1
            ],
84
            'fileBehavior' => [
85 1
                'class' => \nemmo\attachments\behaviors\FileBehavior::className()
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
86 1
            ],
87
            'taggable' => [
88 1
                'class' => TaggableBehavior::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
89
                // 'tagValuesAsArray' => false,
90
                // 'tagRelation' => 'tags',
91
                // 'tagValueAttribute' => 'name',
92
                // 'tagFrequencyAttribute' => 'frequency',
93 1
            ],
94 1
        ];
95
    }
96
97
    /**
98
     * @return array
99
     */
100
    public static function getStatusList()
101
    {
102
        return [
103
            self::STATUS_INACTIVE => Module::t('cms', 'InActive'),
104
            self::STATUS_ACTIVE => Module::t('cms', 'Active'),
105
        ];
106
    }
107
108
    public function getTags()
109
    {
110
        return $this->hasMany(Tag::className(), ['id' => 'tag_id'])
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
111
            ->viaTable('{{%post_tag_assn}}', ['post_id' => 'id']);
112
    }
113
114 1
    public function transactions()
115
    {
116
        return [
117 1
            self::SCENARIO_DEFAULT => self::OP_ALL,
118 1
        ];
119
    }
120
121
    public static function find()
122
    {
123
        return new PostQuery(get_called_class());
124
    }
125
126
    /**
127
     * @return PostCategory
128
     */
129
    public function getCategory()
130
    {
131
        $ids = explode(',', $this->categories);
132
        $category_id = array_shift($ids);
133
        return PostCategory::findOne($category_id);
0 ignored issues
show
Bug Compatibility introduced by
The expression \zacksleo\yii2\cms\model...:findOne($category_id); of type yii\db\ActiveRecordInterface|array|null adds the type array to the return on line 133 which is incompatible with the return type documented by zacksleo\yii2\cms\models\Post::getCategory of type zacksleo\yii2\cms\models\PostCategory|null.
Loading history...
134
    }
135
136
    /**
137
     * @return array
138
     */
139
    public function getCategories()
140
    {
141
        $ids = explode(',', $this->categories);
142
        $res = [];
143
        foreach ($ids as $id) {
144
            $res[] = PostCategory::findOne($id);
145
        }
146
        return $res;
147
    }
148
149
    public function getCategoriesName()
150
    {
151
        $categories = $this->getCategories();
152
        $labels = [];
153
        foreach ($categories as $category) {
154
            $labels[] = $category->name;
155
        }
156
        return implode(',', $labels);
157
    }
158
159
    public function getUrl()
160
    {
161
        return Url::to(['post/view', 'id' => $this->id]);
162
    }
163
164 1
    public function beforeSave($insert)
165
    {
166 1
        $this->categories = ',' . trim($this->categories, ',') . ',';
167 1
        return parent::beforeSave($insert);
168
    }
169
170
    public function afterFind()
171
    {
172
        $this->categories = trim($this->categories, ',');
173
        parent::afterFind();
174
    }
175
}
176