Completed
Push — master ( 9d0cba...630902 )
by Paweł
02:59
created

Account::getAccountNotes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use yii\behaviors\AttributeBehavior;
7
use yii\behaviors\TimestampBehavior;
8
use yii\db\BaseActiveRecord;
9
use yii\db\Expression;
10
use yii\helpers\ArrayHelper;
11
12
/**
13
 * This is the model class for table "account".
14
 *
15
 * @property int $id
16
 * @property string $uid
17
 * @property string $name
18
 * @property string $username
19
 * @property string $profile_pic_url
20
 * @property string $full_name
21
 * @property string $biography
22
 * @property string $external_url
23
 * @property string $instagram_id
24
 * @property string $updated_at
25
 * @property string $created_at
26
 * @property bool $monitoring
27
 * @property int $proxy_id
28
 * @property int $proxy_tag_id
29
 * @property string $notes
30
 * @property bool $disabled
31
 * @property int $accounts_monitoring_level
32
 * @property string $accounts_default_tags
33
 * @property bool $is_private
34
 *
35
 * @property string $usernamePrefixed
36
 * @property string $displayName
37
 *
38
 * @property AccountStats $lastAccountStats
39
 *
40
 * @property Proxy $proxy
41
 * @property Tag $proxyTag
42
 * @property AccountNote[] $accountNotes
43
 * @property AccountStats[] $accountStats
44
 * @property AccountTag[] $accountTags
45
 * @property Tag[] $tags
46
 * @property Media[] $media
47
 * @property MediaAccount[] $mediaAccounts
48
 * @property \app\models\Account[] $accounts
49
 */
50
class Account extends \yii\db\ActiveRecord
51
{
52
    public $occurs;
53
54
    public static function usedTags()
55
    {
56
        return Tag::find()
57
            ->distinct()
58
            ->innerJoin('account_tag', 'tag.id=account_tag.tag_id')
59
            ->orderBy('tag.slug ASC')
60
            ->all();
61
    }
62
63
    public function behaviors()
64
    {
65
        return ArrayHelper::merge(parent::behaviors(), [
66
            'time' => TimestampBehavior::class,
67
            'uid' => [
68
                'class' => AttributeBehavior::class,
69
                'attributes' => [
70
                    BaseActiveRecord::EVENT_BEFORE_INSERT => ['uid'],
71
                    BaseActiveRecord::EVENT_BEFORE_UPDATE => ['uid'],
72
                ],
73
                'preserveNonEmptyValues' => true,
74
                'value' => function() {
75
                    do {
76
                        $uid = Yii::$app->security->generateRandomString(64);
77
                        $uidExist = static::find()
78
                            ->andWhere(['account.uid' => $uid])
79
                            ->exists();
80
                    } while ($uidExist);
81
82
                    return $uid;
83
                },
84
            ],
85
        ]);
86
    }
87
88
    public function getDisplayName()
89
    {
90
        return $this->name ?: $this->getUsernamePrefixed();
91
    }
92
93
    public function getUsernamePrefixed()
94
    {
95
        return "@{$this->username}";
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101
    public static function tableName()
102
    {
103
        return 'account';
104
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109
    public function rules()
110
    {
111
        return [
112
            [['username'], 'required'],
113
            [['updated_at', 'created_at', 'accounts_default_tags'], 'safe'],
114
            [['proxy_id', 'proxy_tag_id', 'occurs'], 'integer'],
115
            ['accounts_monitoring_level', 'integer', 'min' => 0],
116
            [['name', 'username', 'profile_pic_url', 'full_name', 'biography', 'external_url', 'instagram_id', 'notes', 'uid'], 'string', 'max' => 255],
117
            [['monitoring', 'disabled', 'is_private'], 'boolean'],
118
            [['username'], 'unique'],
119
            [['proxy_id'], 'exist', 'skipOnError' => true, 'targetClass' => Proxy::class, 'targetAttribute' => ['proxy_id' => 'id']],
120
            [['proxy_tag_id'], 'exist', 'skipOnError' => true, 'targetClass' => Tag::class, 'targetAttribute' => ['proxy_tag_id' => 'id']],
121
        ];
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127
    public function attributeLabels()
128
    {
129
        return [
130
            'id' => 'ID',
131
            'name' => 'Name',
132
            'username' => 'Username',
133
            'profile_pic_url' => 'Profile Pic Url',
134
            'full_name' => 'Full Name',
135
            'biography' => 'Biography',
136
            'external_url' => 'External Url',
137
            'instagram_id' => 'Instagram ID',
138
            'updated_at' => 'Updated At',
139
            'created_at' => 'Created At',
140
            'monitoring' => 'Monitoring',
141
            'proxy_id' => 'Proxy ID',
142
            'proxy_tag_id' => 'Proxy Tag ID',
143
            'notes' => 'Notes',
144
            'accounts_monitoring_level' => 'Accounts Monitoring Level',
145
        ];
146
    }
147
148
    public function attributeHints()
149
    {
150
        return [
151
            'name' => 'The name displayed in the lists, if empty, the \'username\' will be used.',
152
            'accounts_monitoring_level' => 'Automatically monitors discovered accounts. Be careful.',
153
            'accounts_default_tags' => 'Automatically tag discovered accounts. If not set, parent tags will be used.',
154
        ];
155
    }
156
157
    /**
158
     * @return \yii\db\ActiveQuery
159
     */
160
    public function getProxy()
161
    {
162
        return $this->hasOne(Proxy::class, ['id' => 'proxy_id']);
163
    }
164
165
    /**
166
     * @return \yii\db\ActiveQuery
167
     */
168
    public function getProxyTag()
169
    {
170
        return $this->hasOne(Tag::class, ['id' => 'proxy_tag_id']);
171
    }
172
173
    /**
174
     * @return \yii\db\ActiveQuery
175
     */
176
    public function getAccountNotes()
177
    {
178
        return $this->hasMany(AccountNote::class, ['account_id' => 'id']);
179
    }
180
181
    /**
182
     * @return \yii\db\ActiveQuery
183
     */
184
    public function getAccountStats()
185
    {
186
        return $this->hasMany(AccountStats::class, ['account_id' => 'id']);
187
    }
188
189
    /**
190
     * @return \yii\db\ActiveQuery
191
     */
192
    public function getLastAccountStats()
193
    {
194
        return $this->hasOne(AccountStats::class, ['account_id' => 'id'])
195
            ->orderBy('account_stats.id DESC')
196
            ->limit(1);
197
    }
198
199
    /**
200
     * @return \yii\db\ActiveQuery
201
     */
202
    public function getAccountTags()
203
    {
204
        return $this->hasMany(AccountTag::class, ['account_id' => 'id']);
205
    }
206
207
    /**
208
     * @return \yii\db\ActiveQuery
209
     */
210
    public function getTags()
211
    {
212
        return $this->hasMany(Tag::class, ['id' => 'tag_id'])->via('accountTags');
213
    }
214
215
    /**
216
     * @return \yii\db\ActiveQuery
217
     */
218
    public function getMedia()
219
    {
220
        return $this->hasMany(Media::class, ['account_id' => 'id']);
221
    }
222
223
    /**
224
     * @return \yii\db\ActiveQuery
225
     */
226
    public function getMediaMediaAccounts()
227
    {
228
        return $this->hasMany(MediaAccount::class, ['media_id' => 'id'])
229
            ->via('media');
230
    }
231
232
    /**
233
     * @return \yii\db\ActiveQuery
234
     */
235
    public function getAccounts()
236
    {
237
        return $this->hasMany(Account::class, ['id' => 'account_id'])
238
            ->via('mediaMediaAccounts');
239
    }
240
241
    /**
242
     * @return \yii\db\ActiveQuery
243
     */
244
    public function getMediaAccounts()
245
    {
246
        return $this->hasMany(MediaAccount::class, ['account_id' => 'id']);
247
    }
248
249
    /**
250
     * @inheritdoc
251
     * @return AccountQuery the active query used by this AR class.
252
     */
253
    public static function find()
254
    {
255
        return new AccountQuery(get_called_class());
256
    }
257
}
258