Tokens.php
7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
namespace App\Models;
use App\Models\Pivots\DictionaryCampaign;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
/**
* App\Models\Tokens
*
* @property int $id
* @property string $token
* @property string $login
* @property string $api
* @property string|null $type
* @property int $created_by
* @property string|null $timestamp
* @property \Illuminate\Support\Carbon|null $check_changes_ad_group
* @property \Illuminate\Support\Carbon|null $check_changes_ad_group_at
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property int $limit
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaigns
* @property-read int|null $campaigns_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaignsNotEnabledNotDisabled
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaignsEnabledDisabled
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaignsEnabledNotDisabledUpdatedChildren
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $cities
* @property-read int|null $cities_count
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaigns
* @property-read int|null $dictionary_campaigns_count
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsForExternal
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForNotExternal
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsNotEnabledForExternalNotDisabled
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForExternalDisabled
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForExternalNeedUpdated
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForExternalSynchronizedUpdatedChildren
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $campaignsForManaged
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $campaignsNotForManaged
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Limits[] $limits
* @property-read int|null $limits_count
* @method static \Illuminate\Database\Eloquent\Builder|Tokens filter(array $filters)
* @method static \Illuminate\Database\Eloquent\Builder|Tokens newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Tokens newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Tokens query()
* @method static \Illuminate\Database\Eloquent\Builder|Tokens whereApi($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tokens whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tokens whereCreatedBy($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tokens whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tokens whereLimit($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tokens whereLogin($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tokens whereTimestamp($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tokens whereToken($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tokens whereType($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tokens whereUpdatedAt($value)
* @mixin \Eloquent
*/
class Tokens extends Model
{
CONST MAIN = 'main';
CONST GOAL = 'goal';
protected $fillable = [
'token',
'login',
'type',
'timestamp',
'check_changes_ad_group',
];
protected $casts = [
'check_changes_ad_group' => 'datetime',
];
public function getCheckChangesAdGroupAtAttribute()
{
return $this->check_changes_ad_group
? $this->check_changes_ad_group
: Carbon::now()->addDays(-1);
}
public function scopeFilter($query, array $filters)
{
$query->when($filters['login'] ?? null, function ($query, $search) {
$query->where('login', 'like', '%'.$search.'%');
})->when($filters['type'] ?? null, function ($query, $type) {
$query->where('type', $type);
})->when($filters['api'] ?? null, function ($query, $api) {
$query->where('api', $api);
});
}
public function isMain()
{
return $this->type === $this::MAIN;
}
public function limits()
{
return $this->hasMany(Limits::class, 'token', 'id')
->orderBy('updated_at', 'DESC');
}
public function campaigns()
{
return $this->hasMany(Campaigns::class, 'token', 'id')
->orderBy('updated_at', 'DESC');
}
public function campaignsNotEnabledNotDisabled()
{
return $this->campaigns()->forEnabled(false)->notDisabled();
}
public function campaignsEnabledDisabled()
{
return $this->campaigns()->forEnabled()->disabled();
}
public function campaignsEnabledNotDisabledUpdatedChildren()
{
return $this->campaigns()->forEnabled()->notDisabled()->forUpdatedChildren();
}
public function cities()
{
return $this->hasMany(Dictionary::class, 'token_id', 'id')
->orderBy('updated_at', 'DESC');
}
public function dictionaryCampaigns()
{
return $this->hasManyThrough(DictionaryCampaign::class, Dictionary::class, 'token_id', 'dictionary_id')
->orderBy(DictionaryCampaign::getModel()->getTable() . '.created_at', 'ASC')
->orderBy(DictionaryCampaign::getModel()->getTable() . '.' . DictionaryCampaign::getModel()->getKeyName(), 'ASC');
}
public function dictionaryCampaignsForExternal()
{
return $this->dictionaryCampaigns()->forExternal();
}
public function dictionaryCampaignsEnabledForNotExternal()
{
return $this->dictionaryCampaigns()->enabled()->forNotExternal();
}
public function dictionaryCampaignsNotEnabledForExternalNotDisabled()
{
return $this->dictionaryCampaigns()->enabled(false)->forExternal()->notDisabled();
}
public function dictionaryCampaignsEnabledForExternalDisabled()
{
return $this->dictionaryCampaigns()->enabled()->forExternal()->disabled();
}
public function dictionaryCampaignsEnabledForExternalNeedUpdated()
{
return $this->dictionaryCampaigns()->enabled()->forExternal()->needUpdated();
}
public function dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf()
{
return $this->dictionaryCampaignsForExternal()->enabled()->synchronized()->forUpdatedSelf();
}
public function dictionaryCampaignsEnabledForExternalSynchronizedUpdatedChildren()
{
return $this->dictionaryCampaignsForExternal()->enabled()->synchronized()->forUpdatedChildren();
}
public function campaignsForManaged()
{
return $this->campaigns()->forManaged();
}
public function campaignsNotForManaged()
{
return $this->campaigns()->forManaged(false);
}
}