/home/mip/mip/app/Modules/Likod/Controllers/Clients/ClientsController.php
<?php
namespace QxCMS\Modules\Likod\Controllers\Clients;
use Illuminate\Http\Request;
use QxCMS\Modules\Likod\Requests\Clients\ClientRequest;
use QxCMS\Http\Controllers\Controller;
use QxCMS\Modules\Likod\Repositories\Clients\Clients\ClientRepositoryInterface as Client;
use QxCMS\Modules\Client\Repositories\Settings\Roles\RoleRepositoryInterface as Role;
use DB;
use Datatables;
use Auth;
use Lang;
use Gate;
class ClientsController extends Controller
{
protected $client;
protected $role;
protected $auth;
protected $prefix_name = '';
public function __construct(Client $client, Role $role)
{
$this->auth = Auth::guard('likod');
$this->prefix_name = config('modules.likod');
$this->client = $client;
$this->role = $role;
}
public function index()
{
return view('Likod::clients.index');
}
public function create()
{
$data['datepickerformats'] = config('account.dateformat.lists');
$data['displaydateformats'] = config('account.displaydateformat.lists');
$data['nameformats'] = config('account.nameformat');
$data['mail_driver'] = config('account.mail.driver');
$data['storage_type'] = config('account.storage.types');
return view('Likod::clients.create', $data);
}
public function store(ClientRequest $request)
{
$this->client->create($request->all());
$request->session()->flash('success', 'Client successfully created.');
return redirect()->route($this->prefix_name.'.clients.index');
}
public function edit($hashid)
{
$id = decode($hashid);
$data['client'] = $client = $this->client->findById($id);
$data['datepickerformats'] = config('account.dateformat.lists');
$data['displaydateformats'] = config('account.displaydateformat.lists');
$data['nameformats'] = config('account.nameformat');
$data['mail_driver'] = config('account.mail.driver');
$data['storage_type'] = config('account.storage.types');
return view('Likod::clients.edit', $data);
}
public function update(ClientRequest $request, $hashid)
{
$id = decode($hashid);
$this->client->update($id, $request->all());
return redirect($this->prefix_name.'/clients');
}
public function login(Request $request, $hashid)
{
$id = decode($hashid);
$user = $this->client->mainUser($id);
if (Auth::guard('client')->loginUsingId($user->id)) {
if(Auth::guard('client')->user()->client->status==1) {
$this->role->setConfig($user->client);
$this->role->write_menu($user->client->id, $user->role_id);
return redirect(config('modules.client').'/dashboard');
} else {
Auth::guard('client')->logout();
$request->session()->regenerate();
session()->flash('error', 'This client is inactive.');
return redirect(config('modules.client').'/auth/login');
}
}
$request->session()->flash('error','Unable to login this account.');
return redirect($this->prefix_name.'/clients');
}
/*
* Datatables
*/
public function getClientsData()
{
$clients = $this->client->select([
'id',
'client_name',
'number_of_users',
'monthly_mail_quota',
'disk_size',
'status'
]);
return Datatables::of($clients)
->editColumn('number_of_users',function($client){
if($client->number_of_users == 0) return 'Unlimited';
return $client->number_of_users;
})
->editColumn('monthly_mail_quota',function($client){
if($client->monthly_mail_quota == 0) return 'Unlimited';
return $client->monthly_mail_quota;
})
->editColumn('disk_size',function($client){
if($client->disk_size == 0) return 'Unlimited';
return $client->disk_size;
})
->editColumn('status',function($client){
if($client->status == 0) return '<span class="label label-danger">Deactivated</span>';
return '<span class="label label-success">Activated</span>';
})
->addColumn('action', function ($client) {
$html_out = '';
$html_out .= '<a href="'.url($this->prefix_name.'/clients/'.$client->hashid.'/edit').'" class="btn btn-xs btn-flat btn-warning" data-toggle="tooltip" data-placement="top" title="Edit"><i class="fa fa-pencil"></i></a> ';
$html_out .= '<a href="'.url($this->prefix_name.'/clients/'.$client->hashid.'/login').'" class="btn btn-xs btn-flat btn-success" data-toggle="tooltip" data-placement="top" title="Login" target="_blank"><i class="fa fa-sign-in"></i></a> ';
return $html_out;
})
->rawColumns(['status', 'action'])
->make(true);
}
public function setConfig($client)
{
if(!empty($client)) {
return Config::set('database.connections.client', array(
'driver' => 'mysql',
'host' => $client->database_host,
'database' => 'myradh_'.$client->database_name,
'username' => $client->database_username,
'password' => $client->database_password,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
));
}
}
}