TokensController.php
4.05 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
<?php
namespace App\Http\Controllers;
use App\Models\Campaigns;
use App\Models\Dictionary;
use App\Models\Tokens;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Request;
use Inertia\Inertia;
use phpDocumentor\Reflection\Types\Boolean;
use phpDocumentor\Reflection\Types\Integer;
class TokensController extends Controller
{
function index(){
return Inertia::render('Tokens/Index', [
'filters' => Request::all('api', 'type', 'login'),
'types' => [Tokens::MAIN => 'Основной аккаунт', Tokens::GOAL=> 'Целевой аккаунт'],
'tokens' => Tokens::filter(Request::only('api', 'type', 'login'))
->paginate()
->withQueryString()
->through(function ($token) {
return [
'id' => $token->id,
'login' => $token->login,
'type' => $token->type,
'api' => $token->api,
];
}),
]);
}
function edit(Tokens $token){
$mainToken = Tokens::where('type', Tokens::MAIN)->get();
return Inertia::render('Tokens/Edit', [
'token' => [
'id' => $token->id,
'login' => $token->login,
'api' => $token->api,
'token' => $token->token,
'type' => $token->type,
'campaigns' => (
$token->type === Tokens::MAIN
? $token->campaignsForManaged
: []
),
'cities' => ($token->type==Tokens::GOAL?$token->cities:null),
],
'cities' => Dictionary::where('type', Dictionary::CITY)->get(),
'campaigns' => $token->campaigns,
'types' => $mainToken->count() && $token->type!=Tokens::MAIN
? [Tokens::GOAL=> 'Целевой аккаунт'] :
[Tokens::MAIN => 'Основной аккаунт', Tokens::GOAL=> 'Целевой аккаунт'],
]);
}
public function update(Tokens $token)
{
$token->update(
Request::validate([
'type' => ['required', 'in:' . Tokens::MAIN . "," . Tokens::GOAL],
])
);
return Redirect::back()->with('success', 'Token updated.');
}
public function managedCampaign(Tokens $token, $campaign_id, $managed)
{
$campaign = $token->campaigns()->find($campaign_id);
if (!$campaign) {
return Redirect::back();
}
$campaign->update([
'manage' => !!$managed,
]);
return Redirect::back()->with('success', 'Campaign ' . ($managed ? 'added':'deleted') . '.');
}
public function enabledCampaign(Tokens $token, $campaign_id, $enabled)
{
$campaign = $token->campaigns()->find($campaign_id);
if (!$campaign) {
return Redirect::back();
}
$campaign->update([
'enabled' => !!$enabled,
]);
return Redirect::back()->with('success', 'Campaign ' . ($enabled ? 'enabled':'disabled') . '.');
}
public function destroy(Tokens $token)
{
$token->delete();
return Redirect::route('tokens')->with('success', 'Token deleted.');
}
function get($api){
return Inertia::location(API::getInstance( APIRequest::getInstance($api) )->getAuthLink());
}
function token($api){
$token = API::getInstance( APIRequest::getInstance($api) )->getToken(Request::get('code'));
$tokens = Tokens::firstOrNew(['token'=>$token['token']]);
$tokens->token = $token['token'];
$tokens->login = $token['login'];
$tokens->api = $api;
$tokens->created_by = Auth::user()->id;
$tokens->save();
return Redirect::route('tokens')->with('success', 'Token added.');
}
public function addCity(Tokens $token)
{
$token;
}
}