File: /var/www/vhost/disk-apps/qas.sports-crowd.com/app/Http/Controllers/FacebookController.php
<?php
namespace App\Http\Controllers;
use Datatables;
use App\OfficialFacebook;
use App\SocialNetworkAccount;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class FacebookController extends Controller
{
public function index()
{
return view('official_facebook.list');
}
public function indexAdd()
{
$socialNetworkAccounts = SocialNetworkAccount::select('id', 'name')->where('active', true)->get();
return view('official_facebook.create', compact('socialNetworkAccounts'));
}
public function indexEdit($id)
{
$object = OfficialFacebook::find($id);
$socialNetworkAccounts = SocialNetworkAccount::select('id', 'name')->where('active', true)->get();
return view('official_facebook.edit', compact('object', 'socialNetworkAccounts'));
}
public function tableFilter()
{
DB::statement("SET sql_mode = ''");
$obj = OfficialFacebook::select(
'official_facebooks.id',
'title',
'link',
'social_network_accounts.name AS social_network_accounts',
'official_facebooks.active',
'official_facebooks.created_at'
)
->leftJoin('social_network_accounts', 'social_network_accounts.id', '=', 'official_facebooks.social_network_account_id')
->groupBy('official_facebooks.id');
DB::statement("SET sql_mode = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'");
return Datatables::of($obj)
->addColumn('actions', function ($obj) {
return '
<i class="fa fa-pencil iconMini" onClick="clickEdit(' . $obj->id . ')" data-id="' . $obj->id . '" title="Editar"></i>
<i class="fa fa-trash iconMini" onClick="clickDelete(' . $obj->id . ')" data-id="' . $obj->id . '" title="Eliminar"></i>
';
})
->editColumn('created_at', function ($obj) {
return \Carbon\Carbon::parse($obj->created_at)->format('Y-m-d h:i:s A');
})
->editColumn('active', function ($obj) {
if ($obj->active == 0) {
return '<div class="switch"><label><div class="checkbox checbox-switch switch-success"> <label> No <input type="checkbox" class="chk" onChange="chk(' . $obj->id . ')" data-id="' . $obj->id . '" id="Checkactive' . $obj->id . '" name="Checkactivo" /> <span></span>Si </label></div> </label> </div>';
} else {
return '<div class="switch"><label> <div class="checkbox checbox-switch switch-success"> <label> No <input type="checkbox" class="chk" onChange="chk(' . $obj->id . ')" data-id="' . $obj->id . '" id="Checkactive' . $obj->id . '" name="Checkactivo" checked="" /><span></span> Si </label> </div> </label> </div>';
}
})
->rawColumns(['active', 'actions'])
->make(true);
}
public function create(Request $request)
{
try {
if (OfficialFacebook::where('link', $request->input('link'))->first()) {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('link')]), "data" => null));
}
if ($model = OfficialFacebook::create($request->all())) {
return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.created_successfully'), "data" => $model->id));
} else {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_creating'), "data" => null));
}
} catch (\Exception $e) {
return response(
array(
"r" => false,
"type" => "error",
"title" => "Oops...",
"m" => __($e->getMessage()),
"data" => null
)
);
}
}
public function update(Request $request, $id)
{
try {
$request['id'] = $id;
if (OfficialFacebook::where([['id', '!=', $id], ['link', $request->input('link')]])->first()) {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('link')]), "data" => null));
}
if (OfficialFacebook::where('id', $id)->update($request->all())) {
return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.updated_successfully'), "data" => $id));
} else {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_updating'), "data" => null));
}
} catch (\Exception $e) {
return response(
array(
"r" => false,
"type" => "error",
"title" => "Oops...",
"m" => __($e->getMessage()),
"data" => null
)
);
}
}
public function delete($id)
{
try {
if (OfficialFacebook::where('id', $id)->delete()) {
return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.deleted_successfully'), "data" => null));
} else {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_removing'), "data" => null));
}
} catch (\Illuminate\Database\QueryException $e) {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.delete_relation_data'), "data" => null));
}
}
public function activate(Request $request)
{
try {
$id = $request['id'];
$state = $request['state'];
$facebook = OfficialFacebook::find($id);
$facebook->active = $state;
$facebook->update();
return array('r' => true, 'd' => null, 'm' => __('messages.updated_successfully'));
} catch (\Throwable $th) {
return array('r' => false, 'd' => null, 'm' => __('messages.error_updating'));
}
}
}