Viewing file: ServerController.php (4.82 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Http\Controllers\Customer;
use App\Http\Controllers\Controller;
use App\Models\SendingServer;
use Illuminate\Http\Request;
class ServerController extends Controller
{
public function index()
{
return view('customer.servers.index');
}
public function getAll()
{
dd('jkk');
$customers = auth('customer')->user()->sending_servers()->select(['id','title','from', 'status', 'created_at']);
return datatables()->of($customers)
->addColumn('created_at', function ($q) {
return $q->created_at->format('d-m-Y');
})
->addColumn('from',function ($q){
return format_field_name($q->from);
})
->addColumn('action', function ($q) {
return "<a class='btn btn-sm btn-info' href='" . route('customer.servers.edit', [$q->id]) . "'>Edit</a> " .
'<button class="btn btn-sm btn-danger" data-message="Are you sure you want to delete this server?"
data-action=' . route('customer.servers.destroy', [$q]) . '
data-input={"_method":"delete"}
data-toggle="modal" data-target="#modal-confirm">Delete</button>';
})
->rawColumns(['action'])
->toJson();
}
public function getFields(Request $request)
{
return servers_fields($request->sending_server);
}
public function create()
{
$current_plan = auth('customer')->user()->plan;
if (!$current_plan)
return back()->with('fail', 'Doesn\'t have any plan right now');
$sendingServer= auth('customer')->user()->sending_servers()->count();
$planServerLimit= $current_plan->server_limit;
if ($sendingServer >= $planServerLimit){
return back()->with('fail', 'Doesn\'t have enough server limit');
}
return view('customer.servers.create');
}
public function store(Request $request)
{
$request->validate([
'title'=>'required',
'from' => 'required|in:'.implode(',', available_servers()),
'status' => 'required|in:active,inactive'
],[
'from.in'=>'Invalid server selected'
]);
$current_plan = auth('customer')->user()->plan;
if (!$current_plan)
return back()->with('fail', 'Customer doesn\'t have any plan right now');
$sendingServer= auth('customer')->user()->sending_servers()->count();
$planServerLimit= $current_plan->server_limit;
if ($sendingServer >= $planServerLimit){
return back()->with('fail', 'Customer doesn\'t have enough server limit');
}
$preServer = auth('customer')->user()->sending_servers()->where('from', $request->from)->first();
if($preServer){
return redirect()->route('customer.servers.edit', $preServer->id)->withErrors(['failed'=> trans('customer.messages.server_already_configured')]);
}
auth('customer')->user()->sending_servers()->create([
'title'=>$request->title,
'from'=>$request->from,
'value'=>json_encode($request->only(servers_fields($request->from))),
'status'=>$request->status
]);
return redirect()->route('customer.servers.index')->with('success', 'Server successfully added');
}
public function edit(SendingServer $server)
{
$data['server'] = $server;
return view('customer.servers.edit', $data);
}
public function update(SendingServer $server, Request $request)
{
$request->validate([
'title'=>'required',
'from' => 'required|in:'.implode(',', available_servers()),
'status' => 'required|in:active,inactive'
],[
'from.in'=>'Invalid server selected'
]);
auth('customer')->user()->sending_servers()->where('id',$server->id)->update([
'title'=>$request->title,
'value'=>json_encode($request->only(servers_fields($request->from))),
'status'=>$request->status
]);
return back()->with('success', 'Server successfully updated');
}
public function destroy(SendingServer $server)
{
$sendingServer=auth('customer')->user()->sending_servers()->where('id',$server->id)->first();
//TODO:: check customer servers
/* if ($sendingServer->customer_servers->isNotEmpty()) {
return back()->with('fail', 'Sorry server is already in used');
}*/
$sendingServer->delete();
return back()->with('success', 'Server successfully deleted');
}
}
|